Skip to content

Commit

Permalink
fix(Button): default to type=button
Browse files Browse the repository at this point in the history
  • Loading branch information
zettca committed May 29, 2023
1 parent 5c25254 commit 41b8494
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`ActionsGeneric > should render correctly 1`] = `
class="HvActionsGeneric-button e4fkrgz0 HvButton-root css-rf0utc-StyledButton-StyledButton e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -45,6 +46,7 @@ exports[`ActionsGeneric > should render correctly 1`] = `
</button>
<button
class="HvActionsGeneric-button e4fkrgz0 HvButton-root css-1thpith-StyledButton-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -80,6 +82,7 @@ exports[`ActionsGeneric > should render correctly 1`] = `
</button>
<button
class="HvActionsGeneric-button e4fkrgz0 HvButton-root css-1thpith-StyledButton-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -115,6 +118,7 @@ exports[`ActionsGeneric > should render correctly 1`] = `
</button>
<button
class="HvActionsGeneric-button e4fkrgz0 HvButton-root css-1thpith-StyledButton-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports[`Banner > should render correctly 1`] = `
aria-label="Close"
class="HvBanner-ActionContainer-closeAction HvButton-root css-48f9xl-StyledButton e138pvrm0"
tabindex="0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ exports[`BulkActions > With actions > should render correctly 1`] = `
class="HvActionsGeneric-button e4fkrgz0 HvButton-root css-rf0utc-StyledButton-StyledButton e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -114,6 +115,7 @@ exports[`BulkActions > With actions > should render correctly 1`] = `
class="HvActionsGeneric-button e4fkrgz0 HvButton-root css-rf0utc-StyledButton-StyledButton e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -165,6 +167,7 @@ exports[`BulkActions > With actions > should render correctly 1`] = `
disabled=""
id="hv-drop-down-menu-20-icon-button"
tabindex="-1"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
30 changes: 24 additions & 6 deletions packages/core/src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,44 @@ describe("Button", () => {
</div>
);
const primary = getByRole("button", { name: "Primary" });
expect(primary).toBeInTheDocument();
expect(primary).toBeDisabled();
const primarySubtle = getByRole("button", { name: "Primary Subtle" });
expect(primarySubtle).toBeInTheDocument();
expect(primarySubtle).toBeDisabled();
const primaryGhost = getByRole("button", { name: "Primary Ghost" });
expect(primaryGhost).toBeInTheDocument();
expect(primaryGhost).toBeDisabled();

const secondary = getByRole("button", { name: "Secondary" });
expect(secondary).toBeInTheDocument();
expect(secondary).toBeDisabled();
const secondarySubtle = getByRole("button", { name: "Secondary Subtle" });
expect(secondarySubtle).toBeInTheDocument();
expect(secondarySubtle).toBeDisabled();
const secondaryGhost = getByRole("button", { name: "Secondary Ghost" });
expect(secondaryGhost).toBeInTheDocument();
expect(secondaryGhost).toBeDisabled();
});

it(`is type="button" by default`, () => {
render(
<form>
<HvButton>Button</HvButton>
</form>
);
const button = screen.getByRole("button", { name: "Button" });
expect(button).toHaveAttribute("type", "button");
});

it(`submits form when type="submit"`, async () => {
const submitFn = vi.fn();
render(
<form onSubmit={submitFn}>
<HvButton type="submit">Button</HvButton>
</form>
);
const button = screen.getByRole("button", { name: "Button" });
expect(button).toHaveAttribute("type", "submit");

await userEvent.click(button);
expect(submitFn).toHaveBeenCalled();
});

