Skip to content

chore(FormField): use React.forwardRef() #4359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/collections/Form/FormField.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Radio from '../../addons/Radio'
* @see Radio
* @see Select
*/
function FormField(props) {
const FormField = React.forwardRef(function (props, ref) {
const {
children,
className,
Expand Down Expand Up @@ -77,14 +77,14 @@ function FormField(props) {
if (_.isNil(control)) {
if (_.isNil(label)) {
return (
<ElementType {...rest} className={classes} id={id}>
<ElementType {...rest} className={classes} id={id} ref={ref}>
{childrenUtils.isNil(children) ? content : children}
</ElementType>
)
}

return (
<ElementType {...rest} className={classes} id={id}>
<ElementType {...rest} className={classes} id={id} ref={ref}>
{errorLabelBefore}
{createHTMLLabel(label, { autoGenerateKey: false })}
{errorLabelAfter}
Expand All @@ -101,7 +101,7 @@ function FormField(props) {
'aria-describedby': ariaDescribedBy,
'aria-invalid': error ? true : undefined,
}
const controlProps = { ...rest, content, children, disabled, required, type, id }
const controlProps = { ...rest, content, children, disabled, required, type, id, ref }

// wrap HTML checkboxes/radios in the label
if (control === 'input' && (type === 'checkbox' || type === 'radio')) {
Expand Down Expand Up @@ -142,7 +142,9 @@ function FormField(props) {
{errorLabelAfter}
</ElementType>
)
}
})

FormField.displayName = 'FormField'

FormField.propTypes = {
/** An element type to render as (string or function). */
Expand Down
35 changes: 35 additions & 0 deletions test/specs/collections/Form/FormField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@ describe('FormField', () => {
common.isConformant(FormField)
common.rendersChildren(FormField)

// No Control
common.forwardsRef(FormField)
common.forwardsRef(FormField, {
tagName: 'div',
requiredProps: {
children: <input />,
},
})

// HTML Checkbox/Radio Control
common.forwardsRef(FormField, {
tagName: 'input',
requiredProps: { control: 'input', type: 'radio' },
})
common.forwardsRef(FormField, {
tagName: 'input',
requiredProps: { control: 'input', type: 'checkbox' },
})

// Checkbox/Radio Control
common.forwardsRef(FormField, {
tagName: 'input',
requiredProps: { control: Checkbox },
})
common.forwardsRef(FormField, {
tagName: 'input',
requiredProps: { control: Radio },
})

// Other Control
common.forwardsRef(FormField, {
tagName: 'input',
requiredProps: { control: 'input' },
})

common.implementsHTMLLabelProp(FormField, { autoGenerateKey: false })
common.implementsWidthProp(FormField, SUI.WIDTHS, {
canEqual: false,
Expand Down