Skip to content

fix: focus input element when overlay is opened - #5966

Merged
bwajtr merged 13 commits into
mainfrom
5860-multi-select-combo-box-clicking-toggle-button-doesnt-focus-field
Jun 16, 2023
Merged

bwajtr merged 13 commits into
mainfrom
5860-multi-select-combo-box-clicking-toggle-button-doesnt-focus-field

Conversation

@bwajtr

@bwajtr bwajtr commented Jun 12, 2023 •

Copy link
Copy Markdown
Contributor

Description

When a vaadin-combo-box toggle button is clicked with a mouse, then the overlay is opened and the internal input field receives focus. For the other "combobox-like" components only the overlay is opened, but the input field of the component does not get the focus. In fact the focus is not changed at all. See this comment for a list of affected components and a test page where it can be easily reproduced.

I've hunted down the problem to the vaadin-combo-box-mixin.js -> _openedChanged() method, where the focus is handled. There is a call to this.focus() which should change the focus to the internal input. However, this line does not work for the affected components: because it seems that this in that context does not refer to an element that would delegate the focus to the internal input (like for the basic vaadin-combo-box).

I was thinking about fixing this somehow using the DelegateFocusMixin, however, I believe that a simpler fix is the one that I came up with in this PR. I think that we can focus directly the this.inputElement in that part of the code, instead of relying on the delegation of the focus. The other parts of the code speak in favor of such solution: for example methods _onOverlayTouchAction() or __onClearButtonMouseDown() also work directly with a focus of this.inputElement.

The change caused a lot of changes in the tests, because the focus started to work correctly in some of the scenarios:

  • packages/combo-box/test/combo-box-light.test.js, packages/multi-select-combo-box/test/basic.test.js, packages/time-picker/test/time-picker.test.js
    • I added a new tests here to check that the internal input gets focus when a toggle button is clicked
  • packages/multi-select-combo-box/test/dom/__snapshots__/multi-select-combo-box.test.snap.js, packages/time-picker/test/dom/__snapshots__/time-picker.test.snap.js
    • Snapshot tests had to be updated, because now the main component has the focus when opened
  • packages/multi-select-combo-box/test/visual/common.js
    • This change reduces flakiness of the visual tests
  • packages/multi-select-combo-box/test/visual/lumo/multi-select-combo-box.test.js, packages/multi-select-combo-box/test/visual/material/multi-select-combo-box.test.js, packages/time-picker/test/visual/lumo/time-picker.test.js, packages/time-picker/test/visual/material/time-picker.test.js
    • I'm not sure if this is the best way how to do it, but something like that is definitely needed. There are some tests in those suites which use sendKeys() function, which sets the global value of focus-utils.js -> isKeyboardActive to true. This affects how a focus-ring is rendered in the other tests - it is rendered even in tests where we would not expect it and where focus-ring wouldn't be rendered if test is executed alone. So I'm using one mouse click in the browser to reset focus-utils.js -> isKeyboardActive back to false
  • Changes in visual tests screenshots
    • After the components handle focus correctly, some of the screenshots had to be updated to align with correct functionality

Fixes #5860

Type of change

  • Bugfix
  • Feature

Checklist

  • I have read the contribution guide: https://vaadin.com/docs/latest/contributing/overview
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

@bwajtr

bwajtr commented Jun 12, 2023

Copy link
Copy Markdown
Contributor Author

I see Visual, Snapshot and Webkit tests are failing -> will check on those tomorrow

@bwajtr
bwajtr marked this pull request as draft June 13, 2023 05:48
@bwajtr
bwajtr marked this pull request as ready for review June 15, 2023 05:48
@bwajtr bwajtr changed the title fix: focus input element when toggle button is clicked fix: focus input element when overlay is opened Jun 15, 2023

@web-padawan web-padawan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for such a detailed PR description and meaningful commits! 💯

Added a few small comments, otherwise the fix LGTM.

bwajtr and others added 2 commits June 15, 2023 14:50
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>

@DiegoCardoso DiegoCardoso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although I was trying to understand why the selected-focused-chip visual test lost the focus ring.

@bwajtr

bwajtr commented Jun 15, 2023

Copy link
Copy Markdown
Contributor Author

LGTM, although I was trying to understand why the selected-focused-chip visual test lost the focus ring.

When you run only the selected-focused-chip visual test now (without my changes), it will fail - because the baseline screenshot is really wrong. It's not failing in the pipeline only because there are other tests in that suite, which call sendKeys()
and which therefore set the global value of focus-utils.js -> isKeyboardActive to true. That causes focus-ring to be rendered also in other tests - where it's unwanted.

The change I made in packages/multi-select-combo-box/test/visual/lumo/multi-select-combo-box.test.js resets isKeyboardActive back to false after every test.

That sort of fixed how the selected-focused-chip output looks like - and now it's correctly rendered without the focus-ring even when executed as part of the whole suite.

@bwajtr
bwajtr requested a review from web-padawan June 16, 2023 10:41
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@bwajtr
bwajtr merged commit 99a497e into main Jun 16, 2023
@bwajtr
bwajtr deleted the 5860-multi-select-combo-box-clicking-toggle-button-doesnt-focus-field branch June 16, 2023 12:13
@vaadin-bot

Copy link
Copy Markdown
Collaborator

Hi @bwajtr , this commit cannot be picked to 24.0 by this bot, can you take a look and pick it manually?
Error Message: Error: Command failed: git cherry-pick 99a497e
error: could not apply 99a497e... fix: focus input element when overlay is opened (#5966)
hint: After resolving the conflicts, mark them with
hint: "git add/rm ", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".

@vaadin-bot

Copy link
Copy Markdown
Collaborator

Hi @bwajtr , this commit cannot be picked to 23.3 by this bot, can you take a look and pick it manually?
Error Message: Error: Command failed: git cherry-pick 99a497e
warning: Cannot merge binary files: packages/multi-select-combo-box/test/visual/lumo/screenshots/multi-select-combo-box/baseline/selected-focused-chip.png (HEAD vs. 99a497e (fix: focus input element when overlay is opened (#5966)))
error: could not apply 99a497e... fix: focus input element when overlay is opened (#5966)
hint: After resolving the conflicts, mark them with
hint: "git add/rm ", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".

@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 24.2.0.alpha1 and is also targeting the upcoming stable 24.2.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[multi-select-combo-box] Clicking toggle button doesn't focus field

5 participants