use-form lists
Manage array state with use-form hook
Source
Docs
Package
License
Usage
Name
Status
Form values:
{ "employees": [ { "name": "", "active": false, "key": "mantine-tskzgdl75" } ] }
List handlers
useForm
hook provides the following handlers to manage list state:
setListItem
– sets list item at specified field and indexremoveListItem
– removes list item at specified field and indexaddListItem
– appends list item to the end of the list at specified fieldreorderListItem
– reorders list item with given position at specified field
formList function
To create list state wrap an array with formList
function exported from @mantine/form
.
This function will let formList
know that data in field should be treated as form list
and not as a regular array.
Value returned from the formList
function is a regular array – it will serialize to the
same string as array with the same content. It also supports all array methods (map
, forEach
, reduce
, etc.).
Validation with rules object
List items reordering
Example of items reordering with react-beautiful-dnd:
Form values:
{ "employees": [ { "name": "John Doe", "email": "john@mantine.dev" }, { "name": "Bill Love", "email": "bill@mantine.dev" }, { "name": "Nancy Eagle", "email": "nanacy@mantine.dev" }, { "name": "Lim Notch", "email": "lim@mantine.dev" }, { "name": "Susan Seven", "email": "susan@mantine.dev" } ] }