Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace keyCode with key value #120

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,15 @@ export class DataTableBodyCellComponent<TRow extends { level?: number } = any>

@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent): void {
const keyCode = event.keyCode;
const key = event.key;
const isTargetCell = event.target === this._element;

const isAction =
keyCode === Keys.return ||
keyCode === Keys.down ||
keyCode === Keys.up ||
keyCode === Keys.left ||
keyCode === Keys.right;
key === Keys.return ||
key === Keys.down ||
key === Keys.up ||
key === Keys.left ||
key === Keys.right;

if (isAction && isTargetCell) {
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ export class DataTableBodyRowComponent<TRow = any> implements DoCheck, OnChanges

@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent): void {
const keyCode = event.keyCode;
const key = event.key;
const isTargetRow = event.target === this._element;

const isAction =
keyCode === Keys.return ||
keyCode === Keys.down ||
keyCode === Keys.up ||
keyCode === Keys.left ||
keyCode === Keys.right;
key === Keys.return ||
key === Keys.down ||
key === Keys.up ||
key === Keys.left ||
key === Keys.right;

const isCtrlA = event.key === 'a' && (event.ctrlKey || event.metaKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class DataTableSelectionComponent<TRow = any> {
if (select) {
this.selectRow(event, index, row);
} else if (type === 'keydown') {
if ((event as KeyboardEvent).keyCode === Keys.return) {
if ((event as KeyboardEvent).key === Keys.return) {
this.selectRow(event, index, row);
} else if ((event as KeyboardEvent).key === 'a' && (event.ctrlKey || event.metaKey)) {
this.selectRow(event, 0, this.rows[this.rows.length - 1]);
Expand All @@ -86,12 +86,9 @@ export class DataTableSelectionComponent<TRow = any> {
}

onKeyboardFocus(model: ActivateEvent<TRow>): void {
const { keyCode } = model.event as KeyboardEvent;
const { key } = model.event as KeyboardEvent;
const shouldFocus =
keyCode === Keys.up ||
keyCode === Keys.down ||
keyCode === Keys.right ||
keyCode === Keys.left;
key === Keys.up || key === Keys.down || key === Keys.right || key === Keys.left;

if (shouldFocus) {
const isCellSelection = this.selectionType === SelectionType.cell;
Expand All @@ -102,28 +99,28 @@ export class DataTableSelectionComponent<TRow = any> {
}
}
if (!model.cellElement || !isCellSelection) {
this.focusRow(model.rowElement, keyCode);
this.focusRow(model.rowElement, key);
} else if (isCellSelection) {
this.focusCell(model.cellElement, model.rowElement, keyCode, model.cellIndex);
this.focusCell(model.cellElement, model.rowElement, key, model.cellIndex);
}
}
}

focusRow(rowElement: HTMLElement, keyCode: number): void {
const nextRowElement = this.getPrevNextRow(rowElement, keyCode);
focusRow(rowElement: HTMLElement, key: Keys): void {
const nextRowElement = this.getPrevNextRow(rowElement, key);
if (nextRowElement) {
nextRowElement.focus();
}
}

getPrevNextRow(rowElement: HTMLElement, keyCode: number): any {
getPrevNextRow(rowElement: HTMLElement, key: Keys): any {
const parentElement = rowElement.parentElement;

if (parentElement) {
let focusElement: Element;
if (keyCode === Keys.up) {
if (key === Keys.up) {
focusElement = parentElement.previousElementSibling;
} else if (keyCode === Keys.down) {
} else if (key === Keys.down) {
focusElement = parentElement.nextElementSibling;
}

Expand All @@ -133,20 +130,15 @@ export class DataTableSelectionComponent<TRow = any> {
}
}

focusCell(
cellElement: HTMLElement,
rowElement: HTMLElement,
keyCode: number,
cellIndex: number
): void {
focusCell(cellElement: HTMLElement, rowElement: HTMLElement, key: Keys, cellIndex: number): void {
let nextCellElement: Element;

if (keyCode === Keys.left) {
if (key === Keys.left) {
nextCellElement = cellElement.previousElementSibling;
} else if (keyCode === Keys.right) {
} else if (key === Keys.right) {
nextCellElement = cellElement.nextElementSibling;
} else if (keyCode === Keys.up || keyCode === Keys.down) {
const nextRowElement = this.getPrevNextRow(rowElement, keyCode);
} else if (key === Keys.up || key === Keys.down) {
const nextRowElement = this.getPrevNextRow(rowElement, key);
if (nextRowElement) {
const children = nextRowElement.getElementsByClassName('datatable-body-cell');
if (children.length) {
Expand Down
12 changes: 6 additions & 6 deletions projects/ngx-datatable/src/lib/utils/keys.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export enum Keys {
up = 38,
down = 40,
return = 13,
escape = 27,
left = 37,
right = 39
up = 'ArrowUp',
down = 'ArrowDown',
return = 'Enter',
escape = 'Escape',
left = 'ArrowLeft',
right = 'ArrowRight'
}