From ba0349ce3b29058b84386c35149edcf8178ef909 Mon Sep 17 00:00:00 2001 From: rjh Date: Wed, 8 Feb 2023 13:43:43 +0900 Subject: [PATCH] fix: applied filter appenRow focus index (fix #1819) --- .../toast-ui.grid/cypress/integration/data.spec.ts | 13 +++++++++++++ packages/toast-ui.grid/src/grid.tsx | 10 ++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/toast-ui.grid/cypress/integration/data.spec.ts b/packages/toast-ui.grid/cypress/integration/data.spec.ts index 5f6271146..96dca08e6 100644 --- a/packages/toast-ui.grid/cypress/integration/data.spec.ts +++ b/packages/toast-ui.grid/cypress/integration/data.spec.ts @@ -191,6 +191,19 @@ describe('appendRow()', () => { cy.getCell(2, 'name').should('have.text', 'observable'); }); + + it('if a filter applied and focus option exist, set focus to the first cell of the inserted row', () => { + createGrid(); + cy.gridInstance().invoke('appendRow', { name: 'Park', age: 15 }); + cy.gridInstance().invoke('filter', 'name', [{ code: 'eq', value: 'Park' }]); + cy.gridInstance().invoke('appendRow', { name: 'Park', age: 30 }, { focus: true }); + + cy.gridInstance().invoke('getFocusedCell').should('eql', { + rowKey: 3, + columnName: 'name', + value: 'Park', + }); + }); }); describe('prependRow()', () => { diff --git a/packages/toast-ui.grid/src/grid.tsx b/packages/toast-ui.grid/src/grid.tsx index 91d627e03..f1c5aac30 100644 --- a/packages/toast-ui.grid/src/grid.tsx +++ b/packages/toast-ui.grid/src/grid.tsx @@ -54,6 +54,7 @@ import { includes, isEmpty, isNil, + isNull, } from './helper/common'; import { Observable, getOriginObject } from './helper/observable'; import { createEventBus, EventBus } from './event/eventBus'; @@ -1178,8 +1179,13 @@ export default class Grid implements TuiGrid { } if (options.focus) { - const rowIdx = isUndefined(options.at) ? this.getRowCount() - 1 : options.at; - this.focusAt(rowIdx, 0); + if (!isUndefined(options.at)) { + this.focusAt(options.at, 0); + } else if (isNull(this.store.data.filters)) { + this.focusAt(this.getRowCount() - 1, 0); + } else { + this.focusAt(this.store.data.filteredRawData.length - 1, 0); + } } }