A hook for using forms with Inertia.js, meant to be used as a direct replacement of Inertia's useForm hook.
It address two issues with the original: the bug preventing the transform method from running on submit, and the lack of support for nested form data.
This was developed alongside a Rails project, so the form handling ethos follows Rails conventions, however, effort was taken to make it as agnostic as possible and it should be useable in a Laravel application as well.
I've chosen to keep the main readme clean and only provide basic examples to help get started.
More in depth documentation is available on the Wiki section of this repo
Here is a codesandbox with usage examples for all hooks and components
If you encounter a bug, please try to recreate it with a fork of the codesandbox below and submit it with the issue. I don't have much time to address issues, so seeing an actual recreation can really help me identify if it's something I should look into.
Below are basic usage examples of the exported members of this project
Drop in replacement for Inertia.js' useForm, with support for nested data
const{ data, setData, getData, unsetData, errors, setError, getError }=useInertiaForm({user: {firstName: 'Finn'lastName: undefined}})getData('user.firstName')setData('user.lastName','Human')Create a custom form component for your project
import{FormasInertiaForm,typeFormProps,typeNestedObject}from'use-inertia-form'constForm=<TFormextendsNestedObject>({ children, railsAttributes =true, className, ...props}: FormProps<TForm>,)=>{return(<divclassName="form-wrapper"><InertiaFormclassName={ `form ${className}` }railsAttributes={railsAttributes}{ ...props}>{ children }</InertiaForm></div>)}Create custom inputs for your project
import{useInertiaInput}from'use-inertia-form'constTextInput=({ name, model, label })=>{const{ inputName, inputId, value, setValue, error }=useInertiaInput({ name, model })return(<labelfor={ inputId }>{ label }</label><inputtype='text'id={ inputId }name={ inputName }value={ value }onChange={e=>setValue(e.target.value)}>{ error&&<divclassName="error">{ error }</div>})}Helper component for visually specifying nested data lookup context
import{NestedFields}from'use-inertia-form'import{Form,TextInput}from'my/project/components'constuser={firstName: 'Finn',preferences: {princess: 'Bubblegum',sword: 'Scarlet'}}constEditUserForm=()=>{return(<Formmodel="user"data={{user}}to={ `users/${user.id}` }method="patch"><TextInputname="firstName"label="First Name"/>{/* Create a nested model context of `form.data.user.preferences` */}<NestedFieldsmodel="preferences">{/* This input would sync to `form.data.user.preferences.princess` */}<TextInputname="princess"label="Favorite Princess"/>{/* And this would sync to `form.data.user.preferences.sword` */}<TextInputname="sword"label="Favorite Sword"/></NestedFields></Form>)}Build a custom component for handling arrays in nested data object
import{useDynamicInputs,NestedFields}from'use-inertia-form'constDynamicInputs=({ children, model, label, emptyData })=>{const{ addInput, removeInput, paths }=useDynamicInputs({ model, emptyData })return(<><divstyle={{display: 'flex'}}><labelstyle={{flex: 1}}>{label}</label><buttononClick={ addInput }>+</button></div>{ paths.map((path,i)=>(<NestedFieldskey={i}model={path}><divstyle={{display: 'flex'}}><div>{ children }</div><buttononClick={onClick: ()=>removeInput(i)}>-</button></div></NestedFields>))}</>)}Not necessary to use, any submit button in the <Form> will cause the form to submit. This component just has a few standard features already built in.
You can now use those components together to build forms with nested data support.
import{NestedFields,Submit}from'use-inertia-form'import{Form,TextInput,DynamicInputs}from'my/project/components'// This probably comes from the serverconstuser={user: {id: 1,firstName: "Jake",email: "jake@thetreehouse.ooo",home: {name: "The Treehouse",location: "Ooo",},friends: [{name: "Finn",email: 'finn@thetreehouse.ooo'},{name: "BMO",email: 'bmo@thetreehouse.ooo'},]}}constEditUserForm=({ user })=>{return(<Formmodel="user"data={{user}}to={ `users/${user.id}` }method="patch"filter="user.id"><TextInputname="firstName"label="First Name"/><TextInputname="email"label="Email"/><NestedFieldsmodel="home"><TextInputname="name"label="Home Name"><TextInputname="location"label="Home Location"></NestedFields><DynamicInputsmodel="friends"emptyData={{name: ''}}label="Friends"><TextInputname="name"label="Name"/><TextInputname="email"label="Email"/></DynamicInputs><Submit>UpdateUser</Submit></Form>)}