Skip to content

Commit a2a810a

Browse files
authored
refactor: remove no longer needed internal-tab event workaround (#7870)
1 parent fb8144f commit a2a810a

4 files changed

Lines changed: 127 additions & 107 deletions

File tree

‎packages/grid-pro/src/vaadin-grid-pro-edit-column-mixin.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ export const GridProEditColumnMixin = (superClass) =>
281281
const editor = this._getEditorComponent(cell);
282282
editor.addEventListener('focusout', this._grid.__boundEditorFocusOut);
283283
editor.addEventListener('focusin', this._grid.__boundEditorFocusIn);
284-
editor.addEventListener('internal-tab', this._grid.__boundCancelCellSwitch);
285284
this._setEditorOptions(editor);
286285
this._setEditorValue(editor, get(this.path, model.item));
287286
editor._grid = this._grid;

‎packages/grid-pro/src/vaadin-grid-pro-inline-editing-mixin.js‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export const InlineEditingMixin = (superClass) =>
6565
this.__boundItemPropertyChanged = this._onItemPropertyChanged.bind(this);
6666
this.__boundEditorFocusOut = this._onEditorFocusOut.bind(this);
6767
this.__boundEditorFocusIn = this._onEditorFocusIn.bind(this);
68-
this.__boundCancelCellSwitch = this._setCancelCellSwitch.bind(this);
6968

7069
this._addEditColumnListener('mousedown', (e) => {
7170
// Prevent grid from resetting navigating state
@@ -305,8 +304,7 @@ export const InlineEditingMixin = (superClass) =>
305304

306305
/** @private */
307306
_onEditorFocusOut(event) {
308-
// Ignore focusout from internal tab event
309-
if (this.__cancelCellSwitch || this.__shouldIgnoreFocusOut(event)) {
307+
if (this.__shouldIgnoreFocusOut(event)) {
310308
return;
311309
}
312310

@@ -416,20 +414,12 @@ export const InlineEditingMixin = (superClass) =>
416414
}
417415
}
418416

419-
/** @private */
420-
_setCancelCellSwitch() {
421-
this.__cancelCellSwitch = true;
422-
window.requestAnimationFrame(() => {
423-
this.__cancelCellSwitch = false;
424-
});
425-
}
426-
427417
/**
428418
* @param {!KeyboardEvent} e
429419
* @protected
430420
*/
431421
async _switchEditCell(e) {
432-
if (this.__cancelCellSwitch || (e.defaultPrevented && e.keyCode === 9)) {
422+
if (e.defaultPrevented && e.keyCode === 9) {
433423
return;
434424
}
435425

‎packages/grid-pro/test/keyboard-navigation.common.js‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { fixtureSync } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import { createItems, dblclick, dragAndDropOver, flushGrid, getCellEditor, getContainerCell } from './helpers.js';
@@ -167,32 +167,6 @@ describe('keyboard navigation', () => {
167167
expect(getCellEditor(firstCell)).to.be.ok;
168168
});
169169

170-
it('should not focus cell next available for editing on Tab if `internal-tab` was fired right before it', async () => {
171-
const firstCell = getContainerCell(grid.$.items, 1, 0);
172-
dblclick(firstCell._content);
173-
174-
const editor = getCellEditor(firstCell);
175-
editor.addEventListener('keydown', (e) => {
176-
if (e.key === 'Tab') {
177-
editor.dispatchEvent(new CustomEvent('internal-tab'));
178-
}
179-
});
180-
await sendKeys({ press: 'Tab' });
181-
expect(getCellEditor(firstCell)).to.be.ok;
182-
});
183-
184-
it('should be possible to switch edit cell on Tab with delay after `internal-tab` was fired', async () => {
185-
const firstCell = getContainerCell(grid.$.items, 1, 0);
186-
dblclick(firstCell._content);
187-
188-
getCellEditor(firstCell).dispatchEvent(new CustomEvent('internal-tab'));
189-
const secondCell = getContainerCell(grid.$.items, 1, 1);
190-
await nextFrame();
191-
192-
await sendKeys({ press: 'Tab' });
193-
expect(getCellEditor(secondCell)).to.be.ok;
194-
});
195-
196170
it('should focus previous cell available for editing within a same row in edit mode on Shift Tab', async () => {
197171
const firstCell = getContainerCell(grid.$.items, 1, 1);
198172
dblclick(firstCell._content);

‎test/integration/grid-pro-custom-editor.test.js‎

Lines changed: 124 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,107 @@ import { expect } from '@vaadin/chai-plugins';
22
import { fixtureSync, middleOfNode, nextRender } from '@vaadin/testing-helpers';
33
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
44
import '@vaadin/combo-box';
5+
import '@vaadin/custom-field';
56
import '@vaadin/date-picker';
67
import '@vaadin/date-time-picker';
78
import '@vaadin/grid-pro';
89
import '@vaadin/grid-pro/vaadin-grid-pro-edit-column.js';
10+
import '@vaadin/text-field';
911
import '@vaadin/time-picker';
1012
import { waitForOverlayRender } from '@vaadin/date-picker/test/helpers.js';
1113
import { flushGrid, getContainerCell } from '@vaadin/grid-pro/test/helpers.js';
1214

1315
describe('grid-pro custom editor', () => {
14-
let grid;
16+
let grid, cell;
1517

16-
beforeEach(async () => {
18+
function createGrid(path) {
1719
grid = fixtureSync(`
1820
<vaadin-grid-pro>
19-
<vaadin-grid-pro-edit-column path="date" editor-type="custom" width="50px"></vaadin-grid-pro-edit-column>
20-
<vaadin-grid-pro-edit-column path="status" editor-type="custom" width="50px"></vaadin-grid-pro-edit-column>
21-
<vaadin-grid-pro-edit-column path="time" editor-type="custom" width="50px"></vaadin-grid-pro-edit-column>
22-
<vaadin-grid-pro-edit-column path="datetime" editor-type="custom"></vaadin-grid-pro-edit-column>
21+
<vaadin-grid-pro-edit-column path="${path}" editor-type="custom"></vaadin-grid-pro-edit-column>
2322
</vaadin-gri-pro>
2423
`);
2524

25+
const column = grid.querySelector(`[path="${path}"]`);
26+
switch (path) {
27+
case 'date':
28+
column.editModeRenderer = (root, _, model) => {
29+
root.innerHTML = `
30+
<vaadin-date-picker value="${model.item.date}" auto-open-disabled></vaadin-date-picker>
31+
`;
32+
};
33+
break;
34+
case 'time':
35+
column.editModeRenderer = (root, _, model) => {
36+
root.innerHTML = `
37+
<vaadin-time-picker value="${model.item.time}" auto-open-disabled></vaadin-time-picker>
38+
`;
39+
};
40+
break;
41+
case 'status':
42+
column.editModeRenderer = (root, _, model) => {
43+
root.innerHTML = `
44+
<vaadin-combo-box
45+
value="${model.item.status}"
46+
items='["active", "suspended"]'
47+
auto-open-disabled
48+
></vaadin-combo-box>
49+
`;
50+
};
51+
break;
52+
case 'datetime':
53+
column.editModeRenderer = (root, _, model) => {
54+
root.innerHTML = `
55+
<vaadin-date-time-picker value="${model.item.datetime}" auto-open-disabled>
56+
</vaadin-date-time-picker>
57+
`;
58+
};
59+
break;
60+
case 'expires':
61+
column.editModeRenderer = (root, _, model) => {
62+
if (!root.firstChild) {
63+
// NOTE: using `innerHTML` doesn't work due to the timing issue in custom-field
64+
// See https://github.com/vaadin/web-components/issues/7871
65+
const field = document.createElement('vaadin-custom-field');
66+
field.appendChild(document.createElement('vaadin-text-field'));
67+
field.appendChild(document.createElement('vaadin-text-field'));
68+
root.appendChild(field);
69+
}
70+
root.firstChild.value = model.item.expires;
71+
};
72+
break;
73+
default:
74+
// Do nothing
75+
}
76+
2677
grid.items = [
27-
{ date: '1984-01-13', status: 'suspended', time: '09:00' },
28-
{ date: '1977-07-12', status: 'active', time: '09:30' },
29-
{ date: '1976-12-14', status: 'suspended', time: '10:00' },
30-
{ date: '1972-12-04', status: 'active', time: '10:00' },
31-
{ date: '1978-02-03', status: 'active', time: '10:00' },
78+
{ date: '1984-01-13', status: 'suspended', time: '09:00', expires: '01\t25' },
79+
{ date: '1977-07-12', status: 'active', time: '09:30', expires: '02\t26' },
80+
{ date: '1976-12-14', status: 'suspended', time: '10:00', expires: '03\t27' },
81+
{ date: '1972-12-04', status: 'active', time: '10:00', expires: '04\t28' },
82+
{ date: '1978-02-03', status: 'active', time: '10:00', expires: '05\t29' },
3283
].map((item) => {
3384
return { ...item, datetime: `${item.date}T${item.time}` };
3485
});
3586

36-
grid.querySelector('[path="date"]').editModeRenderer = (root, _, model) => {
37-
root.innerHTML = `
38-
<vaadin-date-picker value="${model.item.date}" auto-open-disabled>
39-
</vaadin-date-picker>
40-
`;
41-
};
42-
43-
grid.querySelector('[path="status"]').editModeRenderer = (root, _, model) => {
44-
if (!root.firstChild) {
45-
const comboBox = document.createElement('vaadin-combo-box');
46-
comboBox.autoOpenDisabled = true;
47-
comboBox.items = ['active', 'suspended'];
48-
root.appendChild(comboBox);
49-
}
50-
root.firstChild.value = model.item.status;
51-
};
52-
53-
grid.querySelector('[path="time"]').editModeRenderer = (root, _, model) => {
54-
root.innerHTML = `
55-
<vaadin-time-picker value="${model.item.time}" auto-open-disabled></vaadin-time-picker>
56-
`;
57-
};
58-
59-
grid.querySelector('[path="datetime"]').editModeRenderer = (root, _, model) => {
60-
root.innerHTML = `
61-
<vaadin-date-time-picker value="${model.item.datetime}" auto-open-disabled>
62-
</vaadin-date-time-picker>
63-
`;
64-
};
65-
6687
flushGrid(grid);
67-
await nextRender();
68-
});
88+
return grid;
89+
}
90+
91+
async function editFirstCell() {
92+
cell = getContainerCell(grid.$.items, 0, 0);
93+
cell.focus();
94+
await sendKeys({ press: 'Enter' });
95+
}
6996

7097
afterEach(async () => {
7198
await resetMouse();
7299
});
73100

74101
describe('date-picker', () => {
75-
let cell;
76-
77102
beforeEach(async () => {
78-
cell = getContainerCell(grid.$.items, 0, 0);
79-
cell.focus();
80-
81-
await sendKeys({ press: 'Enter' });
103+
grid = createGrid('date');
104+
await nextRender();
105+
await editFirstCell();
82106
});
83107

84108
it('should apply the updated date to the cell when exiting on Tab', async () => {
@@ -145,13 +169,10 @@ describe('grid-pro custom editor', () => {
145169
});
146170

147171
describe('combo-box', () => {
148-
let cell;
149-
150172
beforeEach(async () => {
151-
cell = getContainerCell(grid.$.items, 0, 1);
152-
cell.focus();
153-
154-
await sendKeys({ press: 'Enter' });
173+
grid = createGrid('status');
174+
await nextRender();
175+
await editFirstCell();
155176
});
156177

157178
it('should apply the updated value to the cell when exiting on Tab', async () => {
@@ -181,13 +202,10 @@ describe('grid-pro custom editor', () => {
181202
});
182203

183204
describe('time-picker', () => {
184-
let cell;
185-
186205
beforeEach(async () => {
187-
cell = getContainerCell(grid.$.items, 0, 2);
188-
cell.focus();
189-
190-
await sendKeys({ press: 'Enter' });
206+
grid = createGrid('time');
207+
await nextRender();
208+
await editFirstCell();
191209
});
192210

193211
it('should apply the updated time to the cell when exiting on Tab', async () => {
@@ -217,13 +235,10 @@ describe('grid-pro custom editor', () => {
217235
});
218236

219237
describe('date-time-picker', () => {
220-
let cell;
221-
222238
beforeEach(async () => {
223-
cell = getContainerCell(grid.$.items, 0, 3);
224-
cell.focus();
225-
226-
await sendKeys({ press: 'Enter' });
239+
grid = createGrid('datetime');
240+
await nextRender();
241+
await editFirstCell();
227242
});
228243

229244
it('should not stop editing when switching between pickers using Tab', async () => {
@@ -280,4 +295,46 @@ describe('grid-pro custom editor', () => {
280295
expect(editor.value).to.equal('1984-01-13T00:00');
281296
});
282297
});
298+
299+
describe('custom-field', () => {
300+
beforeEach(async () => {
301+
grid = createGrid('expires');
302+
await nextRender();
303+
await editFirstCell();
304+
});
305+
306+
it('should not stop editing when switching between fields using mouse', async () => {
307+
// Move focus to the second field
308+
const { x, y } = middleOfNode(cell._content.querySelectorAll('input')[1]);
309+
await sendMouse({ type: 'click', position: [Math.floor(x), Math.floor(y)] });
310+
await nextRender();
311+
expect(cell._content.querySelector('vaadin-custom-field')).to.be.ok;
312+
});
313+
314+
it('should not stop editing when switching between fields using Tab', async () => {
315+
// Move focus to the second field
316+
await sendKeys({ press: 'Tab' });
317+
await nextRender();
318+
expect(cell._content.querySelector('vaadin-custom-field')).to.be.ok;
319+
320+
// Move focus to the first field
321+
await sendKeys({ down: 'Shift' });
322+
await sendKeys({ press: 'Tab' });
323+
await sendKeys({ up: 'Shift' });
324+
await nextRender();
325+
expect(cell._content.querySelector('vaadin-custom-field')).to.be.ok;
326+
});
327+
328+
it('should stop editing when moving focus outside the field using Tab', async () => {
329+
// Move focus to the second field
330+
await sendKeys({ press: 'Tab' });
331+
await nextRender();
332+
expect(cell._content.querySelector('vaadin-custom-field')).to.be.ok;
333+
334+
// Move focus outside of the field
335+
await sendKeys({ press: 'Tab' });
336+
await nextRender();
337+
expect(cell._content.querySelector('vaadin-custom-field')).to.be.not.ok;
338+
});
339+
});
283340
});

0 commit comments

Comments
 (0)