Form arrays are a rather complicated topic, since you need to be able to dynamically add/remove forms on demand. react-fluent-form comes with a built-in solution by providing two additional hooks: useFluentFormArray and useFluentFormItem. Keep following image in mind for examples below:
Like for single forms you also need to create a config for form arrays but using createFormArray function instead. It returns similar config as createForm but with additional configuration properties which are only relevant for form arrays:
withInitialArray: specifiy inital values for the form array
withKeyGenerator: items inside of the form array should be identifiable, which is why each form item has an unique key. On default the key will be generated by a key counter. To override this behaviour you can use this function to generate a key based on values.
caution
withKeyGenerator generates the key just once for each item directly when it's added.
Form items represent the actual forms inside the form array and can be created via useFluentFormItem hook.
Since react hooks can not be called inside of loops (like map in the example above), a new component for form items needs to be implemented.
useFluentFormItem returns the same properties as useFluentForm, but also following ones:
removeSelf: removes form item from the array
key: value, which is used to identify form item
constUserRoleForm=({ formItem })=>{
const{ removeSelf, handleSubmit /* and more.. */}=useFluentFormItem(
useFluentFormArray returns formStates, which is an array that stores the state of each form item. It can be accessed via index or via a helper function called getFormStateByKey.