UNPKG

@smart-react-components/ui

Version:
49 lines (48 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const useSelectSearch = (props) => { const lastOptionList = (0, react_1.useRef)([]); const applySearch = (0, react_1.useCallback)((children, list, value) => { children.forEach(item => { var _a; switch ((_a = item === null || item === void 0 ? void 0 : item.type) === null || _a === void 0 ? void 0 : _a.displayName) { case 'Option': if (!value || (typeof item.props.value === 'string' && item.props.value.trim().toLowerCase().includes(value)) || (typeof item.props.children === 'string' && item.props.children.trim().toLowerCase().includes(value))) { list.push(item); } break; case 'OptionGroup': const group = (0, react_1.cloneElement)(item, { children: [] }); if (item.props.children) { applySearch(Array.isArray(item.props.children) ? item.props.children : [item.props.children], group.props.children, value); } if (group.props.children.length > 0) { list.push(group); } break; } }); }, []); const optionList = (0, react_1.useMemo)(() => { /** * Returns the same list when the dropdown gets closed since it is not needed to update during the closing animation. */ if (!props.dropdownStatus && lastOptionList.current.length > 0) { return lastOptionList.current; } const children = []; const value = props.searchValue.trim().toLowerCase(); applySearch(Array.isArray(props.children) ? props.children : [props.children], children, value); if (props.dropdownStatus) { lastOptionList.current = children; } return children; }, [props.children, props.dropdownStatus, props.searchValue]); return { optionList, }; }; exports.default = useSelectSearch;