private onInputKeyDown()

in src/draft-components/grid/grid.tsx [370:407]


  private onInputKeyDown(event: KeyboardEvent) {
    // allow input to handle its own keystrokes
    event.stopPropagation();

    const { key, shiftKey } = event;

    if (key === 'Escape') {
      this.preventSave = true;
    }

    // switch out of edit mode on enter or escape
    if (key === 'Escape' || key === 'Enter') {
      this.updateEditing(false, true);
    }

    // save value on enter
    if (key === 'Enter') {
      const cellIndex = this.rowSelection === RowSelectionPattern.Checkbox ? this.activeCell[0] - 1 : this.activeCell[0];
      this.saveCell(cellIndex, this.activeCell[1], (event.target as HTMLInputElement).value);
    }

    // allow tab and shift+tab to move through cells in a row for edit on click grid
    else if (key === 'Tab' && this.editOnClick) {
      const maxCellIndex = this.rowSelection === RowSelectionPattern.Checkbox ? this.columns.length : this.columns.length - 1;
      if (shiftKey && this.activeCell[0] > 0) {
        this.saveCell(this.activeCell[0], this.activeCell[1], (event.target as HTMLInputElement).value);
        this.updateActiveCell(this.activeCell[0] - 1, this.activeCell[1]);
        this.preventSave = true;
        event.preventDefault();
      }
      else if (!shiftKey && this.activeCell[0] < maxCellIndex) {
        this.saveCell(this.activeCell[0], this.activeCell[1], (event.target as HTMLInputElement).value);
        this.updateActiveCell(this.activeCell[0] + 1, this.activeCell[1]);
        this.preventSave = true;
        event.preventDefault();
      }
    }
  }