UNPKG

styled-hook-form

Version:

React form library for styled-components based on grommet and react-hook-form

38 lines (37 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFormBuilderContext = exports.FormBuilderContextProvider = exports.FormBuilderContext = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const base_theme_1 = require("../themes/base-theme"); const grommet_1 = require("grommet"); const actions_1 = require("./actions"); const defaults = { config: { ssr: true, }, locale: "en", translate: actions_1.translate, dispatch: () => { }, }; exports.FormBuilderContext = react_1.createContext(defaults); const FormBuilderContextProvider = (props) => { var _a, _b; const { children, options } = props; const _model = Object.assign({}, defaults.config); const reducer = (state) => { return state; }; const [config, dispatch] = react_1.useReducer(reducer, _model); const theme = (_a = options === null || options === void 0 ? void 0 : options.theme) !== null && _a !== void 0 ? _a : base_theme_1.FormBuilderTheme; const renderGrommet = (options === null || options === void 0 ? void 0 : options.renderGrommet) === undefined ? true : options.renderGrommet; return (jsx_runtime_1.jsx(exports.FormBuilderContext.Provider, Object.assign({ value: { config, translate: (_b = options === null || options === void 0 ? void 0 : options.translator) !== null && _b !== void 0 ? _b : actions_1.translate, dispatch, locale: (options === null || options === void 0 ? void 0 : options.locale) || "en", } }, { children: renderGrommet ? (jsx_runtime_1.jsx(grommet_1.Grommet, Object.assign({ theme: theme }, { children: children }), void 0)) : (children) }), void 0)); }; exports.FormBuilderContextProvider = FormBuilderContextProvider; const useFormBuilderContext = () => react_1.useContext(exports.FormBuilderContext); exports.useFormBuilderContext = useFormBuilderContext;