-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathButtonWithDropdownMenu.tsx
188 lines (171 loc) · 7.45 KB
/
ButtonWithDropdownMenu.tsx
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type {RefObject} from 'react';
import React, {useEffect, useRef, useState} from 'react';
import type {GestureResponderEvent, StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import type {AnchorPosition} from '@styles/index';
import CONST from '@src/CONST';
import type IconAsset from '@src/types/utils/IconAsset';
import Button from './Button';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import type {AnchorAlignment} from './Popover/types';
import PopoverMenu from './PopoverMenu';
type DropdownOption = {
value: string;
text: string;
icon: IconAsset;
iconWidth?: number;
iconHeight?: number;
iconDescription?: string;
};
type ButtonWithDropdownMenuProps = {
/** Text to display for the menu header */
menuHeaderText?: string;
/** Callback to execute when the main button is pressed */
onPress: (event: GestureResponderEvent | KeyboardEvent | undefined, value: string) => void;
/** Call the onPress function on main button when Enter key is pressed */
pressOnEnter?: boolean;
/** Whether we should show a loading state for the main button */
isLoading?: boolean;
/** The size of button size */
buttonSize: ValueOf<typeof CONST.DROPDOWN_BUTTON_SIZE>;
/** Should the confirmation button be disabled? */
isDisabled?: boolean;
/** Additional styles to add to the component */
style?: StyleProp<ViewStyle>;
/** Menu options to display */
/** e.g. [{text: 'Pay with Expensify', icon: Wallet}] */
options: DropdownOption[];
/** The anchor alignment of the popover menu */
anchorAlignment?: AnchorAlignment;
/* ref for the button */
buttonRef: RefObject<View>;
};
function ButtonWithDropdownMenu({
isLoading = false,
isDisabled = false,
pressOnEnter = false,
menuHeaderText = '',
style,
buttonSize = CONST.DROPDOWN_BUTTON_SIZE.MEDIUM,
anchorAlignment = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP, // we assume that popover menu opens below the button, anchor is at TOP
},
buttonRef,
onPress,
options,
}: ButtonWithDropdownMenuProps) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [selectedItemIndex, setSelectedItemIndex] = useState(0);
const [isMenuVisible, setIsMenuVisible] = useState(false);
const [popoverAnchorPosition, setPopoverAnchorPosition] = useState<AnchorPosition | null>(null);
const {windowWidth, windowHeight} = useWindowDimensions();
const caretButton = useRef<View & HTMLDivElement>(null);
const selectedItem = options[selectedItemIndex] || options[0];
const innerStyleDropButton = StyleUtils.getDropDownButtonHeight(buttonSize);
const isButtonSizeLarge = buttonSize === CONST.DROPDOWN_BUTTON_SIZE.LARGE;
useEffect(() => {
if (!caretButton.current) {
return;
}
if (!isMenuVisible) {
return;
}
if ('measureInWindow' in caretButton.current) {
caretButton.current.measureInWindow((x, y, w, h) => {
setPopoverAnchorPosition({
horizontal: x + w,
vertical:
anchorAlignment.vertical === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP
? y + h + CONST.MODAL.POPOVER_MENU_PADDING // if vertical anchorAlignment is TOP, menu will open below the button and we need to add the height of button and padding
: y - CONST.MODAL.POPOVER_MENU_PADDING, // if it is BOTTOM, menu will open above the button so NO need to add height but DO subtract padding
});
});
}
}, [windowWidth, windowHeight, isMenuVisible, anchorAlignment.vertical]);
return (
<View>
{options.length > 1 ? (
<View style={[styles.flexRow, styles.justifyContentBetween, styles.alignItemsCenter, style]}>
<Button
success
pressOnEnter={pressOnEnter}
ref={buttonRef}
onPress={(event) => onPress(event, selectedItem.value)}
text={selectedItem.text}
isDisabled={isDisabled}
isLoading={isLoading}
shouldRemoveRightBorderRadius
style={[styles.flex1, styles.pr0]}
large={isButtonSizeLarge}
medium={!isButtonSizeLarge}
innerStyles={[innerStyleDropButton]}
/>
<Button
ref={caretButton}
success
isDisabled={isDisabled}
style={[styles.pl0]}
onPress={() => setIsMenuVisible(!isMenuVisible)}
shouldRemoveLeftBorderRadius
large={isButtonSizeLarge}
medium={!isButtonSizeLarge}
innerStyles={[styles.dropDownButtonCartIconContainerPadding, innerStyleDropButton]}
>
<View style={[styles.dropDownButtonCartIconView, innerStyleDropButton]}>
<View style={[styles.buttonDivider]} />
<View style={[styles.dropDownButtonArrowContain]}>
<Icon
src={Expensicons.DownArrow}
fill={theme.textLight}
/>
</View>
</View>
</Button>
</View>
) : (
<Button
success
ref={buttonRef}
pressOnEnter={pressOnEnter}
isDisabled={isDisabled}
style={[styles.w100, style]}
isLoading={isLoading}
text={selectedItem.text}
onPress={(event) => onPress(event, options[0].value)}
large={isButtonSizeLarge}
medium={!isButtonSizeLarge}
innerStyles={[innerStyleDropButton]}
/>
)}
{options.length > 1 && popoverAnchorPosition && (
<PopoverMenu
isVisible={isMenuVisible}
onClose={() => setIsMenuVisible(false)}
onItemSelected={() => setIsMenuVisible(false)}
anchorPosition={popoverAnchorPosition}
anchorRef={caretButton}
withoutOverlay
anchorAlignment={anchorAlignment}
headerText={menuHeaderText}
menuItems={options.map((item, index) => ({
...item,
onSelected: () => {
setSelectedItemIndex(index);
},
}))}
/>
)}
</View>
);
}
ButtonWithDropdownMenu.displayName = 'ButtonWithDropdownMenu';
export default React.memo(ButtonWithDropdownMenu);