Skip to content

Commit 1ced810

Browse files
committed
Fix review comments
1 parent 4baaa2a commit 1ced810

File tree

4 files changed

+64
-80
lines changed

4 files changed

+64
-80
lines changed

src/dropdown/dropdown.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
305305
this.dropdownButton.nativeElement.focus();
306306
} else if (this.menuIsClosed && (event.key === " " || event.key === "ArrowDown" || event.key === "ArrowUp" ||
307307
event.key === "Spacebar" || event.key === "Down" || event.key === "Up")) {
308-
event.preventDefault();
309308
if (this.disableArrowKeys && (event.key === "ArrowDown" || event.key === "ArrowUp" || event.key === "Down" || event.key === "Up")) {
310309
return;
311310
}
311+
event.preventDefault();
312312
this.openMenu();
313313
}
314314

src/table/data-grid-focus.directive.ts

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {
22
Directive,
33
Input,
44
ElementRef,
5-
HostListener
5+
HostListener,
6+
Output,
7+
EventEmitter
68
} from "@angular/core";
79
import { Table } from "./table.component";
810
import { getFocusElementList, tabbableSelectorIgnoreTabIndex } from "../common/tab.service";
@@ -11,21 +13,32 @@ import { setTabIndex } from "../utils/a11y";
1113
@Directive({
1214
selector: "[ibmDataGridFocus]"
1315
})
14-
1516
export class DataGridFocus {
1617
@Input() ibmDataGridFocus: boolean;
17-
@Input() columnIndex: number;
18+
@Input() set columnIndex(value: number) {
19+
const shouldEmit = value !== this._columnIndex;
20+
this._columnIndex = value;
21+
if (shouldEmit) {
22+
this.columnIndexChange.emit(value);
23+
}
24+
}
25+
get columnIndex(): number {
26+
return this._columnIndex;
27+
}
28+
@Output() columnIndexChange: EventEmitter<number> = new EventEmitter();
1829

19-
constructor(private elementRef: ElementRef) {}
30+
protected _columnIndex: number;
2031

21-
focus(focusElement) {
22-
const focusElementList = getFocusElementList(focusElement, tabbableSelectorIgnoreTabIndex);
23-
if (focusElement.firstElementChild && focusElement.firstElementChild.classList.contains("bx--table-sort-v2")) {
32+
constructor(protected elementRef: ElementRef) {}
33+
34+
focus(element) {
35+
const focusElementList = getFocusElementList(element, tabbableSelectorIgnoreTabIndex);
36+
if (element.firstElementChild && element.firstElementChild.classList.contains("bx--table-sort-v2")) {
2437
focusElementList[1].focus();
2538
} else if (focusElementList.length > 0) {
2639
focusElementList[0].focus();
2740
} else {
28-
focusElement.focus();
41+
element.focus();
2942
}
3043
}
3144

@@ -36,12 +49,16 @@ export class DataGridFocus {
3649
}
3750
const element = this.elementRef.nativeElement;
3851
const rows = element.closest("table").rows;
39-
let rowIndex = Array.from(rows).findIndex(item => item === element.closest("tr"));
52+
const closestTr = element.closest("tr");
53+
let rowIndex = Array.from(rows).indexOf(closestTr);
54+
55+
const headerRow = rows[0].querySelectorAll("th");
4056

4157
switch (event.key) {
4258
case "Right": // IE specific value
4359
case "ArrowRight":
44-
if (element.nextElementSibling && element.nextElementSibling.style.width !== "0px") {
60+
if (element.nextElementSibling && Array.from(headerRow).indexOf(element.nextElementSibling) < headerRow.length - 1) {
61+
this.columnIndex++;
4562
const nextSibling = element.nextElementSibling;
4663
setTabIndex(element, -1);
4764
setTabIndex(nextSibling, 0);
@@ -51,6 +68,7 @@ export class DataGridFocus {
5168
case "Left": // IE specific value
5269
case "ArrowLeft":
5370
if (element.previousElementSibling) {
71+
this.columnIndex--;
5472
const previousSibling = element.previousElementSibling;
5573
setTabIndex(element, -1);
5674
setTabIndex(previousSibling, 0);
@@ -63,7 +81,7 @@ export class DataGridFocus {
6381
rowIndex++;
6482
const row = rows[rowIndex].querySelectorAll("td");
6583
setTabIndex(element, -1);
66-
if (rows[rowIndex].className === "bx--expandable-row-v2") {
84+
if (rows[rowIndex].classList.item(0) === "bx--expandable-row-v2") {
6785
setTabIndex(row[0], 0);
6886
this.focus(row[0]);
6987
} else {
@@ -74,27 +92,25 @@ export class DataGridFocus {
7492
break;
7593
case "Up": // IE specific value
7694
case "ArrowUp":
77-
const headerRow = rows[0].querySelectorAll("th");
95+
if ((rowIndex !== 1 && Array.from(headerRow).some(th => getFocusElementList(th, tabbableSelectorIgnoreTabIndex).length < 0)) ||
96+
rowIndex === 0) {
97+
return;
98+
}
7899
setTabIndex(element, -1);
79-
if (rowIndex === 1 && Array.from(headerRow).some(th => getFocusElementList(th, tabbableSelectorIgnoreTabIndex).length > 0)) {
80-
setTabIndex(headerRow[this.columnIndex], 0);
81-
this.focus(headerRow[this.columnIndex]);
82-
} else if (rowIndex > 1) {
83-
rowIndex--;
84-
const row = rows[rowIndex].querySelectorAll("td");
85-
if (rows[rowIndex].className === "bx--expandable-row-v2") {
86-
setTabIndex(row[0], 0);
87-
this.focus(row[0]);
88-
} else {
89-
setTabIndex(row[this.columnIndex], 0);
90-
this.focus(row[this.columnIndex]);
91-
}
100+
rowIndex--;
101+
const row = rows[rowIndex].querySelectorAll("td, th");
102+
if (rows[rowIndex].classList.item(0) === "bx--expandable-row-v2") {
103+
setTabIndex(row[0], 0);
104+
this.focus(row[0]);
105+
} else {
106+
setTabIndex(row[this.columnIndex], 0);
107+
this.focus(row[this.columnIndex]);
92108
}
93109
break;
94110
case "Home":
111+
this.columnIndex = 0;
95112
setTabIndex(element, -1);
96113
if (event.ctrlKey) {
97-
const headerRow = rows[0].querySelectorAll("th");
98114
if (Array.from(headerRow).some(th => getFocusElementList(th, tabbableSelectorIgnoreTabIndex).length > 0)) {
99115
setTabIndex(headerRow[0], 0);
100116
this.focus(headerRow[0]);
@@ -103,7 +119,7 @@ export class DataGridFocus {
103119
setTabIndex(firstBodyCell, 0);
104120
this.focus(firstBodyCell);
105121
}
106-
} else if (element.parentElement.className !== "bx--expandable-row-v2") {
122+
} else {
107123
const firstRowCell = rows[rowIndex].querySelectorAll("th, td")[0];
108124
setTabIndex(firstRowCell, 0);
109125
this.focus(firstRowCell);
@@ -116,11 +132,11 @@ export class DataGridFocus {
116132
this.columnIndex = lastRow.length - 1;
117133
setTabIndex(lastRow[this.columnIndex], 0);
118134
this.focus(lastRow[this.columnIndex]);
119-
} else if (element.parentElement.className !== "bx--expandable-row-v2") {
120-
this.columnIndex = lastRow.length - 1;
121-
const lastRowCell = rows[rowIndex].querySelectorAll("th, td")[this.columnIndex];
122-
setTabIndex(lastRowCell, 0);
123-
this.focus(lastRowCell);
135+
} else {
136+
const currentRow = rows[rowIndex].querySelectorAll("th, td");
137+
this.columnIndex = currentRow.length - 1;
138+
setTabIndex(currentRow[this.columnIndex], 0);
139+
this.focus(currentRow[this.columnIndex]);
124140
}
125141
break;
126142
}

src/table/table.component.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { Component, OnInit } from "@angular/core";
22
import { DialogModule } from "./../dialog/dialog.module";
33
import { TestBed } from "@angular/core/testing";
44
import { FormsModule } from "@angular/forms";
5-
import { TableModule, TableModel, TableHeaderItem, TableItem, DataGridFocus } from "./table.module";
5+
import {
6+
TableModule,
7+
TableModel,
8+
TableHeaderItem,
9+
TableItem,
10+
DataGridFocus
11+
} from "./table.module";
612
import { Table } from "./table.component";
713
import { StaticIconModule } from "./../icon/static-icon.module";
814

src/table/table.component.ts

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,12 @@ import { I18n } from "./../i18n/i18n.module";
180180
<tr>
181181
<th *ngIf="model.hasExpandableRows()"
182182
[ibmDataGridFocus]="isDataGrid"
183-
[columnIndex]="columnIndex"
184-
(keyup)="handleInteractions($event)"
183+
[(columnIndex)]="columnIndex"
185184
(click)="setExpandIndex($event)">
186185
</th>
187186
<th *ngIf="!skeleton && showSelectionColumn"
188187
[ibmDataGridFocus]="isDataGrid"
189-
[columnIndex]="columnIndex"
190-
(keyup)="handleInteractions($event)"
188+
[(columnIndex)]="columnIndex"
191189
(click)="setCheckboxIndex()"
192190
style="width: 10px;">
193191
<ibm-checkbox
@@ -205,11 +203,10 @@ import { I18n } from "./../i18n/i18n.module";
205203
[class]="column.className"
206204
[ngStyle]="column.style"
207205
[ibmDataGridFocus]="isDataGrid"
208-
[columnIndex]="columnIndex"
206+
[(columnIndex)]="columnIndex"
209207
[draggable]="columnsDraggable"
210208
(dragstart)="columnDragStart($event, i)"
211209
(dragend)="columnDragEnd($event, i)"
212-
(keyup)="handleInteractions($event)"
213210
(click)="setIndex($event, i)">
214211
<span *ngIf="skeleton"></span>
215212
<div
@@ -330,9 +327,8 @@ import { I18n } from "./../i18n/i18n.module";
330327
*ngIf="model.hasExpandableRows()"
331328
class="bx--table-expand-v2"
332329
[ibmDataGridFocus]="isDataGrid"
333-
[columnIndex]="columnIndex"
330+
[(columnIndex)]="columnIndex"
334331
[attr.data-previous-value]="(model.rowsExpanded[i] ? 'collapsed' : null)"
335-
(keyup)="handleInteractions($event)"
336332
(click)="setExpandIndex($event)">
337333
<button
338334
*ngIf="model.isRowExpandable(i)"
@@ -347,8 +343,7 @@ import { I18n } from "./../i18n/i18n.module";
347343
<td
348344
*ngIf="!skeleton && showSelectionColumn"
349345
[ibmDataGridFocus]="isDataGrid"
350-
[columnIndex]="columnIndex"
351-
(keyup)="handleInteractions($event)"
346+
[(columnIndex)]="columnIndex"
352347
(click)="setCheckboxIndex()">
353348
<ibm-checkbox
354349
inline="true"
@@ -363,8 +358,7 @@ import { I18n } from "./../i18n/i18n.module";
363358
[class]="model.header[j].className"
364359
[ngStyle]="model.header[j].style"
365360
[ibmDataGridFocus]="isDataGrid"
366-
[columnIndex]="columnIndex"
367-
(keyup)="handleInteractions($event)"
361+
[(columnIndex)]="columnIndex"
368362
(click)="setIndex($event, j)">
369363
<ng-container *ngIf="!item.template">{{item.data}}</ng-container>
370364
<ng-template
@@ -379,7 +373,7 @@ import { I18n } from "./../i18n/i18n.module";
379373
[attr.data-child-row]="(model.rowsExpanded[i] ? 'true' : null)">
380374
<td
381375
[ibmDataGridFocus]="isDataGrid"
382-
[columnIndex]="columnIndex"
376+
[(columnIndex)]="columnIndex"
383377
[attr.colspan]="row.length + 2"
384378
(click)="setExpandIndex($event)">
385379
<ng-container *ngIf="!firstExpandedTemplateInRow(row)">{{firstExpandedDataInRow(row)}}</ng-container>
@@ -482,6 +476,7 @@ export class Table implements AfterViewInit {
482476
});
483477
if (this.isDataGrid) {
484478
this._model.rowsExpandedChange.subscribe(() => {
479+
// Allows the expanded row to have a focus state when it exists in the DOM
485480
setTimeout(() => {
486481
const expandedRows = this.elementRef.nativeElement.querySelectorAll(".bx--expandable-row-v2:not(.bx--parent-row-v2)");
487482
Array.from<any>(expandedRows).forEach(row => {
@@ -946,30 +941,6 @@ export class Table implements AfterViewInit {
946941
}
947942
}
948943

949-
// This is handled in the component so that the columnIndex is saved as you go into expandable rows
950-
handleInteractions(event: KeyboardEvent) {
951-
switch (event.key) {
952-
case "Right": // IE specific value
953-
case "ArrowRight":
954-
if (this.columnIndex < this.getTotalColumns() - 1) {
955-
this.columnIndex++;
956-
}
957-
break;
958-
case "Left": // IE specific value
959-
case "ArrowLeft":
960-
if (this.columnIndex > 0) {
961-
this.columnIndex--;
962-
}
963-
break;
964-
case "Home":
965-
this.columnIndex = 0;
966-
break;
967-
case "End":
968-
this.columnIndex = this.getTotalColumns() - 1;
969-
break;
970-
}
971-
}
972-
973944
setIndex(event, columnIndex) {
974945
if (this.model.hasExpandableRows() && this.showSelectionColumn) {
975946
this.columnIndex = columnIndex + 2;
@@ -989,13 +960,4 @@ export class Table implements AfterViewInit {
989960
setExpandIndex(event) {
990961
this.columnIndex = 0;
991962
}
992-
993-
@HostListener("keyup", ["$event"])
994-
keyUp(event: KeyboardEvent) {
995-
if (event.key === "Home" && event.ctrlKey) {
996-
this.columnIndex = 0;
997-
} else if (event.key === "End" && event.ctrlKey) {
998-
this.columnIndex = this.getTotalColumns() - 1;
999-
}
1000-
}
1001963
}

0 commit comments

Comments
 (0)