Skip to content

Commit

Permalink
fix: toggle checkbox on selection column cell Space key (#7178) (#7194)
Browse files Browse the repository at this point in the history
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan authored Mar 12, 2024
1 parent d45b7a3 commit de8d83d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/grid/src/vaadin-grid-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ export const GridMixin = (superClass) =>
});
}

if (column && column._onCellKeyDown) {
cell.addEventListener('keydown', column._onCellKeyDown.bind(column));
}

const slot = document.createElement('slot');
slot.setAttribute('name', slotName);

Expand Down Expand Up @@ -705,7 +709,7 @@ export const GridMixin = (superClass) =>
// Header & footer
const tagName = section === 'header' ? 'th' : 'td';
if (isColumnRow || column.localName === 'vaadin-grid-column-group') {
cell = column[`_${section}Cell`] || this._createCell(tagName);
cell = column[`_${section}Cell`] || this._createCell(tagName, column);
cell._column = column;
row.appendChild(cell);
column[`_${section}Cell`] = cell;
Expand Down
10 changes: 10 additions & 0 deletions packages/grid/src/vaadin-grid-selection-column-base-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ export const GridSelectionColumnBaseMixin = (superClass) =>
}
}

/** @private */
_onCellKeyDown(e) {
const target = e.composedPath()[0];
// Toggle on Space without having to enter interaction mode first
if (e.keyCode === 32 && (target === this._headerCell || (this._cells.includes(target) && !this.autoSelect))) {
const checkbox = target._content.firstElementChild;
checkbox.checked = !checkbox.checked;
}
}

/** @private */
__dragAutoScroller() {
if (this.__dragStartIndex === undefined) {
Expand Down
83 changes: 83 additions & 0 deletions packages/grid/test/selection.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@esm-bundle/chai';
import { click, fixtureSync, listenOnce, mousedown, nextFrame } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import {
fire,
Expand Down Expand Up @@ -252,6 +253,73 @@ describe('multi selection column', () => {
expect(grid.selectedItems).to.eql([]);
});

it('should add the item to selectedItems on selection column cell Space key', async () => {
const cell = getRowCells(rows[1])[0];
cell.focus();
await sendKeys({ press: 'Space' });

expect(grid.selectedItems).to.eql([grid.items[1]]);
});

it('should remove the item from selectedItems on selection column cell Space key', async () => {
grid.selectItem(grid.items[1]);
const cell = getRowCells(rows[1])[0];

cell.focus();
await sendKeys({ press: 'Space' });

expect(grid.selectedItems).to.eql([]);
});

it('should add the item to selectedItems on selection column cell Space key when autoSelect is false', async () => {
selectionColumn.autoSelect = false;

const cell = getRowCells(rows[1])[0];
cell.focus();
await sendKeys({ press: 'Space' });

expect(grid.selectedItems).to.eql([grid.items[1]]);
});

it('should remove the item from selectedItems on selection column cell Space key when autoSelect is false', async () => {
selectionColumn.autoSelect = false;

grid.selectItem(grid.items[1]);
const cell = getRowCells(rows[1])[0];
cell.focus();
await sendKeys({ press: 'Space' });

expect(grid.selectedItems).to.eql([]);
});

it('should add the item to selectedItems on selection column checkbox Space key', async () => {
selectionColumn.autoSelect = false;

const cell = getRowCells(rows[1])[0];
cell.focus();

// Enter interaction mode to focus checkbox
await sendKeys({ press: 'Enter' });
await sendKeys({ press: 'Space' });

expect(grid.selectedItems).to.eql([grid.items[1]]);
});

it('should remove the item from selectedItems on selection column checkbox Space key', async () => {
selectionColumn.autoSelect = false;

grid.selectItem(grid.items[1]);

const cell = getRowCells(rows[1])[0];
cell.focus();

// Enter interaction mode to focus checkbox
await sendKeys({ press: 'Enter' });
await sendKeys({ press: 'Space' });

expect(grid.selectedItems).to.eql([]);
});

it('should have bound the body checkbox to selected items', () => {
const selectCheckbox = firstBodyCheckbox;

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

it('should set selectAll on header cell Space key', async () => {
const headerCell = getRowCells(headerRows[1])[0];
headerCell.focus();
await sendKeys({ press: 'Space' });
expect(selectionColumn.selectAll).to.be.true;
});

it('should set selectAll on header cell checkbox Space key', async () => {
const headerCell = getRowCells(headerRows[1])[0];
headerCell.focus();
await sendKeys({ press: 'Enter' });
await sendKeys({ press: 'Space' });
expect(selectionColumn.selectAll).to.be.true;
});

it('should select all items when select all is set', () => {
selectionColumn.selectAll = true;

Expand Down

0 comments on commit de8d83d

Please sign in to comment.