Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete: forceselection="false" no longer working since primefaces release 10.0.1 #8006

Closed
HessiJames2 opened this issue Oct 22, 2021 · 3 comments · Fixed by #8009
Closed
Assignees
Labels
10.0.12 🐞 defect Bug...Something isn't working workaround A workaround has been provided
Milestone

Comments

@HessiJames2
Copy link

Describe the defect
The autocomplete component with forceselection="false" doesn't add a new value (which is not on the suggestion list) after enter key is fired.
This worked with 10.0.0 but stopped working since 10.0.1.
Looks like this was the issue introduced in 10.0.1: #7211

Reproducer
I used the example code from the showcase:
https://www.primefaces.org/showcase/ui/input/autoComplete.xhtml

Only change I did was setting forceSelection="false".

Environment:

  • PF Version: 10.0.7

To Reproduce
Starting with release 10.0.1 we see a changed behaviour. When we enter a new value (which is not on the sugestion list) and hit 'enter' key -> nothing happens
image

Expected behavior
With release 10.0.0 it worked fine, we could add values from suggestion list and also new values could be entered (like 'aaa', 'bbb', ...):
image

This defect is related to https://forum.primefaces.org/viewtopic.php?f=3&t=69186

@HessiJames2 HessiJames2 added the 🐞 defect Bug...Something isn't working label Oct 22, 2021
@melloware melloware self-assigned this Oct 22, 2021
@melloware melloware added this to the 11.0.0-RC2 milestone Oct 22, 2021
@melloware
Copy link
Member

MonkeyPatch:

