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

fix: finish editing when type enter from last cell when using moveDirectionOnEnter #2007

Merged
merged 2 commits into from
Jan 10, 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
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
Loading