11// @flow
22import * as React from 'react'
3- import PropTypes from 'prop-types'
43import { fieldSubscriptionItems } from 'final-form'
54import diffSubscription from './diffSubscription'
65import type { FieldSubscription , FieldState } from 'final-form'
7- import type {
8- FieldProps as Props ,
9- FieldRenderProps ,
10- ReactContext
11- } from './types'
6+ import type { FieldProps as Props , FieldRenderProps } from './types'
127import renderComponent from './renderComponent'
138import isReactNative from './isReactNative'
149import getValue from './getValue'
10+ import { withReactFinalForm } from './reactFinalFormContext'
1511
1612const all : FieldSubscription = fieldSubscriptionItems . reduce ( ( result , key ) => {
1713 result [ key ] = true
@@ -23,32 +19,27 @@ type State = {
2319}
2420
2521class Field extends React . Component < Props , State > {
26- context : ReactContext
2722 props : Props
2823 state : State
2924 unsubscribe : ( ) => void
3025
31- static contextTypes = {
32- reactFinalForm : PropTypes . object
33- }
34-
3526 static defaultProps = {
3627 format : ( value : ?any , name : string ) => ( value === undefined ? '' : value ) ,
3728 parse : ( value : ?any , name : string ) => ( value === '' ? undefined : value )
3829 }
3930
40- constructor ( props : Props , context : ReactContext ) {
41- super ( props , context )
31+ constructor ( props : Props ) {
32+ super ( props )
4233 let initialState
4334
4435 // istanbul ignore next
45- if ( process . env . NODE_ENV !== 'production' && ! context . reactFinalForm ) {
36+ if ( process . env . NODE_ENV !== 'production' && ! this . props . reactFinalForm ) {
4637 console . error (
4738 'Warning: Field must be used inside of a ReactFinalForm component'
4839 )
4940 }
5041
51- if ( this . context . reactFinalForm ) {
42+ if ( this . props . reactFinalForm ) {
5243 // avoid error, warning will alert developer to their mistake
5344 this . subscribe ( props , ( state : FieldState ) => {
5445 if ( initialState ) {
@@ -65,7 +56,7 @@ class Field extends React.Component<Props, State> {
6556 { isEqual, name, subscription, validateFields } : Props ,
6657 listener : ( state : FieldState ) = > void
6758 ) => {
68- this . unsubscribe = this . context . reactFinalForm . registerField (
59+ this . unsubscribe = this . props . reactFinalForm . registerField (
6960 name ,
7061 listener ,
7162 subscription || all ,
@@ -89,7 +80,7 @@ class Field extends React.Component<Props, State> {
8980 fieldSubscriptionItems
9081 )
9182 ) {
92- if ( this . context . reactFinalForm ) {
83+ if ( this . props . reactFinalForm ) {
9384 // avoid error, warning will alert developer to their mistake
9485 this . unsubscribe ( )
9586 this . subscribe ( this . props , this . notify )
@@ -171,6 +162,7 @@ class Field extends React.Component<Props, State> {
171162 subscription,
172163 validate,
173164 validateFields,
165+ reactFinalForm,
174166 value : _value ,
175167 ...rest
176168 } = this . props
@@ -235,4 +227,4 @@ class Field extends React.Component<Props, State> {
235227 }
236228}
237229
238- export default Field
230+ export default withReactFinalForm ( Field )
0 commit comments