describe("interactions", () => {
it("executes passed function on a click", () => {
const buttonSpy = vi.fn();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const HvButton: <C extends React.ElementType = "button">(
<StyledComponent
id={id}
ref={ref}
type="button"
className={clsx(className, classes?.root, buttonClasses.root)}
onClick={onClick}
onFocus={onFocusHandler}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`Button > sample snapshot testing > should match the snapshot 1`] = `
<DocumentFragment>
<button
class="HvButton-root css-or2r1m-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ exports[`<HvControls> > sample snapshot testing > Controls 1`] = `
class="button selected e17vrbb40 HvButton-root css-6jfwuk-StyledButton-StyledButton e138pvrm0"
id="card"
label="Select card view"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -172,6 +173,7 @@ exports[`<HvControls> > sample snapshot testing > Controls 1`] = `
class="button e17vrbb40 HvButton-root css-6jfwuk-StyledButton-StyledButton e138pvrm0"
id="list"
label="Select list view"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -752,6 +754,7 @@ exports[`<HvControls> > sample snapshot testing > Controls Controlled 1`] = `
class="button selected e17vrbb40 HvButton-root css-6jfwuk-StyledButton-StyledButton e138pvrm0"
id="card"
label="Select card view"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -786,6 +789,7 @@ exports[`<HvControls> > sample snapshot testing > Controls Controlled 1`] = `
class="button e17vrbb40 HvButton-root css-6jfwuk-StyledButton-StyledButton e138pvrm0"
id="list"
label="Select list view"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -1191,6 +1195,7 @@ exports[`<HvControls> > sample snapshot testing > Custom Controls 1`] = `
aria-pressed="true"
class="button selected e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="all"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -1207,6 +1212,7 @@ exports[`<HvControls> > sample snapshot testing > Custom Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="critical"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -1223,6 +1229,7 @@ exports[`<HvControls> > sample snapshot testing > Custom Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="major"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -1239,6 +1246,7 @@ exports[`<HvControls> > sample snapshot testing > Custom Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="average"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -1255,6 +1263,7 @@ exports[`<HvControls> > sample snapshot testing > Custom Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="minor"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -2175,6 +2184,7 @@ exports[`<HvControls> > sample snapshot testing > Mixed Controls 1`] = `
aria-pressed="true"
class="button selected e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="all"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -2191,6 +2201,7 @@ exports[`<HvControls> > sample snapshot testing > Mixed Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="critical"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -2207,6 +2218,7 @@ exports[`<HvControls> > sample snapshot testing > Mixed Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="major"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -2223,6 +2235,7 @@ exports[`<HvControls> > sample snapshot testing > Mixed Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="average"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -2239,6 +2252,7 @@ exports[`<HvControls> > sample snapshot testing > Mixed Controls 1`] = `
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-drqpdh-StyledButton-StyledButton e138pvrm0"
id="minor"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports[`DropDownMenu > should render correctly 1`] = `
aria-label="Dropdown menu"
class="HvDropDownMenu-icon e13q7myz1 HvButton-root hv-uikit-css-15tynx0-StyledButton-StyledButton e138pvrm0"
id="hv-drop-down-menu-4-icon-button"
type="button"
>
<div
class="hv-uikit-css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports[`File > should render correctly fail status 1`] = `
<button
aria-label="removeFileButtonLabel"
class="HvFile-removeButton e6pb7kb0 HvButton-root css-6nk431-StyledButton-StyledIconButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -111,6 +112,7 @@ exports[`File > should render correctly success status 1`] = `
<button
aria-label="removeFileButtonLabel"
class="HvFile-removeButton e6pb7kb0 HvButton-root css-6nk431-StyledButton-StyledIconButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports[`FileList > should render correctly 1`] = `
aria-label="removeFileButtonLabel"
class="HvFile-removeButton e6pb7kb0 HvButton-root css-6nk431-StyledButton-StyledIconButton e138pvrm0"
id="list-values-remove-button"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -115,6 +116,7 @@ exports[`FileList > should render correctly 1`] = `
aria-label="removeFileButtonLabel"
class="HvFile-removeButton e6pb7kb0 HvButton-root css-6nk431-StyledButton-StyledIconButton e138pvrm0"
id="list-values-remove-button"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ exports[`FileUploader > should render correctly 1`] = `
aria-label="Remove File"
class="HvFile-removeButton e6pb7kb0 HvButton-root css-6nk431-StyledButton-StyledIconButton e138pvrm0"
id="hvfilelist4-values-remove-button"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -157,6 +158,7 @@ exports[`FileUploader > should render correctly 1`] = `
aria-label="Remove File"
class="HvFile-removeButton e6pb7kb0 HvButton-root css-6nk431-StyledButton-StyledIconButton e138pvrm0"
id="hvfilelist4-values-remove-button"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ exports[`Pagination > should render correctly 1`] = `
class="HvPagination-iconContainer e1tn2n0k2 HvButton-root hv-uikit-css-purpg8-StyledButton-StyledButtonIconTooltip e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="hv-uikit-css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -111,6 +112,7 @@ exports[`Pagination > should render correctly 1`] = `
class="HvPagination-iconContainer e1tn2n0k2 HvButton-root hv-uikit-css-purpg8-StyledButton-StyledButtonIconTooltip e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="hv-uikit-css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -219,6 +221,7 @@ exports[`Pagination > should render correctly 1`] = `
class="HvPagination-iconContainer e1tn2n0k2 HvButton-root hv-uikit-css-purpg8-StyledButton-StyledButtonIconTooltip e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="hv-uikit-css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -258,6 +261,7 @@ exports[`Pagination > should render correctly 1`] = `
class="HvPagination-iconContainer e1tn2n0k2 HvButton-root hv-uikit-css-purpg8-StyledButton-StyledButtonIconTooltip e138pvrm0"
disabled=""
tabindex="-1"
type="button"
>
<div
class="hv-uikit-css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports[`QueryBuilder > snapshot > matches snapshot 1`] = `
<button
aria-pressed="true"
class="button selected e17vrbb40 HvButton-root css-1gx6mqk-StyledButton-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -31,6 +32,7 @@ exports[`QueryBuilder > snapshot > matches snapshot 1`] = `
<button
aria-pressed="false"
class="button e17vrbb40 HvButton-root css-1gx6mqk-StyledButton-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand All @@ -53,6 +55,7 @@ exports[`QueryBuilder > snapshot > matches snapshot 1`] = `
<button
aria-label="Reset query"
class="HvQueryBuilder-removeButton HvButton-root css-17gbypf-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -157,6 +160,7 @@ exports[`QueryBuilder > snapshot > matches snapshot 1`] = `
>
<button
class="HvButton-root css-1u5hgoy-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down Expand Up @@ -196,6 +200,7 @@ exports[`QueryBuilder > snapshot > matches snapshot 1`] = `
>
<button
class="HvButton-root css-1u5hgoy-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`ToggleButton > should render correctly 1`] = `
<button
aria-pressed="false"
class="HvButton-root css-1711hqm-StyledButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exports[`VerticalNavigation > should render correctly 1`] = `
aria-expanded="true"
aria-label="collapseButton"
class="e1arrhkt0 HvButton-root css-135ktzz-StyledButton-StyledCollapseButton e138pvrm0"
type="button"
>
<div
class="css-1nxr552-StyledContentDiv e138pvrm3"
Expand Down

0 comments on commit 41b8494

Please sign in to comment.