Skip to content

Commit 17eed8f

Browse files
authored
fix(FormItem): allow ids on children (#4412)
It's sometimes necessary to set the `id` on controls yourself, e.g. when displaying a popover.
1 parent 7265c84 commit 17eed8f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/main/src/components/Form/Form.cy.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const component = (
2424
<Input data-testid="formInput2" type={InputType.Text} />
2525
</FormItem>
2626
<FormItem label={<Label>item 4</Label>}>
27-
<Input type={InputType.Number} />
27+
<Input type={InputType.Number} id="test-id" />
2828
</FormItem>
2929
</FormGroup>
3030
</Form>
@@ -81,6 +81,10 @@ describe('Form', () => {
8181
// custom `Label`
8282
cy.findAllByText(`item 4`).eq(0).should('be.visible');
8383
cy.findAllByText(`item 4`).eq(1).should('exist').should('not.be.visible');
84+
85+
// custom id child of FormItem
86+
cy.get('#test-id').should('have.length', 1).should('be.visible');
87+
cy.get('[for="test-id"]').should('have.length', 1).should('not.be.visible');
8488
});
8589

8690
it('FilterItem: doesnt crash with portal as child', () => {

packages/main/src/components/FormItem/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,12 @@ const FormItem = (props: FormItemPropTypes) => {
156156
// @ts-expect-error: type can't be string because of `isValidElement`
157157
if (isValidElement(child) && child.type && child.type.$$typeof !== Symbol.for('react.portal')) {
158158
const content = getContentForHtmlLabel(label);
159+
const childId = child?.props?.id;
159160
return (
160161
<Fragment key={`${content}-${uniqueId}`}>
161162
{/*@ts-expect-error: child is ReactElement*/}
162-
{cloneElement(child, { id: `${uniqueId}-${index}` })}
163-
<label htmlFor={`${uniqueId}-${index}`} style={{ display: 'none' }} aria-hidden={true}>
163+
{cloneElement(child, { id: childId ?? `${uniqueId}-${index}` })}
164+
<label htmlFor={childId ?? `${uniqueId}-${index}`} style={{ display: 'none' }} aria-hidden={true}>
164165
{content}
165166
</label>
166167
</Fragment>

0 commit comments

Comments
 (0)