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

Cannot select same option for multiple dropdowns #142

Open
wants to merge 2 commits into
base: master
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
36 changes: 36 additions & 0 deletions src/__tests__/select-event.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,42 @@ describe("The select event helpers", () => {
expect(form).toHaveFormValues({ food: "vanilla" });
});

it("selects same options on multiple dropdowns", async () => {
const { getByLabelText, getByRole } = render(
<form role="form">
<label htmlFor="food-for-monday">Food for monday</label>
<Select
options={OPTIONS}
name="foodForMonday"
inputId="food-for-monday"
/>
<label htmlFor="food-for-tuesday">Food for tuesday</label>
<Select
options={OPTIONS}
name="foodForTuesday"
inputId="food-for-tuesday"
/>
</form>
);
const form = getByRole("form");
const mondayInput = getByLabelText("Food for monday");
const tuesdayInput = getByLabelText("Food for tuesday");

expect(form).toHaveFormValues({ foodForMonday: "", foodForTuesday: "" });

await selectEvent.select(tuesdayInput, "Chocolate");
expect(form).toHaveFormValues({
foodForMonday: "",
foodForTuesday: "chocolate",
});

await selectEvent.select(mondayInput, "Chocolate");
expect(form).toHaveFormValues({
foodForMonday: "chocolate",
foodForTuesday: "chocolate",
});
});

describe("when asynchronously generating the list of options", () => {
// from https://github.com/JedWatson/react-select/blob/v3.0.0/docs/examples/CreatableAdvanced.js
// mixed with Async Creatable Example from https://react-select.com/creatable
Expand Down
22 changes: 22 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@ import {

import act from "./act-compat";

function findReactSelectContainerByClassName(
element: HTMLElement
): HTMLElement | null {
if (
Array.from(element.classList).some((className) =>
className.match(/^css-[a-z0-9]+-container$/)
)
) {
return element;
}
if (!element.parentElement) {
return null;
}
return findReactSelectContainerByClassName(element.parentElement);
}

// find the react-select container from its input field 🤷
function getReactSelectContainerFromInput(input: HTMLElement): HTMLElement {
const container = findReactSelectContainerByClassName(input);
if (container) {
return container;
}
// for older versions of react-select fall back to old parent crawler
// istanbul ignore if
return input.parentNode!.parentNode!.parentNode!.parentNode!
.parentNode as HTMLElement;
}
Expand Down