Skip to content

Commit c20ac66

Browse files
rosskevinerikras
authored andcommitted
[typescript] possibly undefined mutators (on render) are difficult to use (#129)
* [typescript] show mutator example - undefined mutators are difficult * Fixed and tested with final-form/final-form#68
1 parent 5237673 commit c20ac66

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/ReactFinalForm.d.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react'
22
import { Form, Field } from './index'
3+
import { Mutator } from 'final-form/dist'
34

45
const onSubmit = async (values: any) => {
56
console.log(values)
@@ -63,3 +64,36 @@ const simpleSubscription = () => (
6364
)}
6465
</Form>
6566
)
67+
68+
const setValue: Mutator = ([name, newValue], state, { changeValue }) => {
69+
changeValue(state, name, value => newValue)
70+
}
71+
72+
const mutated = () => (
73+
<Form onSubmit={onSubmit} mutators={{ setValue }}>
74+
{({
75+
handleSubmit,
76+
mutators: { setValue },
77+
submitting,
78+
pristine,
79+
values
80+
}) => (
81+
<form onSubmit={handleSubmit}>
82+
<Field
83+
name="firstName"
84+
component="input"
85+
type="text"
86+
placeholder="First Name"
87+
/>
88+
<button
89+
type="button"
90+
onClick={e => setValue('firstName', 'Kevin')}
91+
disabled={submitting || pristine}
92+
>
93+
Reset
94+
</button>
95+
<pre>{JSON.stringify(values)}</pre>
96+
</form>
97+
)}
98+
</Form>
99+
)

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface SubsetFormApi {
4444
change: (name: string, value: any) => void
4545
focus: (name: string) => void
4646
initialize: (values: object) => void
47-
mutators?: { [key: string]: Function }
47+
mutators: { [key: string]: Function }
4848
reset: () => void
4949
}
5050

0 commit comments

Comments
 (0)