UNPKG

react-formal

Version:

Classy HTML form management for React

58 lines (56 loc) 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFormContext = exports.FormContext = exports.BITS = void 0; var _react = _interopRequireDefault(require("react")); var _Errors = require("./Errors"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const BITS = { errors: 1 << 1, // =2 touched: 1 << 2, // =4 actions: 1 << 3, // =8 submits: 1 << 4, // =16 resets: 1 << 5 // =32 }; exports.BITS = BITS; const FormContext = /*#__PURE__*/_react.default.createContext({ errors: _Errors.EMPTY_ERRORS, touched: {}, actions: null, submits: { submitCount: 0, submitAttempts: 0, submitting: false }, resets: 0 }, // @ts-ignore (a, b) => { let bits = 0; if (a.errors !== b.errors) bits |= BITS.errors; if (a.touched !== b.touched) bits |= BITS.touched; if (a.actions !== b.actions) bits |= BITS.actions; if (a.submits !== b.submits) bits |= BITS.submits; if (a.resets !== b.resets) bits |= BITS.resets; return bits; }); // ref: https://github.com/dai-shi/react-hooks-global-state/issues/5 exports.FormContext = FormContext; const useFormContext = observedBits => { const { ReactCurrentDispatcher } = // @ts-ignore _react.default.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; const dispatcher = ReactCurrentDispatcher.current; if (!dispatcher) { throw new Error('Hooks can only be called inside the body of a function component. (https://fb.me/react-invalid-hook-call)'); } return dispatcher.useContext(FormContext, observedBits); }; exports.useFormContext = useFormContext;