@modular-forms/react
Version:
The modular and type-safe form library for React
34 lines (33 loc) • 1.21 kB
JavaScript
import { signal } from '@preact/signals-react';
import { getFieldStore } from './getFieldStore';
import { getPathValue } from './getPathValue';
export function initializeFieldStore(form, name) {
// Initialize store on first request
if (!getFieldStore(form, name)) {
// Get initial value of field
const initialValue = getPathValue(name, form.internal.initialValues);
// Add store of field to form
// @ts-expect-error
form.internal.fields[name] = {
// Signals
elements: signal([]),
initialValue: signal(initialValue),
startValue: signal(initialValue),
value: signal(initialValue),
error: signal(''),
active: signal(false),
touched: signal(false),
dirty: signal(false),
// Other
validate: [],
validateOn: undefined,
revalidateOn: undefined,
transform: [],
consumers: new Set(),
};
// Add name of field to form
form.internal.fieldNames.value = [...form.internal.fieldNames.peek(), name];
}
// Return store of field
return getFieldStore(form, name);
}