diff --git a/.changeset/spotty-bulldogs-tell.md b/.changeset/spotty-bulldogs-tell.md new file mode 100644 index 0000000..4ecabb6 --- /dev/null +++ b/.changeset/spotty-bulldogs-tell.md @@ -0,0 +1,5 @@ +--- +"@openfun/cunningham-react": patch +--- + +♻️(react) use react aria for select menu diff --git a/packages/react/src/components/Forms/Select/mono-common.tsx b/packages/react/src/components/Forms/Select/mono-common.tsx index c622db0..f50e95f 100644 --- a/packages/react/src/components/Forms/Select/mono-common.tsx +++ b/packages/react/src/components/Forms/Select/mono-common.tsx @@ -1,4 +1,4 @@ -import React, { HTMLAttributes } from "react"; +import React, { HTMLAttributes, useRef } from "react"; import { UseSelectReturnValue } from "downshift"; import classNames from "classnames"; import { useCunningham } from ":/components/Provider"; @@ -7,6 +7,7 @@ import { LabelledBox } from ":/components/Forms/LabelledBox"; import { Button } from ":/components/Button"; import { Option, SelectProps } from ":/components/Forms/Select"; import { isOptionWithRender } from ":/components/Forms/Select/utils"; +import { SelectMenu } from ":/components/Forms/Select/select-menu"; export function getOptionsFilter(inputValue?: string) { return (option: Option) => { @@ -87,131 +88,130 @@ export const SelectMonoAux = ({ }: SelectAuxProps) => { const { t } = useCunningham(); const labelProps = downshiftReturn.getLabelProps(); + const ref = useRef(null); + return ( - -
- onBlur && - onBlur({ target: { value: downshiftReturn.selectedItem?.value } }) - } - > - {/* We disabled linting for this specific line because we consider that the onClick props is only used for */} - {/* mouse users, so this do not engender any issue for accessibility. */} - {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} + <> +
- {downshiftReturn.selectedItem && ( - + ref={ref} + className={classNames( + "c__select", + "c__select--mono", + "c__select--" + state, + { + "c__select--disabled": disabled, + }, )} - - -
-
{children}
-
- {clearable && !disabled && downshiftReturn.selectedItem && ( - <> -
-
-
-
-
+ onBlur?.({ target: { value: downshiftReturn.selectedItem?.value } }) + } > -
    - {(downshiftReturn.isOpen || false) && ( - <> - {options.map((item, index) => { - const isActive = index === downshiftReturn.highlightedIndex; - return ( -
  • - {renderOption(item)} -
  • - ); - })} - {options.length === 0 && ( -
  • - {t("components.forms.select.menu_empty_placeholder")} -
  • - )} - + {/* We disabled linting for this specific line because we consider that the onClick props is only used for */} + {/* mouse users, so this do not engender any issue for accessibility. */} + {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} +
    + {downshiftReturn.selectedItem && ( + )} -
+ + +
+
{children}
+
+ {clearable && !disabled && downshiftReturn.selectedItem && ( + <> +
+
+
+
- -
+ + + + + ); }; diff --git a/packages/react/src/components/Forms/Select/mono.stories.tsx b/packages/react/src/components/Forms/Select/mono.stories.tsx index 821fd8e..c6865f7 100644 --- a/packages/react/src/components/Forms/Select/mono.stories.tsx +++ b/packages/react/src/components/Forms/Select/mono.stories.tsx @@ -1,6 +1,6 @@ import { Meta, StoryFn } from "@storybook/react"; import React, { useRef, useState } from "react"; -import { useForm, FormProvider } from "react-hook-form"; +import { FormProvider, useForm } from "react-hook-form"; import * as Yup from "yup"; import { yupResolver } from "@hookform/resolvers/yup"; import { onSubmit } from ":/components/Forms/Examples/ReactHookForm/reactHookFormUtils"; @@ -10,6 +10,8 @@ import { getCountryOption, RhfSelect, } from ":/components/Forms/Select/stories-utils"; +import { Modal, ModalSize, useModal } from ":/components/Modal"; +import { Input } from ":/components/Forms/Input"; export default { title: "Components/Forms/Select/Mono", @@ -17,7 +19,7 @@ export default { } as Meta; const Template: StoryFn = (args) => ( -
+
+
+ + +
+ +
+
+ +
+ +
+ - ))} - -
-
- {props.clearable && - !props.disabled && - props.selectedItems.length > 0 && ( - <> -
+
+ + {children} +
-
- - {children} -
-
- + +
- - - + + + ); }; diff --git a/packages/react/src/components/Forms/Select/multi-menu.tsx b/packages/react/src/components/Forms/Select/multi-menu.tsx index 5b4c291..cd18e34 100644 --- a/packages/react/src/components/Forms/Select/multi-menu.tsx +++ b/packages/react/src/components/Forms/Select/multi-menu.tsx @@ -8,19 +8,20 @@ import { import { useCunningham } from ":/components/Provider"; import { Checkbox } from ":/components/Forms/Checkbox"; import { Option } from ":/components/Forms/Select/index"; +import { SelectMenu } from ":/components/Forms/Select/select-menu"; -export const SelectMultiMenu = (props: SelectMultiAuxProps) => { +export const SelectMultiMenu = ( + props: SelectMultiAuxProps & { + selectRef: React.RefObject; + }, +) => { const { t } = useCunningham(); return ( -
    {props.downshiftReturn.isOpen && ( @@ -41,7 +42,7 @@ export const SelectMultiMenu = (props: SelectMultiAuxProps) => { )}
-
+ ); }; diff --git a/packages/react/src/components/Forms/Select/multi.stories.tsx b/packages/react/src/components/Forms/Select/multi.stories.tsx index 9baebb1..4e4efaf 100644 --- a/packages/react/src/components/Forms/Select/multi.stories.tsx +++ b/packages/react/src/components/Forms/Select/multi.stories.tsx @@ -10,6 +10,8 @@ import { getCountryOption, RhfSelect, } from ":/components/Forms/Select/stories-utils"; +import { Modal, ModalSize, useModal } from ":/components/Modal"; +import { Input } from ":/components/Forms/Input"; export default { title: "Components/Forms/Select/Multi", @@ -418,6 +420,76 @@ export const FormExample = () => { ); }; +export const SelectInModal = () => { + const modal = useModal({ isOpenDefault: true }); + return ( + +
+ + +
+ +
+
+ +
+ +
+