Skip to content

Commit de8d83d

Browse files
fix: toggle checkbox on selection column cell Space key (#7178) (#7194)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent d45b7a3 commit de8d83d

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

packages/grid/src/vaadin-grid-mixin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ export const GridMixin = (superClass) =>
574574
});
575575
}
576576

577+
if (column && column._onCellKeyDown) {
578+
cell.addEventListener('keydown', column._onCellKeyDown.bind(column));
579+
}
580+
577581
const slot = document.createElement('slot');
578582
slot.setAttribute('name', slotName);
579583

@@ -705,7 +709,7 @@ export const GridMixin = (superClass) =>
705709
// Header & footer
706710
const tagName = section === 'header' ? 'th' : 'td';
707711
if (isColumnRow || column.localName === 'vaadin-grid-column-group') {
708-
cell = column[`_${section}Cell`] || this._createCell(tagName);
712+
cell = column[`_${section}Cell`] || this._createCell(tagName, column);
709713
cell._column = column;
710714
row.appendChild(cell);
711715
column[`_${section}Cell`] = cell;

packages/grid/src/vaadin-grid-selection-column-base-mixin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ export const GridSelectionColumnBaseMixin = (superClass) =>
233233
}
234234
}
235235

236+
/** @private */
237+
_onCellKeyDown(e) {
238+
const target = e.composedPath()[0];
239+
// Toggle on Space without having to enter interaction mode first
240+
if (e.keyCode === 32 && (target === this._headerCell || (this._cells.includes(target) && !this.autoSelect))) {
241+
const checkbox = target._content.firstElementChild;
242+
checkbox.checked = !checkbox.checked;
243+
}
244+
}
245+
236246
/** @private */
237247
__dragAutoScroller() {
238248
if (this.__dragStartIndex === undefined) {

packages/grid/test/selection.common.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
22
import { click, fixtureSync, listenOnce, mousedown, nextFrame } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
34
import sinon from 'sinon';
45
import {
56
fire,
@@ -252,6 +253,73 @@ describe('multi selection column', () => {
252253
expect(grid.selectedItems).to.eql([]);
253254
});
254255

256+
it('should add the item to selectedItems on selection column cell Space key', async () => {
257+
const cell = getRowCells(rows[1])[0];
258+
cell.focus();
259+
await sendKeys({ press: 'Space' });
260+
261+
expect(grid.selectedItems).to.eql([grid.items[1]]);
262+
});
263+
264+
it('should remove the item from selectedItems on selection column cell Space key', async () => {
265+
grid.selectItem(grid.items[1]);
266+
const cell = getRowCells(rows[1])[0];
267+
268+
cell.focus();
269+
await sendKeys({ press: 'Space' });
270+
271+
expect(grid.selectedItems).to.eql([]);
272+
});
273+
274+
it('should add the item to selectedItems on selection column cell Space key when autoSelect is false', async () => {
275+
selectionColumn.autoSelect = false;
276+
277+
const cell = getRowCells(rows[1])[0];
278+
cell.focus();
279+
await sendKeys({ press: 'Space' });
280+
281+
expect(grid.selectedItems).to.eql([grid.items[1]]);
282+
});
283+
284+
it('should remove the item from selectedItems on selection column cell Space key when autoSelect is false', async () => {
285+
selectionColumn.autoSelect = false;
286+
287+
grid.selectItem(grid.items[1]);
288+
const cell = getRowCells(rows[1])[0];
289+
cell.focus();
290+
await sendKeys({ press: 'Space' });
291+
292+
expect(grid.selectedItems).to.eql([]);
293+
});
294+
295+
it('should add the item to selectedItems on selection column checkbox Space key', async () => {
296+
selectionColumn.autoSelect = false;
297+
298+
const cell = getRowCells(rows[1])[0];
299+
cell.focus();
300+
301+
// Enter interaction mode to focus checkbox
302+
await sendKeys({ press: 'Enter' });
303+
await sendKeys({ press: 'Space' });
304+
305+
expect(grid.selectedItems).to.eql([grid.items[1]]);
306+
});
307+
308+
it('should remove the item from selectedItems on selection column checkbox Space key', async () => {
309+
selectionColumn.autoSelect = false;
310+
311+
grid.selectItem(grid.items[1]);
312+
313+
const cell = getRowCells(rows[1])[0];
314+
cell.focus();
315+
316+
// Enter interaction mode to focus checkbox
317+
await sendKeys({ press: 'Enter' });
318+
await sendKeys({ press: 'Space' });
319+
320+
expect(grid.selectedItems).to.eql([]);
321+
});
322+
255323
it('should have bound the body checkbox to selected items', () => {
256324
const selectCheckbox = firstBodyCheckbox;
257325

@@ -274,6 +342,21 @@ describe('multi selection column', () => {
274342
expect(selectionColumn.selectAll).to.be.true;
275343
});
276344

345+
it('should set selectAll on header cell Space key', async () => {
346+
const headerCell = getRowCells(headerRows[1])[0];
347+
headerCell.focus();
348+
await sendKeys({ press: 'Space' });
349+
expect(selectionColumn.selectAll).to.be.true;
350+
});
351+
352+
it('should set selectAll on header cell checkbox Space key', async () => {
353+
const headerCell = getRowCells(headerRows[1])[0];
354+
headerCell.focus();
355+
await sendKeys({ press: 'Enter' });
356+
await sendKeys({ press: 'Space' });
357+
expect(selectionColumn.selectAll).to.be.true;
358+
});
359+
277360
it('should select all items when select all is set', () => {
278361
selectionColumn.selectAll = true;
279362

0 commit comments

Comments
 (0)