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

chore: Add radio example #941

Merged
merged 1 commit into from
Dec 12, 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
22 changes: 22 additions & 0 deletions packages/ariakit/src/radio/__examples__/radio/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Radio, RadioGroup, useRadioState } from "ariakit/radio";
import "./style.css";

export default function Example() {
const radio = useRadioState();
return (
<RadioGroup state={radio}>
<label className="label">
<Radio className="radio" value="apple" />
apple
</label>
<label className="label">
<Radio className="radio" value="orange" />
orange
</label>
<label className="label">
<Radio className="radio" value="watermelon" />
watermelon
</label>
</RadioGroup>
);
}
7 changes: 7 additions & 0 deletions packages/ariakit/src/radio/__examples__/radio/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.label {
@apply flex items-center gap-2;
}

.radio[data-focus-visible] {
@apply outline-primary dark:outline-primary-dark;
}
144 changes: 144 additions & 0 deletions packages/ariakit/src/radio/__examples__/radio/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {
click,
getByLabelText,
getByRole,
press,
render,
} from "ariakit-test-utils";
import Example from ".";

test("render radiogroup", () => {
render(<Example />);
expect(getByRole("radiogroup")).toMatchInlineSnapshot(`
<div
role="radiogroup"
>
<label
class="label"
>
<input
aria-checked="false"
class="radio"
data-command=""
id="r:0"
type="radio"
value="apple"
/>
apple
</label>
<label
class="label"
>
<input
aria-checked="false"
class="radio"
data-command=""
id="r:2"
tabindex="-1"
type="radio"
value="orange"
/>
orange
</label>
<label
class="label"
>
<input
aria-checked="false"
class="radio"
data-command=""
id="r:4"
tabindex="-1"
type="radio"
value="watermelon"
/>
watermelon
</label>
</div>
`);
});

test("Check radio button on click", async () => {
render(<Example />);
expect(getByLabelText("apple")).toHaveAttribute("aria-checked", "false");
expect(getByLabelText("orange")).toHaveAttribute("aria-checked", "false");
expect(getByLabelText("watermelon")).toHaveAttribute("aria-checked", "false");
await click(getByLabelText("apple"));
expect(getByLabelText("apple")).toHaveAttribute("aria-checked", "true");
expect(getByLabelText("orange")).toHaveAttribute("aria-checked", "false");
expect(getByLabelText("watermelon")).toHaveAttribute("aria-checked", "false");
await click(getByLabelText("watermelon"));
expect(getByLabelText("apple")).toHaveAttribute("aria-checked", "false");
expect(getByLabelText("watermelon")).toHaveAttribute("aria-checked", "true");
});

test("Tab key - Moves keyboard focus to the radio group", async () => {
render(<Example />);
expect(getByLabelText("apple")).not.toHaveFocus();
expect(getByLabelText("orange")).not.toHaveFocus();
expect(getByLabelText("watermelon")).not.toHaveFocus();

await press.Tab();
expect(getByLabelText("apple")).toHaveFocus();
expect(getByLabelText("apple")).not.toBeChecked();
expect(getByLabelText("orange")).not.toHaveFocus();
expect(getByLabelText("watermelon")).not.toHaveFocus();
});

test("Space key - Changes the state to checked, if the radio button with focus is not checked", async () => {
render(<Example />);
await press.Tab();
expect(getByLabelText("apple")).toHaveFocus();
expect(getByLabelText("apple")).not.toBeChecked();
await press.Space();
expect(getByLabelText("apple")).toHaveFocus();
expect(getByLabelText("apple")).toBeChecked();
});

test("Right arrow - Moves the focus to and checks the next radio button in the group", async () => {
render(<Example />);
await press.Tab();
await press.ArrowRight();
expect(getByLabelText("orange")).toHaveFocus();
expect(getByLabelText("orange")).toBeChecked();
await press.ArrowRight();
expect(getByLabelText("orange")).not.toBeChecked();
expect(getByLabelText("watermelon")).toBeChecked();
expect(getByLabelText("watermelon")).toHaveFocus();
});

test("Down arrow - Moves the focus to and checks the next radio button in the group", async () => {
render(<Example />);
await press.Tab();
await press.ArrowDown();
expect(getByLabelText("orange")).toHaveFocus();
expect(getByLabelText("orange")).toBeChecked();
await press.ArrowDown();
expect(getByLabelText("orange")).not.toBeChecked();
expect(getByLabelText("watermelon")).toBeChecked();
expect(getByLabelText("watermelon")).toHaveFocus();
});

test("Left arrow - Moves the focus to and checks the previous radio button in the group", async () => {
render(<Example />);
await press.Tab();
await press.ArrowLeft();
expect(getByLabelText("watermelon")).toHaveFocus();
expect(getByLabelText("watermelon")).toBeChecked();
await press.ArrowLeft();
expect(getByLabelText("watermelon")).not.toBeChecked();
expect(getByLabelText("orange")).toBeChecked();
expect(getByLabelText("orange")).toHaveFocus();
});

test("Up arrow - Moves the focus to and checks the previous radio button in the group", async () => {
render(<Example />);
await press.Tab();
await press.ArrowUp();
expect(getByLabelText("watermelon")).toHaveFocus();
expect(getByLabelText("watermelon")).toBeChecked();
await press.ArrowUp();
expect(getByLabelText("watermelon")).not.toBeChecked();
expect(getByLabelText("orange")).toBeChecked();
expect(getByLabelText("orange")).toHaveFocus();
});
3 changes: 3 additions & 0 deletions packages/ariakit/src/radio/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Radio

<a href="./__examples__/radio/index.tsx" data-playground>Example</a>