|
| 1 | +import { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { Box, Paper, Stack } from '@mui/material'; |
| 3 | +import { Dropdown } from '../dropdown'; |
| 4 | +import { MultiselectDropdownButton } from './dropdown-button/dropdown-button'; |
| 5 | +import { Checkbox } from '../checkbox'; |
| 6 | +import { DropdownFooter } from './dropdown-content/footer'; |
| 7 | +import { SearchInput } from '../search-input'; |
| 8 | +import { useDebounce } from '@chirp/ui/hooks/use-debounce'; |
| 9 | + |
| 10 | +type CheckedStateType<T> = { |
| 11 | + array: T[]; |
| 12 | + map: { [key: number | string]: boolean }; |
| 13 | +}; |
| 14 | + |
| 15 | +interface IDropdownMultiselectProps<T> { |
| 16 | + title: string; |
| 17 | + width: string; |
| 18 | + |
| 19 | + options: T[]; |
| 20 | + idKey: keyof T; |
| 21 | + nameKey: keyof T; |
| 22 | + selectedOptions: T[]; |
| 23 | + |
| 24 | + onAccept: (arr: T[]) => void; |
| 25 | + onClear: () => void; |
| 26 | +} |
| 27 | + |
| 28 | +export const DropdownMultiselect = <T extends Record<keyof T, unknown>>({ |
| 29 | + title = '', |
| 30 | + width = '230px', |
| 31 | + selectedOptions = [], |
| 32 | + |
| 33 | + options = [], |
| 34 | + idKey, |
| 35 | + nameKey, |
| 36 | + |
| 37 | + onAccept, |
| 38 | + onClear, |
| 39 | +}: IDropdownMultiselectProps<T>) => { |
| 40 | + const [openState, setOpenState] = useState(false); |
| 41 | + const [searchState, setSearchState] = useState(''); |
| 42 | + const [checkedItemsState, setCheckedItemsState] = useState<CheckedStateType<T>>({ |
| 43 | + array: [], |
| 44 | + map: {}, |
| 45 | + }); |
| 46 | + |
| 47 | + const debouncedSearch = useDebounce(searchState); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!selectedOptions) return; |
| 51 | + const mappedSelectedItems = selectedOptions.reduce( |
| 52 | + (acc, item) => ({ ...acc, [item[idKey] as PropertyKey]: true }), |
| 53 | + {} as CheckedStateType<T>['map'], |
| 54 | + ); |
| 55 | + setCheckedItemsState({ array: selectedOptions, map: mappedSelectedItems }); |
| 56 | + }, [selectedOptions]); |
| 57 | + |
| 58 | + const handleChangeCheckedITem = (elem: T, checked: boolean) => { |
| 59 | + console.log(checked); |
| 60 | + if (checked) { |
| 61 | + setCheckedItemsState((prev) => ({ |
| 62 | + array: [...prev.array, elem], |
| 63 | + map: { ...prev.map, [elem[idKey] as PropertyKey]: true }, |
| 64 | + })); |
| 65 | + } else { |
| 66 | + setCheckedItemsState((prev) => ({ |
| 67 | + array: prev.array.filter((item) => item[idKey] !== elem[idKey]), |
| 68 | + map: { ...prev.map, [elem[idKey] as PropertyKey]: false }, |
| 69 | + })); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + const handleApplyClick = () => { |
| 74 | + onAccept(checkedItemsState.array); |
| 75 | + setOpenState(false); |
| 76 | + }; |
| 77 | + |
| 78 | + const handleClearClick = () => { |
| 79 | + onClear(); |
| 80 | + setOpenState(false); |
| 81 | + }; |
| 82 | + |
| 83 | + const filteredOptions = useMemo(() => { |
| 84 | + return options.filter((item) => |
| 85 | + String(item[nameKey]).toString().toLowerCase().includes(debouncedSearch.toLowerCase()), |
| 86 | + ); |
| 87 | + }, [options, debouncedSearch]); |
| 88 | + |
| 89 | + return ( |
| 90 | + <Box width={width}> |
| 91 | + <Dropdown |
| 92 | + isOpened={openState} |
| 93 | + anchorEl={<MultiselectDropdownButton title={title} onClick={() => setOpenState(!openState)} />} |
| 94 | + > |
| 95 | + <Paper> |
| 96 | + <Stack width={width}> |
| 97 | + <Box p={4}> |
| 98 | + <Stack gap={4}> |
| 99 | + <SearchInput |
| 100 | + value={searchState} |
| 101 | + onChange={setSearchState} |
| 102 | + placeholder="Search by name" |
| 103 | + /> |
| 104 | + {filteredOptions.map((item, index) => ( |
| 105 | + <Checkbox |
| 106 | + key={`${index}-${item[idKey]}`} |
| 107 | + variant="check" |
| 108 | + label={item[nameKey] as string} |
| 109 | + checked={!!checkedItemsState.map[item[idKey] as number | string]} |
| 110 | + onChange={(_, checked) => handleChangeCheckedITem(item, checked)} |
| 111 | + labelTypographyVariant="body1" |
| 112 | + formControlLabelProps={{ |
| 113 | + sx: { |
| 114 | + color: 'text.text4', |
| 115 | + }, |
| 116 | + }} |
| 117 | + sx={{ |
| 118 | + height: '26px', |
| 119 | + }} |
| 120 | + /> |
| 121 | + ))} |
| 122 | + </Stack> |
| 123 | + </Box> |
| 124 | + <DropdownFooter |
| 125 | + selectedCount={checkedItemsState.array?.length} |
| 126 | + onAccept={handleApplyClick} |
| 127 | + onClear={handleClearClick} |
| 128 | + /> |
| 129 | + </Stack> |
| 130 | + </Paper> |
| 131 | + </Dropdown> |
| 132 | + </Box> |
| 133 | + ); |
| 134 | +}; |
0 commit comments