Skip to content

Commit af24cfa

Browse files
authored
fix(FormLabel): forward ref when rendering as Col (#5835)
1 parent 38e5cf3 commit af24cfa

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
@@ -106,7 +106,13 @@ const FormLabel: BsPrefixRefForwardingComponent<'label', FormLabelProps> =
106106

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

112118
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)