Skip to content

Commit

Permalink
feat: add remove rows API (#1658)
Browse files Browse the repository at this point in the history
* test: add tests for removeRows

* feat: add removeRows API

* chore: apply code reviews
  • Loading branch information
jajugoguma authored May 4, 2022
1 parent 405518b commit 9112312
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/toast-ui.grid/cypress/integration/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/toast-ui.grid/src/dispatch/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions packages/toast-ui.grid/src/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
getRowHeight,
getFormattedValue,
getOmittedInternalProp,
getRemoveRowInfoList,
} from './query/data';
import { isRowHeader } from './helper/column';
import { createProvider } from './dataSource/serverSideDataProvider';
Expand Down Expand Up @@ -1210,6 +1211,19 @@ export default class Grid implements TuiGrid {
}
}

/**
* Remove the rows identified by the specified rowKeys.
* @param {Array<RowKey>} 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
Expand Down
24 changes: 22 additions & 2 deletions packages/toast-ui.grid/src/query/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand All @@ -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<any>
Expand Down
2 changes: 2 additions & 0 deletions packages/toast-ui.grid/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/toast-ui.grid/types/store/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export interface Data {
}

export type RemoveTargetRows = {
rowIndexes: number[];
rowIndices: number[];
rows: Row[];
nextRows: Row[];
};

0 comments on commit 9112312

Please sign in to comment.