Skip to content

Commit 1a4aa49

Browse files
authored
fix: restore focus on crud editor close (#5898)
1 parent f4d9433 commit 1a4aa49

4 files changed

Lines changed: 319 additions & 1 deletion

File tree

‎packages/crud/src/vaadin-crud-grid.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '@vaadin/grid/src/vaadin-grid-column-group.js';
1313
import '@vaadin/grid/src/vaadin-grid-sorter.js';
1414
import '@vaadin/grid/src/vaadin-grid-filter.js';
1515
import './vaadin-crud-edit-column.js';
16+
import { getClosestElement } from '@vaadin/component-base/src/dom-utils.js';
1617
import { Grid } from '@vaadin/grid/src/vaadin-grid.js';
1718
import { capitalize, getProperty } from './vaadin-crud-helpers.js';
1819
import { IncludedMixin } from './vaadin-crud-include-mixin.js';
@@ -58,6 +59,30 @@ class CrudGrid extends IncludedMixin(Grid) {
5859
return ['__onItemsChange(items)', '__onHideEditColumnChange(hideEditColumn)'];
5960
}
6061

62+
/** @protected */
63+
_getRowContainingNode(node) {
64+
const content = getClosestElement('vaadin-grid-cell-content', node);
65+
if (!content) {
66+
return;
67+
}
68+
69+
const cell = content.assignedSlot.parentElement;
70+
return cell.parentElement;
71+
}
72+
73+
/** @protected */
74+
_isItemAssigedToRow(item, row) {
75+
const model = this.__getRowModel(row);
76+
return this.getItemId(item) === this.getItemId(model.item);
77+
}
78+
79+
/** @protected */
80+
_focusFirstVisibleRow() {
81+
const row = this.__getFirstVisibleItem();
82+
this.__rowFocusMode = true;
83+
row.focus();
84+
}
85+
6186
/** @private */
6287
__onItemsChange(items) {
6388
if ((!this.dataProvider || this.dataProvider === this._arrayDataProvider) && !this.include && items && items[0]) {

‎packages/crud/src/vaadin-crud.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import './vaadin-crud-form.js';
1717
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
1818
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
1919
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
20+
import { FocusRestorationController } from '@vaadin/a11y-base/src/focus-restoration-controller.js';
2021
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
2122
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
2223
import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
@@ -701,6 +702,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
701702
this._cancelButtonController = new ButtonSlotController(this, 'cancel', 'tertiary');
702703
this._deleteButtonController = new ButtonSlotController(this, 'delete', 'tertiary error');
703704

705+
this.__focusRestorationController = new FocusRestorationController();
706+
704707
this._observer = new FlattenedNodesObserver(this, (info) => {
705708
this.__onDomChange(info.addedNodes);
706709
});
@@ -754,6 +757,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
754757
this._fullscreen = matches;
755758
}),
756759
);
760+
761+
this.addController(this.__focusRestorationController);
757762
}
758763

759764
/**
@@ -1261,6 +1266,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
12611266

12621267
/** @private */
12631268
__openEditor(type, item) {
1269+
this.__focusRestorationController.saveFocus();
1270+
12641271
this.__isDirty = false;
12651272
this.__isNew = !item;
12661273
const result = this.__fireEvent(this.__isNew ? 'new' : 'edit', item);
@@ -1271,6 +1278,31 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
12711278
}
12721279
}
12731280

1281+
/** @private */
1282+
__restoreFocusOnDelete() {
1283+
if (this._grid._effectiveSize === 1) {
1284+
this._newButton.focus();
1285+
} else {
1286+
this._grid._focusFirstVisibleRow();
1287+
}
1288+
}
1289+
1290+
/** @private */
1291+
__restoreFocusOnSaveOrCancel() {
1292+
const focusNode = this.__focusRestorationController.focusNode;
1293+
const row = this._grid._getRowContainingNode(focusNode);
1294+
if (!row) {
1295+
this.__focusRestorationController.restoreFocus();
1296+
return;
1297+
}
1298+
1299+
if (this._grid._isItemAssigedToRow(this.editedItem, row) && this._grid._isInViewport(row)) {
1300+
this.__focusRestorationController.restoreFocus();
1301+
} else {
1302+
this._grid._focusFirstVisibleRow();
1303+
}
1304+
}
1305+
12741306
/** @private */
12751307
__save() {
12761308
if (!this.__validate()) {
@@ -1298,6 +1330,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
12981330
}
12991331
Object.assign(this.editedItem, item);
13001332
}
1333+
1334+
this.__restoreFocusOnSaveOrCancel();
13011335
this._grid.clearCache();
13021336
this.__closeEditor();
13031337
}
@@ -1316,6 +1350,7 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
13161350
__confirmCancel() {
13171351
const result = this.__fireEvent('cancel', this.editedItem);
13181352
if (result) {
1353+
this.__restoreFocusOnSaveOrCancel();
13191354
this.__closeEditor();
13201355
}
13211356
}
@@ -1332,6 +1367,8 @@ class Crud extends ControllerMixin(ElementMixin(ThemableMixin(PolymerElement)))
13321367
if (this.items && this.items.indexOf(this.editedItem) >= 0) {
13331368
this.items.splice(this.items.indexOf(this.editedItem), 1);
13341369
}
1370+
1371+
this.__restoreFocusOnDelete();
13351372
this._grid.clearCache();
13361373
this.__closeEditor();
13371374
}

