UNPKG

@modular-forms/solid

Version:

The modular and type-safe form library for SolidJS

27 lines (26 loc) 972 B
import { batch } from 'solid-js'; import { initializeFieldStore, updateFieldDirty, validateIfRequired, } from '../utils'; export function setValue(form, name, value, { shouldTouched = true, shouldDirty = true, shouldValidate = true, shouldFocus = true, } = {}) { batch(() => { // Initialize store of specified field const field = initializeFieldStore(form, name); // Set new value field.value.set(() => value); // Update touched if set to "true" if (shouldTouched) { field.touched.set(true); form.internal.touched.set(true); } // Update dirty if set to "true" if (shouldDirty) { updateFieldDirty(form, field); } // Validate if set to "true" and necessary if (shouldValidate) { validateIfRequired(form, field, name, { on: ['touched', 'input'], shouldFocus, }); } }); }