if (PrimeFaces.widget.AutoComplete) {
    PrimeFaces.widget.AutoComplete.prototype.bindKeyEvents = function() {
        var $this = this;

        // GitHub #6711 use DOM if non-CSP and JQ event if CSP
        var originalOnchange = this.input.prop('onchange');
        if (!originalOnchange && this.input[0]) {
            var events = $._data(this.input[0], "events");
            if (events.change) {
                originalOnchange = events.change[0].handler;
            }
        }
        this.cfg.onChange = originalOnchange;
        if (originalOnchange) {
            this.input.prop('onchange', null).off('change');
        }

        if (this.cfg.queryEvent !== 'enter') {
            this.input.on('input propertychange', function(e) {
                $this.processKeyEvent(e);
            });
        }

        this.input.on('keyup.autoComplete', function(e) {
            var keyCode = $.ui.keyCode,
                key = e.which;

            if (PrimeFaces.env.isIE(9) && (key === keyCode.BACKSPACE || key === keyCode.DELETE)) {
                $this.processKeyEvent(e);
            }

            if ($this.cfg.queryEvent === 'enter' && (key === keyCode.ENTER)) {
                if ($this.itemSelectedWithEnter)
                    $this.itemSelectedWithEnter = false;
                else
                    $this.search($this.input.val());
            }

            if ($this.panel.is(':visible')) {
                if (key === keyCode.ESCAPE) {
                    $this.hide();
                } else if (key === keyCode.UP || key === keyCode.DOWN) {
                    var highlightedItem = $this.items.filter('.ui-state-highlight');
                    if (highlightedItem.length) {
                        $this.displayAriaStatus(highlightedItem.data('item-label'));
                    }
                }
            }

            $this.checkMatchedItem = true;
            $this.isTabPressed = false;

        }).on('keydown.autoComplete', function(e) {
            var keyCode = $.ui.keyCode;

            $this.suppressInput = false;
            if ($this.panel.is(':visible')) {
                var highlightedItem = $this.items.filter('.ui-state-highlight');

                switch (e.which) {
                    case keyCode.UP:
                        var prev = highlightedItem.length == 0 ? $this.items.eq(0) : highlightedItem.prevAll('.ui-autocomplete-item:first');

                        if (prev.length == 1) {
                            highlightedItem.removeClass('ui-state-highlight');
                            prev.addClass('ui-state-highlight');

                            if ($this.cfg.scrollHeight) {
                                PrimeFaces.scrollInView($this.panel, prev);
                            }

                            if ($this.cfg.itemtip) {
                                $this.showItemtip(prev);
                            }
                        }

                        e.preventDefault();
                        break;

                    case keyCode.DOWN:
                        var next = highlightedItem.length == 0 ? $this.items.eq(0) : highlightedItem.nextAll('.ui-autocomplete-item:first');

                        if (next.length == 1) {
                            highlightedItem.removeClass('ui-state-highlight');
                            next.addClass('ui-state-highlight');

                            if ($this.cfg.scrollHeight) {
                                PrimeFaces.scrollInView($this.panel, next);
                            }

                            if ($this.cfg.itemtip) {
                                $this.showItemtip(next);
                            }
                        }

                        e.preventDefault();
                        break;

                    case keyCode.ENTER:
                        if ($this.timeout) {
                            $this.deleteTimeout();
                        }

                        if (highlightedItem.length > 0) {
                            $this.preventInputChangeEvent = true;
                            highlightedItem.trigger("click");
                            $this.itemSelectedWithEnter = true;
                        }

                        e.preventDefault();
                        e.stopPropagation();

                        break;

                    case 18: //keyCode.ALT:
                    case 224:
                        break;

                    case keyCode.TAB:
                        if (highlightedItem.length && $this.cfg.autoSelection) {
                            highlightedItem.trigger('click');
                        } else {
                            $this.hide();
                            if ($this.timeout) {
                                $this.deleteTimeout();
                            }
                        }
                        $this.isTabPressed = true;
                        break;
                }
            } else {
                switch (e.which) {
                    case keyCode.TAB:
                        if ($this.timeout) {
                            $this.deleteTimeout();
                        }
                        $this.isTabPressed = true;
                        break;

                    case keyCode.ENTER:
                        var itemValue = $(this).val();
                        var valid = true;
                        if ($this.cfg.queryEvent === 'enter' || ($this.timeout > 0) || $this.querying) {
                            e.preventDefault();
                        }

                        if ($this.cfg.queryEvent !== 'enter') {
                            valid = $this.isValid(itemValue, true);
                            if (!$this.cfg.forceSelection) {
                                valid = true;
                            }
                        }

                        if ($this.cfg.multiple && itemValue && valid) {
                            $this.addItem(itemValue);
                            e.preventDefault();
                            e.stopPropagation();
                        }
                        break;

                    case keyCode.BACKSPACE:
                        if ($this.cfg.multiple && !$this.input.val().length) {

                            if (e.metaKey || e.ctrlKey || e.shiftKey) {
                                $this.removeAllItems();
                            } else {
                                $this.removeItem($(this).parent().prev());
                            }

                            e.preventDefault();
                        }
                        break;
                };
            }

        }).on('paste.autoComplete', function() {
            $this.suppressInput = false;
            $this.checkMatchedItem = true;
        }).on('change.autoComplete', function(e) {
            var value = e.currentTarget.value,
                valid = $this.isValid(value);

            if ($this.cfg.forceSelection && $this.currentInputValue === '' && !valid) {
                $this.preventInputChangeEvent = true;
            }

            if ($this.cfg.onChange && !$this.preventInputChangeEvent) {
                $this.cfg.onChange.call(this);
            }

            $this.currentInputValue = $this.cfg.forceSelection && !valid ? '' : value;
            $this.preventInputChangeEvent = false;
        });
    }
}

@melloware melloware added workaround A workaround has been provided LTS-PORT The fix can be ported to LTS versions labels Oct 22, 2021
melloware added a commit to melloware/primefaces that referenced this issue Oct 22, 2021
@HessiJames2
Copy link
Author

Thank you so much for fixing this so quickly!
Is it possible to add this fix to PF 10.0.8?

@melloware
Copy link
Member

You can use the MonkeyPatch for now but i marked it with "elite" label so maybe PrimeTek will pick it up and put it in 10.0.8.

@mertsincan mertsincan added 10.0.12 and removed LTS-PORT The fix can be ported to LTS versions labels Apr 29, 2022
@melloware melloware modified the milestones: 11.0.0-RC2, 11.0.0 Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
10.0.12 🐞 defect Bug...Something isn't working workaround A workaround has been provided
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants