styled-hook-form
Version:
React form library for styled-components based on grommet and react-hook-form
39 lines (38 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldView = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_hook_form_1 = require("react-hook-form");
const shared_1 = require("./layouts/shared");
const react_1 = require("react");
const internal_context_1 = require("./internal-context");
const internal_form_context_1 = require("../form/internal-form-context");
const FieldView = (props) => {
var _a;
const methods = react_hook_form_1.useFormContext();
const { formOptions, wrapComponent } = internal_context_1.useInternalContext();
const internalFormContext = react_1.useContext(internal_form_context_1.InternalFormContext);
const { name, onChange, defaultValue, submitTrigger } = props;
react_1.useEffect(() => {
// this would be true if this is a partial form
if (internalFormContext) {
if (submitTrigger) {
internalFormContext.registerAutoSubmitField({
name: name,
handler: () => 0,
defaultValue: defaultValue,
});
}
if (onChange) {
internalFormContext.registerChangeHandler({
name: name,
handler: onChange,
defaultValue: defaultValue,
});
}
}
}, [internalFormContext, name, onChange, defaultValue, submitTrigger]);
let field = shared_1.renderField(Object.assign({}, props), methods, wrapComponent !== null && wrapComponent !== void 0 ? wrapComponent : props.wrapComponent, (_a = props.shouldUnregister) !== null && _a !== void 0 ? _a : formOptions === null || formOptions === void 0 ? void 0 : formOptions.shouldUnregister);
return jsx_runtime_1.jsxs(jsx_runtime_1.Fragment, { children: [" ", field] }, void 0);
};
exports.FieldView = FieldView;