‎packages/crud/test/a11y.test.js‎

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
3+
import { setViewport } from '@web/test-runner-commands';
4+
import { sendKeys } from '@web/test-runner-commands';
5+
import '../src/vaadin-crud.js';
6+
import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js';
7+
import { getVisibleRows } from './helpers.js';
8+
9+
describe('a11y', () => {
10+
let crud;
11+
12+
before(async () => {
13+
await setViewport({ width: 1024, height: 768 });
14+
});
15+
16+
describe('focus restoration', () => {
17+
let grid, form, overlay, newButton, saveButton, cancelButton, editButtons;
18+
19+
describe('create item', () => {
20+
beforeEach(async () => {
21+
crud = fixtureSync('<vaadin-crud></vaadin-crud>');
22+
crud.items = [{ title: 'Item 1' }];
23+
await nextRender();
24+
overlay = crud.$.dialog.$.overlay;
25+
form = crud.querySelector('vaadin-crud-form');
26+
newButton = crud.querySelector('[slot=new-button]');
27+
saveButton = crud.querySelector('[slot=save-button]');
28+
cancelButton = crud.querySelector('[slot=cancel-button]');
29+
editButtons = crud.querySelectorAll('vaadin-crud-edit');
30+
});
31+
32+
it('should move focus to the dialog on new dialog open', async () => {
33+
newButton.focus();
34+
newButton.click();
35+
await nextRender();
36+
expect(getDeepActiveElement()).to.equal(overlay.$.overlay);
37+
});
38+
39+
it('should restore focus to previous element on new dialog close', async () => {
40+
newButton.focus();
41+
newButton.click();
42+
await nextRender();
43+
await sendKeys({ press: 'Escape' });
44+
expect(getDeepActiveElement()).to.equal(newButton);
45+
});
46+
47+
it('should restore focus to previous element on save', async () => {
48+
newButton.focus();
49+
newButton.click();
50+
await nextRender();
51+
52+
form.querySelector('vaadin-text-field').focus();
53+
await sendKeys({ type: 'something' });
54+
55+
saveButton.focus();
56+
saveButton.click();
57+
expect(getDeepActiveElement()).to.equal(newButton);
58+
});
59+
60+
it('should restore focus to previous element on cancel', async () => {
61+
newButton.focus();
62+
newButton.click();
63+
await nextRender();
64+
65+
cancelButton.focus();
66+
cancelButton.click();
67+
expect(getDeepActiveElement()).to.equal(newButton);
68+
});
69+
});
70+
71+
describe('edit item', () => {
72+
beforeEach(async () => {
73+
crud = fixtureSync('<vaadin-crud editor-position="aside"></vaadin-crud>');
74+
crud.items = Array.from({ length: 50 }, (_, i) => {
75+
return { title: `Item ${i}` };
76+
});
77+
await nextRender();
78+
grid = crud.querySelector('vaadin-crud-grid');
79+
form = crud.querySelector('vaadin-crud-form');
80+
newButton = crud.querySelector('[slot=new-button]');
81+
saveButton = crud.querySelector('[slot=save-button]');
82+
cancelButton = crud.querySelector('[slot=cancel-button]');
83+
editButtons = crud.querySelectorAll('vaadin-crud-edit');
84+
});
85+
86+
it('should restore focus to previous element on save', async () => {
87+
editButtons[0].focus();
88+
editButtons[0].click();
89+
await nextRender();
90+
91+
// Edit the item
92+
form.querySelector('vaadin-text-field').focus();
93+
await sendKeys({ type: 'something' });
94+
95+
saveButton.focus();
96+
saveButton.click();
97+
expect(getDeepActiveElement()).to.equal(editButtons[0]);
98+
});
99+
100+
it('should restore focus to first visible row on save if previous element has been re-used for another item', async () => {
101+
editButtons[0].focus();
102+
editButtons[0].click();
103+
await nextRender();
104+
105+
// Scroll to the end to trigger the grid to re-use
106+
// the saved focused element (the edit button) for another item.
107+
grid.scrollToIndex(crud.items.length - 1);
108+
await nextRender();
109+
110+
// Edit the item
111+
form.querySelector('vaadin-text-field').focus();
112+
await sendKeys({ type: 'something' });
113+
114+
saveButton.focus();
115+
saveButton.click();
116+
117+
const firstVisibleRow = getVisibleRows(grid.$.items)[0];
118+
expect(getDeepActiveElement()).to.equal(firstVisibleRow);
119+
});
120+
121+
it('should restore focus to previous element on cancel', async () => {
122+
editButtons[0].focus();
123+
editButtons[0].click();
124+
await nextRender();
125+
126+
// Edit the item
127+
form.querySelector('vaadin-text-field').focus();
128+
await sendKeys({ type: 'something' });
129+
130+
cancelButton.focus();
131+
cancelButton.click();
132+
await nextRender();
133+
134+
const confirmButton = getDeepActiveElement();
135+
confirmButton.focus();
136+
confirmButton.click();
137+
expect(getDeepActiveElement()).to.equal(editButtons[0]);
138+
});
139+
140+
it('should restore focus to first visible row on cancel if previous element has been re-used for another item', async () => {
141+
editButtons[0].focus();
142+
editButtons[0].click();
143+
await nextRender();
144+
145+
// Scroll to the end to trigger the grid to re-use
146+
// the saved focused element (the edit button) for another item.
147+
grid.scrollToIndex(crud.items.length - 1);
148+
await nextRender();
149+
150+
cancelButton.focus();
151+
cancelButton.click();
152+
await nextRender();
153+
154+
const firstVisibleRow = getVisibleRows(grid.$.items)[0];
155+
expect(getDeepActiveElement()).to.equal(firstVisibleRow);
156+
});
157+
158+
it('should switch to row focus mode when restoring focus to first visible row', async () => {
159+
editButtons[0].focus();
160+
editButtons[0].click();
161+
await nextRender();
162+
163+
// Scroll to the end to trigger the grid to re-use
164+
// the saved focused element (the edit button) for another item.
165+
grid.scrollToIndex(crud.items.length - 1);
166+
await nextRender();
167+
168+
cancelButton.focus();
169+
cancelButton.click();
170+
await nextRender();
171+
172+
await sendKeys({ press: 'ArrowDown' });
173+
const secondVisibleRow = getVisibleRows(grid.$.items)[1];
174+
expect(getDeepActiveElement()).to.equal(secondVisibleRow);
175+
});
176+
});
177+
178+
describe('delete item', () => {
179+
let deleteButton;
180+
181+
beforeEach(() => {
182+
crud = fixtureSync('<vaadin-crud editor-position="aside"></vaadin-crud>');
183+
newButton = crud.querySelector('[slot=new-button]');
184+
deleteButton = crud.querySelector('[slot=delete-button]');
185+
});
186+
187+
describe('multiple rows', () => {
188+
beforeEach(async () => {
189+
crud.items = Array.from({ length: 50 }, (_, i) => {
190+
return { title: `Item ${i}` };
191+
});
192+
await nextRender();
193+
grid = crud.querySelector('vaadin-crud-grid');
194+
editButtons = [...crud.querySelectorAll('vaadin-crud-edit')];
195+
});
196+
197+
it('should restore focus to first visible row on delete', async () => {
198+
editButtons[8].focus();
199+
editButtons[8].click();
200+
await nextRender();
201+
202+
// Scroll a bit to get a couple of rows out of the grid's viewport.
203+
grid.scrollToIndex(2);
204+
await nextRender();
205+
206+
deleteButton.focus();
207+
deleteButton.click();
208+
await nextRender();
209+
210+
const confirmButton = getDeepActiveElement();
211+
confirmButton.focus();
212+
confirmButton.click();
213+
214+
const firstVisibleRow = getVisibleRows(grid.$.items)[0];
215+
expect(getDeepActiveElement()).to.equal(firstVisibleRow);
216+
});
217+
});
218+
219+
describe('single row', () => {
220+
beforeEach(async () => {
221+
crud.items = [{ title: 'Item 1' }];
222+
await nextRender();
223+
editButtons = [...crud.querySelectorAll('vaadin-crud-edit')];
224+
});
225+
226+
it('should restore focus to new button on delete', async () => {
227+
editButtons[0].focus();
228+
editButtons[0].click();
229+
await nextRender();
230+
231+
deleteButton.focus();
232+
deleteButton.click();
233+
await nextRender();
234+
235+
const confirmButton = getDeepActiveElement();
236+
confirmButton.focus();
237+
confirmButton.click();
238+
expect(getDeepActiveElement()).to.equal(newButton);
239+
});
240+
});
241+
});
242+
});
243+
});

0 commit comments

Comments
 (0)