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

SelectOneMenu: Incorrect selection using Enter key and Labelname contains "Enter" #11809

Closed
marioinfang opened this issue Apr 25, 2024 · 3 comments · Fixed by #11811
Closed
Assignees
Labels
🐞 defect Bug...Something isn't working workaround A workaround has been provided
Milestone

Comments

@marioinfang
Copy link

Describe the bug

If a SelectOneMenu item contains "Enter" at the beginning of a label, selecting with the Enter key does not work correctly.
The UI shows always the first Item with "Enter" in the label.
However, the correctly selected item is sent to the backend.

Reproducer

PrimefacesTest.zip

  1. Open SelectOneMenu
  2. Go down the menu using the arrow keys
  3. Confirm your choice using the enter key
  4. See behaviour

Expected behavior

If I select an item with the Enter key, the selected item should be displayed.

PrimeFaces edition

Community

PrimeFaces version

13.0.7 / 13.0.8

Theme

No response

JSF implementation

MyFaces

JSF version

3

Java version

JDK 11

Browser(s)

Chrome Version 124.0.6367.79 (Offizieller Build) (64-Bit)

@marioinfang marioinfang added ‼️ needs-triage Issue needs triaging 🐞 defect Bug...Something isn't working labels Apr 25, 2024
@melloware melloware removed the ‼️ needs-triage Issue needs triaging label Apr 25, 2024
@melloware melloware self-assigned this Apr 25, 2024
@melloware melloware added this to the 13.0.9 milestone Apr 25, 2024
@melloware
Copy link
Member

Fixed with monkeypatch in test.js
pf-11809.zip

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

        this.keyboardTarget.on('keydown.ui-selectonemenu', function(e) {
                switch (e.key) {
                    case 'ArrowUp':
                    case 'ArrowLeft':
                        $this.callHandleMethod($this.highlightPrev, e);
                        break;

                    case 'ArrowDown':
                    case 'ArrowRight':
                        $this.callHandleMethod($this.highlightNext, e);
                        break;

                    case 'Enter':
                        $this.handleEnterKey(e);
                        break;

                    case 'Tab':
                        $this.handleTabKey(e);
                        break;

                    case 'Escape':
                        $this.handleEscapeKey(e);
                        break;

                    case ' ':
                        $this.handleSpaceKey(e);
                        break;
                }
            })
            .on('keyup.ui-selectonemenu', function(e) {
                if (PrimeFaces.utils.ignoreFilterKey(e) || !PrimeFaces.utils.isPrintableKey(e)) {
                    return;
                }

                var matchedOptions = null,
                    metaKey = e.metaKey || e.ctrlKey || e.altKey;

                if (!metaKey) {
                    clearTimeout($this.searchTimer);

                    // #4682: check for word match
                    var text = $(this).val();
                    if (!$this.focusInput) {
                        $this.searchValue += e.key;
                        text = $this.searchValue;
                    }

                    matchedOptions = $this.matchOptions(text);
                    if (matchedOptions.length) {
                        var matchIndex = matchedOptions[0].index;
                        if ($this.panel.is(':hidden')) {
                            $this.callHandleMethod(function() {
                                var highlightItem = $this.items.eq(matchIndex);
                                $this.selectItem(highlightItem);
                            }, e);
                        } else {
                            var highlightItem = $this.items.eq(matchIndex);
                            $this.highlightItem(highlightItem);
                            PrimeFaces.scrollInView($this.itemsWrapper, highlightItem);
                        }
                    } else {
                        // #4682: check for first letter match
                        text = e.key.toLowerCase();
                        // find all options with the same first letter
                        matchedOptions = $this.matchOptions(text);
                        if (matchedOptions.length) {
                            $this.callHandleMethod(function() {
                                var selectedIndex = -1;

                                // is current selection one of our matches?
                                matchedOptions.each(function() {
                                    var option = $(this);
                                    var currentIndex = option[0].index;
                                    var currentItem = $this.items.eq(currentIndex);
                                    if (currentItem.hasClass('ui-state-highlight')) {
                                        selectedIndex = currentIndex;
                                        return false;
                                    }
                                });

                                matchedOptions.each(function() {
                                    var option = $(this);
                                    var currentIndex = option[0].index;
                                    var currentItem = $this.items.eq(currentIndex);

                                    // select next item after the current selection
                                    if (currentIndex > selectedIndex) {
                                        if ($this.panel.is(':hidden')) {
                                            $this.selectItem(currentItem);
                                        } else {
                                            $this.highlightItem(currentItem);
                                            PrimeFaces.scrollInView($this.itemsWrapper, currentItem);
                                        }
                                        return false;
                                    }
                                });
                            }, e);
                        }
                    }

                    $this.searchTimer = setTimeout(function() {
                        $this.searchValue = '';
                        $this.focusInput ? $this.focusInput.val('') : null;
                    }, 1000);
                }
            });
    }
};

@marioinfang
Copy link
Author

Thanks! The monkeypatch fixes the problem.

@melloware
Copy link
Member

Thanks for testing. Fix will be in 13.0.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 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.

2 participants