Keep selected option index after selecting value in multiselect #5445
Replies: 12 comments
-
Greetings @5ebastianMeier , I could probably come up with a rough way of doing this by modifying the onMouseDown/onTouchStart events to identify the next option before calling onChange, saving the ref, scrolling the ref into view onMenuOpen. I think your suggestion presents some problems as the index could change especially as user types or if selection in any way disables or changes the list of options which is why you may want to save a ref instead. But for your use-case, setting What are your thoughts about this? |
Beta Was this translation helpful? Give feedback.
-
@ebonow thanks for mentioning the The proposed solution via refs seems like the way to go - I'm not very deep into the internals of I think you're right that other actions like typing a search term should have precedence over the "sticky selected option" I proposed above. When applying the example on this use case, this seems like the appropriate behavior to me:
Slightly more advanced use case where the preserved selected index could be maintained during a search:
In a nutshell, these are the open questions: What happens when a preserved selected index is present, but the associated entry gets hidden (e.g. by search or by being disabled)
What happens when the menu is closedThe preserved selected index should be cleared because the feature is only relevant as long as the menu stays open. I don't want this feature to be too prominent and interfere with existing features. It should just close the gap where appropriate so that the selected index is not lost if not necessary. Sorry for the rather unstructured wall of text 🤪 - looking forward to your thoughts |
Beta Was this translation helpful? Give feedback.
-
My thoughts are that the behavior you are describing is too narrow of a use-case to be considered as a built-in feature request given the complexity of criteria, so this would be a pass from me and likely the other collaborators. Perhaps better are some discussions being had in regards to #4080 to better define what the default focused option should be when the menu opens giving the user more control to define what should be selected. Given your use-case and wanting to preserve the focus option, I would err on the side of simplicity and consider an existing implementation of Select that would better fit what you are trying to achieve. Since you are already using From a UX perspective, it sounds like the use-case is that the user is very likely to want to select consecutive items. Keeping the selected options maintains the context of the last selection as opposed to focusing an option that may appear arbitrary. This would allow the selected option to remain focused which benefits the user by giving them more context as to what is focused. Generally this approach is better clarified by providing providing checkboxes in front of each option and seems more in line with achieving what the UX you are looking for. This approach also takes into account what happens if a user does open/close the menu or decides to type more filter text. <Select
isMulti={true}
closeMenuOnSelect={false}
hideSelectedOptions={false}
{...otherProps}
/> https://codesandbox.io/s/codesandboxer-example-forked-wdq8n?file=/example.js If you are steadfast on your approach, then it could be complex to obtain with the existing api. For starters, it won't be as responsive as it would have to rely on I'm believer that this library is not meant to be everything for everyone, but rather anything for anyone, so while I don't think the proposal fits as a feature request, I could try to assist with such an implementation for your own use-case if you create a sandbox. |
Beta Was this translation helpful? Give feedback.
-
Hey @5ebastianMeier and @ebonow thank you for your effort on this! I'm experiencing the issue in a similar way
Something like Would that make a difference in how complex it is to implement? |
Beta Was this translation helpful? Give feedback.
-
Related to (or a duplicate of) #2813. There's a possibility that this is a bug because there is code that is trying to do something like this already. |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issue. I worked it around by saving const onChangeOption = (selectedOptions, data) => {
handleSelectedOptions(selectedOptions);
window.requestAnimationFrame(() => {
selectRef.setState.call(selectRef, {
focusedOption: data.option,
});
});
}; I know it's not very elegant but works. |
Beta Was this translation helpful? Give feedback.
-
could you provide an example please, I couldn't make it work using this |
Beta Was this translation helpful? Give feedback.
-
mb it'll help someone |
Beta Was this translation helpful? Give feedback.
-
This worked for me. But it does make me curious if this will inadvertently break something else. |
Beta Was this translation helpful? Give feedback.
-
Awakening an old thread, but it looks like a simple change to Something like:
Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Hey! I'm having this issue with a MultiSelect, the thing is, I set attribute |
Beta Was this translation helpful? Give feedback.
-
Any update to this? Seems to be caused by this line in setValue this.setState({ clearFocusValueOnUpdate: true }); Which cause the next call to getDerivedStateFromProps to reset the focus index. This makes multi select with Does this logic of clearing focus index make sense when |
Beta Was this translation helpful? Give feedback.
-
Use Case
We have a multiselect control that is designed to select multiple users in succession. Currently this can be tedious because after selecting an option, the former selected position gets reset to start at the first option.
Example
Let's say you wanted to add option 6, 7 and 8.
With this new feature you could just navigate to 6 using the arrow keys and the press tab or enter thrice.
Without the feature you would have to navigate back to the next option after each individuell tab or enter press.
I couldn't find a working example in the wild, but I think the intention is pretty straight forward. If not, I'll gladly provide more insight.
Beta Was this translation helpful? Give feedback.
All reactions