-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.js
124 lines (111 loc) · 4.76 KB
/
index.js
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
import PropTypes from 'prop-types';
import React, {useRef} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import MenuItem from '@components/MenuItem';
import PopoverWithMeasuredContent from '@components/PopoverWithMeasuredContent';
import refPropTypes from '@components/refPropTypes';
import Text from '@components/Text';
import withWindowDimensions, {windowDimensionsPropTypes} from '@components/withWindowDimensions';
import useArrowKeyFocusManager from '@hooks/useArrowKeyFocusManager';
import useKeyboardShortcut from '@hooks/useKeyboardShortcut';
import useWindowDimensions from '@hooks/useWindowDimensions';
import useThemeStyles from '@styles/useThemeStyles';
import CONST from '@src/CONST';
import {defaultProps as createMenuDefaultProps, propTypes as createMenuPropTypes} from './popoverMenuPropTypes';
const propTypes = {
...createMenuPropTypes,
...windowDimensionsPropTypes,
/** The horizontal and vertical anchors points for the popover */
anchorPosition: PropTypes.shape({
horizontal: PropTypes.number.isRequired,
vertical: PropTypes.number.isRequired,
}).isRequired,
/** Ref of the anchor */
anchorRef: refPropTypes,
/** Where the popover should be positioned relative to the anchor points. */
anchorAlignment: PropTypes.shape({
horizontal: PropTypes.oneOf(_.values(CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL)),
vertical: PropTypes.oneOf(_.values(CONST.MODAL.ANCHOR_ORIGIN_VERTICAL)),
}),
withoutOverlay: PropTypes.bool,
/** Should we announce the Modal visibility changes? */
shouldSetModalVisibility: PropTypes.bool,
};
const defaultProps = {
...createMenuDefaultProps,
anchorAlignment: {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
},
anchorRef: () => {},
withoutOverlay: false,
shouldSetModalVisibility: true,
};
function PopoverMenu(props) {
const styles = useThemeStyles();
const {isSmallScreenWidth} = useWindowDimensions();
const selectedItemIndex = useRef(null);
const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({initialFocusedIndex: -1, maxIndex: props.menuItems.length - 1, isActive: props.isVisible});
const selectItem = (index) => {
const selectedItem = props.menuItems[index];
props.onItemSelected(selectedItem, index);
selectedItemIndex.current = index;
};
useKeyboardShortcut(
CONST.KEYBOARD_SHORTCUTS.ENTER,
() => {
if (focusedIndex === -1) {
return;
}
selectItem(focusedIndex);
setFocusedIndex(-1); // Reset the focusedIndex on selecting any menu
},
{isActive: props.isVisible},
);
return (
<PopoverWithMeasuredContent
anchorPosition={props.anchorPosition}
anchorRef={props.anchorRef}
anchorAlignment={props.anchorAlignment}
onClose={props.onClose}
isVisible={props.isVisible}
onModalHide={() => {
setFocusedIndex(-1);
if (selectedItemIndex.current !== null) {
props.menuItems[selectedItemIndex.current].onSelected();
selectedItemIndex.current = null;
}
}}
animationIn={props.animationIn}
animationOut={props.animationOut}
animationInTiming={props.animationInTiming}
disableAnimation={props.disableAnimation}
fromSidebarMediumScreen={props.fromSidebarMediumScreen}
withoutOverlay={props.withoutOverlay}
shouldSetModalVisibility={props.shouldSetModalVisibility}
>
<View style={isSmallScreenWidth ? {} : styles.createMenuContainer}>
{!_.isEmpty(props.headerText) && <Text style={[styles.createMenuHeaderText, styles.ml3]}>{props.headerText}</Text>}
{_.map(props.menuItems, (item, menuIndex) => (
<MenuItem
key={item.text}
icon={item.icon}
iconWidth={item.iconWidth}
iconHeight={item.iconHeight}
iconFill={item.iconFill}
title={item.text}
shouldCheckActionAllowedOnPress={false}
description={item.description}
onPress={() => selectItem(menuIndex)}
focused={focusedIndex === menuIndex}
/>
))}
</View>
</PopoverWithMeasuredContent>
);
}
PopoverMenu.propTypes = propTypes;
PopoverMenu.defaultProps = defaultProps;
PopoverMenu.displayName = 'PopoverMenu';
export default React.memo(withWindowDimensions(PopoverMenu));