import { Box, Flex, useDisclosure, useOutsideClick } from '@chakra-ui/react'; import React, { useRef } from 'react'; import { useTranslation } from 'next-i18next'; import MyTag from '../Tag/index'; import MyIcon from '../Icon'; export type SelectProps = { value?: string[]; placeholder?: string; list: { icon?: string; alias?: string; label: string | React.ReactNode; value: string; }[]; maxH?: number; onSelect: (val: any[]) => void; }; const MultipleSelect = ({ value = [], placeholder, list = [], maxH = 400, onSelect }: SelectProps) => { const { t } = useTranslation(); const ref = useRef(null); const { isOpen, onOpen, onClose } = useDisclosure(); useOutsideClick({ ref: ref, handler: onClose }); return ( (isOpen ? onClose() : onOpen())} > {value.map((item) => { const listItem = list.find((i) => i.value === item); if (!listItem) return null; return ( {listItem.alias || listItem.label} { e.stopPropagation(); onSelect(value.filter((i) => i !== item)); }} /> ); })} {value.length === 0 && placeholder && ( {placeholder} )} {isOpen && ( {list.map((item) => { const selected = value.includes(item.value); return ( { e.stopPropagation(); onSelect(value.filter((i) => i !== item.value)); } } : { onClick: (e) => { e.stopPropagation(); onSelect([...value, item.value]); } })} > {item.icon && } {item.label} ); })} )} ); }; export default React.memo(MultipleSelect);