Skip to content

Commit 50e613c

Browse files
PivotGrid - Add keyboard accessibility to expand icons
1 parent 9152bf1 commit 50e613c

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import $ from '@js/core/renderer';
2+
3+
import { AreaItem } from '../area_item/m_area_item';
4+
5+
class TestAreaItem extends AreaItem {
6+
_getAreaName() {
7+
return 'row';
8+
}
9+
}
10+
11+
const createMockComponent = () => ({
12+
option(name: string) {
13+
const options: Record<string, any> = {
14+
rtlEnabled: false,
15+
encodeHtml: true,
16+
onCellPrepared: null,
17+
};
18+
return options[name];
19+
},
20+
_eventsStrategy: {
21+
hasEvent: () => false,
22+
},
23+
_defaultActionArgs: () => ({}),
24+
});
25+
26+
describe('PivotGrid expand icon accessibility', () => {
27+
let $container: any = null;
28+
29+
beforeEach(() => {
30+
$container = $('<div>').appendTo('body');
31+
});
32+
33+
afterEach(() => {
34+
$container.remove();
35+
});
36+
37+
it('should set aria-expanded="true" on td when cell is expanded', () => {
38+
const areaItem = new TestAreaItem(createMockComponent());
39+
const tableData = [[{ expanded: true, text: 'Category' }]];
40+
41+
areaItem.render($container, tableData);
42+
43+
const $td = $container.find('td');
44+
expect($td.attr('aria-expanded')).toBe('true');
45+
});
46+
47+
it('should set aria-expanded="false" on td when cell is collapsed', () => {
48+
const areaItem = new TestAreaItem(createMockComponent());
49+
const tableData = [[{ expanded: false, text: 'Category' }]];
50+
51+
areaItem.render($container, tableData);
52+
53+
const $td = $container.find('td');
54+
expect($td.attr('aria-expanded')).toBe('false');
55+
});
56+
57+
it('should set tabindex="0" on td when cell has expand icon', () => {
58+
const areaItem = new TestAreaItem(createMockComponent());
59+
const tableData = [[{ expanded: true, text: 'Category' }]];
60+
61+
areaItem.render($container, tableData);
62+
63+
const $td = $container.find('td');
64+
expect($td.attr('tabindex')).toBe('0');
65+
});
66+
67+
it('should not set tabindex on td when cell has no expand state', () => {
68+
const areaItem = new TestAreaItem(createMockComponent());
69+
const tableData = [[{ text: 'Plain cell' }]];
70+
71+
areaItem.render($container, tableData);
72+
73+
const $td = $container.find('td');
74+
expect($td.attr('tabindex')).toBeUndefined();
75+
});
76+
77+
it('should not set aria-expanded on td when cell has no expand state', () => {
78+
const areaItem = new TestAreaItem(createMockComponent());
79+
const tableData = [[{ text: 'Plain cell' }]];
80+
81+
areaItem.render($container, tableData);
82+
83+
const $td = $container.find('td');
84+
expect($td.attr('aria-expanded')).toBeUndefined();
85+
});
86+
});

packages/devextreme/js/__internal/grids/pivot_grid/area_item/m_area_item.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ abstract class AreaItem {
198198
span.classList.add(PIVOTGRID_EXPAND_CLASS);
199199
div.appendChild(span);
200200
td.appendChild(div);
201+
td.setAttribute('aria-expanded', String(cell.expanded));
202+
td.setAttribute('tabindex', '0');
201203
}
202204

203205
cellText = this._getCellText(cell, encodeHtml);

packages/devextreme/js/__internal/grids/pivot_grid/m_widget.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,16 @@ class PivotGrid extends Widget {
886886
});
887887
}
888888

889+
_handleCellKeyDown(e) {
890+
if (e.key === 'Enter' || e.key === ' ') {
891+
const args = this._createEventArgs(e.currentTarget, e);
892+
if (args.cell && isDefined(args.cell.expanded)) {
893+
e.preventDefault();
894+
this._handleCellClick({ currentTarget: e.currentTarget, preventDefault: noop });
895+
}
896+
}
897+
}
898+
889899
_getNoDataText() {
890900
return this.option('texts.noData');
891901
}
@@ -1077,6 +1087,7 @@ class PivotGrid extends Widget {
10771087
.toggleClass('dx-word-wrap', !!that.option('wordWrapEnabled'));
10781088

10791089
eventsEngine.on($table, addNamespace(clickEventName, 'dxPivotGrid'), 'td', that._handleCellClick.bind(that));
1090+
eventsEngine.on($table, addNamespace('keydown', 'dxPivotGrid'), 'td', that._handleCellKeyDown.bind(that));
10801091

10811092
return $table;
10821093
}

0 commit comments

Comments
 (0)