🏁 Final Form Calculate
Decorator for 🏁 Final Form that
allows you to define calculations that happen between fields, i.e. "When field X
changes, update field Y."
Installation
npm install --save final-form-calculate
or
yarn add final-form-calculate
Usage
import { createForm } from 'final-form'
import createDecorator from 'final-form-calculate'
const form = createForm({ onSubmit })
const decorator = createDecorator(
{
field: 'foo',
updates: [
{
doubleFoo: (fooValue, allValues) => fooValue * 2
}
]
},
{
field: /items\[\d+\]/,
updates: {
total: (itemValue, allValues) =>
(allValues.items || []).reduce((sum, value) => sum + value, 0)
}
}
)
const undecorate = decorator(form)
Table of Contents
Example
Example using
🏁 React Final Form.
API
createDecorator: (...calculations: Calculation[]) => Decorator
A function that takes a set of calculations and returns a 🏁 Final Form
Decorator
.
Types
Calculation: { field: FieldPattern, updates: Updates }
A calculation to perform
FieldName: string
FieldPattern: FieldName | RegExp
A pattern to match a field with.
Updates: { [FieldName]: (value: any, allValues: Object) => any }
Updates to make on other fields.