UNPKG

@geneui/components

Version:

The Gene UI components library designed for BI tools

78 lines (71 loc) 2.09 kB
import React__default, { useContext, useReducer, useState, useCallback, useMemo, createContext } from 'react'; const Context = /*#__PURE__*/createContext(); const useFormContext = () => useContext(Context); const reducer = (state, _ref) => { let { type, field } = _ref; switch (type) { case 'add': return [...state, field]; case 'validation': return state.map(item => ({ ...item, isValid: field.name === item.name ? field.isValid : item.isValid })); case 'change': return state.map(item => ({ ...item, isChanged: field.name === item.name ? field.isChanged : item.isChanged })); case 'unmount': return state.filter(item => field.name !== item.name); default: return state; } }; function FormProvider(_ref2) { let { value, children } = _ref2; const [fields, dispatch] = useReducer(reducer, []); const [readOnlyState, setReadOnly] = useState(value.readOnly); const [allowValidation, setAllowValidation] = useState(false); // handle field `isChanged` prop when value changes const handleFieldChange = useCallback(field => dispatch({ type: 'change', field }), []); // add field to store when mounted const handleFieldMount = useCallback(field => dispatch({ type: 'add', field }), []); // remove field when unmounted const handleFieldUnMount = useCallback(field => dispatch({ type: 'unmount', field }), []); // handle field `isValid` prop when validation changes const handleValidationChange = useCallback(field => dispatch({ type: 'validation', field }), []); const contextProps = useMemo(() => ({ fields, setReadOnly, readOnlyState, allowValidation, handleFieldMount, handleFieldUnMount, handleFieldChange, setAllowValidation, handleValidationChange }), [fields, readOnlyState, allowValidation]); return /*#__PURE__*/React__default.createElement(Context.Provider, { value: contextProps }, children); } export { FormProvider as F, useFormContext as u };