Skip to content

Commit 1796beb

Browse files
chore(FormField): use React.forwardRef() (#4359)
* Add ref to form field component * Move ref to controlProps * Add more forwardsRef tests * Remove unnecessary describe wrapper * update tests Co-authored-by: Oleksandr Fediashov <[email protected]>
1 parent ae0e904 commit 1796beb

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/collections/Form/FormField.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Radio from '../../addons/Radio'
2727
* @see Radio
2828
* @see Select
2929
*/
30-
function FormField(props) {
30+
const FormField = React.forwardRef(function (props, ref) {
3131
const {
3232
children,
3333
className,
@@ -77,14 +77,14 @@ function FormField(props) {
7777
if (_.isNil(control)) {
7878
if (_.isNil(label)) {
7979
return (
80-
<ElementType {...rest} className={classes} id={id}>
80+
<ElementType {...rest} className={classes} id={id} ref={ref}>
8181
{childrenUtils.isNil(children) ? content : children}
8282
</ElementType>
8383
)
8484
}
8585

8686
return (
87-
<ElementType {...rest} className={classes} id={id}>
87+
<ElementType {...rest} className={classes} id={id} ref={ref}>
8888
{errorLabelBefore}
8989
{createHTMLLabel(label, { autoGenerateKey: false })}
9090
{errorLabelAfter}
@@ -101,7 +101,7 @@ function FormField(props) {
101101
'aria-describedby': ariaDescribedBy,
102102
'aria-invalid': error ? true : undefined,
103103
}
104-
const controlProps = { ...rest, content, children, disabled, required, type, id }
104+
const controlProps = { ...rest, content, children, disabled, required, type, id, ref }
105105

106106
// wrap HTML checkboxes/radios in the label
107107
if (control === 'input' && (type === 'checkbox' || type === 'radio')) {
@@ -142,7 +142,9 @@ function FormField(props) {
142142
{errorLabelAfter}
143143
</ElementType>
144144
)
145-
}
145+
})
146+
147+
FormField.displayName = 'FormField'
146148

147149
FormField.propTypes = {
148150
/** An element type to render as (string or function). */

test/specs/collections/Form/FormField-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,41 @@ describe('FormField', () => {
1313
common.isConformant(FormField)
1414
common.rendersChildren(FormField)
1515

16+
// No Control
17+
common.forwardsRef(FormField)
18+
common.forwardsRef(FormField, {
19+
tagName: 'div',
20+
requiredProps: {
21+
children: <input />,
22+
},
23+
})
24+
25+
// HTML Checkbox/Radio Control
26+
common.forwardsRef(FormField, {
27+
tagName: 'input',
28+
requiredProps: { control: 'input', type: 'radio' },
29+
})
30+
common.forwardsRef(FormField, {
31+
tagName: 'input',
32+
requiredProps: { control: 'input', type: 'checkbox' },
33+
})
34+
35+
// Checkbox/Radio Control
36+
common.forwardsRef(FormField, {
37+
tagName: 'input',
38+
requiredProps: { control: Checkbox },
39+
})
40+
common.forwardsRef(FormField, {
41+
tagName: 'input',
42+
requiredProps: { control: Radio },
43+
})
44+
45+
// Other Control
46+
common.forwardsRef(FormField, {
47+
tagName: 'input',
48+
requiredProps: { control: 'input' },
49+
})
50+
1651
common.implementsHTMLLabelProp(FormField, { autoGenerateKey: false })
1752
common.implementsWidthProp(FormField, SUI.WIDTHS, {
1853
canEqual: false,

0 commit comments

Comments
 (0)