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
import { createForm } from 'final-form' | |
import arrayMutators from 'final-form-arrays' | |
// Create Form | |
const form = createForm({ | |
mutators: { | |
// potentially other mutators here | |
...arrayMutators | |
}, | |
onSubmit |
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
import { createForm } from 'final-form' | |
import createDecorator from 'final-form-calculate' | |
// Create Form | |
const form = createForm({ onSubmit }) | |
// Create Decorator | |
const decorator = createDecorator( | |
// Calculations: | |
{ |
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
import { createForm } from 'final-form' | |
import setFieldData from 'final-form-set-field-data' | |
// Create Form | |
const form = createForm({ | |
mutators: { setFieldData }, | |
onSubmit | |
}) | |
form.mutators.setFieldData('firstName', { awesome: true }) |
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
const WarningEngine = ({ mutators: { setFieldData } }) => ( | |
// FormSpy lets you listen to any part of the form state you want. | |
// If you provide an onChange prop, FormSpy will not render to the DOM. | |
<FormSpy | |
subscription={{ values: true }} | |
onChange={({ values }) => { | |
setFieldData('firstName', { | |
warning: values.firstName ? undefined : 'Recommended' | |
}) | |
setFieldData('lastName', { |
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
import React from 'react' | |
import { Form, Field } from 'react-final-form | |
import createDecorator from 'final-form-focus' | |
const focusOnErrors = createDecorator() | |
... | |
<Form | |
onSubmit={submit} | |
decorators={[ focusOnErrors ]} // <--------- 😎 | |
validate={validate} |
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
const WhenFieldChanges = ({ field, becomes, set, to }) => ( | |
<Field name={set} subscription={{}}> | |
{( | |
// No subscription. We only use Field to get to the change function | |
{ input: { onChange } } | |
) => ( | |
<OnChange name={field}> | |
{value => { | |
if (value === becomes) { | |
onChange(to) |
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
<MakeAsyncFunction | |
listener={promiseListener} | |
start="START_ACTION_TYPE" // the type of action to dispatch when this function is called | |
resolve="RESOLVE_ACTION_TYPE" // the type of action that will resolve the promise | |
reject="REJECT_ACTION_TYPE" // the type of action that will reject the promise | |
>{asyncFunc => ( | |
<SomeFormLibrary onSubmit={asyncFunc}> | |
... |
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
function focusOnFirstError(formName, hasError) { | |
const form = document.forms[formName] | |
for (let i = 0; i < form.length; i++) { | |
if (hasError(form[i].name)) { | |
form[i].focus() | |
break | |
} | |
} | |
} |
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
/* | |
* As I have been migrating a project from redux-form v5 to v6, I have come across this useful pattern. | |
* | |
* I have many such "custom inputs and errors beside them" structures around my app. | |
*/ | |
// v5 code | |
<div> | |
<MyCustomInputThing | |
{...myField} |
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
componentWillReceiveProps(nextProps) { | |
if(!this.props.deadDrop && nextProps.deadDrop) { | |
// dispatch an action to clear the value | |
this.props.clearDeadDrop() | |
// do what your case officer has instructed, Agent | |
this.performOperation(nextProps.deadDrop) | |
} | |
} |