Skip to content
Closed
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
30 changes: 26 additions & 4 deletions packages/react/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,17 @@ const Dropdown = React.forwardRef(
return isObject && 'disabled' in item && item.disabled === true;
}, []);

const items = useMemo(() => itemsProp, [itemsProp]);
const [ariaLiveText, setAriaLiveText] = useState('');

const onHighlightedIndexChange = useCallback(
(changes: UseSelectStateChange<ItemType>) => {
const { highlightedIndex } = changes;

if (
highlightedIndex !== undefined &&
highlightedIndex > -1 &&
// eslint-disable-next-line valid-typeof , no-constant-binary-expression -- https://github.com/carbon-design-system/carbon/issues/20452
typeof window !== undefined
typeof window !== 'undefined'
) {
const itemArray = document.querySelectorAll(
`li.${prefix}--list-box__menu-item[role="option"]`
Expand All @@ -401,12 +403,16 @@ const Dropdown = React.forwardRef(
block: 'nearest',
});
}

const currentItem = items[highlightedIndex] ?? null;
setAriaLiveText(itemToString(currentItem));
} else {
setAriaLiveText('');
}
},
[prefix]
[items, itemToString, prefix]
);

const items = useMemo(() => itemsProp, [itemsProp]);
const selectProps = useMemo(
() => ({
items,
Expand Down Expand Up @@ -452,6 +458,12 @@ const Dropdown = React.forwardRef(

const [isFocused, setIsFocused] = useState(false);

useEffect(() => {
if (!isOpen) {
setAriaLiveText('');
}
}, [isOpen]);

const className = cx(`${prefix}--dropdown`, {
[`${prefix}--dropdown--invalid`]: invalid,
[`${prefix}--dropdown--warning`]: showWarning,
Expand Down Expand Up @@ -677,9 +689,11 @@ const Dropdown = React.forwardRef(
{isOpen &&
items.map((item, index) => {
const isObject = item !== null && typeof item === 'object';
const isSelected = selectedItem === item;
const itemProps = getItemProps({
item,
index,
['aria-selected']: isSelected || undefined,
});
const title =
isObject && 'text' in item && itemToElement
Expand Down Expand Up @@ -709,6 +723,14 @@ const Dropdown = React.forwardRef(
);
})}
</ListBox.Menu>
{isOpen && ariaLiveText ? (
<span
aria-live="polite"
aria-atomic="true"
className={`${prefix}--assistive-text ${prefix}--visually-hidden`}>
{ariaLiveText}
</span>
) : null}
</ListBox>
{!inline && !invalid && !warn && helper}
</div>
Expand Down
Loading