Skip to content

Conversation

@rubencarvalho
Copy link
Contributor

@rubencarvalho rubencarvalho commented Jan 19, 2026

Description

This PR fixes an issue where a Picker component with a value set before menu-items are rendered doesn't display the label when menu-items are added later (e.g., when using Lit's when() directive for conditional rendering or lazy loading).

The fix preserves the Picker's value when manageSelection() runs before menu-items are available, allowing the value to be matched and the label to display correctly once menu-items are added.

Motivation and context

When a Picker has a value attribute set but its menu-items are conditionally rendered or loaded asynchronously, the component's manageSelection() method runs before menu-items exist. Previously, this would clear the value immediately, preventing the label from displaying even after menu-items were added.

This is a common pattern in modern web applications where:

  • Menu-items are lazy-loaded from an API
  • Menu-items are conditionally rendered based on user actions or application state
  • Menu-items are dynamically generated based on other component state

The fix ensures the Picker maintains the value until menu-items are available, then correctly matches and displays the label.

Related issue(s)

Author's checklist

  • I have read the CONTRIBUTING and PULL_REQUESTS documents.
  • I have reviewed at the Accessibility Practices for this feature, see: Aria Practices
  • I have added automated tests to cover my changes.
  • I have included a well-written changeset if my change needs to be published.
  • I have included updated documentation if my change required it.

Reviewer's checklist

  • Includes a Github Issue with appropriate flag or Jira ticket number without a link
  • Includes thoughtfully written changeset if changes suggested include patch, minor, or major features
  • Automated tests cover all use cases and follow best practices for writing
  • Validated on all supported browsers
  • All VRTs are approved before the author can update Golden Hash

Manual review test cases

  • Verify StackBlitz isolated example now works

    1. Visit this example story here
    2. Click the button to render the menu items
    3. Verify the Picker now shows the correct item (is not empty!)

@changeset-bot
Copy link

changeset-bot bot commented Jan 19, 2026

🦋 Changeset detected

Latest commit: 82fdcb1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 78 packages
Name Type
@spectrum-web-components/picker Patch
@spectrum-web-components/action-menu Patch
@spectrum-web-components/bundle Patch
@spectrum-web-components/breadcrumbs Patch
@spectrum-web-components/accordion Patch
@spectrum-web-components/action-bar Patch
@spectrum-web-components/action-button Patch
@spectrum-web-components/action-group Patch
@spectrum-web-components/alert-banner Patch
@spectrum-web-components/alert-dialog Patch
@spectrum-web-components/asset Patch
@spectrum-web-components/avatar Patch
@spectrum-web-components/badge Patch
@spectrum-web-components/button-group Patch
@spectrum-web-components/button Patch
@spectrum-web-components/card Patch
@spectrum-web-components/checkbox Patch
@spectrum-web-components/clear-button Patch
@spectrum-web-components/close-button Patch
@spectrum-web-components/coachmark Patch
@spectrum-web-components/color-area Patch
@spectrum-web-components/color-field Patch
@spectrum-web-components/color-handle Patch
@spectrum-web-components/color-loupe Patch
@spectrum-web-components/color-slider Patch
@spectrum-web-components/color-wheel Patch
@spectrum-web-components/combobox Patch
@spectrum-web-components/contextual-help Patch
@spectrum-web-components/dialog Patch
@spectrum-web-components/divider Patch
@spectrum-web-components/dropzone Patch
@spectrum-web-components/field-group Patch
@spectrum-web-components/field-label Patch
@spectrum-web-components/help-text Patch
@spectrum-web-components/icon Patch
@spectrum-web-components/icons-ui Patch
@spectrum-web-components/icons-workflow Patch
@spectrum-web-components/icons Patch
@spectrum-web-components/iconset Patch
@spectrum-web-components/illustrated-message Patch
@spectrum-web-components/infield-button Patch
@spectrum-web-components/link Patch
@spectrum-web-components/menu Patch
@spectrum-web-components/meter Patch
@spectrum-web-components/modal Patch
@spectrum-web-components/number-field Patch
@spectrum-web-components/overlay Patch
@spectrum-web-components/picker-button Patch
@spectrum-web-components/popover Patch
@spectrum-web-components/progress-bar Patch
@spectrum-web-components/progress-circle Patch
@spectrum-web-components/radio Patch
@spectrum-web-components/search Patch
@spectrum-web-components/sidenav Patch
@spectrum-web-components/slider Patch
@spectrum-web-components/split-view Patch
@spectrum-web-components/status-light Patch
@spectrum-web-components/swatch Patch
@spectrum-web-components/switch Patch
@spectrum-web-components/table Patch
@spectrum-web-components/tabs Patch
@spectrum-web-components/tags Patch
@spectrum-web-components/textfield Patch
@spectrum-web-components/thumbnail Patch
@spectrum-web-components/toast Patch
@spectrum-web-components/tooltip Patch
@spectrum-web-components/top-nav Patch
@spectrum-web-components/tray Patch
@spectrum-web-components/underlay Patch
@spectrum-web-components/base Patch
@spectrum-web-components/grid Patch
@spectrum-web-components/opacity-checkerboard Patch
@spectrum-web-components/reactive-controllers Patch
@spectrum-web-components/shared Patch
@spectrum-web-components/styles Patch
@spectrum-web-components/theme Patch
@spectrum-web-components/truncated Patch
@spectrum-web-components/eslint-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rubencarvalho rubencarvalho added the Status: Ready for review PR ready for review or re-review. label Jan 19, 2026
@rubencarvalho rubencarvalho marked this pull request as ready for review January 19, 2026 15:48
@rubencarvalho rubencarvalho requested a review from a team as a code owner January 19, 2026 15:48
@github-actions
Copy link
Contributor

github-actions bot commented Jan 19, 2026

📚 Branch Preview Links

🔍 First Generation Visual Regression Test Results

When a visual regression test fails (or has previously failed while working on this branch), its results can be found in the following URLs:

Deployed to Azure Blob Storage: pr-5972

If the changes are expected, update the current_golden_images_cache hash in the circleci config to accept the new images. Instructions are included in that file.
If the changes are unexpected, you can investigate the cause of the differences and update the code accordingly.

@rubencarvalho rubencarvalho changed the title fix(picker): preserve value when menu-items are added after value is set fix(picker): preserve selection when menu-items are added after value is set Jan 19, 2026
Comment on lines 870 to 873
if (this.menuItems.length > 0) {
this.value = '';
this.selectedItem = undefined;
}
Copy link
Contributor

@Rajdeepc Rajdeepc Jan 20, 2026

Choose a reason for hiding this comment

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

Currently with this change we are assuming items exist and items are meaningful, which might not always be true coz menu-item can exist in DOM in incomplete states before upgrade with no value or value not populated yet during async render. We should only clear only when items exist and at least one item has a real value and none of those values match the picker's value. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! I updated the code + tests for this! 👍

Comment on lines +869 to +871
const hasItemsWithValues = this.menuItems.some(
(item) => item.value && item.value.trim() !== ''
);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good but it’s still unsafe in a custom-elements lifecycle. This assumes item.value is always present and authoritative. <sp-menu-item> can exist in the DOM before it is upgraded. In that state, item.value is undefined even if the element already has a value attribute. This will therefore return false for items that do have meaningful values at the DOM level. We should check both the property and the attribute, and only care about existence.

const hasItemsWithValues = this.menuItems.some(
    (item) =>
        (item as any).value != null ||
        item.getAttribute?.('value') != null
);

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

Labels

Status: Ready for review PR ready for review or re-review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Picker don't update label if menu-items render or change later

3 participants