UNPKG

@zohodesk/hooks

Version:

Unified Component Library - Hooks

73 lines (69 loc) 1.82 kB
// import usePopup from '../popup/usePopup'; // import useSelectControl from './useSelectControl'; import { useEffect } from 'react'; import * as constants from "./constants"; import useCommonReducer from "../utils/useCommonReducer"; import selectSearchReducer from "./selectSearchReducer"; export default function useSelectSearch() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; let customReducer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; const { list, selected } = props; const { state, dispatch } = useCommonReducer(selectSearchReducer, customReducer, { list, selected, handleSelect, filteredOptions: list, searchString: '' }); useEffect(() => { if (list.length > 0) { dispatch({ type: constants.UPDATE_FILTERED_OPTIONS, data: { filteredOptions: state.filteredOptions, list: state.list } }); } }, []); function handleSearch(str) { let key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'value'; dispatch({ type: constants.SEARCH, data: { searchString: str, searchKey: key, list: state.list, selected: state.selected } }); } function filterOptionsCheck(opt) { return isArrayContainsOption(opt, state.list); } function handleSelect(opt) { dispatch({ type: constants.SELECT, data: { option: opt, filteredOptions: state.filteredOptions, selected: state.selected } }); } return { selected: state.selected, handleSelect, handleSearch, filterOptionsCheck, searchString: state.searchString, list: state.list, filteredOptions: state.filteredOptions }; }