Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A component for rendering and editing arrays 🏁 React Final Form

License

NotificationsYou must be signed in to change notification settings

final-form/react-final-form-arrays

💰Wanna get paid the big bucks writing React?Take this quiz and get offers from top tech companies! 💰


🏁 React Final Form Arrays

NPM VersionNPM DownloadsBuild Statuscodecov.iostyled with prettier


Installation

npm install --save react-final-form-arrays react-final-form final-form final-form-arrays

or

yarn add react-final-form-arrays react-final-form final-form final-form-arrays

Usage

🏁 React Final Form Arrays provides a way to render arrays in 🏁 React FinalForm.

import{Form,Field}from'react-final-form'importarrayMutatorsfrom'final-form-arrays'import{FieldArray}from'react-final-form-arrays'constMyForm=()=>(<FormonSubmit={onSubmit}mutators={{// potentially other mutators could be merged here      ...arrayMutators}}validate={validate}render={({ handleSubmit, pristine, invalid})=>(<formonSubmit={handleSubmit}><FieldArrayname="customers">{({ fields})=>(<div>{fields.map((name,index)=>(<divkey={name}><div><label>First Name</label><Fieldname={`${name}.firstName`}component="input"/></div><div><label>Last Name</label><Fieldname={`${name}.lastName`}component="input"/></div><buttontype="button"onClick={()=>fields.remove(index)}>                    Remove</button></div>))}<buttontype="button"onClick={()=>fields.push({firstName:'',lastName:''})}>                Add</button></div>)}</FieldArray></form>)}/>)

Table of Contents

Examples

Demostrates how to use<FieldArray/> to render an array of inputs, as well asusepush,pop, andremove mutations.

Demostrates how to integrate the simple example withreact-beautiful-dnd

Rendering

There are three ways to tell<FieldArray/> what to render:

MethodHow it is rendered
component propreturn React.createElement(this.props.component, props)
render propreturn this.props.render(props)
a render function aschildrenreturn this.props.children(props)

API

The following can be imported fromreact-final-form-arrays.

FieldArray : React.ComponentType<FieldArrayProps>

A component that takesFieldArrayProps and renders anarray of fields

useFieldArray

TheuseFieldArray hook takes two parameters, the first is the name of the field, and the second is an optional object that looks just likeFieldArrayProps, except without the name. It returns an object just likeFieldArrayRenderProps.

useFieldArray is used interally insideFieldArray.

version: string

The current used version of 🏁 React Final Form Arrays.


Types

FieldArrayProps

These are props that you pass to<FieldArray/>. You mustprovide one of the ways to render:component,render, orchildren.

children?: ((props: FieldArrayRenderProps) => React.Node) | React.Node

A render function that is givenFieldArrayRenderProps, as well as any non-API propspassed into the<FieldArray/> component.

component?: React.ComponentType<FieldArrayRenderProps>

A component that is givenFieldArrayRenderProps asprops, as well as any non-API props passed into the<FieldArray/> component.

name: string

The name of your field array.

render?: (props: FieldArrayRenderProps) => React.Node

A render function that is givenFieldArrayRenderProps, as well as any non-API propspassed into the<FieldArray/> component.

defaultValue?: any

⚠️ You probably wantinitialValue!⚠️

Before using this prop, read and understand the 🏁 Final Form documentation oninitialValue anddefaultValue!

initialValue?: any

See the 🏁 Final Form docs oninitialValue

isEqual?: (allPreviousValues: Array<any>, allNewValues: Array<any>) => boolean

A function that can be used to compare two arrays of values (before and after every change) and calculate pristine/dirty checks. Defaults to a function that will=== check each element of the array.

subscription?: FieldSubscription

AFieldSubscriptionthat selects of all the items ofFieldState that youwish to update for. If you don't pass asubscription prop, it defaults toall ofFieldState.

validate?: (value: ?any[], allValues: Object) => ?any

A function that takes the field value, and all the values of the form andreturns an error if the array value is invalid, orundefined if the value isvalid.

FieldArrayRenderProps

These are the props that<FieldArray/> provides toyour render function or component. This object is divided into afields objectthat mimics an iterable (e.g. it hasmap() andforEach() andlength), andmeta data about the field array. Keep in mind thatthe values inmeta aredependent on you having subscribed to them with thesubscription prop

fields.forEach: (iterator: (name: string, index: number) => void) => void

Iterates through all of the names of the fields in the field array in bracketformat, e.g.foo[0],foo[1],foo[2].

fields.insert: (index: number, value: any) => void

A function to insert a value into any arbitrary index of the array.

fields.map: (iterator: (name: string, index: number) => any) => any[]

Iterates through all of the names of the fields in the field array in bracketformat, e.g.foo[0],foo[1],foo[2], and collects the results of theiterator function. You will use this in almost every implementation.

fields.move: (from: number, to: number) => void

A function to move a value from one index to another. Useful for drag-and-dropreordering.

fields.name: string

The name of the field array.

fields.pop: () => any

A function to remove a value from the end of the array. The value will bereturned.

fields.push: (value: any) => void

A function to add a value to the end of the array.

fields.remove: (index: number) => any

A function to remove a value from an arbitrary index of the array.

fields.shift: () => any

A function to remove a value from the beginning of the array. The value will bereturned.

fields.swap: (indexA: number, indexB: number) => void

A function to swap two values in the array.

fields.update: (index: number, value: any) => void

Updates a value of the specified index of the array field.

fields.unshift: (value: any) => void

A function to add a value to the beginning of the array.

fields.value: any[]

The value of the array. Should be treated as readonly.

meta.active?: boolean

See the 🏁 Final Form docs onactive.

meta.data: Object

See the 🏁 Final Form docs ondata.

meta.dirty?: boolean

See the 🏁 Final Form docs ondirty.

meta.error?: any

See the 🏁 Final Form docs onerror.

meta.initial?: any

See the 🏁 Final Form docs oninitial.

meta.invalid?: boolean

See the 🏁 Final Form docs oninvalid.

meta.pristine?: boolean

See the 🏁 Final Form docs onpristine.

meta.submitError?: any

See the 🏁 Final Form docs onsubmitError.

meta.submitFailed?: boolean

See the 🏁 Final Form docs onsubmitFailed.

meta.submitSucceeded?: boolean

See the 🏁 Final Form docs onsubmitSucceeded.

meta.touched?: boolean

See the 🏁 Final Form docs ontouched.

meta.valid?: boolean

See the 🏁 Final Form docs onvalid.

meta.visited?: boolean

See the 🏁 Final Form docs onvisited.

About

A component for rendering and editing arrays 🏁 React Final Form

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors23


[8]ページ先頭

©2009-2025 Movatter.jp