Skip to content

Conversation

millus
Copy link
Collaborator

@millus millus commented Sep 17, 2025

Description

This PR fixes an issue regarding scroll position and start index when navigating single-select in the Autocomplete component.

Why

Users have expressed that the current behaviour in single-select is breaking with how expected UX for select option should be.

How

  • Scroll position is now preserved in single-select so it feels more natural when closing and reopening
  • Remember previous selected option and scroll position when reopening the single-select, so when using arrows on the keyboard you are continuing down the list from the previous selected option not the top option

Related Issue

Resolves #3430

…ed highlighted index and improved scrolling behavior starting at selected index
@millus millus added the 🐛 bug Something isn't working label Sep 17, 2025
@millus millus marked this pull request as draft September 17, 2025 12:45
@millus millus changed the title fix(eds-core-react): 🐛 Autocomplete - start at selected index and preserve scroll position fix(eds-core-react): 🐛 Autocomplete - should start at selected index and preserve scroll position Sep 18, 2025
@millus millus marked this pull request as ready for review September 18, 2025 12:20
@millus millus requested a review from Copilot September 18, 2025 12:21
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes UX issues in the Autocomplete component's single-select mode by preserving scroll position and starting navigation from the previously selected option.

  • Adds scroll position preservation when closing and reopening the dropdown
  • Implements starting navigation from the last selected index instead of always beginning at index 0
  • Refactors state change handlers to support controlled highlighted index behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
Autocomplete.tsx Implements scroll position preservation and controlled highlighted index logic with new state variables and updated event handlers
Autocomplete.test.tsx.snap Updates test snapshot to reflect changes in selected state and CSS class assignments

Comment on lines +638 to +651
setTimeout(() => {
if (controlledHighlightedIndex === 0) {
rowVirtualizer.scrollToOffset?.(0)
} else if (rowVirtualizer.scrollToOffset && lastScrollOffset > 0) {
rowVirtualizer.scrollToOffset(lastScrollOffset)
}
const visibleIndexes =
rowVirtualizer.getVirtualItems?.().map((v) => v.index) || []
if (!visibleIndexes.includes(controlledHighlightedIndex)) {
rowVirtualizer.scrollToIndex(controlledHighlightedIndex, {
align: allowSelectAll ? 'center' : 'auto',
})
}
}, 10)
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The setTimeout with a 10ms delay appears to be a workaround for timing issues. Consider using a more reliable approach like waiting for the virtual list to be ready or using a callback-based solution to avoid potential race conditions.

Copilot uses AI. Check for mistakes.

@millus millus self-assigned this Sep 18, 2025
@millus millus removed their assignment Sep 18, 2025
Copy link
Collaborator

@pomfrida pomfrida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this @millus. 👌 The UX and keyboard navigation feels very smooth now for single select.

While I understand from the issue discussion that multi-select was intentionally deferred, both scroll position preservation and smart start index would benefit multi-select users as well. Consider a user working through a long list who accidentally presses ESC - they should be able to reopen the dropdown at the same scroll position and continue navigation from a relevant item, regardless of selection mode.

For multi-select implementation:

  • Scroll preservation: Move the logic outside the if (!multiple) condition
  • Smart start index: Could use the last selected item as the starting point

Both changes would create consistent UX across selection modes while leveraging the existing logic you've built.

I'd also suggest adding unit tests in Autocomplete.test.tsx specifically for the scroll position preservation behaviour and keyboard navigation starting from selected items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remember selected option/scroll position for in the autocomplete dropdown

2 participants