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

[BUG] - Autocomplete - Text selection with Shift + Home/End isn't working #7228

Open
c0d3rm0n opened this issue Oct 22, 2024 · 3 comments · May be fixed by #7403
Open

[BUG] - Autocomplete - Text selection with Shift + Home/End isn't working #7228

c0d3rm0n opened this issue Oct 22, 2024 · 3 comments · May be fixed by #7403

Comments

@c0d3rm0n
Copy link

Provide a general summary of the issue here

I'm using Autocomplete from NextUI. They created they UI based in yours.
I created an issue there to solve one problem with text selection in that component.
nextui-org/nextui#3854
They requested me to create a new issue upstream.

Text selection using Shift + Home/End doesn't work in Autocomplete/Combobox input (Text Input).
For example, I can make Shift + Left/Right Arrow and it works just fine. But the other key combination no...
It happens in Firefox and Chrome browsers, in my project or even in your website...
Is it possible to make it work?

🤔 Expected Behavior?

1 - Click in autocomplete
2 - Type something...
3 - Click or move the cursor to the middle of the text, for example.
4 - Make Shift + Home/End
5 - Part of the text should be selected

😯 Current Behavior

1 - Click in autocomplete
2 - Type something...
3 - Click or move the cursor to the middle of the text, for example.
4 - Make Shift + Home/End
5 - It does nothing at all...

💁 Possible Solution

No response

🔦 Context

No response

🖥️ Steps to Reproduce

1 - Click in autocomplete
2 - Type something...
3 - Click or move the cursor to the middle of the text, for example.
4 - Make Shift + Home/End

Version

0

What browsers are you seeing the problem on?

Firefox, Chrome

If other, please specify.

No response

What operating system are you using?

Linux x64

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

@snowystinger
Copy link
Member

So the pattern says

Home (Optional): Either moves focus to and selects the first option or, if the combobox is editable, returns focus to the combobox and places the cursor on the first character.
End (Optional): Either moves focus to the last option or, if the combobox is editable, returns focus to the combobox and places the cursor after the last character.

https://www.w3.org/WAI/ARIA/apg/patterns/combobox/

Will need to consult with Accessibility. Historically for Combobox, Home/End has only controlled the Selection in the popup.

@majornista ^

@c0d3rm0n
Copy link
Author

Are there any news related to this subject?

@majornista
Copy link
Collaborator

majornista commented Nov 19, 2024

@snowystinger and @c0d3rm0n

Shift+Home/End will work on the input when the Listbox for the Autocomplete is closed. You can test this behavior in the RAC ComboBox example. If one were to type in a value that returns no results, like "Flamingo" for example, you can use ArrowLeft to move the cursor to the middle of the word, and then Shift+Home to select the first half of the word. However, with the ListBox open, Home and End move focus in the ListBox. The usage example for the NextUI implementation opens the ListBox to display "No results found." when there are no matches, so Home/End is always interpreted as being intended for the ListBox.

Perhaps we should interpret the Shift key modifier differently for Home/End, when the ListBox is open, or when there are less than 2 results. This however could have implications for any future multiselect or TagGroup implementation.

When the Autocomplete is open, the keyboard events are first handled in useSelectableCollection.ts#L223-L250. Also see useComboBox.ts#L209, where keydown event handlers are chained depending on the open state.

A possible solution might be to add the following before preventing default at useSelectableCollection.ts#L223-L250:

          if (manager.focusedKey === null && e.shiftKey) {
            return;
          }

like this:

      case 'Home':
        if (delegate.getFirstKey) {
          if (manager.focusedKey === null && e.shiftKey) {
            return;
          }
          e.preventDefault();
          let firstKey = delegate.getFirstKey(manager.focusedKey, isCtrlKeyPressed(e));
          manager.setFocusedKey(firstKey);
          if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {
            manager.extendSelection(firstKey);
          } else if (selectOnFocus) {
            manager.replaceSelection(firstKey);
          }
        }
        break;
      case 'End':
        if (delegate.getLastKey) {
          if (manager.focusedKey === null && e.shiftKey) {
            return;
          }
          e.preventDefault();
          let lastKey = delegate.getLastKey(manager.focusedKey, isCtrlKeyPressed(e));
          manager.setFocusedKey(lastKey);
          if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {
            manager.extendSelection(lastKey);
          } else if (selectOnFocus) {
            manager.replaceSelection(lastKey);
          }
        }
        break;

majornista added a commit that referenced this issue Nov 19, 2024
majornista added a commit that referenced this issue Nov 19, 2024
majornista added a commit that referenced this issue Nov 20, 2024
majornista added a commit that referenced this issue Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants