Skip to content

Commit 66e7623

Browse files
ability to change the behaviour of ENTER key to focus cell below
1 parent a74eca3 commit 66e7623

6 files changed

Lines changed: 42 additions & 18 deletions

File tree

component/src/activeTable.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ export class ActiveTable extends LitElement {
292292
@property({type: Object})
293293
columnDropdown?: ColumnDropdownSettingsDefault;
294294

295+
@property({
296+
type: Boolean,
297+
converter: LITElementTypeConverters.convertToBoolean,
298+
})
299+
enterKeyMoveDown = false;
300+
295301
@property({type: Object})
296302
pagination?: boolean | Pagination;
297303

component/src/elements/cell/cellsWithTextDiv/selectCell/baseEvents/selectCellTextBaseEvents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FocusNextCellFromSelectCell} from '../../../../../utils/focusedElements/focusNextCellFromSelectCell';
1+
import {FocusNextRowCell} from '../../../../../utils/focusedElements/focusNextRowCell';
22
import {CellDropdownItem} from '../../../../dropdown/cellDropdown/cellDropdownItem';
33
import {OptionButton} from '../../../../dropdown/cellDropdown/buttons/optionButton';
44
import {ColumnDetailsT, ColumnsDetailsT} from '../../../../../types/columnDetails';
@@ -26,7 +26,7 @@ export class SelectCellTextBaseEvents {
2626
CellTextEvents.tabOutOfCell(at, rowIndex, columnIndex, event);
2727
} else if (event.key === KEYBOARD_KEY.ENTER) {
2828
event.preventDefault();
29-
FocusNextCellFromSelectCell.focusOrBlurSelectNextCell(elements, rowIndex);
29+
FocusNextRowCell.focusOrBlurSelect(elements, rowIndex);
3030
} else if (event.key === KEYBOARD_KEY.ARROW_UP) {
3131
event.preventDefault();
3232
CellDropdownItem.setSiblingItemOnCell(at, activeItems, 'previousSibling');

component/src/elements/cell/cellsWithTextDiv/text/cellTextEvents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {ActiveTable} from '../../../../activeTable';
55
export class CellTextEvents {
66
public static tabOutOfCell(at: ActiveTable, rowIndex: number, columnIndex: number, event: KeyboardEvent) {
77
event.preventDefault();
8-
DataCellEvents.keyDownCell.bind(at)(event);
8+
DataCellEvents.keyDownCell.bind(at, rowIndex, columnIndex)(event);
99
FocusNextColumnCellFromTextDiv.focusOrBlurNext(at, columnIndex, rowIndex);
1010
}
1111
}

component/src/elements/cell/dataCell/dataCellEvents.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {OverwriteCellsViaCSVOnPaste} from '../../../utils/paste/CSV/overwriteCel
22
import {UserKeyEventsStateUtils} from '../../../utils/userEventsState/userEventsStateUtils';
33
import {DateCellInputElement} from '../cellsWithTextDiv/dateCell/dateCellInputElement';
44
import {FocusedCellUtils} from '../../../utils/focusedElements/focusedCellUtils';
5+
import {FocusNextRowCell} from '../../../utils/focusedElements/focusNextRowCell';
56
import {CaretPosition} from '../../../utils/focusedElements/caretPosition';
67
import {CaretDisplayFix} from '../../../utils/browser/caretDisplayFix';
78
import {CellDropdown} from '../../dropdown/cellDropdown/cellDropdown';
@@ -15,10 +16,13 @@ import {CellElement} from '../cellElement';
1516
import {CellEvents} from '../cellEvents';
1617

1718
export class DataCellEvents {
18-
public static keyDownCell(this: ActiveTable, event: KeyboardEvent) {
19+
public static keyDownCell(this: ActiveTable, rowIndex: number, columnIndex: number, event: KeyboardEvent) {
20+
const {elements, activeType} = this._columnsDetails[columnIndex];
1921
// REF-7
2022
if (event.key === KEYBOARD_KEY.TAB) {
2123
UserKeyEventsStateUtils.temporarilyIndicateEvent(this._userKeyEventsState, KEYBOARD_KEY.TAB);
24+
} else if (event.key === KEYBOARD_KEY.ENTER && this.enterKeyMoveDown && !activeType.cellDropdownProps) {
25+
FocusNextRowCell.focus(this, rowIndex, elements, event);
2226
}
2327
}
2428

@@ -111,6 +115,6 @@ export class DataCellEvents {
111115
cellElement.onmousedown = () => {};
112116
cellElement.oninput = DataCellEvents.inputCell.bind(at, rowIndex, columnIndex);
113117
cellElement.onpaste = DataCellEvents.pasteCell.bind(at, rowIndex, columnIndex);
114-
cellElement.onkeydown = DataCellEvents.keyDownCell.bind(at);
118+
cellElement.onkeydown = DataCellEvents.keyDownCell.bind(at, rowIndex, columnIndex);
115119
}
116120
}

component/src/utils/focusedElements/focusNextCellFromSelectCell.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {ActiveTable} from '../../activeTable';
2+
import {CaretPosition} from './caretPosition';
3+
4+
export class FocusNextRowCell {
5+
// does not work in Safari
6+
public static focus(at: ActiveTable, rowIndex: number, elements: HTMLElement[], event: KeyboardEvent) {
7+
event.preventDefault();
8+
const nextRowCell = elements[rowIndex + 1];
9+
if (nextRowCell) {
10+
nextRowCell.focus(); // required for firefox browser
11+
CaretPosition.setToEndOfText(at, nextRowCell);
12+
}
13+
}
14+
15+
public static focusOrBlurSelect(elements: HTMLElement[], rowIndex: number) {
16+
const nextColumnCell = elements[rowIndex + 1];
17+
if (nextColumnCell) {
18+
// needs to be mousedown in order to set focusedCell
19+
nextColumnCell.dispatchEvent(new Event('mousedown'));
20+
nextColumnCell.scrollIntoView({block: 'nearest'});
21+
return nextColumnCell;
22+
}
23+
// if no next cell - blur current as the dropdown will be closed but the cursor would otherwise stay
24+
(elements[rowIndex].children[0] as HTMLElement).blur();
25+
return undefined;
26+
}
27+
}

0 commit comments

Comments
 (0)