From 6177830c3eb31f0669621281269fa5799a80df02 Mon Sep 17 00:00:00 2001 From: Peter Hellstrand Date: Fri, 16 Aug 2024 10:57:01 +0200 Subject: [PATCH] fix(ffe-searchable-dropdown-react): aria-selected Only when actually selected aria-selected should be true. Not when item in list is highlighted. --- .../package.json | 1 + .../src/ListItemContainer.tsx | 4 +- .../src/Results.tsx | 44 +++++++++++-------- .../src/SearchableDropdown.spec.tsx | 7 +++ .../src/SearchableDropdown.tsx | 1 + 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/packages/ffe-searchable-dropdown-react/package.json b/packages/ffe-searchable-dropdown-react/package.json index ae0cbcf9ef..b51d497436 100644 --- a/packages/ffe-searchable-dropdown-react/package.json +++ b/packages/ffe-searchable-dropdown-react/package.json @@ -31,6 +31,7 @@ "@sb1/ffe-form": "^29.1.5", "@sb1/ffe-icons-react": "^11.0.2", "@sb1/ffe-spinner-react": "^9.0.6", + "@types/lodash.isequal": "^4.5.8", "classnames": "^2.3.1", "compute-scroll-into-view": "^3.1.0", "lodash.debounce": "^4.0.8", diff --git a/packages/ffe-searchable-dropdown-react/src/ListItemContainer.tsx b/packages/ffe-searchable-dropdown-react/src/ListItemContainer.tsx index 6767759f18..0ead529ebd 100644 --- a/packages/ffe-searchable-dropdown-react/src/ListItemContainer.tsx +++ b/packages/ffe-searchable-dropdown-react/src/ListItemContainer.tsx @@ -4,6 +4,7 @@ import { fixedForwardRef } from './fixedForwardRef'; interface ListItemContainerProps> { item: Item; isHighlighted: boolean; + isSelected: boolean; children: ({ item, isHighlighted, @@ -19,6 +20,7 @@ function ListItemContainerWithForwardRef>( { item, isHighlighted, + isSelected, children, onMouseEnter, onClick, @@ -32,7 +34,7 @@ function ListItemContainerWithForwardRef>( id={id} role="option" onMouseEnter={onMouseEnter} - aria-selected={isHighlighted} + aria-selected={isSelected} ref={ref} onClick={onClick} className="ffe-searchable-dropdown__list-item-container" diff --git a/packages/ffe-searchable-dropdown-react/src/Results.tsx b/packages/ffe-searchable-dropdown-react/src/Results.tsx index c5fdd91a1f..1360a6965e 100644 --- a/packages/ffe-searchable-dropdown-react/src/Results.tsx +++ b/packages/ffe-searchable-dropdown-react/src/Results.tsx @@ -3,6 +3,7 @@ import { Scrollbars } from 'react-custom-scrollbars-2'; import { ListItemContainer } from './ListItemContainer'; import { NoMatch } from './NoMatch'; import { Locale } from './types'; +import isEqual from 'lodash.isequal'; interface ResultProps> { noMatch?: { @@ -17,6 +18,7 @@ interface ResultProps> { dropdownAttributes: (keyof Item)[]; locale: Locale; }>; + selectedItem?: Item | null; highlightedIndex?: number; refs: React.Ref[]; dropdownAttributes: (keyof Item)[]; @@ -34,6 +36,7 @@ export function Results>({ dropdownAttributes, locale, onChange, + selectedItem, }: ResultProps) { return ( @@ -45,25 +48,28 @@ export function Results>({ locale={locale} /> )} - {listToRender.map((item, index) => ( - { - onChange(item); - }} - item={item} - > - {props => ( - - )} - - ))} + {listToRender.map((item, index) => { + return ( + { + onChange(item); + }} + item={item} + > + {props => ( + + )} + + ); + })} ); } diff --git a/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.spec.tsx b/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.spec.tsx index 09288512fd..6d20d59019 100644 --- a/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.spec.tsx +++ b/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.spec.tsx @@ -331,6 +331,13 @@ describe('SearchableDropdown', () => { const input = screen.getByRole('combobox'); + // open list and select second item + await user.click(input); + await user.type(input, '{arrowdown}'); + await user.type(input, '{arrowdown}'); + await user.type(input, '{enter}'); + + // reopen the list and navigate to second item await user.click(input); await user.type(input, '{arrowdown}'); await user.type(input, '{arrowdown}'); diff --git a/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.tsx b/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.tsx index 8e4aa76fe3..aaaa5208ed 100644 --- a/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.tsx +++ b/packages/ffe-searchable-dropdown-react/src/SearchableDropdown.tsx @@ -420,6 +420,7 @@ function SearchableDropdownWithForwardRef>( }} noMatch={state.noMatch ? noMatch : undefined} noMatchMessageId={noMatchMessageId} + selectedItem={state.selectedItem} /> )} {postListElement && (