|
1 | 1 | /* eslint-disable react/display-name */
|
2 | 2 | import React from 'react';
|
| 3 | +import { FixedSizeList as VirtualizedList } from 'react-window'; |
3 | 4 | import { StyledListBoxWrapper } from './styles/StyledListBoxWrapper';
|
4 | 5 | import type { SectionData } from './actionListUtils';
|
| 6 | +import { actionListMaxHeight, getActionListPadding } from './styles/getBaseListBoxWrapperStyles'; |
5 | 7 | import { useBottomSheetContext } from '~components/BottomSheet/BottomSheetContext';
|
6 | 8 | import { assignWithoutSideEffects } from '~utils/assignWithoutSideEffects';
|
7 | 9 | import { makeAccessible } from '~utils/makeAccessible';
|
8 | 10 | import type { DataAnalyticsAttribute } from '~utils/types';
|
9 | 11 | import { makeAnalyticsAttribute } from '~utils/makeAnalyticsAttribute';
|
| 12 | +import { useIsMobile } from '~utils/useIsMobile'; |
| 13 | +import { getItemHeight } from '~components/BaseMenu/BaseMenuItem/tokens'; |
| 14 | +import { useTheme } from '~utils'; |
| 15 | +import type { Theme } from '~components/BladeProvider'; |
| 16 | +import { useDropdown } from '~components/Dropdown/useDropdown'; |
| 17 | +import { dropdownComponentIds } from '~components/Dropdown/dropdownComponentIds'; |
10 | 18 |
|
11 | 19 | type ActionListBoxProps = {
|
12 | 20 | childrenWithId?: React.ReactNode[] | null;
|
@@ -36,6 +44,126 @@ const _ActionListBox = React.forwardRef<HTMLDivElement, ActionListBoxProps>(
|
36 | 44 | },
|
37 | 45 | );
|
38 | 46 |
|
39 |
| -const ActionListBox = assignWithoutSideEffects(_ActionListBox, { displayName: 'ActionListBox' }); |
| 47 | +const ActionListBox = assignWithoutSideEffects(React.memo(_ActionListBox), { |
| 48 | + displayName: 'ActionListBox', |
| 49 | +}); |
40 | 50 |
|
41 |
| -export { ActionListBox }; |
| 51 | +/** |
| 52 | + * Returns the height of item and height of container based on theme and device |
| 53 | + */ |
| 54 | +const getVirtualItemParams = ({ |
| 55 | + theme, |
| 56 | + isMobile, |
| 57 | +}: { |
| 58 | + theme: Theme; |
| 59 | + isMobile: boolean; |
| 60 | +}): { |
| 61 | + itemHeight: number; |
| 62 | + actionListBoxHeight: number; |
| 63 | +} => { |
| 64 | + const itemHeightResponsive = getItemHeight(theme); |
| 65 | + const actionListPadding = getActionListPadding(theme); |
| 66 | + const actionListBoxHeight = actionListMaxHeight - actionListPadding * 2; |
| 67 | + |
| 68 | + return { |
| 69 | + itemHeight: isMobile |
| 70 | + ? itemHeightResponsive.itemHeightMobile |
| 71 | + : itemHeightResponsive.itemHeightDesktop, |
| 72 | + actionListBoxHeight, |
| 73 | + }; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Takes the children (ActionListItem) and returns the filtered items based on `filteredValues` state |
| 78 | + */ |
| 79 | +const useFilteredItems = ( |
| 80 | + children: React.ReactNode[], |
| 81 | +): { |
| 82 | + itemData: React.ReactNode[]; |
| 83 | + itemCount: number; |
| 84 | +} => { |
| 85 | + const childrenArray = React.Children.toArray(children); // Convert children to an array |
| 86 | + |
| 87 | + const { filteredValues, hasAutoCompleteInBottomSheetHeader, dropdownTriggerer } = useDropdown(); |
| 88 | + |
| 89 | + const items = React.useMemo(() => { |
| 90 | + const hasAutoComplete = |
| 91 | + hasAutoCompleteInBottomSheetHeader || |
| 92 | + dropdownTriggerer === dropdownComponentIds.triggers.AutoComplete; |
| 93 | + |
| 94 | + if (!hasAutoComplete) { |
| 95 | + return childrenArray; |
| 96 | + } |
| 97 | + |
| 98 | + // @ts-expect-error: props does exist |
| 99 | + const filteredItems = childrenArray.filter((item) => filteredValues.includes(item.props.value)); |
| 100 | + return filteredItems; |
| 101 | + }, [filteredValues, hasAutoCompleteInBottomSheetHeader, dropdownTriggerer, childrenArray]); |
| 102 | + |
| 103 | + return { |
| 104 | + itemData: items, |
| 105 | + itemCount: items.length, |
| 106 | + }; |
| 107 | +}; |
| 108 | + |
| 109 | +const VirtualListItem = ({ |
| 110 | + index, |
| 111 | + style, |
| 112 | + data, |
| 113 | +}: { |
| 114 | + index: number; |
| 115 | + style: React.CSSProperties; |
| 116 | + data: React.ReactNode[]; |
| 117 | +}): React.ReactElement => { |
| 118 | + return <div style={style}>{data[index]}</div>; |
| 119 | +}; |
| 120 | + |
| 121 | +const _ActionListVirtualizedBox = React.forwardRef<HTMLDivElement, ActionListBoxProps>( |
| 122 | + ({ childrenWithId, actionListItemWrapperRole, isMultiSelectable, ...rest }, ref) => { |
| 123 | + const items = React.Children.toArray(childrenWithId); // Convert children to an array |
| 124 | + const { isInBottomSheet } = useBottomSheetContext(); |
| 125 | + const { itemData, itemCount } = useFilteredItems(items); |
| 126 | + |
| 127 | + const isMobile = useIsMobile(); |
| 128 | + const { theme } = useTheme(); |
| 129 | + const { itemHeight, actionListBoxHeight } = React.useMemo( |
| 130 | + () => getVirtualItemParams({ theme, isMobile }), |
| 131 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 132 | + [theme.name, isMobile], |
| 133 | + ); |
| 134 | + |
| 135 | + return ( |
| 136 | + <StyledListBoxWrapper |
| 137 | + isInBottomSheet={isInBottomSheet} |
| 138 | + ref={ref} |
| 139 | + {...makeAccessible({ |
| 140 | + role: actionListItemWrapperRole, |
| 141 | + multiSelectable: actionListItemWrapperRole === 'listbox' ? isMultiSelectable : undefined, |
| 142 | + })} |
| 143 | + {...makeAnalyticsAttribute(rest)} |
| 144 | + > |
| 145 | + {itemCount < 10 ? ( |
| 146 | + childrenWithId |
| 147 | + ) : ( |
| 148 | + <VirtualizedList |
| 149 | + height={actionListBoxHeight} |
| 150 | + width="100%" |
| 151 | + itemSize={itemHeight} |
| 152 | + itemCount={itemCount} |
| 153 | + itemData={itemData} |
| 154 | + // @ts-expect-error: props does exist |
| 155 | + itemKey={(index) => itemData[index]?.props.value} |
| 156 | + > |
| 157 | + {VirtualListItem} |
| 158 | + </VirtualizedList> |
| 159 | + )} |
| 160 | + </StyledListBoxWrapper> |
| 161 | + ); |
| 162 | + }, |
| 163 | +); |
| 164 | + |
| 165 | +const ActionListVirtualizedBox = assignWithoutSideEffects(React.memo(_ActionListVirtualizedBox), { |
| 166 | + displayName: 'ActionListVirtualizedBox', |
| 167 | +}); |
| 168 | + |
| 169 | +export { ActionListBox, ActionListVirtualizedBox }; |
0 commit comments