dojo / widgets

:rocket: Dojo - UI widgets.
https://widgets.dojo.io
Other
89 stars 65 forks source link

Typeahead widget does not trigger onValue event when user clears the field (no selection) #1655

Open arndbeissner opened 3 years ago

arndbeissner commented 3 years ago

Bug

If set to controlled but not required the user can select items from the list which triggers the onValue event. This is expected and needed. However, if the user then emptied the Typeahead field so that there is no selection, the onValue event is NOT triggered. This is a critical bug as it does not allow for status changes on dependent widgets.

Package Version: @dojo/widgets 7.0.3

Code

The issue can be seen with Validation.tsx example from the Dojo widgets documentation. Just remove the required attribute from the widget, add alert("value=" + value ? value.label :" -"); to the existing onValue event.

Expected behavior:

The onValue event is triggered will value undefined (to set to empty the Typeahead wants undefined instead of null, so the event should use undefined as well).

Actual behavior:

The onValue event is not trigger when the user changes the Typeahead to be empty

tomdye commented 3 years ago

@arndbeissner thanks for raising this issue. It has started a discussion within our team regarding the behaviour of the typeahead and that of the select widget it is based upon and how a standard html select behaves with regard to deselection.

Looking at the default behaviour of a native html select, it will select the first value (if no selected value is specified) and does not allow you to deselect. The standard way to get around this is to add an empty value option to the select which therefor gets selected by default and can be used to effectively de-select. We believe that following this approach and standardising it across both the Typeahead and the Select is the best and most consistent way forward.

The changes we put forward to achieve this are:

arndbeissner commented 3 years ago

@tomdye thanks for looking quite deep into this. I think it's worth it, a good Typeahead widget is a core component of so many applications.

What you outline seems to be a very flexible solution, and it also gives a way to make that "empty" selection from within the list, not just from within the textbox. I just have two amendments (which may already be on your list):

That being said, for 7.x, I will have to make a private fork with a simple behavior patch that makes onValue fire on empty, because honestly, I just can't ship as it is. Even if it's just a small admin application.

tomdye commented 3 years ago

I agree with both your amendments @arndbeissner , I thought they might have been implied by the changes I suggested but I'm thankful that you clarified.

tomdye commented 3 years ago

This issue is related to https://github.com/dojo/widgets/issues/583 also