Skip to content

Commit 8b4230d

Browse files
authored
Added FormApi to Form and FormSpy props. Deprecated old. (#224)
1 parent ccb43fe commit 8b4230d

File tree

11 files changed

+415
-56
lines changed

11 files changed

+415
-56
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,38 +649,56 @@ as well as:
649649

650650
#### `batch: (fn: () => void) => void)`
651651

652+
_**[DEPRECATED]** Use `form.batch()` instead_
653+
652654
A function that allows batch updates to be done to the form state.
653655
[See the 🏁 Final Form docs on `batch`](https://github.com/final-form/final-form#batch-fn---void--void).
654656

655657
#### `blur: (name: string) => void`
656658

659+
_**[DEPRECATED]** Use `form.blur()` instead_
660+
657661
A function to blur (mark inactive) any field.
658662

659663
#### `change: (name: string, value: any) => void`
660664

665+
_**[DEPRECATED]** Use `form.change()` instead_
666+
661667
A function to change the value of any field.
662668

663669
#### `focus: (name: string) => void`
664670

671+
_**[DEPRECATED]** Use `form.focus()` instead_
672+
665673
A function to focus (mark active) any field.
666674

675+
#### `form: FormApi`
676+
677+
The 🏁 Final Form [`FormApi`](https://github.com/final-form/final-form#formapi).
678+
667679
#### `handleSubmit: (?SyntheticEvent<HTMLFormElement>) => void`
668680

669681
A function intended for you to give directly to the `<form>` tag: `<form onSubmit={handleSubmit}/>`.
670682

671683
#### `initialize: (values: Object) => void`
672684

685+
_**[DEPRECATED]** Use `form.initialize()` instead_
686+
673687
A function that initializes the form values.
674688
[See the 🏁 Final Form docs on `initialize`](https://github.com/final-form/final-form#initialize-values-object--void).
675689

676690
#### `mutators?: { [string]: Function }`
677691

692+
_**[DEPRECATED]** Use `form.mutators` instead_
693+
678694
[See the 🏁 Final Form docs on `mutators`](https://github.com/final-form/final-form#mutators--string-function-).
679695

680-
#### `reset: () => void`
696+
#### `reset: (newInitialValues?: Object) => void`
697+
698+
_**[DEPRECATED]** Use `form.reset()` instead_
681699

682700
A function that resets the form values to their last initialized values.
683-
[See the 🏁 Final Form docs on `reset`](https://github.com/final-form/final-form#reset---void).
701+
[See the 🏁 Final Form docs on `reset`](https://github.com/final-form/final-form#reset-initialvalues-object--void).
684702

685703
### `FormSpyProps`
686704

@@ -734,34 +752,52 @@ subscribed to with the
734752

735753
#### `batch: (fn: () => void) => void)`
736754

755+
_**[DEPRECATED]** Use `form.batch()` instead_
756+
737757
A function that allows batch updates to be done to the form state.
738758
[See the 🏁 Final Form docs on `batch`](https://github.com/final-form/final-form#batch-fn---void--void).
739759

740760
#### `blur: (name: string) => void`
741761

762+
_**[DEPRECATED]** Use `form.blur()` instead_
763+
742764
A function to blur (mark inactive) any field.
743765

744766
#### `change: (name: string, value: any) => void`
745767

768+
_**[DEPRECATED]** Use `form.change()` instead_
769+
746770
A function to change the value of any field.
747771

748772
#### `focus: (name: string) => void`
749773

774+
_**[DEPRECATED]** Use `form.focus()` instead_
775+
750776
A function to focus (mark active) any field.
751777

778+
#### `form: FormApi`
779+
780+
The 🏁 Final Form [`FormApi`](https://github.com/final-form/final-form#formapi).
781+
752782
#### `initialize: (values: Object) => void`
753783

784+
_**[DEPRECATED]** Use `form.initialize()` instead_
785+
754786
A function that initializes the form values.
755787
[See the 🏁 Final Form docs on `initialize`](https://github.com/final-form/final-form#initialize-values-object--void).
756788

757789
#### `mutators?: { [string]: Function }`
758790

791+
_**[DEPRECATED]** Use `form.mutators` instead_
792+
759793
[See the 🏁 Final Form docs on `mutators`](https://github.com/final-form/final-form#mutators--string-function-).
760794

761-
#### `reset: () => void`
795+
#### `reset: (newInitialValues?: Object) => void`
796+
797+
_**[DEPRECATED]** Use `form.reset()` instead_
762798

763799
A function that resets the form values to their last initialized values.
764-
[See the 🏁 Final Form docs on `reset`](https://github.com/final-form/final-form#reset---void).
800+
[See the 🏁 Final Form docs on `reset`](https://github.com/final-form/final-form#reset-initialvalues-object--void).
765801

766802
## Contributors
767803

package-lock.json

Lines changed: 25 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"homepage": "https://github.com/final-form/react-final-form#readme",
2929
"devDependencies": {
30-
"@types/react": "^16.3.8",
30+
"@types/react": "^16.3.9",
3131
"babel-eslint": "^8.0.3",
3232
"babel-jest": "^22.4.3",
3333
"babel-plugin-external-helpers": "^6.22.0",
@@ -41,10 +41,10 @@
4141
"eslint-config-react-app": "^2.1.0",
4242
"eslint-plugin-babel": "^5.0.0",
4343
"eslint-plugin-flowtype": "^2.46.1",
44-
"eslint-plugin-import": "^2.10.0",
44+
"eslint-plugin-import": "^2.11.0",
4545
"eslint-plugin-jsx-a11y": "^6.0.2",
4646
"eslint-plugin-react": "^7.7.0",
47-
"final-form": "^4.5.1",
47+
"final-form": "^4.5.2",
4848
"flow-bin": "^0.69.0",
4949
"glow": "^1.2.2",
5050
"husky": "^0.14.3",
@@ -53,7 +53,7 @@
5353
"nps": "^5.9.0",
5454
"nps-utils": "^1.5.0",
5555
"opencollective": "^1.0.3",
56-
"prettier": "^1.11.1",
56+
"prettier": "^1.12.0",
5757
"prettier-eslint-cli": "^4.7.1",
5858
"prop-types": "^15.6.1",
5959
"react": "^16.3.1",

src/Field.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import PropTypes from 'prop-types'
44
import { fieldSubscriptionItems } from 'final-form'
55
import diffSubscription from './diffSubscription'
66
import type { FieldSubscription, FieldState } from 'final-form'
7-
import type { FieldProps as Props, ReactContext } from './types'
7+
import type {
8+
FieldProps as Props,
9+
FieldRenderProps,
10+
ReactContext
11+
} from './types'
812
import renderComponent from './renderComponent'
913
import isReactNative from './isReactNative'
1014
import getValue from './getValue'
@@ -160,8 +164,24 @@ export default class Field extends React.Component<Props, State> {
160164
value: _value,
161165
...rest
162166
} = this.props
163-
let { blur, change, focus, value, name: ignoreName, ...meta } =
167+
let { blur, change, focus, value, name: ignoreName, ...otherState } =
164168
this.state.state || {}
169+
const meta = {
170+
// this is to appease the Flow gods
171+
active: otherState.active,
172+
dirty: otherState.dirty,
173+
dirtySinceLastSubmit: otherState.dirtySinceLastSubmit,
174+
error: otherState.error,
175+
initial: otherState.initial,
176+
invalid: otherState.invalid,
177+
pristine: otherState.pristine,
178+
submitError: otherState.submitError,
179+
submitFailed: otherState.submitFailed,
180+
submitSucceeded: otherState.submitSucceeded,
181+
touched: otherState.touched,
182+
valid: otherState.valid,
183+
visited: otherState.visited
184+
}
165185
if (format) {
166186
value = format(value, name)
167187
}
@@ -191,8 +211,9 @@ export default class Field extends React.Component<Props, State> {
191211
// ignore meta, combine input with any other props
192212
return React.createElement(component, { ...input, children, ...rest })
193213
}
214+
const renderProps: FieldRenderProps = { input, meta } // assign to force Flow check
194215
return renderComponent(
195-
{ input, meta, children, component, ...rest },
216+
{ ...renderProps, children, component, ...rest },
196217
`Field(${name})`
197218
)
198219
}

src/Field.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ describe('Field', () => {
422422
const requiredUppercase = value =>
423423
!value
424424
? 'Required'
425-
: value.toUpperCase() === value ? undefined : 'Must be uppercase'
425+
: value.toUpperCase() === value
426+
? undefined
427+
: 'Must be uppercase'
426428
class FieldsContainer extends React.Component {
427429
state = { uppercase: false }
428430

@@ -454,7 +456,9 @@ describe('Field', () => {
454456
expect(input).toHaveBeenCalledTimes(2)
455457
expect(input.mock.calls[1][0].meta.error).toBe('Required')
456458

457-
const { input: { onChange } } = input.mock.calls[1][0]
459+
const {
460+
input: { onChange }
461+
} = input.mock.calls[1][0]
458462

459463
onChange('hi')
460464

@@ -584,7 +588,7 @@ describe('Field', () => {
584588
expect(barInput.checked).toBe(true)
585589
expect(bazInput.checked).toBe(false)
586590

587-
render.mock.calls[0][0].change('foo', 'Baz')
591+
render.mock.calls[0][0].form.change('foo', 'Baz')
588592

589593
expect(barInput.checked).toBe(false)
590594
expect(bazInput.checked).toBe(true)

0 commit comments

Comments
 (0)