-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactored tests into multiple files * call update model with hook * add on change sandbox * only update model if on change is defined * use config flag * default update on * use nullish * rollback changes * added touched object * rollback changes * Update simple-delayed-fields.tsx * renamed fields
- Loading branch information
1 parent
7444c15
commit 98822db
Showing
13 changed files
with
1,001 additions
and
863 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { useForm } from '../src'; | ||
|
||
export function OnChange(): JSX.Element { | ||
const data = React.useRef({ | ||
name: 'John Doe', | ||
confirm: false, | ||
}); | ||
|
||
const { fields, changes, touchedValues, onSubmit, reset } = useForm({ | ||
model: data.current, | ||
onSubmit: async ({ model }) => { | ||
data.current = model; | ||
}, | ||
onChange: async ({ changes, touchedValues }) => { | ||
data.current = { | ||
...data.current, | ||
...changes, | ||
...touchedValues, | ||
}; | ||
}, | ||
}); | ||
|
||
return ( | ||
<> | ||
<h2 id="on-change">On Change</h2> | ||
<p> | ||
Changes does not reflect a double toggled boolean state because the | ||
value will be the same as the initial value. We can now use the touched | ||
object to get an updated model with the changes. | ||
</p> | ||
|
||
<form onSubmit={onSubmit}> | ||
<label> | ||
<span>Confirm</span> | ||
<input | ||
type="checkbox" | ||
checked={fields.confirm.value} | ||
onChange={(event) => { | ||
fields.confirm.touch(); | ||
fields.confirm.onChange(event.target.checked); | ||
}} | ||
/> | ||
</label> | ||
<footer> | ||
<button>Submit</button> | ||
<button onClick={reset} type="button"> | ||
Reset | ||
</button> | ||
</footer> | ||
</form> | ||
<h4>Data</h4> | ||
<pre> | ||
{JSON.stringify( | ||
{ data: data.current, changes, touchedValues }, | ||
null, | ||
2 | ||
)} | ||
</pre> | ||
<a href="#">Back</a> | ||
</> | ||
); | ||
} |
Oops, something went wrong.