Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type="button" by default to Button component #288

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ exports[`props should render as dismissable 1`] = `
data-g2-component="CloseButton"
data-icon="true"
title="Dismiss"
type="button"
ItsJonQ marked this conversation as resolved.
Show resolved Hide resolved
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-7"
Expand Down Expand Up @@ -1714,6 +1715,7 @@ exports[`props should render with status 1`] = `
data-g2-component="CloseButton"
data-icon="true"
title="Dismiss"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-8"
Expand Down Expand Up @@ -1792,6 +1794,7 @@ exports[`props should render with status 1`] = `
data-g2-component="CloseButton"
data-icon="true"
title="Dismiss"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-8"
Expand Down Expand Up @@ -1870,6 +1873,7 @@ exports[`props should render with status 1`] = `
data-g2-component="CloseButton"
data-icon="true"
title="Dismiss"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-8"
Expand Down Expand Up @@ -1948,6 +1952,7 @@ exports[`props should render with status 1`] = `
data-g2-component="CloseButton"
data-icon="true"
title="Dismiss"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-8"
Expand Down Expand Up @@ -2026,6 +2031,7 @@ exports[`props should render with status 1`] = `
data-g2-component="CloseButton"
data-icon="true"
title="Dismiss"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-8"
Expand Down
14 changes: 13 additions & 1 deletion packages/components/src/BaseButton/__tests__/BaseButton.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import React from 'react';

import { BaseButton } from '../index';
Expand All @@ -8,4 +8,16 @@ describe('props', () => {
const { container } = render(<BaseButton>I like warm hugs</BaseButton>);
expect(container.firstChild).toMatchSnapshot();
});

test('should render type', () => {
render(
<>
<BaseButton type="submit">Submit</BaseButton>
<BaseButton href="#">Link</BaseButton>
</>,
);

expect(screen.getByRole('button')).toHaveAttribute('type', 'submit');
expect(screen.getByRole('link')).not.toHaveAttribute('type');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="BaseButton"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/BaseButton/useBaseButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function useBaseButton(props) {
const as = href ? 'a' : 'button';
const { styles: controlGroupStyles } = useControlGroupContext();
const isIconOnly = !!icon && !children;
const type = as === 'button' ? 'button' : undefined;

const classes = cx(
flexClassName,
Expand All @@ -73,6 +74,7 @@ export function useBaseButton(props) {
return {
...flexProps,
as,
type,
href,
children,
disabled,
Expand Down
14 changes: 13 additions & 1 deletion packages/components/src/Button/__tests__/Button.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import React from 'react';

import { Button } from '../index';
Expand Down Expand Up @@ -98,4 +98,16 @@ describe('props', () => {
);
expect(container.firstChild).toMatchSnapshot();
});

test('should render type', () => {
render(
<>
<Button type="submit">Submit</Button>
<Button href="#">Link</Button>
</>,
);

expect(screen.getByRole('button')).toHaveAttribute('type', 'submit');
expect(screen.getByRole('link')).not.toHaveAttribute('type');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ exports[`props should render (Flex) gap 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -918,6 +919,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -1230,6 +1232,7 @@ exports[`props should render disabled 1`] = `
data-g2-component="Button"
data-icon="false"
disabled=""
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -1541,6 +1544,7 @@ exports[`props should render elevation 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -1925,6 +1929,7 @@ exports[`props should render hasCaret 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -2340,6 +2345,7 @@ exports[`props should render icon 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="true"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -2689,6 +2695,7 @@ exports[`props should render isControl 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -3002,6 +3009,7 @@ exports[`props should render isDestructive 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -3590,6 +3598,7 @@ exports[`props should render isLoading 1`] = `
data-g2-component="Button"
data-icon="false"
disabled=""
type="button"
>
<div
aria-hidden="true"
Expand Down Expand Up @@ -3967,6 +3976,7 @@ exports[`props should render isNarrow 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -4279,6 +4289,7 @@ exports[`props should render isRounded 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -4608,6 +4619,7 @@ exports[`props should render isSubtle 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -4959,6 +4971,7 @@ exports[`props should render prefix 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -5290,6 +5303,7 @@ exports[`props should render size 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -5641,6 +5655,7 @@ exports[`props should render suffix 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -5965,6 +5980,7 @@ exports[`props should render variant 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ exports[`props should render correctly 1`] = `
id="ButtonGroup-1"
role="radio"
tabindex="0"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-2"
Expand Down Expand Up @@ -440,6 +441,7 @@ exports[`props should render correctly 1`] = `
id="ButtonGroup-2"
role="radio"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-2"
Expand Down Expand Up @@ -467,6 +469,7 @@ exports[`props should render correctly 1`] = `
id="ButtonGroup-3"
role="radio"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ exports[`props should render CardInnerBody 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-6"
Expand Down Expand Up @@ -1422,6 +1423,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="ClipboardButton"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="CloseButton"
data-icon="true"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -748,6 +749,7 @@ exports[`props should render size 1`] = `
data-g2-c16t="true"
data-g2-component="CloseButton"
data-icon="true"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down Expand Up @@ -1146,6 +1148,7 @@ exports[`props should render variant 1`] = `
data-g2-c16t="true"
data-g2-component="CloseButton"
data-icon="true"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="ColorControl"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-2"
Expand All @@ -589,6 +590,7 @@ exports[`props should render correctly 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-2"
Expand Down Expand Up @@ -1523,6 +1525,7 @@ exports[`props should render mixed control types 1`] = `
data-g2-c16t="true"
data-g2-component="Button"
data-icon="false"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ exports[`props should render correctly 1`] = `
data-g2-component="Button"
data-icon="true"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-7"
Expand Down Expand Up @@ -1673,6 +1674,7 @@ exports[`props should render isLoading 1`] = `
data-g2-component="Button"
data-icon="true"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-8"
Expand Down Expand Up @@ -2443,6 +2445,7 @@ exports[`props should render placeholder 1`] = `
data-g2-component="Button"
data-icon="true"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-7"
Expand Down Expand Up @@ -3216,6 +3219,7 @@ exports[`props should render prefix 1`] = `
data-g2-component="Button"
data-icon="true"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-7"
Expand Down Expand Up @@ -3989,6 +3993,7 @@ exports[`props should render suffix 1`] = `
data-g2-component="Button"
data-icon="true"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-7"
Expand Down Expand Up @@ -4759,6 +4764,7 @@ exports[`props should render value 1`] = `
data-g2-component="Button"
data-icon="true"
tabindex="-1"
type="button"
>
<span
class="components-flex-item wp-components-flex-item ic-1p3tlc8 ic-192yz5d emotion-7"
Expand Down
Loading