Skip to content

Commit

Permalink
🐛(react) fix multi select menu opening
Browse files Browse the repository at this point in the history
At first we had a simple bug were it was not possible to open the
menu by clicking on the label. This fix is a rework to consider
the select wrapper as the toggle button itself, which is what we
do on the mono version. This change caused various tests to fail.
  • Loading branch information
NathanVss committed Feb 5, 2024
1 parent 1445f4a commit d61ab65
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-teachers-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openfun/cunningham-react": patch
---

fix multi select menu opening
1 change: 0 additions & 1 deletion packages/react/src/components/Forms/Select/mono-common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const SelectMonoAux = ({
}: SelectAuxProps) => {
const { t } = useCunningham();
const labelProps = downshiftReturn.getLabelProps();

return (
<Field state={state} {...props}>
<div
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/Forms/Select/multi-common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface SelectMultiAuxProps extends SubProps {
export const SelectMultiAux = ({ children, ...props }: SelectMultiAuxProps) => {
const { t } = useCunningham();
const labelProps = props.downshiftReturn.getLabelProps();

// We need to remove onBlur from toggleButtonProps because it triggers a menu closing each time
// we tick a checkbox using the monoline style.
const { onBlur, ...toggleProps } = props.downshiftReturn.toggleButtonProps;
return (
<Field {...props}>
<div
Expand All @@ -85,6 +89,7 @@ export const SelectMultiAux = ({ children, ...props }: SelectMultiAuxProps) => {
props.downshiftReturn.isOpen && !props.disabled,
})}
{...props.downshiftReturn.wrapperProps}
{...toggleProps}
>
{props.selectedItems.map((selectedItem, index) => (
<input
Expand Down Expand Up @@ -138,7 +143,6 @@ export const SelectMultiAux = ({ children, ...props }: SelectMultiAuxProps) => {
</span>
}
disabled={props.disabled}
{...props.downshiftReturn.toggleButtonProps}
/>
</div>
<div className="c__select__inner__value">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const SelectMultiSearchable = forwardRef<SelectHandle, SubProps>(
{...inputProps}
onFocus={() => {
setHasInputFocused(true);
downshiftReturn.openMenu();
}}
onBlur={() => {
setHasInputFocused(false);
Expand Down
7 changes: 0 additions & 7 deletions packages/react/src/components/Forms/Select/multi-simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,6 @@ export const SelectMultiSimple = forwardRef<SelectHandle, SubProps>(
menuOptionsStyle={props.monoline ? "checkbox" : "plain"}
downshiftReturn={{
...downshiftReturn,
wrapperProps: {
onClick: () => {
if (!props.disabled) {
downshiftReturn.toggleMenu();
}
},
},
toggleButtonProps: downshiftReturn.getToggleButtonProps({
...useMultipleSelectionReturn.getDropdownProps({
preventKeyAction: downshiftReturn.isOpen,
Expand Down
9 changes: 5 additions & 4 deletions packages/react/src/components/Forms/Select/multi.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,11 @@ describe("<Select multi={true} />", () => {
await user.click(clearButton);
expectSelectedOptions([]);

// Select again London.
// Close and re-open the menu. ( This is needed in this tests but in real life it works, I don't know exactly why .. )
await user.click(input);
await user.click(input);

// Select again Paris.
await user.click(
screen.getByRole("option", {
name: "Paris",
Expand Down Expand Up @@ -830,7 +833,7 @@ describe("<Select multi={true} />", () => {
// Focus the select.
await user.click(input);
expectMenuToBeOpen(menu);
expect(document.activeElement?.className).toContain("c__button");
expect(document.activeElement?.className).toContain("c__select__wrapper");

// Blur the select.
ref.current?.blur();
Expand Down Expand Up @@ -1041,8 +1044,6 @@ describe("<Select multi={true} />", () => {
// Make sure the option is selected.
expectSelectedOptionsText(["London", "Paris"]);

screen.logTestingPlaygroundURL();

// Make sure the menu stays open.
expectMenuToBeOpen(menu);

Expand Down

0 comments on commit d61ab65

Please sign in to comment.