Skip to content

Commit bb128e1

Browse files
agwellskyletsang
authored andcommitted
fix(FormLabel): forward ref when rendering as Col (#5835)
1 parent fa570e0 commit bb128e1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/FormLabel.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ const FormLabel: FormLabel = React.forwardRef(
107107

108108
if (column)
109109
return (
110-
<Col as="label" className={classes} htmlFor={htmlFor} {...props} />
110+
<Col
111+
ref={ref as React.ForwardedRef<HTMLLabelElement>}
112+
as="label"
113+
className={classes}
114+
htmlFor={htmlFor}
115+
{...props}
116+
/>
111117
);
112118

113119
return (

test/FormLabelSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ describe('<FormLabel>', () => {
7171
expect(instance.input.tagName).to.equal('LABEL');
7272
});
7373

74+
it('should support ref forwarding when rendered as a Col', () => {
75+
class Container extends React.Component {
76+
render() {
77+
return (
78+
<FormGroup controlId="foo">
79+
<FormLabel
80+
type="text"
81+
column
82+
ref={(ref) => {
83+
this.input = ref;
84+
}}
85+
/>
86+
</FormGroup>
87+
);
88+
}
89+
}
90+
91+
const instance = mount(<Container />).instance();
92+
expect(instance.input.tagName).to.equal('LABEL');
93+
});
94+
7495
it('accepts as prop', () => {
7596
mount(<FormLabel as="legend">body</FormLabel>).assertSingle('legend');
7697
});

0 commit comments

Comments
 (0)