import React, { useRef, useCallback, useState } from 'react'; import { Button, useDisclosure, Box, Flex, useOutsideClick, Checkbox } from '@chakra-ui/react'; import { MultipleSelectProps } from './type'; import EmptyTip from '../EmptyTip'; import { useTranslation } from 'next-i18next'; import MyIcon from '../../common/Icon'; import { ChevronDownIcon } from '@chakra-ui/icons'; const MultipleRowSelect = ({ placeholder, label, value = [], list, emptyTip, maxH = 300, onSelect, popDirection = 'bottom', styles, isArray = false }: MultipleSelectProps) => { const { t } = useTranslation(); const ref = useRef(null); const { isOpen, onOpen, onClose } = useDisclosure(); const [navigationPath, setNavigationPath] = useState([]); useOutsideClick({ ref: ref, handler: onClose }); const RenderList = useCallback( ({ index, list }: { index: number; list: MultipleSelectProps['list'] }) => { const currentNav = navigationPath[index]; const selectedIndex = list.findIndex((item) => item.value === currentNav); const children = list[selectedIndex]?.children || []; const hasChildren = list.some((item) => item.children && item.children?.length > 0); const handleSelect = (item: any) => { if (hasChildren) { // Update parent menu path const newPath = [...navigationPath]; newPath[index] = item.value; // Clear sub paths newPath.splice(index + 1); setNavigationPath(newPath); } else { if (!isArray) { onSelect([navigationPath[0], item.value]); onClose(); } else { const parentValue = navigationPath[0]; const newValues = [...value]; const newValue = [parentValue, item.value]; if (newValues.some((v) => v[0] === parentValue && v[1] === item.value)) { onSelect(newValues.filter((v) => !(v[0] === parentValue && v[1] === item.value))); } else { onSelect([...newValues, newValue]); } } } }; return ( <> {list.map((item) => { const isSelected = item.value === currentNav; const showCheckbox = isArray && index !== 0; const isChecked = showCheckbox && value.some((v) => v[1] === item.value && v[0] === navigationPath[0]); return ( handleSelect(item)} {...(isSelected ? { color: 'primary.600' } : {})} > {showCheckbox && ( } mr={1} /> )} {item.label} ); })} {list.length === 0 && ( )} {children.length > 0 && } ); }, [navigationPath, value, isArray, onSelect] ); const onOpenSelect = useCallback(() => { setNavigationPath(isArray ? [] : [value[0]?.[0], value[0]?.[1]]); onOpen(); }, [value, isArray, onOpen]); return ( } border={'1px solid'} borderRadius={'md'} bg={'white'} _active={{ transform: 'none' }} _hover={{ borderColor: 'primary.500' }} {...(isOpen ? { borderColor: 'primary.600', color: 'primary.700', boxShadow: '0px 0px 0px 2.4px rgba(51, 112, 255, 0.15)' } : { borderColor: 'myGray.200', boxShadow: 'none' })} {...styles} onClick={() => (isOpen ? onClose() : onOpenSelect())} className="nowheel" > {label ?? placeholder} {isOpen && ( )} ); }; export default React.memo(MultipleRowSelect);