-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[TS migration] Migrate 'PopoverMenu' component to TypeScript #33242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rlinoz
merged 11 commits into
Expensify:main
from
VickyStash:ts-migration/popoverMenu-component
Jan 3, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e9b367
[TS migration] Migrate 'PopoverMenu' component
VickyStash 88ac0b8
Reuse existing AnchorPosition and AnchorAlignment type
VickyStash 4f21839
Update selectedItemIndex to make it mutable
VickyStash c236485
Remove PopoverMenu folder
VickyStash a15c5c6
Merge branch 'main' into ts-migration/popoverMenu-component
VickyStash fe60f79
TS fixes after merging main
VickyStash 572db4a
Merge branch 'main' into ts-migration/popoverMenu-component
VickyStash db7e819
Updates to follow main branch
VickyStash 52f1ac8
Merge branch 'main' into ts-migration/popoverMenu-component
VickyStash 9dc207f
Merge branch 'main' into ts-migration/popoverMenu-component
VickyStash 6ccdb65
TS fixes after merging main
VickyStash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import type {ImageContentFit} from 'expo-image'; | ||
| import React, {RefObject, useRef} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import type {ModalProps} from 'react-native-modal'; | ||
| import type {SvgProps} from 'react-native-svg'; | ||
| import useArrowKeyFocusManager from '@hooks/useArrowKeyFocusManager'; | ||
| import useKeyboardShortcut from '@hooks/useKeyboardShortcut'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import useWindowDimensions from '@hooks/useWindowDimensions'; | ||
| import CONST from '@src/CONST'; | ||
| import type {AnchorPosition} from '@src/styles'; | ||
| import MenuItem from './MenuItem'; | ||
| import type {AnchorAlignment} from './Popover/types'; | ||
| import PopoverWithMeasuredContent from './PopoverWithMeasuredContent'; | ||
| import Text from './Text'; | ||
|
|
||
| type PopoverMenuItem = { | ||
| /** An icon element displayed on the left side */ | ||
| icon: React.FC<SvgProps>; | ||
|
|
||
| /** Text label */ | ||
| text: string; | ||
|
|
||
| /** A callback triggered when this item is selected */ | ||
| onSelected: () => void; | ||
|
|
||
| /** A description text to show under the title */ | ||
| description?: string; | ||
|
|
||
| /** The fill color to pass into the icon. */ | ||
| iconFill?: string; | ||
|
|
||
| /** Icon Width */ | ||
| iconWidth?: number; | ||
|
|
||
| /** Icon Height */ | ||
| iconHeight?: number; | ||
|
|
||
| /** Icon should be displayed in its own color */ | ||
| displayInDefaultIconColor?: boolean; | ||
|
|
||
| /** Determines how the icon should be resized to fit its container */ | ||
| contentFit?: ImageContentFit; | ||
| }; | ||
|
|
||
| type PopoverModalProps = Pick<ModalProps, 'animationIn' | 'animationOut' | 'animationInTiming'>; | ||
|
|
||
| type PopoverMenuProps = PopoverModalProps & { | ||
| /** Callback method fired when the user requests to close the modal */ | ||
| onClose: () => void; | ||
|
|
||
| /** State that determines whether to display the modal or not */ | ||
| isVisible: boolean; | ||
|
|
||
| /** Callback to fire when a CreateMenu item is selected */ | ||
| onItemSelected: (selectedItem: PopoverMenuItem, index: number) => void; | ||
|
|
||
| /** Menu items to be rendered on the list */ | ||
| menuItems: PopoverMenuItem[]; | ||
|
|
||
| /** Optional non-interactive text to display as a header for any create menu */ | ||
| headerText?: string; | ||
|
|
||
| /** Whether disable the animations */ | ||
| disableAnimation?: boolean; | ||
|
|
||
| /** The horizontal and vertical anchors points for the popover */ | ||
| anchorPosition: AnchorPosition; | ||
|
|
||
| /** Ref of the anchor */ | ||
| anchorRef: RefObject<HTMLDivElement>; | ||
|
|
||
| /** Where the popover should be positioned relative to the anchor points. */ | ||
| anchorAlignment?: AnchorAlignment; | ||
|
|
||
| /** Whether we don't want to show overlay */ | ||
| withoutOverlay?: boolean; | ||
|
|
||
| /** Should we announce the Modal visibility changes? */ | ||
| shouldSetModalVisibility?: boolean; | ||
|
|
||
| /** Whether we want to show the popover on the right side of the screen */ | ||
| fromSidebarMediumScreen?: boolean; | ||
| }; | ||
|
|
||
| function PopoverMenu({ | ||
| menuItems, | ||
| onItemSelected, | ||
| isVisible, | ||
| anchorPosition, | ||
| anchorRef, | ||
| onClose, | ||
| headerText, | ||
| fromSidebarMediumScreen, | ||
| anchorAlignment = { | ||
| horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT, | ||
| vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM, | ||
| }, | ||
| animationIn = 'fadeIn', | ||
| animationOut = 'fadeOut', | ||
| animationInTiming = CONST.ANIMATED_TRANSITION, | ||
| disableAnimation = true, | ||
| withoutOverlay = false, | ||
| shouldSetModalVisibility = true, | ||
| }: PopoverMenuProps) { | ||
| const styles = useThemeStyles(); | ||
| const {isSmallScreenWidth} = useWindowDimensions(); | ||
| const selectedItemIndex = useRef<number | null>(null); | ||
| const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({initialFocusedIndex: -1, maxIndex: menuItems.length - 1, isActive: isVisible}); | ||
|
|
||
| const selectItem = (index: number) => { | ||
| const selectedItem = menuItems[index]; | ||
| 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: isVisible}, | ||
| ); | ||
|
|
||
| return ( | ||
| <PopoverWithMeasuredContent | ||
| anchorPosition={anchorPosition} | ||
| anchorRef={anchorRef} | ||
| anchorAlignment={anchorAlignment} | ||
| onClose={onClose} | ||
| isVisible={isVisible} | ||
| onModalHide={() => { | ||
| setFocusedIndex(-1); | ||
| if (selectedItemIndex.current !== null) { | ||
| menuItems[selectedItemIndex.current].onSelected(); | ||
| selectedItemIndex.current = null; | ||
| } | ||
| }} | ||
| animationIn={animationIn} | ||
| animationOut={animationOut} | ||
| animationInTiming={animationInTiming} | ||
| disableAnimation={disableAnimation} | ||
| fromSidebarMediumScreen={fromSidebarMediumScreen} | ||
| withoutOverlay={withoutOverlay} | ||
| shouldSetModalVisibility={shouldSetModalVisibility} | ||
| > | ||
| <View style={isSmallScreenWidth ? {} : styles.createMenuContainer}> | ||
| {!!headerText && <Text style={[styles.createMenuHeaderText, styles.ml3]}>{headerText}</Text>} | ||
| {menuItems.map((item, menuIndex) => ( | ||
| <MenuItem | ||
| key={item.text} | ||
| icon={item.icon} | ||
| iconWidth={item.iconWidth} | ||
| iconHeight={item.iconHeight} | ||
| iconFill={item.iconFill} | ||
| contentFit={item.contentFit} | ||
| title={item.text} | ||
| shouldCheckActionAllowedOnPress={false} | ||
| description={item.description} | ||
| onPress={() => selectItem(menuIndex)} | ||
| focused={focusedIndex === menuIndex} | ||
| displayInDefaultIconColor={item.displayInDefaultIconColor} | ||
| /> | ||
| ))} | ||
| </View> | ||
| </PopoverWithMeasuredContent> | ||
| ); | ||
| } | ||
|
|
||
| PopoverMenu.displayName = 'PopoverMenu'; | ||
|
|
||
| export default React.memo(PopoverMenu); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific reason this is optional?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because
iconTypeshould be optional, there is even a default value for it inside the component (it was a mistake when we migrated MenuItem)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you can also see that
iconTypeisn't passed as a prop to the MenuItem in this file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for explaining