Skip to content

Conversation

@jrieke
Copy link
Collaborator

@jrieke jrieke commented Dec 24, 2024

Describe your changes

When you click on a selectbox (or multiselect), scroll in the dropdown, then close it and click on the selectbox again, the dropdown opens at the top position. This can be annoying if the dropdown is large. With this PR, the dropdown will open at the same position where it was previously closed. Also, when the dropdown opens for the first time, it opens at the position of the selected default value.

Before:

CleanShot.2024-12-31.at.00.55.38.mp4

After:

CleanShot.2024-12-31.at.00.55.03.mp4

GitHub Issue Link (if applicable)

Closes #4901

Testing Plan

  • Added a JS unit test on the Selectbox component that simulates scrolling, closes the dropdown, and checks that it opens at the same position.
  • Did not add an e2e test because it would basically do the same thing and that feels a bit overkill. But I can add e2e test for st.selectbox and st.multiselect if we want to have them.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@jrieke jrieke changed the title Preserve st.selectbox scroll position [WIP] Preserve st.selectbox scroll position Dec 24, 2024
@jrieke jrieke changed the title [WIP] Preserve st.selectbox scroll position [WIP] Preserve scroll position for st.selectbox and st.multiselect Dec 24, 2024
@jrieke jrieke added security-assessment-completed Security assessment has been completed for PR change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Dec 24, 2024
@jrieke jrieke changed the title [WIP] Preserve scroll position for st.selectbox and st.multiselect Preserve scroll position for st.selectbox and st.multiselect Dec 24, 2024
@jrieke jrieke marked this pull request as ready for review December 24, 2024 22:47
@sfc-gh-dmatthews
Copy link
Collaborator

This is so much better! Is it possible to open to the last selected option instead of the last view? (I understand if this is a quicker solution (that is clearly an improvement), but I'd expect the scroll to match the last click event rather than a possibly exploratory scroll.)

@jrieke
Copy link
Collaborator Author

jrieke commented Dec 26, 2024

Hm I'd actually argue it makes more sense at the last scroll position. E.g. if I have option 20 selected and I go through the list looking for something at position 80, and then close the dropdown because I need to look up something else, it would be pretty annoying if it opens at 20 again. (Can see both sides of course but since this is already implemented, will leave it as is.)

@sfc-gh-dmatthews
Copy link
Collaborator

sfc-gh-dmatthews commented Dec 27, 2024

Fair enough. In that case, I'd raise a tiny vote to leave #4901 open because it does say "last selected item." The PR wouldn't really do anything for a widget with a default value that's freshly loaded, for example (at least it didn't when I tested the wheel). When I open a picker, I expect to be at the current selection. I would still upvote the feature request to at least deal with the initial scroll position. (The PR is still a huge improvement and I love it, don't get me wrong...) 😁

@sfc-gh-jrieke
Copy link
Contributor

Oops, I should properly read the issue description before implementing haha. I changed it to 1) open at the default value if the dropdown hasn't been scrolled before and 2) open at the previously scrolled position if it has been scrolled.

component: VirtualDropdown,
props: {
$menuListProps: {
initialScrollOffset: getInitialScrollPosition(),
Copy link
Collaborator

@lukasmasuch lukasmasuch Jan 18, 2025

Choose a reason for hiding this comment

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

What happens if the initialScrollOffset is larger as the available menu list (for some unexpected reason). Does it run into an error, or will it just scroll to the bottom?

E.g. lets say we allow changing the options for the same widget, would it break the dropdown somehow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just tested it, it simply scrolls to the end, doesn't break.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok 👍

// Close dropdown
// TODO: Utilize user-event instead of fireEvent
// eslint-disable-next-line testing-library/prefer-user-event
fireEvent.keyDown(selectbox, { key: "Escape" })
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: did you try to use user-event here? Did it cause some issues?

Copy link
Collaborator Author

@jrieke jrieke Jan 18, 2025

Choose a reason for hiding this comment

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

Ah I thought I did, but apparently I just used fireEvent because it was used in the other test above. I tried it out and it works, so I changed this test to userEvent. Unfortunately, I couldn't get the other test above with fireEvent to work with userEvent, so I'm leaving that as is for now.

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 👍

@jrieke jrieke enabled auto-merge (squash) January 18, 2025 02:48
@jrieke jrieke merged commit b743ac0 into develop Jan 18, 2025
33 checks passed
@jrieke jrieke deleted the feature/selectbox-scroll-position branch January 18, 2025 03:02
edegp pushed a commit to edegp/streamlit that referenced this pull request Jan 19, 2025
…eamlit#10073)

## Describe your changes

When you click on a selectbox (or multiselect), scroll in the dropdown,
then close it and click on the selectbox again, the dropdown opens at
the top position. This can be annoying if the dropdown is large. With
this PR, the dropdown will open at the same position where it was
previously closed. Also, when the dropdown opens for the first time, it
opens at the position of the selected default value.

**Before:**


https://github.com/user-attachments/assets/7b361fb1-0e0d-47db-b627-5391455a2228


**After:**


https://github.com/user-attachments/assets/69ca577a-d78e-43fb-8a26-11ec56003638


## GitHub Issue Link (if applicable)

Closes streamlit#4901

## Testing Plan

- Added a JS unit test on the `Selectbox` component that simulates
scrolling, closes the dropdown, and checks that it opens at the same
position.
- Did not add an e2e test because it would basically do the same thing
and that feels a bit overkill. But I can add e2e test for `st.selectbox`
and `st.multiselect` if we want to have them.

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.

---------

Co-authored-by: Johannes Rieke <[email protected]>
raethlein added a commit that referenced this pull request Jan 23, 2025
raethlein added a commit that referenced this pull request Jan 23, 2025
#10238)

## Describe your changes

Revert #10073 because
@lukasmasuch and @jrieke detected an issue with it.

## GitHub Issue Link (if applicable)

## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
- E2E Tests
- Any manual testing needed?

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement 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.

Make st.selectbox dropdown re-open at position of last selected option, not at the top every time

5 participants