@saplings/forms
v0.3.0
Published
Reusable Saplings forms toolkit
Readme
@saplings/forms
Controlled schema-driven string fields with application-owned validation.
Install
npm install @saplings/forms reactQuick start
import { SchemaForm, validateForm } from '@saplings/forms';
const values = { email: '' };
const errors = validateForm(values, { email: (v) => v.includes('@') ? undefined : 'Enter an email' });
const view = <SchemaForm fields={[{ name: 'email', label: 'Email', type: 'email' }]}
values={values} errors={errors} onChange={(name, value) => console.log(name, value)}
onSubmit={(event) => event.preventDefault()}><button>Save</button></SchemaForm>;API and operational notes
Fields are controlled; supply state, synchronous validation and submission handling. Errors are associated with fields for assistive technology. Server-side validation remains required. The controller below supports nested updates and submission state.
Validation
Run the workspace build and pnpm test after making changes. Confirm application-specific behavior and accessibility in the consuming environment.
Schema validation and steps
import { z } from '@saplings/validations';
import { validateSchema, moveStep } from '@saplings/forms';
const schema = z.object({ email: z.string().email() });
const result = await validateSchema(schema, { email: '' });
if (!result.success) console.log(result.errors.email, result.formErrors);
const nextStep = moveStep(0, 3, 1);The adapter uses safeParseAsync, including asynchronous refinements, and
maps the first issue for each top-level field. Form-wide errors are returned
separately. The application decides when to validate and submit each step;
the server must repeat authorization and validation.
groupIssues(issues) retains nested schema paths and array indexes as
JSON-encoded path arrays, and returns form-wide errors separately. This avoids
conflating literal dots in field names with nested paths.
Submission controller
createFormController(initial, schema, submit) validates using an async
Saplings-compatible schema, publishes validation issues and submission state,
and prevents duplicate submissions. While a submission is pending, edits and
reset are rejected; callers must handle server errors and server-side validation.
Nested forms and validated steps
const wizard = createFormWizard({ profile: { name: '' } }, [
{ id: 'profile', validate: v => v.profile.name.trim() ? [] :
[{ path: ['profile', 'name'], message: 'Enter your name' }] },
{ id: 'review', validate: () => [] },
], schema, save);
<WizardForm wizard={wizard}>{state => state.stepId === 'profile'
? <label>Name<input value={state.values.profile.name}
onChange={e => wizard.setField(['profile', 'name'], e.target.value)} /></label>
: <p>{state.values.profile.name}</p>}</WizardForm>Import both functions from @saplings/forms. Create the wizard once per form instance. Use numeric segments to update existing array entries. Steps can validate asynchronously; editing a field invalidates an older step check. Final submission validates the complete schema and passes the parsed data to save.
