dynamic-modal
Version:
The dynamic-modal is a solution of creation different modals into project using a json configuration file
31 lines (30 loc) • 1.35 kB
JavaScript
'use client';
;
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const component_state_1 = require("../../context/component/component-state");
const MakeWatcher = ({ element, watch }) => {
const [value, setValue] = (0, react_1.useState)('');
const { Input } = (0, react_1.useContext)(component_state_1.ComponentStateContext);
const { watchList, style, ...watcherProps } = element;
const elementId = (0, react_1.useId)();
(0, react_1.useEffect)(() => {
const { unsubscribe } = watch((formData, { name }) => {
if (!name)
return;
if (watchList.includes(name)) {
const watchValue = [];
watchList.forEach((watchName) => {
const fragment = formData[watchName];
if (fragment)
watchValue.push(fragment);
});
setValue(watchValue.join(' '));
}
});
return () => unsubscribe();
}, [watch]);
return ((0, jsx_runtime_1.jsx)(Input, { ...watcherProps, name: `watcher-${elementId}`, id: elementId, value: value ?? '', onChange: () => { }, invalid: false, disabled: true, style: style }));
};
exports.default = MakeWatcher;