diff --git a/examples/src/pages/tests/table/datasource-api/removeDataArray.page.tsx b/examples/src/pages/tests/table/datasource-api/removeDataArray.page.tsx new file mode 100644 index 00000000..d337071d --- /dev/null +++ b/examples/src/pages/tests/table/datasource-api/removeDataArray.page.tsx @@ -0,0 +1,206 @@ +import { + InfiniteTable, + DataSource, + InfiniteTablePropRowStyle, + InfiniteTableRowInfo, + DataSourceApi, + type InfiniteTableColumn, +} from '@infinite-table/infinite-react'; +import React, { useState } from 'react'; + +export type Employee = { + id: number; + companyName: string; + companySize: string; + firstName: string; + lastName: string; + country: string; + countryCode: string; + city: string; + streetName: string; + streetNo: string; + department: string; + team: string; + salary: number; + age: number; + email: string; +}; + +const data: Employee[] = [ + { + id: 1, + firstName: 'John', + lastName: 'Doe', + country: 'USA', + city: 'New York', + salary: 100000, + department: 'Engineering', + team: 'Team A', + companyName: 'Company A', + companySize: 'Large', + countryCode: 'US', + streetName: 'Main St', + streetNo: '123', + age: 30, + email: 'john.doe@example.com', + }, + { + id: 2, + firstName: 'Jane', + lastName: 'Smith', + country: 'Canada', + city: 'Toronto', + salary: 90000, + department: 'Marketing', + team: 'Team B', + companyName: 'Company B', + companySize: 'Medium', + countryCode: 'CA', + streetName: 'Maple Ave', + streetNo: '456', + age: 28, + email: 'jane.smith@example.com', + }, + { + id: 3, + firstName: 'Alice', + lastName: 'Johnson', + country: 'UK', + city: 'London', + salary: 120000, + department: 'Finance', + team: 'Team C', + companyName: 'Company C', + companySize: 'Small', + countryCode: 'UK', + streetName: 'Baker St', + streetNo: '789', + age: 35, + email: 'alice.johnson@example.com', + }, + { + id: 4, + firstName: 'Bob', + lastName: 'Brown', + country: 'Australia', + city: 'Sydney', + salary: 110000, + department: 'Human Resources', + team: 'Team D', + companyName: 'Company D', + companySize: 'Medium', + countryCode: 'AU', + streetName: 'Collins St', + streetNo: '101', + age: 40, + email: 'bob.brown@example.com', + }, +]; + +export const columns: Record> = { + firstName: { + field: 'firstName', + header: 'First Name', + }, + country: { + field: 'country', + header: 'Country', + columnGroup: 'location', + }, + city: { + field: 'city', + header: 'City', + columnGroup: 'address', + }, + salary: { + field: 'salary', + type: 'number', + header: 'Salary', + }, + department: { + field: 'department', + header: 'Department', + }, + team: { + field: 'team', + header: 'Team', + }, + company: { field: 'companyName', header: 'Company' }, + companySize: { field: 'companySize', header: 'Company Size' }, +}; + +const rowStyle: InfiniteTablePropRowStyle = (param: { + rowInfo: InfiniteTableRowInfo; +}) => { + const { rowInfo } = param; + if (rowInfo.isGroupRow) { + return; + } + const { data } = rowInfo; + const salary = data ? data.salary : 0; + + if (salary > 150_000) { + return { background: 'tomato' }; + } + + if (rowInfo.indexInAll % 10 === 0) { + return { background: 'lightblue', color: 'black' }; + } + return undefined; +}; + +export default function App() { + const [dataSourceApi, setDataSourceApi] = + useState | null>(null); + + const removeRowsByPrimaryKey = async () => { + if (!dataSourceApi) { + return; + } + const getAllData = dataSourceApi.getRowInfoArray(); + console.log('data source before remove', getAllData); + + const listOfPrimaryKeys = getAllData.map((row: any) => row.data.id); + console.log('listOfPrimaryKeys', listOfPrimaryKeys); + + await dataSourceApi.removeDataArrayByPrimaryKeys(listOfPrimaryKeys); + console.log('data source after remove', dataSourceApi.getRowInfoArray()); + }; + + const removeRowsByDataRow = async () => { + if (!dataSourceApi) { + return; + } + const getAllData = dataSourceApi.getRowInfoArray(); + console.log('data source before remove', getAllData); + + const listOfRows = getAllData.map((row: any) => row.data); + console.log('listOfRows', listOfRows); + await dataSourceApi.removeDataArray(listOfRows); + console.log('data source after remove', dataSourceApi.getRowInfoArray()); + }; + + return ( + <> + + + + onReady={setDataSourceApi} + data={data} + primaryKey="id" + > + + columns={columns} + rowStyle={rowStyle} + domProps={{ + style: { height: '80vh' }, + }} + /> + + + ); +} diff --git a/examples/src/pages/tests/table/datasource-api/removeDataArray.spec.ts b/examples/src/pages/tests/table/datasource-api/removeDataArray.spec.ts new file mode 100644 index 00000000..92e56a0d --- /dev/null +++ b/examples/src/pages/tests/table/datasource-api/removeDataArray.spec.ts @@ -0,0 +1,40 @@ +import { test, expect } from '@testing'; + +export default test.describe.parallel('DataSourceApi', () => { + test('removeDataArrayByPrimaryKeys - works to remove more rows in one go', async ({ + page, + rowModel, + }) => { + await page.waitForInfinite(); + + let rowCount = await rowModel.getRenderedRowCount(); + + expect(rowCount).toBe(4); + + await page.click('button:first-of-type'); + + await page.evaluate(() => new Promise(requestAnimationFrame)); + + rowCount = await rowModel.getRenderedRowCount(); + + expect(rowCount).toBe(0); + }); + test('removeDataArray - works to remove more rows in one go', async ({ + page, + rowModel, + }) => { + await page.waitForInfinite(); + + let rowCount = await rowModel.getRenderedRowCount(); + + expect(rowCount).toBe(4); + + await page.click('button:last-of-type'); + + await page.evaluate(() => new Promise(requestAnimationFrame)); + + rowCount = await rowModel.getRenderedRowCount(); + + expect(rowCount).toBe(0); + }); +}); diff --git a/source/src/components/DataSource/Indexer.ts b/source/src/components/DataSource/Indexer.ts index b259643c..ba3368da 100644 --- a/source/src/components/DataSource/Indexer.ts +++ b/source/src/components/DataSource/Indexer.ts @@ -79,6 +79,7 @@ export class Indexer { this.primaryKeyToData.delete(pk); deleted = true; arr.splice(i, 1); + i--; } if (info.type === 'update' && !deleted) { diff --git a/www/content/docs/releases/index.page.md b/www/content/docs/releases/index.page.md index 9d5b8831..09c39a11 100644 --- a/www/content/docs/releases/index.page.md +++ b/www/content/docs/releases/index.page.md @@ -3,6 +3,10 @@ title: Releases description: All releases | Infinite Table DataGrid for React --- +## 5.0.4 + +@milestone id="128" + ## 5.0.1 @milestone id="127"