-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathuseLayoutDropdown.js
More file actions
88 lines (79 loc) · 2.93 KB
/
useLayoutDropdown.js
File metadata and controls
88 lines (79 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {useEffect, useState, useMemo} from 'react';
import {I18nManager, Dimensions} from 'react-native';
import {calculateDropdownHeight} from '../helpers/calculateDropdownHeight';
import {useKeyboardRemainingScreenHeight} from './useKeyboardRemainingScreenHeight';
export const useLayoutDropdown = (data, dropdownStyle, rowStyle, search) => {
const [isVisible, setIsVisible] = useState(false); // dropdown visible ?
const [buttonLayout, setButtonLayout] = useState(null);
const [dropdownPX, setDropdownPX] = useState(0); // position x
const [dropdownPY, setDropdownPY] = useState(0); // position y
const [dropdownHEIGHT, setDropdownHEIGHT] = useState(() => {
return calculateDropdownHeight(dropdownStyle, rowStyle, data?.length || 0, search);
}); // dropdown height
const [dropdownWIDTH, setDropdownWIDTH] = useState(0); // dropdown width
const remainigHeightAvoidKeyboard = useKeyboardRemainingScreenHeight();
const safeDropdownViewUnderKeyboard = rowStyle && rowStyle.height ? rowStyle.height * 3 : 50 * 3;
const [height, setHeight] = useState(Dimensions.get('window').height)
useEffect(() => {
setDropdownHEIGHT(calculateDropdownHeight(dropdownStyle, rowStyle, data?.length || 0, search));
}, [dropdownStyle, rowStyle, data]);
useEffect(() => {
const orientationChangeListener = Dimensions.addEventListener(
'change',
() => {
setHeight(Dimensions.get('window').height)
}
);
return () => {
orientationChangeListener.remove();
};
}, []);
const onDropdownButtonLayout = (w, h, px, py) => {
setButtonLayout({w, h, px, py});
if (height - 18 < py + h + dropdownHEIGHT) {
setDropdownPX(px);
setDropdownPY(py - 2 - dropdownHEIGHT);
} else {
setDropdownPX(px);
setDropdownPY(py + h + 2);
}
setDropdownWIDTH(dropdownStyle?.width || w);
};
const getItemLayout = (flatlistData, index) => ({
index,
length: flatlistData?.length || 0,
offset: rowStyle && rowStyle.height ? rowStyle.height * index : 50 * index,
});
const dropdownWindowStyle = useMemo(() => {
const top =
remainigHeightAvoidKeyboard < dropdownPY + safeDropdownViewUnderKeyboard
? remainigHeightAvoidKeyboard - safeDropdownViewUnderKeyboard
: dropdownPY;
return {
...{
borderTopWidth: 0,
overflow: 'hidden',
},
...dropdownStyle,
...{
position: 'absolute',
top: top,
height: dropdownHEIGHT,
width: dropdownWIDTH,
},
...(I18nManager.isRTL ? {right: dropdownStyle?.right || dropdownPX} : {left: dropdownStyle?.left || dropdownPX}),
};
}, [dropdownStyle, remainigHeightAvoidKeyboard, dropdownPX, dropdownPY, dropdownHEIGHT, dropdownWIDTH]);
const onRequestClose = () => {
setIsVisible(false);
};
return {
isVisible,
setIsVisible,
buttonLayout,
onDropdownButtonLayout,
getItemLayout,
dropdownWindowStyle,
onRequestClose,
};
};