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

fix: focus editor only after it is ready #7235

Merged
merged 20 commits into from
Mar 28, 2024

Conversation

ugur-vaadin
Copy link
Contributor

@ugur-vaadin ugur-vaadin commented Mar 18, 2024

Description

The editor for Lit is initialized asynchronously, unlike Polymer. Therefore, during the first focus call, the editor focus element is not ready yet.

This PR

  • separates the Lit and Polymer focus behaviour
  • focuses the Lit editor only when it is ready
  • focuses the Polymer editor immediately
  • removes the extra deferred focus call

Fixes #6117

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.

@@ -362,5 +364,15 @@ describe('edit column editor type', () => {
editor = column._getEditorComponent(cell);
expect(editor).to.be.not.ok;
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New test fails here with Firefox, works locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tried the fix manually for both Lit and Polymer, using all supported browsers on MacOS, Windows, and Android. The issue does not seem to be re reproducible in any of them.

@@ -113,7 +113,8 @@ describe('keyboard navigation', () => {
expect(getCellEditor(firstCell)).to.be.not.ok;
});

it('should focus correct editable cell after column reordering', () => {
// See https://github.com/vaadin/web-components/issues/7229
it.skip('should focus correct editable cell after column reordering', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fails without any change.

dblclick(cell._content);
await nextFrame();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Necessary because of the timing changes.

@@ -180,10 +180,11 @@ describe('edit column renderer', () => {
};

dblclick(cell._content);
expect(spy.calledOnce).to.be.true;
await nextFrame();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Necessary because of the timing changes. Also, the test fails with the existing code when this is added. Focus is always triggered twice.

@@ -194,7 +195,8 @@ describe('edit column renderer', () => {
};

dblclick(cell._content);
expect(spy.calledOnce).to.be.true;
await nextFrame();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above.

@ugur-vaadin ugur-vaadin changed the title fix: defer focus in order to have editor ready before fix: explicitly set initial char on edit start instead of relying on keydown timing Mar 22, 2024
@ugur-vaadin ugur-vaadin marked this pull request as ready for review March 22, 2024 08:37
@vursen
Copy link
Contributor

vursen commented Mar 26, 2024

When running tests locally, I get the following failures:

packages/grid-pro/test/keyboard-navigation-polymer.test.js:

 ❌ keyboard navigation > when `singleCellEdit` is true > should focus correct editable cell after column reordering
      AssertionError: expected false to be true
      + expected - actual

      -false
      +true

      at n.<anonymous> (packages/grid-pro/test/keyboard-navigation.common.js:131:35)

packages/grid-pro/test/keyboard-navigation-lit.test.js:

 ❌ keyboard navigation > when `singleCellEdit` is true > should focus correct editable cell after column reordering
      AssertionError: expected false to be true
      + expected - actual

      -false
      +true

      at n.<anonymous> (packages/grid-pro/test/keyboard-navigation.common.js:131:35)

@ugur-vaadin
Copy link
Contributor Author

When running tests locally, I get the following failures:

packages/grid-pro/test/keyboard-navigation-polymer.test.js:

 ❌ keyboard navigation > when `singleCellEdit` is true > should focus correct editable cell after column reordering
      AssertionError: expected false to be true
      + expected - actual

      -false
      +true

      at n.<anonymous> (packages/grid-pro/test/keyboard-navigation.common.js:131:35)

packages/grid-pro/test/keyboard-navigation-lit.test.js:

 ❌ keyboard navigation > when `singleCellEdit` is true > should focus correct editable cell after column reordering
      AssertionError: expected false to be true
      + expected - actual

      -false
      +true

      at n.<anonymous> (packages/grid-pro/test/keyboard-navigation.common.js:131:35)

Those tests also fail without these changes. They are temporarily disabled in main, and will be restored after the reordering fix is merged.

@vursen
Copy link
Contributor

vursen commented Mar 26, 2024

I wonder if the PR should be marked with ! considering that it changes the timing of the focus event for the Polymer version – it's no longer synchronous.

@ugur-vaadin ugur-vaadin changed the title fix: explicitly set initial char on edit start instead of relying on keydown timing fix!: explicitly set initial char on edit start instead of relying on keydown timing Mar 26, 2024
const editInitiatorKey = this._editInitiatorKey;
this._editInitiatorKey = undefined;
requestAnimationFrame(() => {
if (editInitiatorKey) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Previously it was possible to paste text into a cell, for example with Command + V. When I try this now I only get a v in the cell editor.

Copy link
Contributor

@vursen vursen Mar 26, 2024

Choose a reason for hiding this comment

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

It's just an idea but we could consider redirecting user input to a temporary input element until the editor has finished rendering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This approach was abandoned. Currently, the Polymer editor is focused synchronously and the Lit editor is focused when it is ready.

@ugur-vaadin ugur-vaadin force-pushed the fix-defer-focus-to-have-editor-ready-before branch from 1280cc1 to 774cb53 Compare March 27, 2024 20:13
@ugur-vaadin ugur-vaadin changed the title fix!: explicitly set initial char on edit start instead of relying on keydown timing fix!: focus editor only after it is ready Mar 27, 2024
@ugur-vaadin ugur-vaadin changed the title fix!: focus editor only after it is ready fix: focus editor only after it is ready Mar 27, 2024
@ugur-vaadin ugur-vaadin changed the title fix: focus editor only after it is ready fix: focus lit editor only after it is ready Mar 27, 2024
@ugur-vaadin ugur-vaadin changed the title fix: focus lit editor only after it is ready fix: focus editor only after it is ready Mar 27, 2024
@ugur-vaadin
Copy link
Contributor Author

I wonder if the PR should be marked with ! considering that it changes the timing of the focus event for the Polymer version – it's no longer synchronous.

I removed the breaking change signs since the Polymer focus is still synchronous after the latest updates to the fix.

Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
Copy link

sonarcloud bot commented Mar 28, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@ugur-vaadin ugur-vaadin merged commit 212637f into main Mar 28, 2024
9 checks passed
@ugur-vaadin ugur-vaadin deleted the fix-defer-focus-to-have-editor-ready-before branch March 28, 2024 16:59
vaadin-bot pushed a commit that referenced this pull request Mar 28, 2024
* fix: defer focus in order to have editor ready before

* test: fix unit test for firefox

* test: await next render before assertion

* test: add another await next render after the first keydown

* Revert "test: add another await next render after the first keydown"

This reverts commit 3a62f57.

* Revert "test: await next render before assertion"

This reverts commit d5939d1.

* test: update material visual test reference image

* fix: set edit initiator char explicitly

* test: revert skip test

* fix: do not select input when initialized with character

* Update packages/grid-pro/test/edit-column-type.common.js

Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>

* refactor: extract input selection logic from editor focus

* refactor: change name of the new property

* refactor: focus lit editor once it is ready

* test: revert unnecessary awaits

* test: revery unnecessary test assertion changes

* Revert "test: update material visual test reference image"

This reverts commit f180f2b.

* test: skip new test on firefox

* test: revert test skip change

* Update packages/grid-pro/test/edit-column-type.common.js

Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>

---------

Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with Vaadin 24.4.0.alpha20 and is also targeting the upcoming stable 24.4.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.

GridPro: Edit on typing selects the first entered character
4 participants