@zohodesk/hooks
Version:
Unified Component Library - Hooks
49 lines (45 loc) • 1.22 kB
JavaScript
import { useMemo, useEffect } from 'react';
import useCommonReducer from "../utils/useCommonReducer";
import reducer from "./reducer";
import handleToggle from "./actions/handleToggle";
import handleReset from "./actions/handleReset";
import bindActions from "../utils/bindActions";
/**
@module accordion
*/
/**
* @typedef {Object} accordion
* @property {number} accordionState - accordion state
* @property {function} handleToggle - handle toggle
* @property {function} handleReset - handle reset
*/
/**
* accordion hook
* @function useAccordion
* @param {object} props - previous state
* @param {Array<boolean>} props.opened
* @returns {object} accordion
*/
export default function useAccordion() {
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
let customReducer = arguments.length > 1 ? arguments[1] : undefined;
const {
opened
} = props;
const {
state,
dispatch
} = useCommonReducer(reducer, customReducer, opened);
/* reset */
useEffect(() => {
dispatch(handleReset(opened));
}, [opened]);
const actions = bindActions({
handleToggle,
handleReset
}, dispatch);
return {
accordionState: state,
...actions
};
}