lordfriend/nya-bootstrap-select

Changing the collection with a selected (no longer valid) modelValue results in first element selected

hawkeye-bot opened this issue · 3 comments

When there is a change in the available options, the onCollectionChange function is called. In this function, there is a check that verifies if the previously selected value is still available in the new collection. If the new collection does not contain the previously selected value, the first element of the new collection is selected.

This is undesired behaviour, as it presumes to know what the right value will be in this situation. I cannot imagine there is a scenario where you would want the system to select 'the first' option available if the previous selection is no longer available.

This action is performed in https://github.com/lordfriend/nya-bootstrap-select/blob/master/src/nya-bs-select.js on line 291:
if(!contains(valuesForSelect, modelValue)) { modelValue = valuesForSelect[0];

Well, This makes sense when designing this project. As a reference, I picked system native select component which will always select an option. so I make this. Maybe today there are some components that allow use deselect or select nothing as default when they are in single select mode.

@lordfriend thanks for the quick response. Selecting a default value makes sense from the perspective of a native select component. However, the nya-bootstrap-select provides functionality to clear the selected value (making the element empty). Give these 2 functionalities, there is an inconsistency in the whole.

I can make a pull request, but the desired behaviour would need to be clear. I can imagine that this is something that can be configured along the lines of
if(!contains(valuesForSelect, modelValue)) { if(clearOnExpiredEntry) { modelValue = undefined; } else { modelValue = valuesForSelect[0]; } }

Does that make sense?

Hmm, This makes sense if changes can be configured. That will maintain compatibility with some old project depending on this project.

And, if possible, add a test spec for this change.