Skip to content

Commit

Permalink
🐛(react) fix missing selected option of Select
Browse files Browse the repository at this point in the history
In some cases, when the options were newly built object, due to the
fact that we were using object equality to check for the current selected
item, it was not working in those cases.
  • Loading branch information
NathanVss committed Feb 6, 2024
1 parent 7649366 commit a32bac7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sweet-apes-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openfun/cunningham-react": patch
---

fix missing selected option of Select
3 changes: 2 additions & 1 deletion packages/react/src/components/Forms/Select/mono-common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ export const SelectMonoAux = ({
className={classNames("c__select__menu__item", {
"c__select__menu__item--highlight": isActive,
"c__select__menu__item--selected":
downshiftReturn.selectedItem === item,
downshiftReturn.selectedItem &&
optionsEqual(downshiftReturn.selectedItem, item),
"c__select__menu__item--disabled": item.disabled,
})}
key={`${optionToValue(item)}${index.toString()}`}
Expand Down
64 changes: 64 additions & 0 deletions packages/react/src/components/Forms/Select/mono.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,70 @@ describe("<Select/>", () => {
expect(valueRendered).toHaveTextContent("");
});

it("still shows the selected current choice even if the options are different objects", async () => {
const Wrapper = () => {
const [value, setValue] = useState<string | number | undefined>();
const [count, setCount] = useState(0);
return (
<CunninghamProvider>
<div>
<div>Value = {value}|</div>
<Button onClick={() => setCount(count + 1)}>Rerender</Button>
<Select
label="City"
options={[
{
label: "Paris",
value: "paris",
},
{
label: "Panama",
value: "panama",
},
{
label: "London",
value: "london",
},
]}
value={value}
onChange={(e) => {
setValue(e.target.value as string);
}}
/>
</div>
</CunninghamProvider>
);
};

render(<Wrapper />, {
wrapper: CunninghamProvider,
});

const user = userEvent.setup();
const input = screen.getByRole("combobox", {
name: "City",
});

// Select an option
await user.click(input);
await user.click(
screen.getByRole("option", {
name: "Panama",
}),
);

// Verify that the value is selected.
await user.click(input);
expectOptionToBeSelected(screen.getByRole("option", { name: "Panama" }));

// Rerender the select with the options mutated.
await user.click(screen.getByRole("button", { name: "Rerender" }));

// Verify that the value is still selected.
await user.click(input);
expectOptionToBeSelected(screen.getByRole("option", { name: "Panama" }));
});

it("updates the value if the value is removed from the options (controlled)", async () => {
let myOptions = [
{
Expand Down

0 comments on commit a32bac7

Please sign in to comment.