From daa1a8f2069d5d9d0351bea8351462e328f156b4 Mon Sep 17 00:00:00 2001 From: shaharzil Date: Sun, 1 Sep 2024 12:25:15 +0300 Subject: [PATCH] set focues in use effect --- .../core/src/components/Dropdown/Dropdown.tsx | 32 +++++++++---------- .../Dropdown/components/option/option.jsx | 10 ++++-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/core/src/components/Dropdown/Dropdown.tsx b/packages/core/src/components/Dropdown/Dropdown.tsx index 124ff28ef3..ab9f726a59 100644 --- a/packages/core/src/components/Dropdown/Dropdown.tsx +++ b/packages/core/src/components/Dropdown/Dropdown.tsx @@ -225,34 +225,33 @@ const Dropdown: VibeComponent & { ); const Option = useCallback( - (props: CustomOptionProps) => { - if (props.isFocused && props.innerProps.id !== focusedOptionId) { - setFocusedOptionId(props.innerProps.id); - } - return ( - - ); - }, - [finalOptionRenderer, optionWrapperClassName, focusedOptionId] + (props: CustomOptionProps) => ( + + ), + [finalOptionRenderer, optionWrapperClassName, setFocusedOptionId] ); const Input = useCallback( (props: InputProps | any) => { + const { focusedOptionId, menuIsOpen } = props.selectProps; + const ariaActiveDescendant = focusedOptionId && menuIsOpen ? focusedOptionId : ""; return ( ); }, - [menuId] + [menuId, readOnly] ); const SingleValue = useCallback( @@ -405,7 +404,6 @@ const Dropdown: VibeComponent & { aria-readonly={readOnly} aria-label={overrideAriaLabel} aria-details={tooltipContent} - aria-expanded={!readOnly && menuIsOpen} aria-haspopup="listbox" defaultValue={defaultValue} value={value} @@ -418,7 +416,7 @@ const Dropdown: VibeComponent & { onInputChange={onInputChange} openMenuOnFocus={openMenuOnFocus} openMenuOnClick={openMenuOnClick} - ariaActivedescendant={focusedOptionId ? focusedOptionId : ""} + focusedOptionId={focusedOptionId} isRtl={rtl} styles={inlineStyles} theme={customTheme} diff --git a/packages/core/src/components/Dropdown/components/option/option.jsx b/packages/core/src/components/Dropdown/components/option/option.jsx index a2df696094..cbd07c1355 100644 --- a/packages/core/src/components/Dropdown/components/option/option.jsx +++ b/packages/core/src/components/Dropdown/components/option/option.jsx @@ -1,11 +1,11 @@ import cx from "classnames"; -import React from "react"; +import React, { useEffect } from "react"; import { components } from "react-select"; import Tooltip from "../../../Tooltip/Tooltip"; import { ChildrenContent } from "../ChildrenContent/ChildrenContent"; import styles from "./option.module.scss"; -const Option = ({ Renderer, data, children, optionWrapperClassName, ...props }) => { +const Option = ({ Renderer, data, children, setFocusedOptionId, optionWrapperClassName, ...props }) => { const tooltipProps = data?.tooltipProps || {}; const rendererProps = { children, @@ -18,6 +18,12 @@ const Option = ({ Renderer, data, children, optionWrapperClassName, ...props }) } }; + useEffect(() => { + if (props.isFocused) { + setFocusedOptionId(props.innerProps.id); + } + }, [props.isFocused, props.innerProps.id, setFocusedOptionId]); + return ( {Renderer ? (