-
Notifications
You must be signed in to change notification settings - Fork 4k
[Fix] st.selectbox set via session state does not restore initial value
#13438
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
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
st.selectbox set via session state does not restore initial value
633932a to
40e1820
Compare
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.
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
useExecuteWhenChangedhook callback in Selectbox to resetvalueBeforeRemoval.currenttonullwhenpropValuechanges - 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 |
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0000%
✅ Coverage change is within normal range. |
This stack of pull requests is managed by Graphite. Learn more about stacking. |
40e1820 to
9d75b91
Compare
9d75b91 to
42dfe42
Compare
lukasmasuch
left a comment
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.
LGTM 👍
e2e_playwright/st_selectbox.py
Outdated
| # 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): |
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.
It might be better to put this under the dynamic selectbox to prevent subpixel flakiness

Describe your changes
When you set the
st.selectboxvalue via session state, and then open/close the selectbox dropdown menu without making a selection, thest.selectboxvalue in the UI transitions to the first option (even though session state value is correct).Typically, our
valueBeforeRemovalref 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 tonullwhen the user makes a selection viahandleChange. It is not updated whenpropValuechanges externally via session state. So this PR addresses the issue by resetting the ref tonullwhenpropValuechanges, which ensures the stale ref doesn't interfere after external value changes.GitHub Issue Link (if applicable)
Closes #13435
Testing Plan