|
| 1 | +import { KeyboardEvent, MouseEvent, ReactNode, useMemo } from 'react'; |
| 2 | + |
| 3 | +import { checkbox, checkbox_outline } from '@equinor/eds-icons'; |
| 4 | + |
| 5 | +import { colors } from 'src/atoms'; |
| 6 | +import { Icon, SelectOptionRequired, Typography } from 'src/molecules'; |
| 7 | +import { |
| 8 | + PersistentListItem, |
| 9 | + StyledMenuItem, |
| 10 | +} from 'src/molecules/Select/Select.styles'; |
| 11 | +import { |
| 12 | + MultiSelectMenuItemProps, |
| 13 | + SelectedState, |
| 14 | + SingleSelectMenuItemProps, |
| 15 | +} from 'src/molecules/Select/Select.types'; |
| 16 | +import { |
| 17 | + getParentIcon, |
| 18 | + getParentState, |
| 19 | +} from 'src/molecules/Select/SelectMenuItem.utils'; |
| 20 | + |
| 21 | +interface DynamicMenuItemProps<T extends SelectOptionRequired> { |
| 22 | + menuItemProps: SingleSelectMenuItemProps<T> | MultiSelectMenuItemProps<T>; |
| 23 | + isSelected: boolean; |
| 24 | + handleOnParentKeyDown?: (event: KeyboardEvent<HTMLButtonElement>) => void; |
| 25 | + children?: ReactNode; |
| 26 | +} |
| 27 | + |
| 28 | +export const DynamicMenuItem = <T extends SelectOptionRequired>({ |
| 29 | + menuItemProps, |
| 30 | + isSelected, |
| 31 | + handleOnParentKeyDown, |
| 32 | + children, |
| 33 | +}: DynamicMenuItemProps<T>) => { |
| 34 | + const { |
| 35 | + index, |
| 36 | + depth = 0, |
| 37 | + item, |
| 38 | + itemRefs, |
| 39 | + onItemKeyDown, |
| 40 | + onItemSelect, |
| 41 | + CustomMenuItemComponent, |
| 42 | + mode, |
| 43 | + } = menuItemProps; |
| 44 | + |
| 45 | + let checkboxIcon = isSelected ? checkbox : checkbox_outline; |
| 46 | + let selectedState: SelectedState = isSelected ? 'selected' : 'none'; |
| 47 | + if (item.children && item.children.length > 0 && 'values' in menuItemProps) { |
| 48 | + checkboxIcon = getParentIcon(item, menuItemProps.values); |
| 49 | + selectedState = getParentState(item, menuItemProps.values); |
| 50 | + } |
| 51 | + |
| 52 | + const handleOnItemClick = (e: MouseEvent) => { |
| 53 | + // Stop form submission |
| 54 | + e.preventDefault(); |
| 55 | + onItemSelect(item); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleOnKeyDown = (e: KeyboardEvent<HTMLButtonElement>) => { |
| 59 | + if (handleOnParentKeyDown !== undefined) { |
| 60 | + handleOnParentKeyDown(e); |
| 61 | + } else { |
| 62 | + onItemKeyDown(e); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const itemContent = useMemo(() => { |
| 67 | + if (children) return children; |
| 68 | + |
| 69 | + if (CustomMenuItemComponent) { |
| 70 | + return ( |
| 71 | + <CustomMenuItemComponent item={item} selectedState={selectedState} /> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <> |
| 77 | + {'values' in menuItemProps && ( |
| 78 | + <Icon |
| 79 | + color={colors.interactive.primary__resting.rgba} |
| 80 | + data={checkboxIcon} |
| 81 | + /> |
| 82 | + )} |
| 83 | + <span> |
| 84 | + <Typography group="navigation" variant="menu_title"> |
| 85 | + {item.label} |
| 86 | + </Typography> |
| 87 | + </span> |
| 88 | + </> |
| 89 | + ); |
| 90 | + }, [ |
| 91 | + CustomMenuItemComponent, |
| 92 | + checkboxIcon, |
| 93 | + children, |
| 94 | + item, |
| 95 | + menuItemProps, |
| 96 | + selectedState, |
| 97 | + ]); |
| 98 | + |
| 99 | + if (mode === 'persistent') { |
| 100 | + return ( |
| 101 | + <PersistentListItem |
| 102 | + ref={(element: HTMLButtonElement | null) => { |
| 103 | + itemRefs.current[index] = element; |
| 104 | + }} |
| 105 | + onKeyDownCapture={handleOnKeyDown} |
| 106 | + onClick={handleOnItemClick} |
| 107 | + > |
| 108 | + {itemContent} |
| 109 | + </PersistentListItem> |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return ( |
| 114 | + <StyledMenuItem |
| 115 | + $depth={depth} |
| 116 | + $selected={'value' in menuItemProps ? isSelected : undefined} |
| 117 | + $paddedLeft={ |
| 118 | + menuItemProps.parentHasNestedItems && 'values' in menuItemProps |
| 119 | + ? menuItemProps.parentHasNestedItems |
| 120 | + : undefined |
| 121 | + } |
| 122 | + ref={(element: HTMLButtonElement | null) => { |
| 123 | + itemRefs.current[index] = element; |
| 124 | + }} |
| 125 | + index={index} |
| 126 | + closeMenuOnClick={'value' in menuItemProps} |
| 127 | + onKeyDownCapture={handleOnKeyDown} |
| 128 | + onClick={handleOnItemClick} |
| 129 | + > |
| 130 | + {itemContent} |
| 131 | + </StyledMenuItem> |
| 132 | + ); |
| 133 | +}; |
0 commit comments