Skip to content

Commit 4be0fb3

Browse files
committed
Add updateChildrenByNodePath to DataSourceAPI and release version patch
1 parent 6261d47 commit 4be0fb3

File tree

7 files changed

+402
-2
lines changed

7 files changed

+402
-2
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import {
2+
InfiniteTable,
3+
DataSource,
4+
type InfiniteTableColumn,
5+
} from '@infinite-table/infinite-react';
6+
import * as React from 'react';
7+
8+
export type Employee = {
9+
id: number;
10+
companyName: string;
11+
companySize: string;
12+
firstName: string;
13+
lastName: string;
14+
country: string;
15+
countryCode: string;
16+
city: string;
17+
streetName: string;
18+
streetNo: string;
19+
department: string;
20+
team: string;
21+
salary: number;
22+
age: number;
23+
email: string;
24+
};
25+
26+
const data: Employee[] = [
27+
{
28+
id: 1,
29+
firstName: 'John',
30+
lastName: 'Doe',
31+
country: 'USA',
32+
city: 'New York',
33+
salary: 100000,
34+
department: 'Engineering',
35+
team: 'Team A',
36+
companyName: 'Company A',
37+
companySize: 'Large',
38+
countryCode: 'US',
39+
streetName: 'Main St',
40+
streetNo: '123',
41+
age: 30,
42+
43+
},
44+
{
45+
id: 2,
46+
firstName: 'Jane',
47+
lastName: 'Smith',
48+
country: 'Canada',
49+
city: 'Toronto',
50+
salary: 90000,
51+
department: 'Marketing',
52+
team: 'Team B',
53+
companyName: 'Company B',
54+
companySize: 'Medium',
55+
countryCode: 'CA',
56+
streetName: 'Maple Ave',
57+
streetNo: '456',
58+
age: 28,
59+
60+
},
61+
{
62+
id: 3,
63+
firstName: 'Alice',
64+
lastName: 'Johnson',
65+
country: 'UK',
66+
city: 'London',
67+
salary: 120000,
68+
department: 'Finance',
69+
team: 'Team C',
70+
companyName: 'Company C',
71+
companySize: 'Small',
72+
countryCode: 'UK',
73+
streetName: 'Baker St',
74+
streetNo: '789',
75+
age: 35,
76+
77+
},
78+
{
79+
id: 4,
80+
firstName: 'Bob',
81+
lastName: 'Brown',
82+
country: 'Australia',
83+
city: 'Sydney',
84+
salary: 110000,
85+
department: 'Human Resources',
86+
team: 'Team D',
87+
companyName: 'Company D',
88+
companySize: 'Medium',
89+
countryCode: 'AU',
90+
streetName: 'Collins St',
91+
streetNo: '101',
92+
age: 40,
93+
94+
},
95+
];
96+
97+
export const columns: Record<string, InfiniteTableColumn<Employee>> = {
98+
firstName: {
99+
field: 'firstName',
100+
header: 'First Name',
101+
},
102+
country: {
103+
field: 'country',
104+
header: 'Country',
105+
columnGroup: 'location',
106+
},
107+
city: {
108+
field: 'city',
109+
header: 'City',
110+
columnGroup: 'address',
111+
},
112+
salary: {
113+
field: 'salary',
114+
type: 'number',
115+
header: 'Salary',
116+
},
117+
department: {
118+
field: 'department',
119+
header: 'Department',
120+
},
121+
team: {
122+
field: 'team',
123+
header: 'Team',
124+
},
125+
company: { field: 'companyName', header: 'Company' },
126+
companySize: { field: 'companySize', header: 'Company Size' },
127+
};
128+
129+
export default function App() {
130+
return (
131+
<>
132+
<DataSource<Employee> data={data} primaryKey="id">
133+
<InfiniteTable<Employee>
134+
columns={columns}
135+
domProps={{
136+
style: { height: '80vh' },
137+
}}
138+
/>
139+
</DataSource>
140+
</>
141+
);
142+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { test, expect } from '@testing';
2+
3+
export default test.describe.parallel('DataSourceApi', () => {
4+
test('getRowInfoByIndex', async ({ page, apiModel }) => {
5+
await page.waitForInfinite();
6+
7+
const rowInfo = await apiModel.evaluateDataSource((api) => {
8+
return api.getRowInfoByIndex(0);
9+
});
10+
11+
expect(rowInfo).toMatchObject({
12+
indexInAll: 0,
13+
data: {
14+
id: 1,
15+
},
16+
dataSourceHasGrouping: false,
17+
isGroupRow: false,
18+
isTreeNode: false,
19+
});
20+
21+
expect(
22+
await apiModel.evaluateDataSource((api) => {
23+
return api.getRowInfoArray().length;
24+
}),
25+
).toBe(4);
26+
27+
const rowInfoLast = await apiModel.evaluateDataSource((api) => {
28+
return api.getRowInfoByIndex(3);
29+
});
30+
31+
expect(rowInfoLast).toMatchObject({
32+
indexInAll: 3,
33+
data: {
34+
id: 4,
35+
},
36+
dataSourceHasGrouping: false,
37+
isGroupRow: false,
38+
isTreeNode: false,
39+
});
40+
});
41+
});
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import * as React from 'react';
2+
3+
import { TreeDataSource, TreeGrid } from '@infinite-table/infinite-react';
4+
import {
5+
DataSourceApi,
6+
type InfiniteTableColumn,
7+
} from '@infinite-table/infinite-react';
8+
9+
export type FileSystemNode = {
10+
name: string;
11+
type: 'file' | 'folder';
12+
children?: FileSystemNode[] | null;
13+
sizeKB?: number;
14+
id: string;
15+
collapsed?: boolean;
16+
};
17+
18+
const nodes: FileSystemNode[] = [
19+
{
20+
name: 'Documents',
21+
type: 'folder',
22+
id: '1',
23+
children: [
24+
{
25+
name: 'report.doc',
26+
type: 'file',
27+
sizeKB: 100,
28+
id: '2',
29+
},
30+
{
31+
type: 'folder',
32+
name: 'pictures',
33+
id: '3',
34+
children: [
35+
{
36+
name: 'mountain.jpg',
37+
type: 'file',
38+
sizeKB: 302,
39+
id: '5',
40+
},
41+
],
42+
},
43+
44+
{
45+
type: 'file',
46+
name: 'last.txt',
47+
id: '7',
48+
},
49+
],
50+
},
51+
];
52+
53+
const columns: Record<string, InfiniteTableColumn<FileSystemNode>> = {
54+
name: {
55+
field: 'name',
56+
renderTreeIcon: true,
57+
58+
renderValue: ({ value, data }) => {
59+
return (
60+
<>
61+
{value} - {data!.id}
62+
</>
63+
);
64+
},
65+
},
66+
type: { field: 'type' },
67+
sizeKB: { field: 'sizeKB' },
68+
};
69+
export default function App() {
70+
const [dataSourceApi, setDataSourceApi] =
71+
React.useState<DataSourceApi<FileSystemNode> | null>(null);
72+
73+
const update = async () => {
74+
if (!dataSourceApi) {
75+
return;
76+
}
77+
78+
dataSourceApi.updateChildrenByNodePath(
79+
(current) => {
80+
return [
81+
...(current || []),
82+
{
83+
name: 'new.txt',
84+
type: 'file',
85+
id: '8',
86+
},
87+
];
88+
},
89+
['1', '3'],
90+
);
91+
};
92+
93+
const remove = async () => {
94+
if (!dataSourceApi) {
95+
return;
96+
}
97+
98+
dataSourceApi.updateChildrenByNodePath(() => {
99+
return undefined;
100+
}, ['1', '3']);
101+
};
102+
103+
return (
104+
<>
105+
<button type="button" onClick={update}>
106+
update
107+
</button>
108+
<button type="button" onClick={remove}>
109+
remove
110+
</button>
111+
<TreeDataSource<FileSystemNode>
112+
onReady={setDataSourceApi}
113+
defaultTreeExpandState={{
114+
defaultExpanded: true,
115+
collapsedPaths: [['1', '3']],
116+
}}
117+
data={nodes}
118+
primaryKey="id"
119+
nodesKey="children"
120+
>
121+
<TreeGrid<FileSystemNode>
122+
domProps={{
123+
style: {
124+
margin: '5px',
125+
height: 900,
126+
border: '1px solid gray',
127+
position: 'relative',
128+
},
129+
}}
130+
columns={columns}
131+
/>
132+
</TreeDataSource>
133+
</>
134+
);
135+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { test, expect } from '@testing';
2+
3+
export default test.describe('TreeApi updateChildrenByNodePath', () => {
4+
test('works as expected', async ({ page, tableModel, treeModel }) => {
5+
await page.waitForInfinite();
6+
7+
const treeCol = tableModel.withColumn('name');
8+
9+
await page.click('button:text("update")');
10+
11+
await treeModel.toggleParentNode(2);
12+
13+
let treeData = await treeCol.getValues();
14+
15+
expect(treeData).toEqual([
16+
'Documents - 1',
17+
'report.doc - 2',
18+
'pictures - 3',
19+
'mountain.jpg - 5',
20+
'new.txt - 8',
21+
'last.txt - 7',
22+
]);
23+
24+
await page.click('button:text("remove")');
25+
26+
treeData = await treeCol.getValues();
27+
28+
expect(treeData).toEqual([
29+
'Documents - 1',
30+
'report.doc - 2',
31+
'pictures - 3',
32+
33+
'last.txt - 7',
34+
]);
35+
});
36+
});

0 commit comments

Comments
 (0)