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 (#2007)

* test: add test

* fix: finish editing when type enter from last cell when using moveDirectionOnEnter
  • Loading branch information
jajugoguma authored Jan 10, 2024
1 parent 33f1766 commit 285f7ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
23 changes: 23 additions & 0 deletions packages/toast-ui.grid/cypress/integration/keyMap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,29 @@ describe('Move focus on enter', () => {

assertFocusedCell('name', 0);
});

it('should finish editing on last cell', () => {
createGrid({
columns: [
{ name: 'name', editor: 'text' },
{ name: 'value', editor: 'text' },
{ name: 'age', editor: 'text' },
],
moveDirectionOnEnter: 'nextCell',
});
cy.getCellByIdx(3, 1).click();

clipboardType('{enter}');
editingLayerType('{enter}');

assertFocusedCell('age', 3);
cy.getByCls('layer-editing').should('be.visible');

editingLayerType('{enter}');

assertFocusedCell('age', 3);
cy.getByCls('layer-editing').should('not.be.visible');
});
});

describe('Selection', () => {
Expand Down
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 285f7ae

Please sign in to comment.