UNPKG

@zohodesk/hooks

Version:

Unified Component Library - Hooks

30 lines 783 B
import { useCallback } from 'react'; import { SELECT } from "./constants"; import selectReducer from "./reducer"; import useCommonReducer from "../utils/useCommonReducer"; const actionType = SELECT; export default function useSelectControl() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; let reducer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; const { selected = null } = props; const { state = {}, dispatch } = useCommonReducer(selectReducer, reducer, { selected }); const handleSelect = useCallback(value => { dispatch({ type: actionType, data: { selected: value } }); }, []); return { selected: state.selected, handleSelect }; }