import React, { useRef, useCallback, useState } from 'react'; import { Button, useDisclosure, Box, Flex, useOutsideClick } from '@chakra-ui/react'; import { ChevronDownIcon } from '@chakra-ui/icons'; import { MultipleSelectProps } from './type'; import EmptyTip from '../EmptyTip'; import { useTranslation } from 'next-i18next'; const MultipleRowSelect = ({ placeholder, label, value = [], list, emptyTip, maxH = 300, onSelect, styles }: MultipleSelectProps) => { const { t } = useTranslation(); const ref = useRef(null); const { isOpen, onOpen, onClose } = useDisclosure(); const [cloneValue, setCloneValue] = useState(value); useOutsideClick({ ref: ref, handler: onClose }); const RenderList = useCallback( ({ index, list }: { index: number; list: MultipleSelectProps['list'] }) => { const selectedValue = cloneValue[index]; const selectedIndex = list.findIndex((item) => item.value === selectedValue); const children = list[selectedIndex]?.children || []; const hasChildren = list.some((item) => item.children && item.children?.length > 0); return ( <> {list.map((item) => ( { const newValue = [...cloneValue]; if (item.value === selectedValue) { newValue[index] = undefined; setCloneValue(newValue); onSelect(newValue); } else { newValue[index] = item.value; setCloneValue(newValue); if (!hasChildren) { onSelect(newValue); onClose(); } } }} {...(item.value === selectedValue ? { color: 'primary.600' } : {})} > {item.label} ))} {list.length === 0 && ( )} {children.length > 0 && } ); }, [cloneValue] ); const onOpenSelect = useCallback(() => { setCloneValue(value); onOpen(); }, [value, onOpen]); return ( {isOpen && ( )} ); }; export default React.memo(MultipleRowSelect);