Skip to content

Commit f3f95b3

Browse files
committed
chore: update data table demo with virtulization
1 parent d520b2b commit f3f95b3

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

apps/www/src/components/datatable-demo.tsx

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,21 @@ import {
22
Checkbox,
33
DataTable,
44
DataTableColumnDef,
5-
Flex
5+
Flex,
6+
Switch,
7+
Text
68
} from '@raystack/apsara';
9+
import { useMemo, useState } from 'react';
710

8-
const data: Payment[] = [
9-
{
10-
id: 'm5gr84i9',
11-
amount: 316,
12-
status: 'success',
13-
14-
},
15-
{
16-
id: '3u1reuv4',
17-
amount: 242,
18-
status: 'success',
19-
20-
},
21-
{
22-
id: 'derv1ws0',
23-
amount: 837,
24-
status: 'processing',
25-
26-
},
27-
{
28-
id: '5kma53ae',
29-
amount: 874,
30-
status: 'success',
31-
32-
},
33-
{
34-
id: 'bhqecj4p',
35-
amount: 721,
36-
status: 'failed',
37-
38-
}
39-
];
11+
const statuses = ['pending', 'processing', 'success', 'failed'] as const;
12+
13+
const generateData = (count: number): Payment[] =>
14+
Array.from({ length: count }, (_, i) => ({
15+
id: `row-${i}`,
16+
amount: Math.floor(Math.random() * 1000),
17+
status: statuses[i % 4],
18+
email: `user${i}@example.com`
19+
}));
4020

4121
export type Payment = {
4222
id: string;
@@ -50,14 +30,20 @@ export const columns: DataTableColumnDef<Payment, unknown>[] = [
5030
accessorKey: 'select',
5131
header: '',
5232
cell: ({ row }) => (
53-
<Checkbox
54-
checked={row.getIsSelected()}
55-
onCheckedChange={value => row.toggleSelected(!!value)}
56-
aria-label='Select row'
57-
/>
33+
<Flex align='center' justify='center' width='full'>
34+
<Checkbox
35+
checked={row.getIsSelected()}
36+
onCheckedChange={value => row.toggleSelected(!!value)}
37+
aria-label='Select row'
38+
/>
39+
</Flex>
5840
),
5941
enableSorting: false,
60-
enableHiding: false
42+
enableHiding: false,
43+
styles: {
44+
header: { width: 50, flex: 'none' },
45+
cell: { width: 50, flex: 'none' }
46+
}
6147
},
6248
{
6349
accessorKey: 'status',
@@ -84,7 +70,12 @@ export const columns: DataTableColumnDef<Payment, unknown>[] = [
8470
}
8571
],
8672
filterType: 'multiselect',
87-
enableColumnFilter: true
73+
enableColumnFilter: true,
74+
enableHiding: true,
75+
styles: {
76+
header: { width: 120, flex: 'none' },
77+
cell: { width: 120, flex: 'none' }
78+
}
8879
},
8980
{
9081
accessorKey: 'email',
@@ -103,22 +94,41 @@ export const columns: DataTableColumnDef<Payment, unknown>[] = [
10394
}).format(amount);
10495

10596
return <div className='text-right font-medium'>{formatted}</div>;
97+
},
98+
enableHiding: true,
99+
styles: {
100+
header: { width: 100, flex: 'none' },
101+
cell: { width: 100, flex: 'none' }
106102
}
107103
}
108104
];
109105

110106
const DataTableDemo = () => {
107+
const [virtualized, setVirtualized] = useState(true);
108+
109+
const data = useMemo(() => generateData(100), []);
110+
111111
return (
112-
<Flex direction='column'>
113-
<DataTable
114-
data={data}
115-
mode='client'
116-
columns={columns}
117-
defaultSort={{ name: 'email', order: 'asc' }}
118-
>
119-
<DataTable.Toolbar />
120-
<DataTable.Content />
121-
</DataTable>
112+
<Flex direction='column' gap='4' width='full'>
113+
<Flex align='center' gap='2'>
114+
<Switch checked={virtualized} onCheckedChange={setVirtualized} />
115+
<Text>Virtualized (100 rows)</Text>
116+
</Flex>
117+
<div style={{ height: 400 }}>
118+
<DataTable
119+
data={data}
120+
mode='client'
121+
columns={columns}
122+
defaultSort={{ name: 'email', order: 'asc' }}
123+
>
124+
<DataTable.Toolbar />
125+
{virtualized ? (
126+
<DataTable.VirtualizedContent rowHeight={44.5} />
127+
) : (
128+
<DataTable.Content />
129+
)}
130+
</DataTable>
131+
</div>
122132
</Flex>
123133
);
124134
};

0 commit comments

Comments
 (0)