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

fix(Select): Add support for required attribute #1110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const meta: Meta<typeof Select> = {
disable: true,
},
},

required: {
control: {
type: "boolean",
},
},
},
};

Expand Down Expand Up @@ -119,3 +125,43 @@ export const SelectMultiple: Story = {

name: "Select multiple",
};

export const RequiredSelect: Story = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this prop requires an isolated example.

args: {
required: true,
},

render: (args) => (
<form>
<Select
name="exampleSelectRequired"
id="exampleSelectRequired"
options={[
{
value: "",
disabled: true,
label: "Select an option",
},
{
value: "1",
label: "Cosmic Cuttlefish",
},
{
value: "2",
label: "Bionic Beaver",
},
{
value: "3",
label: "Xenial Xerus",
},
]}
label="Ubuntu releases (required)"
required={args.required}
defaultValue=""
/>
<button type="submit">Submit</button>
</form>
),

name: "Select with required",
};
37 changes: 35 additions & 2 deletions src/components/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{ value: "3", label: "Xenial Xerus" },
]}
wrapperClassName="select"
/>
/>,

Check failure on line 18 in src/components/Select/Select.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build and test

Delete `,`
);
expect(screen.getByRole("combobox")).toMatchSnapshot();
});
Expand All @@ -30,7 +30,7 @@
render(<Select options={[]} onChange={onChangeMock} />);
fireEvent.change(screen.getByRole("combobox"), event);
expect(onChangeMock.mock.calls[0][0].target).toBe(
screen.getByRole("combobox")
screen.getByRole("combobox"),

Check failure on line 33 in src/components/Select/Select.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build and test

Delete `,`
);
});

Expand Down Expand Up @@ -63,4 +63,37 @@
render(<Select help={help} />);
expect(screen.getByRole("combobox")).toHaveAccessibleDescription(help);
});

it("renders with required attribute", () => {
render(
<Select
id="test-required"
options={[
{ value: "", disabled: true, label: "Select an option" },
{ value: "1", label: "Cosmic Cuttlefish" },
{ value: "2", label: "Bionic Beaver" },
{ value: "3", label: "Xenial Xerus" },
]}
required
/>,

Check failure on line 78 in src/components/Select/Select.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build and test

Delete `,`
);
const select = screen.getByRole("combobox");
expect(select).toBeRequired();
});

it("renders without required attribute when not set", () => {
render(
<Select
id="test-not-required"
options={[
{ value: "", disabled: true, label: "Select an option" },
{ value: "1", label: "Cosmic Cuttlefish" },
{ value: "2", label: "Bionic Beaver" },
{ value: "3", label: "Xenial Xerus" },
]}
/>,

Check failure on line 94 in src/components/Select/Select.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build and test

Delete `,`
);
const select = screen.getByRole("combobox");
expect(select).not.toBeRequired();
});
});
1 change: 1 addition & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const Select = ({
id={selectId}
onChange={(evt) => onChange && onChange(evt)}
ref={selectRef}
required={required}
{...selectProps}
>
{generateOptions(options)}
Expand Down
Loading