Skip to content

Commit 8ed6149

Browse files
authored
data-component adr part 3 (#7819)
1 parent 3f847d7 commit 8ed6149

16 files changed

Lines changed: 119 additions & 8 deletions

File tree

.changeset/tiny-hats-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
Add `data-component` attributes for Details, Flash, FormControl (+ update InputValidation to forward from FormControl.Validation), Header, and Heading.

packages/react/src/Details/Details.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const Root = React.forwardRef<HTMLDetailsElement, DetailsProps>(
2727
}, [])
2828

2929
return (
30-
<details className={clsx(className, classes.Details)} {...rest} ref={ref}>
30+
<details className={clsx(className, classes.Details)} {...rest} ref={ref} data-component="Details">
3131
{children}
3232
</details>
3333
)
@@ -47,7 +47,7 @@ export type SummaryProps<As extends React.ElementType> = {
4747
function Summary<As extends React.ElementType>({as, children, ...props}: SummaryProps<As>) {
4848
const Component = as ?? 'summary'
4949
return (
50-
<Component as={Component === 'summary' ? null : 'summary'} {...props}>
50+
<Component as={Component === 'summary' ? null : 'summary'} {...props} data-component="Details.Summary">
5151
{children}
5252
</Component>
5353
)

packages/react/src/Details/__tests__/Details.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,17 @@ describe('Details', () => {
9797
expect(screen.getByText('test summary')).toHaveAttribute('data-testid', 'test')
9898
})
9999
})
100+
101+
describe('Details data-component attributes', () => {
102+
it('renders data-component attributes', () => {
103+
render(
104+
<Details>
105+
<Details.Summary>test summary</Details.Summary>
106+
</Details>,
107+
)
108+
109+
expect(screen.getByRole('group')).toHaveAttribute('data-component', 'Details')
110+
expect(screen.getByText('test summary')).toHaveAttribute('data-component', 'Details.Summary')
111+
})
112+
})
100113
})

packages/react/src/Flash/Flash.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const Flash = React.forwardRef(function Flash(
2020
className={clsx(classes.Flash, className)}
2121
data-full={full ? '' : undefined}
2222
data-variant={variant}
23+
data-component="Flash"
2324
/>
2425
)
2526
}) as PolymorphicForwardRefComponent<'div', FlashProps>

packages/react/src/Flash/__tests__/Flash.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ describe('Flash', () => {
3737
const {container} = render(<Flash data-testid="test" />)
3838
expect(container.firstChild).toHaveAttribute('data-testid', 'test')
3939
})
40+
41+
it('renders data-component attribute', () => {
42+
render(<Flash data-testid="flash" />)
43+
expect(screen.getByTestId('flash')).toHaveAttribute('data-component', 'Flash')
44+
})
4045
})

packages/react/src/FormControl/FormControl.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>(
192192
data-has-leading-visual={slots.leadingVisual ? '' : undefined}
193193
className={clsx(className, classes.ControlHorizontalLayout)}
194194
style={style}
195+
data-component="FormControl"
195196
>
196197
{InputChildren}
197198
</div>
@@ -201,6 +202,7 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>(
201202
data-has-label={!isLabelHidden ? '' : undefined}
202203
className={clsx(className, classes.ControlVerticalLayout)}
203204
style={style}
205+
data-component="FormControl"
204206
>
205207
{slots.label}
206208
{React.isValidElement(InputComponent) &&

packages/react/src/FormControl/FormControlCaption.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function FormControlCaption({id, children, className, style}: FormControlCaption
1919
className={clsx(className, classes.Caption)}
2020
data-control-disabled={disabled ? '' : undefined}
2121
style={style}
22+
data-component="FormControl.Caption"
2223
>
2324
{children}
2425
</Text>

packages/react/src/FormControl/FormControlLabel.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ const FormControlLabel: FCWithSlotMarker<
5050
...props,
5151
}
5252

53-
return <InputLabel {...labelProps}>{children}</InputLabel>
53+
return (
54+
<InputLabel {...labelProps} data-component="FormControl.Label">
55+
{children}
56+
</InputLabel>
57+
)
5458
}
5559

5660
FormControlLabel.__SLOT__ = Symbol('FormControl.Label')

packages/react/src/FormControl/FormControlLeadingVisual.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const FormControlLeadingVisual: FCWithSlotMarker<React.PropsWithChildren<{style?
1414
data-control-disabled={disabled ? '' : undefined}
1515
style={style}
1616
data-has-caption={captionId ? '' : undefined}
17+
data-component="FormControl.LeadingVisual"
1718
>
1819
{children}
1920
</div>

packages/react/src/FormControl/_FormControlValidation.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const FormControlValidation: FCWithSlotMarker<React.PropsWithChildren<FormContro
2525
validationStatus={variant}
2626
id={id || validationMessageId || ''}
2727
style={style}
28+
data-component="FormControl.Validation"
2829
>
2930
{children}
3031
</InputValidation>

0 commit comments

Comments
 (0)