Skip to content

Commit

Permalink
Fixed #6982 - Keyboard navigation Table Edit with arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitfindikli committed Jan 20, 2021
1 parent e6cd720 commit 72d8419
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/app/components/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3252,6 +3252,63 @@ export class EditableColumn implements AfterViewInit {
}
}
}
@HostListener('keydown.arrowdown', ['$event'])
onArrowDown(event: KeyboardEvent) {
if (this.isEnabled()) {
let currentCell = this.findCell(event.target);
if (currentCell) {
let cellIndex = DomHandler.index(currentCell);
let targetCell = this.findNextEditableColumnByIndex(currentCell, cellIndex);

if (targetCell) {
if (this.dt.isEditingCellValid()) {
this.closeEditingCell(true, event);
}

DomHandler.invokeElementMethod(event.target, 'blur');
DomHandler.invokeElementMethod(targetCell, 'click');
}

event.preventDefault();
}
}
}

@HostListener('keydown.arrowup', ['$event'])
onArrowUp(event: KeyboardEvent) {
if (this.isEnabled()) {
let currentCell = this.findCell(event.target);
if (currentCell) {
let cellIndex = DomHandler.index(currentCell);
let targetCell = this.findPrevEditableColumnByIndex(currentCell, cellIndex);

if (targetCell) {
if (this.dt.isEditingCellValid()) {
this.closeEditingCell(true, event);
}

DomHandler.invokeElementMethod(event.target, 'blur');
DomHandler.invokeElementMethod(targetCell, 'click');
}

event.preventDefault();
}
}
}

@HostListener('keydown.arrowleft', ['$event'])
onArrowLeft(event: KeyboardEvent) {
if (this.isEnabled()) {
this.moveToPreviousCell(event);
}
}

@HostListener('keydown.arrowright', ['$event'])
onArrowRight(event: KeyboardEvent) {
if (this.isEnabled()) {
this.moveToNextCell(event);
}
}

findCell(element) {
if (element) {
Expand Down Expand Up @@ -3343,6 +3400,40 @@ export class EditableColumn implements AfterViewInit {
}
}

findNextEditableColumnByIndex(cell: Element, index: number) {
let nextRow = cell.parentElement.nextElementSibling;

if (nextRow) {
let nextCell = nextRow.children[index];

if (nextCell && DomHandler.hasClass(nextCell, 'p-editable-column')) {
return nextCell;
}

return null;
}
else {
return null;
}
}

findPrevEditableColumnByIndex(cell: Element, index: number) {
let prevRow = cell.parentElement.previousElementSibling;

if (prevRow) {
let prevCell = prevRow.children[index];

if (prevCell && DomHandler.hasClass(prevCell, 'p-editable-column')) {
return prevCell;
}

return null;
}
else {
return null;
}
}

isEnabled() {
return this.pEditableColumnDisabled !== true;
}
Expand Down

2 comments on commit 72d8419

@lf-novelt
Copy link

@lf-novelt lf-novelt commented on 72d8419 May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello, this broke the native input text behavior that, when typing some text, you expect the left arrow to go to the previous character, not to change column, or, in an input[type=number], the up/down arrows should increment/decrement.
Is there a way to disable this behavior, at least for the left / right arrows for single line input? or to only change cell if right arrow pressed and cursor is at the last character of the current input?

@yigitfindikli
Copy link
Contributor Author

@yigitfindikli yigitfindikli commented on 72d8419 May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lf-novelt,

I think seems to be a really good feature. I'm gonna add to our 12.future milestone. Thanks for the feedback!

Please sign in to comment.