@modular-forms/solid
Version:
The modular and type-safe form library for SolidJS
43 lines (42 loc) • 1.45 kB
JavaScript
import { createSignal } from '../primitives';
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 initial = getPathValue(name, form.internal.initialValues);
// Create signals of field store
const elements = createSignal([]);
const initialValue = createSignal(initial);
const startValue = createSignal(initial);
const value = createSignal(initial);
const error = createSignal('');
const active = createSignal(false);
const touched = createSignal(false);
const dirty = createSignal(false);
// Add store of field to form
// @ts-expect-error
form.internal.fields[name] = {
// Signals
elements,
initialValue,
startValue,
value,
error,
active,
touched,
dirty,
// Other
validate: [],
validateOn: undefined,
revalidateOn: undefined,
transform: [],
consumers: new Set(),
};
// Add name of field to form
form.internal.fieldNames.set((names) => [...names, name]);
}
// Return store of field
return getFieldStore(form, name);
}