Skip to content

Commit 12c7917

Browse files
authored
fix(Buttons): correctly support overriding default data-garden-id attribute (#1804)
1 parent 1fc76da commit 12c7917

File tree

20 files changed

+127
-78
lines changed

20 files changed

+127
-78
lines changed

packages/buttons/src/elements/Button.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import React from 'react';
99
import { render, renderRtl } from 'garden-test-utils';
1010
import { Button } from './Button';
1111
import TestIcon from '@zendeskgarden/svg-icons/src/16/gear-stroke.svg';
12+
import { COMPONENT_ID } from '../styled/StyledButton';
1213

1314
describe('Button', () => {
1415
it('is rendered as a button', () => {
@@ -24,6 +25,20 @@ describe('Button', () => {
2425
expect(container.firstChild).toBe(ref.current);
2526
});
2627

28+
describe('`data-garden-id` attribute', () => {
29+
it('sets a default data-garden-id attribute', () => {
30+
const { getByRole } = render(<Button />);
31+
32+
expect(getByRole('button')).toHaveAttribute('data-garden-id', COMPONENT_ID);
33+
});
34+
35+
it('supports overriding the data-garden-id attribute', () => {
36+
const { getByRole } = render(<Button data-garden-id="myCoolButton" />);
37+
38+
expect(getByRole('button')).toHaveAttribute('data-garden-id', 'myCoolButton');
39+
});
40+
});
41+
2742
describe('Icons', () => {
2843
it('successfully renders start and end icons', () => {
2944
const { getByTestId } = render(

packages/buttons/src/styled/StyledAnchor.spec.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import React from 'react';
99
import { render, renderRtl } from 'garden-test-utils';
10+
1011
import { PALETTE } from '@zendeskgarden/react-theming';
1112
import { StyledAnchor } from './StyledAnchor';
1213

@@ -34,4 +35,12 @@ describe('StyledAnchor', () => {
3435

3536
expect(container.firstChild).toHaveStyleRule('direction', 'rtl');
3637
});
38+
39+
describe('`data-garden-id` attribute', () => {
40+
it('has the correct `data-garden-id`', () => {
41+
const { container } = render(<StyledAnchor />);
42+
43+
expect(container.firstChild).toHaveAttribute('data-garden-id', 'buttons.anchor');
44+
});
45+
});
3746
});

packages/buttons/src/styled/StyledAnchor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import styled from 'styled-components';
99
import { retrieveComponentStyles, DEFAULT_THEME } from '@zendeskgarden/react-theming';
1010
import { StyledButton } from './StyledButton';
11+
import { IAnchorProps } from './../types';
1112

1213
const COMPONENT_ID = 'buttons.anchor';
1314

1415
/**
1516
* Accepts all `<a>` props
1617
*/
17-
export const StyledAnchor = styled(StyledButton).attrs(props => ({
18+
export const StyledAnchor = styled(StyledButton).attrs<IAnchorProps>(props => ({
1819
'data-garden-id': COMPONENT_ID,
1920
'data-garden-version': PACKAGE_VERSION,
2021
as: 'a',
@@ -24,7 +25,7 @@ export const StyledAnchor = styled(StyledButton).attrs(props => ({
2425
}))`
2526
direction: ${props => props.theme.rtl && 'rtl'};
2627
27-
${props => retrieveComponentStyles(COMPONENT_ID, props)};
28+
${props => retrieveComponentStyles((props as any)['data-garden-id'], props)};
2829
`;
2930

3031
StyledAnchor.defaultProps = {

packages/buttons/src/styled/StyledButton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { IButtonProps } from '../types';
1919
import { StyledSplitButton } from './StyledSplitButton';
2020
import { StyledIcon } from './StyledIcon';
2121

22-
const COMPONENT_ID = 'buttons.button';
22+
export const COMPONENT_ID = 'buttons.button';
2323

2424
const getBorderRadius = (props: IButtonProps & ThemeProps<DefaultTheme>) => {
2525
if (props.isPill) {
@@ -431,7 +431,7 @@ const sizeStyles = (props: IButtonProps & ThemeProps<DefaultTheme>) => {
431431
* 3. Shifting :focus-visible from LVHFA order to preserve `text-decoration` on hover
432432
*/
433433
export const StyledButton = styled.button.attrs<IButtonProps>(props => ({
434-
'data-garden-id': COMPONENT_ID,
434+
'data-garden-id': (props as any)['data-garden-id'] || COMPONENT_ID,
435435
'data-garden-version': PACKAGE_VERSION,
436436
type: props.type || 'button'
437437
}))<IButtonProps>`

packages/buttons/src/styled/StyledIcon.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ describe('StyledIcon', () => {
7979
}).toThrow();
8080
});
8181
});
82+
83+
describe('`data-garden-id` attribute', () => {
84+
it('has the correct `data-garden-id`', () => {
85+
const { container } = render(
86+
<StyledIcon>
87+
<TestIcon />
88+
</StyledIcon>
89+
);
90+
91+
expect(container.firstChild).toHaveAttribute('data-garden-id', 'buttons.icon');
92+
});
93+
});
8294
});

packages/buttons/src/styled/StyledIconButton.spec.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import React from 'react';
99
import { css } from 'styled-components';
1010
import { render } from 'garden-test-utils';
11-
import { StyledIconButton } from './StyledIconButton';
11+
import { COMPONENT_ID, StyledIconButton } from './StyledIconButton';
1212
import { StyledIcon } from './StyledIcon';
1313
import { PALETTE } from '@zendeskgarden/react-theming';
1414
import { rgba } from 'polished';
@@ -88,4 +88,12 @@ describe('StyledIconButton', () => {
8888
});
8989
});
9090
});
91+
92+
describe('`data-garden-id` attribute', () => {
93+
it('has the correct `data-garden-id`', () => {
94+
const { container } = render(<StyledIconButton />);
95+
96+
expect(container.firstChild).toHaveAttribute('data-garden-id', COMPONENT_ID);
97+
});
98+
});
9199
});

packages/buttons/src/styled/StyledIconButton.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import styled, { css, ThemeProps, DefaultTheme } from 'styled-components';
99
import { retrieveComponentStyles, DEFAULT_THEME, getColor } from '@zendeskgarden/react-theming';
1010
import { IButtonProps } from '../types';
11-
import { StyledButton, getHeight } from './StyledButton';
11+
import { COMPONENT_ID as BTN_COMPONENT_ID, StyledButton, getHeight } from './StyledButton';
1212
import { StyledIcon } from './StyledIcon';
1313

14-
const COMPONENT_ID = 'buttons.icon_button';
14+
export const COMPONENT_ID = 'buttons.icon_button';
1515

1616
const iconColorStyles = ({ theme }: IButtonProps & ThemeProps<DefaultTheme>) => {
1717
const options = { theme, variable: 'foreground.default' };
@@ -69,17 +69,21 @@ const iconStyles = (props: IButtonProps & ThemeProps<DefaultTheme>) => {
6969
`;
7070
};
7171

72-
export const StyledIconButton = styled(StyledButton as 'button').attrs({
73-
'data-garden-id': COMPONENT_ID,
74-
'data-garden-version': PACKAGE_VERSION
75-
})<IButtonProps>`
72+
export const StyledIconButton = styled(StyledButton).attrs<IButtonProps>(props => {
73+
const externalId: string = (props as any)['data-garden-id'];
74+
75+
return {
76+
'data-garden-id': externalId && externalId !== BTN_COMPONENT_ID ? externalId : COMPONENT_ID,
77+
'data-garden-version': PACKAGE_VERSION
78+
};
79+
})`
7680
${props => iconButtonStyles(props)};
7781
7882
& ${StyledIcon} {
7983
${props => iconStyles(props)}
8084
}
8185
82-
${props => retrieveComponentStyles(COMPONENT_ID, props)};
86+
${props => retrieveComponentStyles((props as any)['data-garden-id'], props)};
8387
`;
8488

8589
StyledIconButton.defaultProps = {

packages/buttons/src/styled/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77

88
export * from './StyledAnchor';
9-
export * from './StyledButton';
9+
export { StyledButton } from './StyledButton';
1010
export * from './StyledSplitButton';
1111
export * from './StyledExternalIcon';
1212
export * from './StyledIcon';
13-
export * from './StyledIconButton';
13+
export { StyledIconButton } from './StyledIconButton';

packages/colorpickers/src/elements/ColorSwatchDialog/index.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,20 @@ describe('ColorSwatchDialog', () => {
247247
expect(dialog).toBeNull();
248248
});
249249
});
250+
251+
describe('`data-garden-id` attribute', () => {
252+
it('has the correct `data-garden-id`', () => {
253+
const { getByRole } = render(
254+
<ColorSwatchDialog
255+
name="test"
256+
colors={colors}
257+
buttonProps={{
258+
'aria-label': 'Choose your favorite color'
259+
}}
260+
/>
261+
);
262+
263+
expect(getByRole('button')).toHaveAttribute('data-garden-id', 'buttons.button');
264+
});
265+
});
250266
});

packages/colorpickers/src/styled/ColorPickerDialog/StyledButton.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
*/
77

88
import styled from 'styled-components';
9-
import { retrieveComponentStyles, DEFAULT_THEME } from '@zendeskgarden/react-theming';
9+
import { DEFAULT_THEME } from '@zendeskgarden/react-theming';
1010
import { Button } from '@zendeskgarden/react-buttons';
1111

12-
const COMPONENT_ID = 'colorpickers.colordialog_button';
13-
1412
/**
1513
* 1. IE11 group width override.
1614
* 2. Input group border overrides.
1715
*/
1816
export const StyledButton = styled(Button as any).attrs({
1917
isNeutral: true,
20-
'data-garden-id': COMPONENT_ID,
2118
'data-garden-version': PACKAGE_VERSION
2219
})`
2320
padding: 0;
@@ -32,8 +29,6 @@ export const StyledButton = styled(Button as any).attrs({
3229
${props => props.theme.borderRadii.md} !important; /* [2] */
3330
/* stylelint-enable */
3431
}
35-
36-
${props => retrieveComponentStyles(COMPONENT_ID, props)};
3732
`;
3833

3934
StyledButton.defaultProps = {

0 commit comments

Comments
 (0)