Skip to content

Conversation

@mayagbarnes
Copy link
Collaborator

Describe your changes

When you set the st.selectbox value via session state, and then open/close the selectbox dropdown menu without making a selection, the st.selectbox value in the UI transitions to the first option (even though session state value is correct).

Typically, our valueBeforeRemoval ref handles when user starts typing to filter options - it stores the current value so it can be restored if they blur without selecting. This is initialized on mount with the initial selectbox value and only resets to null when the user makes a selection via handleChange. It is not updated when propValue changes externally via session state. So this PR addresses the issue by resetting the ref to null when propValue changes, which ensures the stale ref doesn't interfere after external value changes.

GitHub Issue Link (if applicable)

Closes #13435

Testing Plan

  • JS Unit Tests: ✅ Added
  • E2E Tests: ✅ Added

@mayagbarnes mayagbarnes added security-assessment-completed Security assessment has been completed for PR change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Dec 22, 2025
@snyk-io
Copy link
Contributor

snyk-io bot commented Dec 22, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 22, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13438/streamlit-1.52.2-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13438.streamlit.app (☁️ Deploy here if not accessible)

@mayagbarnes mayagbarnes changed the title [Fix] [Fix] st.selectbox set via session state does not restore initial value Dec 22, 2025
@mayagbarnes mayagbarnes marked this pull request as ready for review December 22, 2025 20:25
Copilot AI review requested due to automatic review settings December 22, 2025 20:25
Copy link
Contributor

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 a bug where st.selectbox would incorrectly revert to displaying the first option in the UI when its value was set via session state and the user opened/closed the dropdown without making a selection. The fix ensures the valueBeforeRemoval ref is reset when the value changes externally, preventing it from restoring a stale value on blur.

Key changes:

  • Modified the useExecuteWhenChanged hook callback in Selectbox to reset valueBeforeRemoval.current to null when propValue changes
  • Added comprehensive test coverage with both frontend unit tests and E2E tests

Reviewed changes

Copilot reviewed 4 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
frontend/lib/src/components/shared/Dropdown/Selectbox.tsx Resets valueBeforeRemoval ref to null when propValue changes externally to prevent stale value restoration
frontend/lib/src/components/shared/Dropdown/Selectbox.test.tsx Adds unit test verifying selectbox preserves value after prop change and blur without selection
e2e_playwright/st_selectbox.py Adds test app section with buttons to trigger session state changes for regression testing
e2e_playwright/st_selectbox_test.py Adds E2E test verifying UI stays in sync when value set via session state and dropdown opened/closed
e2e_playwright/__snapshots__/linux/st_selectbox_test/st_selectbox-dynamic_*.png Updated snapshots for dynamic selectbox tests to reflect the additional test content

@github-actions
Copy link
Contributor

github-actions bot commented Dec 22, 2025

📉 Frontend coverage change detected

The frontend unit test (vitest) coverage has decreased by 0.0000%

  • Current PR: 86.4700% (12740 lines, 1723 missed)
  • Latest develop: 86.4700% (12738 lines, 1723 missed)

✅ Coverage change is within normal range.

📊 View detailed coverage comparison

Copy link
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

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

LGTM 👍

# Regression test for https://github.com/streamlit/streamlit/issues/13435
# Test that selectbox UI stays in sync when value is set via session_state
# and user opens/closes dropdown without selecting
with st.container(horizontal=True):
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be better to put this under the dynamic selectbox to prevent subpixel flakiness

@mayagbarnes mayagbarnes merged commit 2d581a8 into develop Dec 23, 2025
44 checks passed
@mayagbarnes mayagbarnes deleted the fix-13435 branch December 23, 2025 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

st.selectbox UI unexpectedly switches to first option

3 participants