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

Use type button by default in Tab component #572

Merged
merged 1 commit into from May 11, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/tabs/__tests__/tabs.test.tsx
Expand Up @@ -13,6 +13,46 @@ import {
} from "@reach/tabs";

describe("<Tabs />", () => {
describe("rendering", () => {
it("sets the button type to button by default", () => {
const { getByText } = render(
<div>
<Tabs>
<TabList>
<Tab>Tab 1</Tab>
</TabList>
<TabPanels>
<TabPanel>
<p>Panel 1</p>
</TabPanel>
</TabPanels>
</Tabs>
</div>
);

expect(getByText("Tab 1")).toHaveAttribute("type", "button");
});

it("allows a custom button type", () => {
const { getByText } = render(
<div>
<Tabs>
<TabList>
<Tab type="submit">Tab 1</Tab>
</TabList>
<TabPanels>
<TabPanel>
<p>Panel 1</p>
</TabPanel>
</TabPanels>
</Tabs>
</div>
);

expect(getByText("Tab 1")).toHaveAttribute("type", "submit");
});
});

describe("a11y", () => {
it("should not have basic a11y issues", async () => {
const { container } = render(
Expand Down
3 changes: 3 additions & 0 deletions packages/tabs/src/index.tsx
Expand Up @@ -455,6 +455,8 @@ export const Tab = forwardRefWithAs<
context: TabsDescendantsContext,
disabled: !!disabled,
});
const htmlType =
Comp === "button" && props.type == null ? "button" : props.type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this was removed to begin with is because this check isn't reliable. Comp might be a styled component, or a wrapper component of some kind, in which case we'd lose that default setting. Of course if we pass the attribute along to a non-button element then we have invalid HTML. May still be worth it to have either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could keep the check, since it would cover at least some cases, and add some information in the docs about the edge cases?


const isSelected = index === selectedIndex;

Expand Down Expand Up @@ -507,6 +509,7 @@ export const Tab = forwardRefWithAs<
onClick={onSelect}
onFocus={handleFocus}
onBlur={handleBlur}
type={htmlType}
>
{children}
</Comp>
Expand Down