-
Notifications
You must be signed in to change notification settings - Fork 650
perf(Autocomplete): eliminate unnecessary re-renders on arrow-key navigation #7549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hectahertz
wants to merge
3
commits into
main
Choose a base branch
from
hectahertz/perf-autocomplete-memo-deps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−47
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
|
|
||
| perf(Autocomplete): eliminate re-renders on arrow-key navigation by converting highlightedItem to a ref |
Binary file modified
BIN
+1 KB
(110%)
...ocomplete.test.ts-snapshots/Autocomplete-Add-New-Item-dark-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+362 Bytes
(100%)
.../Autocomplete.test.ts-snapshots/Autocomplete-Add-New-Item-dark-dimmed-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+986 Bytes
(110%)
...mplete.test.ts-snapshots/Autocomplete-Add-New-Item-dark-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1 KB
(110%)
...ponents/Autocomplete.test.ts-snapshots/Autocomplete-Add-New-Item-dark-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1 KB
(110%)
...ocomplete.test.ts-snapshots/Autocomplete-Add-New-Item-dark-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+900 Bytes
(110%)
...complete.test.ts-snapshots/Autocomplete-Add-New-Item-light-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+817 Bytes
(110%)
...plete.test.ts-snapshots/Autocomplete-Add-New-Item-light-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+900 Bytes
(110%)
...onents/Autocomplete.test.ts-snapshots/Autocomplete-Add-New-Item-light-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+900 Bytes
(110%)
...complete.test.ts-snapshots/Autocomplete-Add-New-Item-light-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.09 KB
(110%)
....ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-dark-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+850 Bytes
(100%)
...test.ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-dark-dimmed-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.04 KB
(110%)
...-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-dark-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.09 KB
(110%)
...mplete.test.ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-dark-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.09 KB
(110%)
....ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-dark-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+914 Bytes
(110%)
...ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-light-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+927 Bytes
(110%)
...snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-light-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+914 Bytes
(110%)
...plete.test.ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-light-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+914 Bytes
(110%)
...ts-snapshots/Autocomplete-Custom-Overlay-Menu-Anchor-light-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+977 Bytes
(110%)
...s-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-dark-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+490 Bytes
(100%)
...st.ts-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-dark-dimmed-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1017 Bytes
(110%)
...napshots/Autocomplete-Custom-Sort-After-Menu-Close-dark-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+977 Bytes
(110%)
...lete.test.ts-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-dark-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+977 Bytes
(110%)
...s-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-dark-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+856 Bytes
(110%)
...-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-light-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+818 Bytes
(110%)
...apshots/Autocomplete-Custom-Sort-After-Menu-Close-light-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+856 Bytes
(110%)
...ete.test.ts-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+856 Bytes
(110%)
...-snapshots/Autocomplete-Custom-Sort-After-Menu-Close-light-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.02 KB
(110%)
...s/Autocomplete.test.ts-snapshots/Autocomplete-Default-dark-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+452 Bytes
(100%)
...nents/Autocomplete.test.ts-snapshots/Autocomplete-Default-dark-dimmed-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.06 KB
(110%)
...utocomplete.test.ts-snapshots/Autocomplete-Default-dark-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.02 KB
(110%)
...s/components/Autocomplete.test.ts-snapshots/Autocomplete-Default-dark-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.02 KB
(110%)
...s/Autocomplete.test.ts-snapshots/Autocomplete-Default-dark-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+929 Bytes
(110%)
.../Autocomplete.test.ts-snapshots/Autocomplete-Default-light-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+898 Bytes
(110%)
...tocomplete.test.ts-snapshots/Autocomplete-Default-light-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+929 Bytes
(110%)
.../components/Autocomplete.test.ts-snapshots/Autocomplete-Default-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+929 Bytes
(110%)
.../Autocomplete.test.ts-snapshots/Autocomplete-Default-light-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.72 KB
(110%)
...tocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-dark-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.81 KB
(110%)
...s/Autocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-dark-dimmed-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.73 KB
(110%)
...omplete.test.ts-snapshots/Autocomplete-In-A-Dialog-dark-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.72 KB
(110%)
...mponents/Autocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-dark-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.72 KB
(110%)
...tocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-dark-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.32 KB
(100%)
...ocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-light-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.24 KB
(100%)
...mplete.test.ts-snapshots/Autocomplete-In-A-Dialog-light-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.32 KB
(100%)
...ponents/Autocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.32 KB
(100%)
...ocomplete.test.ts-snapshots/Autocomplete-In-A-Dialog-light-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+934 Bytes
(110%)
...ocomplete-In-Overlay-With-Custom-Scroll-Container-Ref-dark-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+452 Bytes
(100%)
.../Autocomplete-In-Overlay-With-Custom-Scroll-Container-Ref-dark-dimmed-linux.png
Oops, something went wrong.
Binary file modified
BIN
+875 Bytes
(110%)
...mplete-In-Overlay-With-Custom-Scroll-Container-Ref-dark-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+933 Bytes
(110%)
...apshots/Autocomplete-In-Overlay-With-Custom-Scroll-Container-Ref-dark-linux.png
Oops, something went wrong.
Binary file modified
BIN
+933 Bytes
(110%)
...ocomplete-In-Overlay-With-Custom-Scroll-Container-Ref-dark-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+869 Bytes
(110%)
...complete-In-Overlay-With-Custom-Scroll-Container-Ref-light-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+876 Bytes
(110%)
...plete-In-Overlay-With-Custom-Scroll-Container-Ref-light-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+869 Bytes
(110%)
...pshots/Autocomplete-In-Overlay-With-Custom-Scroll-Container-Ref-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+869 Bytes
(110%)
...complete-In-Overlay-With-Custom-Scroll-Container-Ref-light-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+935 Bytes
(110%)
...ts/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-dark-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+812 Bytes
(110%)
...pshots/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-dark-dimmed-linux.png
Oops, something went wrong.
Binary file modified
BIN
+949 Bytes
(110%)
...Autocomplete-Rendering-The-Menu-Outside-An-Overlay-dark-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+935 Bytes
(110%)
....ts-snapshots/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-dark-linux.png
Oops, something went wrong.
Binary file modified
BIN
+935 Bytes
(110%)
...ts/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-dark-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+891 Bytes
(110%)
...s/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-light-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+887 Bytes
(110%)
...utocomplete-Rendering-The-Menu-Outside-An-Overlay-light-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+891 Bytes
(110%)
...ts-snapshots/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+891 Bytes
(110%)
...s/Autocomplete-Rendering-The-Menu-Outside-An-Overlay-light-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.39 KB
(110%)
...omplete-With-Callback-When-Overlay-Open-State-Changes-dark-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.5 KB
(110%)
...utocomplete-With-Callback-When-Overlay-Open-State-Changes-dark-dimmed-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.38 KB
(110%)
...lete-With-Callback-When-Overlay-Open-State-Changes-dark-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.39 KB
(110%)
...shots/Autocomplete-With-Callback-When-Overlay-Open-State-Changes-dark-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.39 KB
(110%)
...omplete-With-Callback-When-Overlay-Open-State-Changes-dark-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.31 KB
(110%)
...mplete-With-Callback-When-Overlay-Open-State-Changes-light-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.3 KB
(110%)
...ete-With-Callback-When-Overlay-Open-State-Changes-light-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.31 KB
(110%)
...hots/Autocomplete-With-Callback-When-Overlay-Open-State-Changes-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.31 KB
(110%)
...mplete-With-Callback-When-Overlay-Open-State-Changes-light-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.41 KB
(110%)
...plete.test.ts-snapshots/Autocomplete-With-Token-Input-dark-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.54 KB
(110%)
...ocomplete.test.ts-snapshots/Autocomplete-With-Token-Input-dark-dimmed-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.45 KB
(110%)
...te.test.ts-snapshots/Autocomplete-With-Token-Input-dark-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.41 KB
(110%)
...nts/Autocomplete.test.ts-snapshots/Autocomplete-With-Token-Input-dark-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.41 KB
(110%)
...plete.test.ts-snapshots/Autocomplete-With-Token-Input-dark-tritanopia-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.31 KB
(110%)
...lete.test.ts-snapshots/Autocomplete-With-Token-Input-light-colorblind-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.3 KB
(110%)
...e.test.ts-snapshots/Autocomplete-With-Token-Input-light-high-contrast-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.31 KB
(110%)
...ts/Autocomplete.test.ts-snapshots/Autocomplete-With-Token-Input-light-linux.png
Oops, something went wrong.
Binary file modified
BIN
+1.31 KB
(110%)
...lete.test.ts-snapshots/Autocomplete-With-Token-Input-light-tritanopia-linux.png
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,52 @@ function getdefaultCheckedSelectionChange<T extends AutocompleteMenuItem>( | |
|
|
||
| const isItemSelected = (itemId: string, selectedItemIds: Array<string>) => selectedItemIds.includes(itemId) | ||
|
|
||
| /** | ||
| * Memoized wrapper around ActionList.Item that skips re-rendering when props | ||
| * haven't changed. Combined with the `highlightedItem` ref (instead of state), | ||
| * arrow-key navigation no longer triggers re-renders of the item list. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const MemoizedAutocompleteItem = React.memo(function MemoizedAutocompleteItem<T extends Record<string, any>>({ | ||
| item, | ||
| }: { | ||
| item: T | ||
| }) { | ||
| const { | ||
| id, | ||
| onAction, | ||
| children, | ||
| text, | ||
| leadingVisual: LeadingVisual, | ||
| trailingVisual: TrailingVisual, | ||
| key, | ||
| role, | ||
| ...itemProps | ||
| } = item | ||
| return ( | ||
| <ActionList.Item | ||
| key={(key ?? id) as string | number} | ||
| onSelect={() => onAction(item)} | ||
| {...itemProps} | ||
|
Comment on lines
+73
to
+76
|
||
| id={id} | ||
| data-id={id} | ||
| role={role as AriaRole} | ||
| > | ||
| {LeadingVisual && ( | ||
| <ActionList.LeadingVisual> | ||
| {isElement(LeadingVisual) ? LeadingVisual : <LeadingVisual />} | ||
| </ActionList.LeadingVisual> | ||
| )} | ||
| {(children ?? text) as React.ReactNode} | ||
| {TrailingVisual && ( | ||
| <ActionList.TrailingVisual> | ||
| {isElement(TrailingVisual) ? TrailingVisual : <TrailingVisual />} | ||
| </ActionList.TrailingVisual> | ||
| )} | ||
| </ActionList.Item> | ||
| ) | ||
| }) | ||
|
|
||
| function getItemById<T extends AutocompleteMenuItem>(itemId: string, items: T[]) { | ||
| return items.find(item => item.id === itemId) | ||
| } | ||
|
|
@@ -167,7 +213,11 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| } = props | ||
| const listContainerRef = useRef<HTMLDivElement>(null) | ||
| const allItemsToRenderRef = useRef<T[]>([]) | ||
| const [highlightedItem, setHighlightedItem] = useState<T>() | ||
| // Using a ref instead of state to avoid triggering a re-render of all items | ||
| // on every arrow-key press. The active styling was already handled via DOM by | ||
| // useFocusZone (data-is-active-descendant) before this change, but the | ||
| // previous useState caused React to re-render the entire item list anyway. | ||
| const highlightedItemRef = useRef<T>() | ||
| const [sortedItemIds, setSortedItemIds] = useState<Array<string>>(items.map(({id: itemId}) => itemId)) | ||
| const generatedUniqueId = useId(id) | ||
|
|
||
|
|
@@ -178,7 +228,6 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| ...selectableItem, | ||
| role: 'option', | ||
| id: selectableItem.id, | ||
| active: highlightedItem?.id === selectableItem.id, | ||
| selected: selectionVariant === 'multiple' ? selectedItemIds.includes(selectableItem.id) : undefined, | ||
| onAction: (item: T) => { | ||
| const otherSelectedItemIds = selectedItemIds.filter(selectedItemId => selectedItemId !== item.id) | ||
|
|
@@ -204,7 +253,6 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| } | ||
| }), | ||
| [ | ||
| highlightedItem, | ||
| items, | ||
| selectedItemIds, | ||
| inputRef, | ||
|
|
@@ -246,7 +294,6 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| ...addNewItem, | ||
| role: 'option', | ||
| key: addNewItem.id, | ||
| active: highlightedItem?.id === addNewItem.id, | ||
| selected: selectionVariant === 'multiple' ? selectedItemIds.includes(addNewItem.id) : undefined, | ||
| leadingVisual: () => <PlusIcon />, | ||
| onAction: (item: T) => { | ||
|
|
@@ -269,7 +316,6 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| selectionVariant, | ||
| setInputValue, | ||
| generatedUniqueId, | ||
| highlightedItem, | ||
| selectedItemIds, | ||
| ], | ||
| ) | ||
|
|
@@ -294,13 +340,28 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| activeDescendantFocus: inputRef, | ||
| onActiveDescendantChanged: (current, _previous, directlyActivated) => { | ||
| activeDescendantRef.current = current || null | ||
|
|
||
| // Active styling was already handled via DOM before this change: | ||
| // useFocusZone sets `data-is-active-descendant` on the active element, | ||
| // and ActionList styles that attribute in CSS. The key change is that | ||
| // we no longer call setHighlightedItem (state), avoiding re-renders. | ||
|
|
||
| if (current) { | ||
| const currentLi = current.closest('li') | ||
| const selectedItem = allItemsToRenderRef.current.find(item => { | ||
| return item.id === current.closest('li')?.getAttribute('data-id') | ||
| return item.id === currentLi?.getAttribute('data-id') | ||
| }) | ||
|
|
||
| setHighlightedItem(selectedItem) | ||
| highlightedItemRef.current = selectedItem | ||
| setIsMenuDirectlyActivated(directlyActivated) | ||
|
|
||
| // Update autocomplete suggestion inline (moved from the useEffect | ||
| // that previously depended on highlightedItem state) | ||
| if (selectedItem?.text?.startsWith(deferredInputValue) && !selectedItemIds.includes(selectedItem.id)) { | ||
| setAutocompleteSuggestion(selectedItem.text) | ||
| } else { | ||
| setAutocompleteSuggestion('') | ||
| } | ||
| } | ||
|
|
||
| if (current && customScrollContainerRef && customScrollContainerRef.current && directlyActivated) { | ||
|
|
@@ -314,14 +375,15 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| ) | ||
|
|
||
| useEffect(() => { | ||
| // Use deferredInputValue to avoid running this effect on every keystroke | ||
| // The Input component guards against stale suggestions | ||
| if (highlightedItem?.text?.startsWith(deferredInputValue) && !selectedItemIds.includes(highlightedItem.id)) { | ||
| setAutocompleteSuggestion(highlightedItem.text) | ||
| // When the input value changes, update the autocomplete suggestion based | ||
| // on the currently highlighted item (tracked via ref, not state). | ||
| const highlighted = highlightedItemRef.current | ||
| if (highlighted?.text?.startsWith(deferredInputValue) && !selectedItemIds.includes(highlighted.id)) { | ||
| setAutocompleteSuggestion(highlighted.text) | ||
| } else { | ||
| setAutocompleteSuggestion('') | ||
| } | ||
| }, [highlightedItem, deferredInputValue, selectedItemIds, setAutocompleteSuggestion]) | ||
| }, [deferredInputValue, selectedItemIds, setAutocompleteSuggestion]) | ||
|
|
||
| useEffect(() => { | ||
| const itemIdSortResult = [...sortedItemIds].sort( | ||
|
|
@@ -364,41 +426,9 @@ function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMe | |
| id={`${id}-listbox`} | ||
| aria-labelledby={ariaLabelledBy} | ||
| > | ||
| {allItemsToRender.map(item => { | ||
| const { | ||
| id, | ||
| onAction, | ||
| children, | ||
| text, | ||
| leadingVisual: LeadingVisual, | ||
| trailingVisual: TrailingVisual, | ||
| key, | ||
| role, | ||
| ...itemProps | ||
| } = item | ||
| return ( | ||
| <ActionList.Item | ||
| key={(key ?? id) as string | number} | ||
| onSelect={() => onAction(item)} | ||
| {...itemProps} | ||
| id={id} | ||
| data-id={id} | ||
| role={role as AriaRole} | ||
| > | ||
| {LeadingVisual && ( | ||
| <ActionList.LeadingVisual> | ||
| {isElement(LeadingVisual) ? LeadingVisual : <LeadingVisual />} | ||
| </ActionList.LeadingVisual> | ||
| )} | ||
| {(children ?? text) as React.ReactNode} | ||
| {TrailingVisual && ( | ||
| <ActionList.TrailingVisual> | ||
| {isElement(TrailingVisual) ? TrailingVisual : <TrailingVisual />} | ||
| </ActionList.TrailingVisual> | ||
| )} | ||
| </ActionList.Item> | ||
| ) | ||
| })} | ||
| {allItemsToRender.map(item => ( | ||
| <MemoizedAutocompleteItem key={(item.key ?? item.id) as string | number} item={item} /> | ||
| ))} | ||
| </ActionList> | ||
| ) : emptyStateText !== false && emptyStateText !== null ? ( | ||
| <div className={classes.EmptyStateWrapper}>{emptyStateText}</div> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MemoizedAutocompleteItemis typed asT extends Record<string, any>, but the implementation assumes specific fields exist (id,onAction,leadingVisual, etc.). Using a concrete type (e.g.AutocompleteMenuItemplus the augmented fields added inselectableItems) would prevent accidental misuse and avoid theanyescape hatch.