Skip to content

feat: add setFocusSelectedItem to ComboBox#9239

Open
DiegoCardoso wants to merge 1 commit into
refactor/combo-box-connector-pending-requestsfrom
feat/combo-box-focus-selected-item
Open

feat: add setFocusSelectedItem to ComboBox#9239
DiegoCardoso wants to merge 1 commit into
refactor/combo-box-connector-pending-requestsfrom
feat/combo-box-focus-selected-item

Conversation

@DiegoCardoso
Copy link
Copy Markdown
Contributor

@DiegoCardoso DiegoCardoso commented May 7, 2026

Restores the pre-vaadin/web-components#6055 behavior of focusing the currently selected item when the combo-box dropdown opens, behind an opt-in flag.

comboBox.setFocusSelectedItem(true);

Off by default. When enabled, opening the dropdown scrolls to the currently selected item. The implementation listens for vaadin-combo-box-dropdown-opened, resolves the selected item's index via the data view (in-memory or lazy with ItemIndexProvider), and calls scrollToIndex on the web component (added in vaadin/web-components#11552).

The listener is a silent no-op when:

  • no value is currently selected,
  • a filter is active (the user is narrowing down — don't override their navigation by jumping to the previously selected item),
  • the lazy data view has no ItemIndexProvider configured, or
  • the resolved index cannot be determined.

In-memory data works out of the box (no ItemIndexProvider needed).

Tests:

  • ComboBoxTest: defaults-to-false and getter/setter round-trip.
  • ComboBoxFocusSelectedItemIT: 13 tests covering the happy path, reopen, value change, filter behavior (active and cleared), missing-provider fallback, in-memory case, runtime toggle, detach + reattach, and toggle-off regression guard.

Note

Stacked on #9238. Will retarget to main once that merges.

Part of vaadin/web-components#6061

🤖 Generated with Claude Code

@DiegoCardoso DiegoCardoso force-pushed the feat/combo-box-focus-selected-item branch 2 times, most recently from 457506d to f7b15a7 Compare May 7, 2026 12:13
@DiegoCardoso DiegoCardoso force-pushed the refactor/combo-box-connector-pending-requests branch 4 times, most recently from 43ca860 to fea69b9 Compare May 13, 2026 16:41
Adds an opt-in API on ComboBox<T> that scrolls the dropdown to the
currently selected item when it opens, restoring the pre-#6055 behavior
behind a flag.

  comboBox.setFocusSelectedItem(true);

Off by default. The listener for `vaadin-combo-box-dropdown-opened`
runs server-side, resolves the selected item's index via the data view
(in-memory or lazy with ItemIndexProvider), and calls scrollToIndex on
the web component. No @ClientCallable or connector code is needed:
PR #11552's scrollToIndex queues the call when the overlay is loading,
and the connector refactor (separate PR) means the controller's
pendingRequests map is now coherent across close/reopen.

The listener is a silent no-op when:
- no value is currently selected,
- a filter is active (the user is narrowing down — don't override their
  navigation by jumping to the previously selected item),
- the lazy data view has no ItemIndexProvider configured, or
- the resolved index cannot be determined.

In-memory data works out of the box (no ItemIndexProvider needed).

Tests:
- ComboBoxTest: defaults-to-false and getter/setter round-trip.
- ComboBoxFocusSelectedItemIT: 13 tests covering the happy path,
  reopen, value change, filter behavior, missing-provider fallback,
  in-memory case, runtime toggle, detach+reattach, and toggle-off
  regression guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@DiegoCardoso DiegoCardoso force-pushed the feat/combo-box-focus-selected-item branch from f7b15a7 to 396fc99 Compare May 14, 2026 12:32
@sonarqubecloud
Copy link
Copy Markdown

} else {
index = getLazyDataView().getItemIndex(getValue()).orElse(null);
}
} catch (UnsupportedOperationException e) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does the exception need to be suppressed?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants