diff --git a/packages/toast-ui.grid/cypress/integration/data.spec.ts b/packages/toast-ui.grid/cypress/integration/data.spec.ts index d470869ad..5f6271146 100644 --- a/packages/toast-ui.grid/cypress/integration/data.spec.ts +++ b/packages/toast-ui.grid/cypress/integration/data.spec.ts @@ -387,6 +387,24 @@ describe('removeRow()', () => { }); }); +describe('removeRows', () => { + it('should remove rows matching given row keys', () => { + createGrid(); + + cy.gridInstance().invoke('removeRows', [0, 1]); + + cy.getCellByIdx(0, 0).should('not.exist'); + }); + + it('should add removed rows to modified data', () => { + createGrid(); + + cy.gridInstance().invoke('removeRows', [0, 1]); + + cy.gridInstance().invoke('getModifiedRows').its('deletedRows').should('have.length', 2); + }); +}); + describe('removeCheckedRows()', () => { beforeEach(() => { // @ts-ignore diff --git a/packages/toast-ui.grid/src/dispatch/data.ts b/packages/toast-ui.grid/src/dispatch/data.ts index 71d9a4569..b80f280b5 100644 --- a/packages/toast-ui.grid/src/dispatch/data.ts +++ b/packages/toast-ui.grid/src/dispatch/data.ts @@ -899,7 +899,7 @@ export function appendRows(store: Store, inputData: OptRow[]) { export function removeRows(store: Store, targetRows: RemoveTargetRows) { const { data, id, focus, column } = store; const { sortState, viewData, rawData } = data; - const { rowIndexes, rows, nextRows } = targetRows; + const { rowIndices: rowIndexes, rows, nextRows } = targetRows; const deletedCount = rowIndexes.length; updatePageWhenRemovingRow(store, deletedCount); diff --git a/packages/toast-ui.grid/src/grid.tsx b/packages/toast-ui.grid/src/grid.tsx index 9a591ae0c..dcf23c953 100644 --- a/packages/toast-ui.grid/src/grid.tsx +++ b/packages/toast-ui.grid/src/grid.tsx @@ -66,6 +66,7 @@ import { getRowHeight, getFormattedValue, getOmittedInternalProp, + getRemoveRowInfoList, } from './query/data'; import { isRowHeader } from './helper/column'; import { createProvider } from './dataSource/serverSideDataProvider'; @@ -1210,6 +1211,19 @@ export default class Grid implements TuiGrid { } } + /** + * Remove the rows identified by the specified rowKeys. + * @param {Array} rowKeys - The array of unique keys of the row + */ + public removeRows(rowKeys: RowKey[]) { + const removeRowInfoList = getRemoveRowInfoList(this.store, rowKeys); + const removeRowsCount = removeRowInfoList.rows.length; + + if (removeRowsCount > 0) { + this.dispatch('removeRows', removeRowInfoList); + } + } + /** * Return the object that contains all values in the specified row. * @param {number|string} rowKey - The unique key of the target row diff --git a/packages/toast-ui.grid/src/query/data.ts b/packages/toast-ui.grid/src/query/data.ts index 22165519c..9eda4d653 100644 --- a/packages/toast-ui.grid/src/query/data.ts +++ b/packages/toast-ui.grid/src/query/data.ts @@ -54,13 +54,13 @@ export function isEditableCell(store: Store, rowIndex: number, columnName: strin export function getCheckedRowInfoList({ data }: Store) { const targetRows: RemoveTargetRows = { - rowIndexes: [], + rowIndices: [], rows: [], nextRows: [], }; data.rawData.reduce((acc, row, index) => { if (row._attributes.checked) { - acc.rowIndexes.push(index); + acc.rowIndices.push(index); acc.rows.push(row); acc.nextRows.push(data.rawData[index + 1]); } @@ -70,6 +70,26 @@ export function getCheckedRowInfoList({ data }: Store) { return targetRows; } +export function getRemoveRowInfoList({ data }: Store, rowKeys: RowKey[]) { + const targetRows: RemoveTargetRows = { + rowIndices: [], + rows: [], + nextRows: [], + }; + data.rawData.reduce((acc, row, index) => { + const rowKeyIndex = rowKeys.indexOf(row.rowKey); + if (rowKeyIndex !== -1) { + acc.rowIndices.push(index); + acc.rows.push(row); + acc.nextRows.push(data.rawData[index + 1]); + rowKeys.splice(rowKeyIndex, 1); + } + return acc; + }, targetRows); + + return targetRows; +} + export function getConditionalRows( { data }: Store, conditions: ((row: Row) => boolean) | Dictionary diff --git a/packages/toast-ui.grid/types/index.d.ts b/packages/toast-ui.grid/types/index.d.ts index d20622f8b..581727b52 100644 --- a/packages/toast-ui.grid/types/index.d.ts +++ b/packages/toast-ui.grid/types/index.d.ts @@ -188,6 +188,8 @@ declare namespace tui { public removeRow(rowKey: RowKey, options?: OptRemoveRow): void; + public removeRows(rowKeys: RowKey[]): void; + public getRow(rowKey: RowKey): Row | null; public getRowAt(rowIdx: number): Row | null; diff --git a/packages/toast-ui.grid/types/store/data.d.ts b/packages/toast-ui.grid/types/store/data.d.ts index 9577b8ba7..a0f7c0941 100644 --- a/packages/toast-ui.grid/types/store/data.d.ts +++ b/packages/toast-ui.grid/types/store/data.d.ts @@ -140,7 +140,7 @@ export interface Data { } export type RemoveTargetRows = { - rowIndexes: number[]; + rowIndices: number[]; rows: Row[]; nextRows: Row[]; };