forked from final-form/react-final-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.js
78 lines (72 loc) · 1.39 KB
/
Field.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// @flow
import * as React from 'react'
import type { FieldProps as Props, FieldRenderProps } from './types'
import renderComponent from './renderComponent'
import useField from './useField'
const Field = React.forwardRef<any, Props>(function Field(
{
afterSubmit,
allowNull,
beforeSubmit,
children,
component,
data,
defaultValue,
format,
formatOnBlur,
initialValue,
isEqual,
multiple,
name,
parse,
subscription,
type,
validate,
validateFields,
value,
...rest
}: Props,
ref
) {
const field: FieldRenderProps = useField(name, {
afterSubmit,
allowNull,
beforeSubmit,
children,
component,
data,
defaultValue,
format,
formatOnBlur,
initialValue,
isEqual,
multiple,
parse,
subscription,
type,
validate,
validateFields,
value
})
if (typeof children === 'function') {
return (children: Function)({ ...field, ...rest })
}
if (typeof component === 'string') {
// ignore meta, combine input with any other props
return React.createElement(component, {
...field.input,
children,
ref,
...rest
})
}
if (!name) {
throw new Error('prop name cannot be undefined in <Field> component')
}
return renderComponent(
{ children, component, ref, ...rest },
field,
`Field(${name})`
)
})
export default Field