1- import React , { Fragment } from 'react'
1+ import React , { forwardRef , Fragment } from 'react'
22import PropTypes from 'prop-types'
33
44// Common
@@ -12,97 +12,105 @@ import { Hint } from '../Hint'
1212// Fields
1313import { StyledField } from './styles'
1414
15- export const Field = ( {
16- autoFocus,
17- checked,
18- children,
19- component : Component ,
20- connected,
21- disabled,
22- disabledIcon,
23- error,
24- flexDirection,
25- hint,
26- label,
27- name,
28- onBlur,
29- onChange,
30- onFocus,
31- onKeyDown,
32- options,
33- placeholder,
34- required,
35- touched,
36- type,
37- value,
38- warning,
39- ...props
40- } ) => {
41- // Return early if no component
42- if ( ! Component ) {
43- return null
44- }
15+ export const Field = forwardRef (
16+ (
17+ {
18+ autoFocus,
19+ checked,
20+ children,
21+ component : Component ,
22+ connected,
23+ disabled,
24+ disabledIcon,
25+ error,
26+ flexDirection,
27+ hint,
28+ label,
29+ name,
30+ onBlur,
31+ onChange,
32+ onFocus,
33+ onKeyDown,
34+ options,
35+ placeholder,
36+ required,
37+ touched,
38+ type,
39+ value,
40+ warning,
41+ ...props
42+ } ,
43+ ref
44+ ) => {
45+ // Return early if no component
46+ if ( ! Component ) {
47+ return null
48+ }
4549
46- const baseType = getBaseType ( Component . type || type )
47- const variant = getVariant ( { connected, touched, warning, error } )
48- const hintText = getHintText ( { connected, touched, warning, error, hint } )
49- const isRadio = baseType === 'radio'
50- const isCheckable = [ 'checkbox' , 'radio' ] . includes ( baseType )
50+ const baseType = getBaseType ( Component . type || type )
51+ const variant = getVariant ( { connected, touched, warning, error } )
52+ const hintText = getHintText ( { connected, touched, warning, error, hint } )
53+ const isRadio = baseType === 'radio'
54+ const isCheckable = [ 'checkbox' , 'radio' ] . includes ( baseType )
5155
52- const isShowRequired = isRadio ? null : required
53- const layout = flexDirection || ( isCheckable ? 'row' : 'column' )
54- const Container = layout === 'row' ? RowContainer : Fragment
55- const htmlFor = isRadio ? value : name
56+ const isShowRequired = isRadio ? null : required
57+ const layout = flexDirection || ( isCheckable ? 'row' : 'column' )
58+ const Container = layout === 'row' ? RowContainer : Fragment
59+ const htmlFor = isRadio ? value : name
5660
57- const field = (
58- < Component
59- autoFocus = { autoFocus }
60- checked = { checked }
61- disabled = { disabled }
62- flexDirection = { layout }
63- name = { name }
64- onBlur = { onBlur }
65- onChange = { onChange }
66- onFocus = { onFocus }
67- onKeyDown = { onKeyDown }
68- options = { options }
69- placeholder = { placeholder }
70- required = { required }
71- type = { baseType }
72- value = { value }
73- variant = { variant }
74- { ...props }
75- >
76- { children }
77- </ Component >
78- )
61+ const field = (
62+ < Component
63+ autoFocus = { autoFocus }
64+ checked = { checked }
65+ disabled = { disabled }
66+ flexDirection = { layout }
67+ inputRef = { ref }
68+ name = { name }
69+ onBlur = { onBlur }
70+ onChange = { onChange }
71+ onFocus = { onFocus }
72+ onKeyDown = { onKeyDown }
73+ options = { options }
74+ placeholder = { placeholder }
75+ required = { required }
76+ type = { baseType }
77+ value = { value }
78+ variant = { variant }
79+ { ...props }
80+ >
81+ { children }
82+ </ Component >
83+ )
7984
80- return (
81- < StyledField
82- checkableField = { isCheckable }
83- checked = { checked }
84- fieldType = { Component . type }
85- flexDirection = { layout }
86- >
87- < Container >
88- { label && (
89- < Label
90- disabled = { disabled }
91- disabledIcon = { disabledIcon }
92- htmlFor = { htmlFor }
93- required = { isShowRequired }
94- variant = { variant }
95- >
96- { isCheckable && field }
97- { label }
98- </ Label >
99- ) }
100- { ! isCheckable && field }
101- </ Container >
102- { hintText && < Hint variant = { variant } > { hintText } </ Hint > }
103- </ StyledField >
104- )
105- }
85+ return (
86+ < StyledField
87+ checkableField = { isCheckable }
88+ checked = { checked }
89+ fieldType = { Component . type }
90+ flexDirection = { layout }
91+ >
92+ < Container >
93+ { label && (
94+ < Label
95+ disabled = { disabled }
96+ disabledIcon = { disabledIcon }
97+ htmlFor = { htmlFor }
98+ required = { isShowRequired }
99+ variant = { variant }
100+ >
101+ { isCheckable && field }
102+ { label }
103+ </ Label >
104+ ) }
105+ { ! isCheckable && field }
106+ </ Container >
107+ { hintText && < Hint variant = { variant } > { hintText } </ Hint > }
108+ </ StyledField >
109+ )
110+ }
111+ )
112+
113+ Field . displayName = 'Field'
106114
107115Field . propTypes = {
108116 autoFocus : PropTypes . bool ,
0 commit comments