forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
324 lines (306 loc) · 16.1 KB
/
Copy pathindex.tsx
File metadata and controls
324 lines (306 loc) · 16.1 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import type {RefObject} from 'react';
import React, {useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import {View} from 'react-native';
import type {GestureResponderEvent} from 'react-native';
import Button from '@components/Button';
import Icon from '@components/Icon';
import PopoverMenu from '@components/PopoverMenu';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import usePopoverPosition from '@hooks/usePopoverPosition';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSafeAreaPaddings from '@hooks/useSafeAreaPaddings';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import mergeRefs from '@libs/mergeRefs';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {AnchorPosition} from '@src/styles';
import type {ButtonWithDropdownMenuProps} from './types';
const defaultAnchorAlignment = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
// we assume that popover menu opens below the button, anchor is at TOP
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP,
};
function ButtonWithDropdownMenu<IValueType>({ref, ...props}: ButtonWithDropdownMenuProps<IValueType>) {
const {
success = true,
isSplitButton = true,
isLoading = false,
isDisabled = false,
pressOnEnter = false,
shouldAlwaysShowDropdownMenu = false,
menuHeaderText = '',
customText,
style,
disabledStyle,
buttonSize = CONST.DROPDOWN_BUTTON_SIZE.MEDIUM,
anchorAlignment = defaultAnchorAlignment,
buttonRef,
onPress,
options,
onOptionSelected,
onSubItemSelected,
onOptionsMenuShow,
onOptionsMenuHide,
enterKeyEventListenerPriority = 0,
wrapperStyle,
useKeyboardShortcuts = false,
defaultSelectedIndex = 0,
shouldShowSelectedItemCheck = false,
testID,
secondLineText = '',
icon,
shouldPopoverUseScrollView = false,
containerStyles,
shouldUseModalPaddingStyle = true,
shouldUseShortForm = false,
shouldUseOptionIcon = false,
shouldStayNormalOnDisable = false,
brickRoadIndicator,
sentryLabel,
} = props;
const icons = useMemoizedLazyExpensifyIcons(['DownArrow', 'DotIndicator']);
const hasError = brickRoadIndicator === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [selectedItemIndex, setSelectedItemIndex] = useState(defaultSelectedIndex);
const [isMenuVisible, setIsMenuVisible] = useState(false);
// In tests, skip the popover anchor position calculation. The default values are needed for popover menu to be rendered in tests.
const defaultPopoverAnchorPosition = process.env.NODE_ENV === 'test' ? {horizontal: 100, vertical: 100} : null;
const [popoverAnchorPosition, setPopoverAnchorPosition] = useState<AnchorPosition | null>(defaultPopoverAnchorPosition);
const dropdownAnchor = useRef<View | null>(null);
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to apply correct popover styles
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const dropdownButtonRef = isSplitButton ? buttonRef : mergeRefs(buttonRef, dropdownAnchor);
const selectedItem = options.at(selectedItemIndex) ?? options.at(0);
const areAllOptionsDisabled = options.every((option) => option.disabled);
const innerStyleDropButton = StyleUtils.getDropDownButtonHeight(buttonSize);
const isButtonSizeLarge = buttonSize === CONST.DROPDOWN_BUTTON_SIZE.LARGE;
const isButtonSizeSmall = buttonSize === CONST.DROPDOWN_BUTTON_SIZE.SMALL;
const isButtonSizeExtraSmall = buttonSize === CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL;
const nullCheckRef = (refParam: RefObject<View | null>) => refParam ?? null;
const shouldShowButtonRightIcon = !!options.at(0)?.shouldShowButtonRightIcon;
useEffect(() => {
setSelectedItemIndex(defaultSelectedIndex);
}, [defaultSelectedIndex]);
const {paddingBottom} = useSafeAreaPaddings(true);
const {calculatePopoverPosition} = usePopoverPosition();
useEffect(() => {
if (!dropdownAnchor.current || !isMenuVisible) {
return;
}
calculatePopoverPosition(dropdownAnchor, anchorAlignment).then(setPopoverAnchorPosition);
}, [isMenuVisible, calculatePopoverPosition, anchorAlignment]);
const handleSingleOptionPress = useCallback(
(event: GestureResponderEvent | KeyboardEvent | undefined) => {
const option = options.at(0);
if (!option) {
return;
}
if (option.onSelected) {
option.onSelected();
} else {
onOptionSelected?.(option);
onPress(event, option.value);
}
onSubItemSelected?.(option, 0, event);
},
[options, onPress, onOptionSelected, onSubItemSelected],
);
useKeyboardShortcut(
CONST.KEYBOARD_SHORTCUTS.CTRL_ENTER,
(e) => {
if (shouldAlwaysShowDropdownMenu || options.length) {
if (!isSplitButton) {
setIsMenuVisible(!isMenuVisible);
return;
}
if (selectedItem?.value) {
onPress(e, selectedItem.value);
}
} else {
handleSingleOptionPress(e);
}
},
{
captureOnInputs: true,
shouldBubble: false,
isActive: useKeyboardShortcuts,
},
);
const splitButtonWrapperStyle = isSplitButton ? [styles.flexRow, styles.justifyContentBetween, styles.alignItemsCenter] : {};
const isTextTooLong = customText && customText?.length > 6;
const accessibilityExpandState = isMenuVisible ? CONST.ACCESSIBILITY_LABELS.EXPANDED : CONST.ACCESSIBILITY_LABELS.COLLAPSED;
const accessibilityLabel = `${accessibilityExpandState}, ${customText}`;
const handlePress = useCallback(
(event?: GestureResponderEvent | KeyboardEvent) => {
if (!isSplitButton) {
setIsMenuVisible(!isMenuVisible);
} else if (selectedItem?.value) {
onPress(event, selectedItem.value);
}
},
[isMenuVisible, isSplitButton, onPress, selectedItem?.value],
);
useImperativeHandle(ref, () => ({
setIsMenuVisible,
}));
return (
<View style={wrapperStyle}>
{shouldAlwaysShowDropdownMenu || options.length > 1 ? (
<View style={[splitButtonWrapperStyle, style]}>
<Button
success={success}
pressOnEnter={pressOnEnter}
ref={dropdownButtonRef}
onPress={handlePress}
text={customText ?? selectedItem?.text ?? ''}
isDisabled={isDisabled || areAllOptionsDisabled}
shouldStayNormalOnDisable={shouldStayNormalOnDisable}
isLoading={isLoading}
shouldRemoveRightBorderRadius
style={isSplitButton ? [styles.flex1, styles.pr0] : {}}
extraSmall={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL}
large={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.LARGE}
medium={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.MEDIUM}
small={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.SMALL}
innerStyles={[innerStyleDropButton, !isSplitButton && styles.dropDownButtonCartIconView, isTextTooLong && shouldUseShortForm && {...styles.pl2, ...styles.pr1}]}
enterKeyEventListenerPriority={enterKeyEventListenerPriority}
iconRight={icons.DownArrow}
iconRightStyles={isMenuVisible ? styles.flipUpsideDown : undefined}
shouldShowRightIcon={!isSplitButton && !isLoading && options?.length > 0}
testID={testID}
textStyles={[isTextTooLong && shouldUseShortForm ? {...styles.textExtraSmall, ...styles.textBold} : {}]}
secondLineText={secondLineText}
icon={hasError ? icons.DotIndicator : icon}
iconFill={hasError ? theme.danger : undefined}
iconHoverFill={hasError ? theme.danger : undefined}
iconRightFill={hasError ? theme.icon : undefined}
iconRightHoverFill={hasError ? theme.icon : undefined}
sentryLabel={sentryLabel}
accessibilityLabel={!isSplitButton ? accessibilityLabel : undefined}
accessibilityHint=""
/>
{isSplitButton && (
<Button
ref={dropdownAnchor}
success={success}
isDisabled={isDisabled}
shouldStayNormalOnDisable={shouldStayNormalOnDisable}
style={[styles.pl0]}
onPress={() => setIsMenuVisible(!isMenuVisible)}
shouldRemoveLeftBorderRadius
extraSmall={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL}
large={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.LARGE}
medium={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.MEDIUM}
small={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.SMALL}
innerStyles={[styles.dropDownButtonCartIconContainerPadding, innerStyleDropButton, isButtonSizeSmall && styles.dropDownButtonCartIcon]}
enterKeyEventListenerPriority={enterKeyEventListenerPriority}
sentryLabel={sentryLabel}
accessibilityLabel={accessibilityLabel}
accessibilityHint=""
>
<View style={[styles.dropDownButtonCartIconView, innerStyleDropButton]}>
<View style={[success ? styles.buttonSuccessDivider : styles.buttonDivider]} />
<View
style={[
isButtonSizeLarge && styles.dropDownLargeButtonArrowContain,
isButtonSizeSmall && shouldUseShortForm ? styles.dropDownSmallButtonArrowContain : styles.dropDownMediumButtonArrowContain,
isButtonSizeExtraSmall && styles.dropDownSmallButtonArrowContain,
]}
>
<Icon
medium={isButtonSizeLarge}
small={!isButtonSizeLarge && !shouldUseShortForm}
inline={shouldUseShortForm}
width={shouldUseShortForm ? variables.iconSizeExtraSmall : undefined}
height={shouldUseShortForm ? variables.iconSizeExtraSmall : undefined}
src={icons.DownArrow}
additionalStyles={[...(shouldUseShortForm ? [styles.pRelative, styles.t0] : []), isMenuVisible ? styles.flipUpsideDown : undefined]}
fill={success ? theme.buttonSuccessText : theme.icon}
testID="dropdown-arrow-icon"
/>
</View>
</View>
</Button>
)}
</View>
) : (
<Button
success={success}
ref={buttonRef}
pressOnEnter={pressOnEnter}
isDisabled={isDisabled || !!options.at(0)?.disabled}
shouldStayNormalOnDisable={shouldStayNormalOnDisable}
style={[styles.w100, style]}
disabledStyle={disabledStyle}
isLoading={isLoading}
text={selectedItem?.text}
onPress={handleSingleOptionPress}
extraSmall={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.EXTRA_SMALL}
large={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.LARGE}
medium={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.MEDIUM}
small={buttonSize === CONST.DROPDOWN_BUTTON_SIZE.SMALL}
innerStyles={[innerStyleDropButton, shouldShowButtonRightIcon && styles.dropDownButtonCartIconView]}
iconRightStyles={shouldShowButtonRightIcon && styles.ml2}
enterKeyEventListenerPriority={enterKeyEventListenerPriority}
secondLineText={secondLineText}
icon={shouldUseOptionIcon && !shouldShowButtonRightIcon ? options.at(0)?.icon : icon}
iconRight={shouldShowButtonRightIcon ? options.at(0)?.icon : undefined}
shouldShowRightIcon={shouldShowButtonRightIcon}
testID={testID}
sentryLabel={sentryLabel}
/>
)}
{(shouldAlwaysShowDropdownMenu || options.length > 1) && !!popoverAnchorPosition && (
<PopoverMenu
isVisible={isMenuVisible}
onClose={() => {
setIsMenuVisible(false);
onOptionsMenuHide?.();
}}
onModalShow={onOptionsMenuShow}
onItemSelected={(selectedSubitem, index, event) => {
onSubItemSelected?.(selectedSubitem, index, event);
if (selectedSubitem.shouldCloseModalOnSelect !== false) {
setIsMenuVisible(false);
}
}}
anchorPosition={popoverAnchorPosition}
shouldShowSelectedItemCheck={shouldShowSelectedItemCheck}
anchorRef={nullCheckRef(dropdownAnchor)}
scrollContainerStyle={!shouldUseModalPaddingStyle && isSmallScreenWidth && {...styles.pt4, paddingBottom}}
anchorAlignment={anchorAlignment}
shouldUseModalPaddingStyle={shouldUseModalPaddingStyle}
headerText={menuHeaderText}
shouldUseScrollView={shouldPopoverUseScrollView}
containerStyles={containerStyles}
menuItems={options.map((item, index) => ({
...item,
onSelected: item.onSelected
? () => {
item.onSelected?.();
if (item.shouldUpdateSelectedIndex) {
setSelectedItemIndex(index);
}
}
: () => {
onOptionSelected?.(item);
if (item.shouldUpdateSelectedIndex === false) {
return;
}
setSelectedItemIndex(index);
},
shouldCallAfterModalHide: true,
subMenuItems: item.subMenuItems?.map((subItem) => ({...subItem, shouldCallAfterModalHide: true})),
}))}
/>
)}
</View>
);
}
export default ButtonWithDropdownMenu;