Create new options on the fly from user-written values => add a callback for unknown choices #594
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Sometimes the user needs to be allowed to add some not-yet-existing options on the fly.
Motivation and Context
If this library is to allow the user to enter a set of freely-written tags, choices need to be created from the user input on the fly if they don't exist yet.
I guess the motivation is similar to the one for a tagging mode in Select2: https://select2.org/tagging , and related to pull-request #525 "Allow user-created choices for selects". So this addresses #39 as well. (We only discovered those issues after implementing our approach.)
In this implementation here, we provide a callback to be used when the user enters a value not yet available among the existing options, and allow the user to bypass the dropdown consultation entirely by holding the Shift key together with the Enter key. This approach is different from the ones mentioned above.
@caramiki: maybe you'd like to comment / compare?
How Has This Been Tested?
It works on multiple-value selects in our application. Something like the following callback function can be passed as a configuration option to Choices:
Of course, this uses internal functions...
Types of changes
Checklist: