|
| 1 | +import React, {useRef} from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import PopoverWithMeasuredContent from '@components/PopoverWithMeasuredContent'; |
| 4 | +import type PopoverWithMeasuredContentProps from '@components/PopoverWithMeasuredContent/types'; |
| 5 | +import type {ListItem} from '@components/SelectionList/types'; |
| 6 | +import useStyleUtils from '@hooks/useStyleUtils'; |
| 7 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 8 | +import CONST from '@src/CONST'; |
| 9 | +import CategoryPicker from '.'; |
| 10 | + |
| 11 | +const popoverDimensions = { |
| 12 | + width: CONST.POPOVER_DROPDOWN_WIDTH, |
| 13 | + height: CONST.POPOVER_DROPDOWN_MAX_HEIGHT, |
| 14 | +}; |
| 15 | + |
| 16 | +const DEFAULT_ANCHOR_ALIGNMENT = { |
| 17 | + horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT, |
| 18 | + vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP, |
| 19 | +}; |
| 20 | + |
| 21 | +type CategoryPickerModalProps = { |
| 22 | + /** Callback to close the modal */ |
| 23 | + onClose: () => void; |
| 24 | + |
| 25 | + /** The policy whose categories should be shown */ |
| 26 | + policyID: string | undefined; |
| 27 | + |
| 28 | + /** Currently selected category */ |
| 29 | + selectedCategory?: string; |
| 30 | + |
| 31 | + /** Called when the user confirms a category selection */ |
| 32 | + onSelected?: (item: ListItem) => void; |
| 33 | +} & Omit<PopoverWithMeasuredContentProps, 'anchorRef' | 'children' | 'onClose'>; |
| 34 | + |
| 35 | +function CategoryPickerModal({ |
| 36 | + isVisible, |
| 37 | + onClose, |
| 38 | + anchorPosition, |
| 39 | + policyID, |
| 40 | + selectedCategory, |
| 41 | + onSelected, |
| 42 | + anchorAlignment = DEFAULT_ANCHOR_ALIGNMENT, |
| 43 | + shouldMeasureAnchorPositionFromTop = false, |
| 44 | +}: CategoryPickerModalProps) { |
| 45 | + const styles = useThemeStyles(); |
| 46 | + const StyleUtils = useStyleUtils(); |
| 47 | + const anchorRef = useRef<View>(null); |
| 48 | + |
| 49 | + const handleCategorySelect = (item: ListItem) => { |
| 50 | + // If clicking the same category that's already selected, treat it as deselection |
| 51 | + if (item.keyForList === selectedCategory) { |
| 52 | + onSelected?.({keyForList: '', searchText: ''}); |
| 53 | + } else { |
| 54 | + onSelected?.(item); |
| 55 | + } |
| 56 | + onClose(); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <PopoverWithMeasuredContent |
| 61 | + anchorRef={anchorRef} |
| 62 | + isVisible={isVisible} |
| 63 | + onClose={onClose} |
| 64 | + anchorPosition={anchorPosition} |
| 65 | + popoverDimensions={popoverDimensions} |
| 66 | + anchorAlignment={anchorAlignment} |
| 67 | + innerContainerStyle={StyleUtils.getWidthStyle(popoverDimensions.width)} |
| 68 | + restoreFocusType={CONST.MODAL.RESTORE_FOCUS_TYPE.DELETE} |
| 69 | + shouldSwitchPositionIfOverflow |
| 70 | + shouldEnableNewFocusManagement |
| 71 | + shouldMeasureAnchorPositionFromTop={shouldMeasureAnchorPositionFromTop} |
| 72 | + shouldSkipRemeasurement |
| 73 | + shouldDisplayBelowModals |
| 74 | + > |
| 75 | + <View style={[StyleUtils.getHeight(popoverDimensions.height), styles.flexColumn, styles.pt4]}> |
| 76 | + <CategoryPicker |
| 77 | + selectedCategory={selectedCategory} |
| 78 | + policyID={policyID} |
| 79 | + onSubmit={handleCategorySelect} |
| 80 | + /> |
| 81 | + </View> |
| 82 | + </PopoverWithMeasuredContent> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +export default CategoryPickerModal; |
0 commit comments