Skip to content

Commit

Permalink
Add example with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vjee committed Feb 8, 2024
1 parent 0ad47a1 commit 2d0c4f4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
23 changes: 21 additions & 2 deletions examples/react/src/fields.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import {useFormInstance, useFormField} from '@formulier/react'
import * as React from 'react'

export function TextField({name, label, info = null}: {name: string; info?: string | null; label: string}) {
export function TextField({
name,
label,
info = null,
required = false,
}: {
name: string
info?: string | null
label: string
required?: boolean
}) {
const validate = React.useCallback(
(value: unknown): string | null => {
if (required && !value) return 'This field is required'
return null
},
[required],
)

const form = useFormInstance()
const [field, meta] = useFormField(form, {name})
const [field, meta] = useFormField(form, {name, validate})

const {id, value, onChange, onBlur} = field
const {error} = meta
Expand All @@ -19,6 +37,7 @@ export function TextField({name, label, info = null}: {name: string; info?: stri
onBlur={onBlur}
aria-describedby={!error ? `${id}-info` : `${id}-info ${id}-error`}
aria-invalid={!!error}
aria-required={required ? true : undefined}
/>
</Field>
)
Expand Down
41 changes: 41 additions & 0 deletions examples/react/src/forms/validation-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {FormProvider, useCreateForm, useSubmitHandler} from '@formulier/react'
import * as React from 'react'
import * as Field from '../fields'

interface FormState {
firstName: string
lastName: string
age: number
}

export function ValidationForm() {
const form = useCreateForm<FormState>({
initialValues: {
firstName: 'John',
lastName: null,
age: 20,
},
})

const onSubmit = useSubmitHandler(form, values => {
alert(JSON.stringify(values, null, 2))
})

return (
<div>
<h1>PersonForm</h1>

<form onSubmit={onSubmit}>
<FormProvider form={form}>
<div className="column">
<Field.TextField name="firstName" label="First name" required />
<Field.TextField name="lastName" label="Last name" info="Some extra info about this field" required />
<Field.IntegerField name="age" label="Age" />

<button type="submit">Submit</button>
</div>
</FormProvider>
</form>
</div>
)
}
2 changes: 2 additions & 0 deletions examples/react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import {createRoot} from 'react-dom/client'
import {createBrowserRouter, RouterProvider, NavLink, Outlet, Navigate, RouteObject} from 'react-router-dom'
import {PersonForm} from './forms/person-form'
import {ValidationForm} from './forms/validation-form'
import {SwitchFieldTypeForm} from './forms/switch-field-type-form'
import {CheckboxForm} from './forms/checkbox-form'
import {SelectForm} from './forms/select-form'
Expand All @@ -13,6 +14,7 @@ createRoot(document.querySelector('#root')!).render(<App />)

const formRoutes: RouteObject[] = [
{path: 'person-form', element: <PersonForm />},
{path: 'validation-form', element: <ValidationForm />},
{path: 'switch-field-type-form', element: <SwitchFieldTypeForm />},
{path: 'checkbox-form', element: <CheckboxForm />},
{path: 'select-form', element: <SelectForm />},
Expand Down

0 comments on commit 2d0c4f4

Please sign in to comment.