sebfz1 / wicket-jquery-ui

jQuery UI & Kendo UI integration in Wicket
http://www.7thweb.net/wicket-jquery-ui/
Other
93 stars 58 forks source link

AutoCompleteTextField headaches #313

Closed reckart closed 4 years ago

reckart commented 4 years ago

I have observed some issues with the AutoCompleteTextField.

The first issue is actually in Kendo itself: the AutoComplete JS assumes that every item in the auto-suggest dropdown has a different label. If multiple items have the same label, then:

The second issue is with the Wicket Kendo AutoCompleteTextField itself. When used in conjunction with a AjaxFormComponentUpdatingBehavior('change'), the component suffers from the same problems as above. In particular because the component model value is then set using by locating the first choice item which has the same label as the value from the input field.

After a lot of fiddling around, I discovered that the onSelected() method actually does the right thing because it considers the index of the selected item. However, onSelected() is not called when the input field is cleared (e.g. using the clear button, or manually).

What I am now doing is this:

(I could do the last two things using an index position as the onSelected() does it, but because I only found out about that later, I don't).

I think that I would have been happy using onSelected() if it would get notifications about e.g. the user clearing the selection.

Am I missing something obvious or is it normal to have these kinds of issues?

sebfz1 commented 4 years ago

Hi Richard, I'm not surprised the onSelected is not called when the user clears the input, for the reason that... There is no selection. Also I'm surprised there is no onChange method... I will have a deeper look at your issue, maybe I missed something in your explanation...

reckart commented 4 years ago

Reported the upstream part of the problem here: https://github.com/telerik/kendo-ui-core/issues/5476

sebfz1 commented 4 years ago

Hi Richard,

Sorry for having taking so long handling this issue.

I confirm what I wrote before. onSelect is not triggered for the reason there is no selection per se. When the user clears the input, it is a "change".

So, I bound the onChange event (not enabled by default), I guess the following code will correspond to what you need:

final AutoCompleteTextField<String> autocomplete = new AutoCompleteTextField<String>("autocomplete", Model.of("Heavy metal")) {

    private static final long serialVersionUID = 1L;

    @Override
    protected List<String> getChoices(String input)
    {
        return ListUtils.startsWith(input, CHOICES);
    }

    @Override
    public boolean isChangeEventEnabled()
    {
        return true;
    }

    @Override
    public void onChange(AjaxRequestTarget target, String value)
    {
        super.onChange(target, value);

        // resets the model to null when the user clears the selection //
        if (Strings.isEmpty(value))
        {
            this.onSelected(target, null); // will set the model to null and trigger onSelected(ART)
        }
    }

    @Override
    protected void onSelected(AjaxRequestTarget target)
    {
        super.onSelected(target);

        // use the model here 
    }
};
reckart commented 4 years ago

Thanks :) Well, I've been living with the workaround described above ever since. Need to see when I might get back to that part of the code. So many things to do, so little time...

sebfz1 commented 4 years ago

And for the double items issue: yes using a custom converter was the wise approach (I presume you wrapped the values in a simple pojo)...

sebfz1 commented 4 years ago

So many things to do, so little time...

I soooo know that! :)

reckart commented 4 years ago

My approach is basically around here (in case you'd like to have a quick look): https://github.com/webanno/webanno/blob/6d1960080ed8c21b7486bf23e9751c0d2fd28be9/webanno-api-annotation/src/main/java/de/tudarmstadt/ukp/clarin/webanno/api/annotation/feature/editor/KendoAutoCompleteTextFeatureEditor.java#L104