Skip to content

Commit

Permalink
fix: finish editing when type enter from last cell when using moveDir…
Browse files Browse the repository at this point in the history
…ectionOnEnter
  • Loading branch information
jajugoguma committed Jan 4, 2024
1 parent fce9765 commit 293dd57
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
26 changes: 16 additions & 10 deletions packages/toast-ui.grid/src/dispatch/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Store } from '@t/store';
import { SelectionRange } from '@t/store/selection';
import { EnterCommandType, KeyboardEventCommandType, TabCommandType } from '../helper/keyboard';
import { getNextCellIndex, getRemoveRange, getNextCellIndexWithRowSpan } from '../query/keyboard';
import { changeFocus, startEditing } from './focus';
import { changeFocus, saveAndFinishEditing, startEditing } from './focus';
import { changeSelectionRange } from './selection';
import { isRowHeader } from '../helper/column';
import { getRowRangeWithRowSpan, isRowSpanEnabled } from '../query/rowSpan';
Expand Down Expand Up @@ -64,7 +64,11 @@ export function editFocus(store: Store, command: KeyboardEventCommandType) {
}
}

export function moveTabAndEnterFocus(store: Store, command: TabCommandType | EnterCommandType) {
export function moveTabAndEnterFocus(
store: Store,
command: TabCommandType | EnterCommandType,
moveFocusByEnter = false
) {
const { focus, data, column, id } = store;
const { visibleColumnsWithRowHeader } = column;
const { rowKey, columnName, rowIndex, totalColumnIndex: columnIndex } = focus;
Expand All @@ -76,19 +80,21 @@ export function moveTabAndEnterFocus(store: Store, command: TabCommandType | Ent
const [nextRowIndex, nextColumnIndex] = getNextCellIndex(store, command, [rowIndex, columnIndex]);
const nextRowKey = getRowKeyByIndexWithPageRange(data, nextRowIndex);
const nextColumnName = visibleColumnsWithRowHeader[nextColumnIndex].name;
const moveAndEditFromLastCellByEnter =
rowIndex === nextRowIndex && columnIndex === nextColumnIndex && moveFocusByEnter;

if (!isRowHeader(nextColumnName)) {
focus.navigating = true;
changeFocus(store, nextRowKey, nextColumnName, id);

if (
focus.tabMode === 'moveAndEdit' &&
focus.rowKey === nextRowKey &&
focus.columnName === nextColumnName
) {
setTimeout(() => {
startEditing(store, nextRowKey, nextColumnName);
});
if (focus.tabMode === 'moveAndEdit') {
if (moveAndEditFromLastCellByEnter) {
saveAndFinishEditing(store);
} else if (focus.rowKey === nextRowKey && focus.columnName === nextColumnName) {
setTimeout(() => {
startEditing(store, nextRowKey, nextColumnName);
});
}
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions packages/toast-ui.grid/src/view/editingLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ export class EditingLayerComp extends Component<Props> {

private longestTextWidths: { [columnName: string]: number } = {};

private moveTabAndEnterFocus(ev: KeyboardEvent, command: TabCommandType | EnterCommandType) {
private moveTabAndEnterFocus(
ev: KeyboardEvent,
command: TabCommandType | EnterCommandType,
moveFocusByEnter = false
) {
const { dispatch } = this.props;

ev.preventDefault();
dispatch('moveTabAndEnterFocus', command);
dispatch('moveTabAndEnterFocus', command, moveFocusByEnter);
dispatch('setScrollToFocus');
}

Expand All @@ -75,7 +79,7 @@ export class EditingLayerComp extends Component<Props> {
if (isUndefined(moveDirectionOnEnter)) {
this.saveAndFinishEditing(true);
} else {
this.moveTabAndEnterFocus(ev, moveDirectionOnEnter);
this.moveTabAndEnterFocus(ev, moveDirectionOnEnter, true);
}
break;
case 'esc':
Expand Down

0 comments on commit 293dd57

Please sign in to comment.