-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathPinArbitraryColumnsExamples.tsx
More file actions
227 lines (212 loc) · 8.58 KB
/
PinArbitraryColumnsExamples.tsx
File metadata and controls
227 lines (212 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use client';
import { ActionIcon, Box, Button, DirectionProvider, Group, SegmentedControl, Stack, Text } from '@mantine/core';
import { IconEdit, IconEye, IconTrash } from '@tabler/icons-react';
import { DataTable, useDataTableColumns } from '__PACKAGE__';
import { useState } from 'react';
import { employees, type Employee } from '~/data';
const records = employees.slice(0, 5);
// example-start multiple-left-pinned
export function PinMultipleLeftColumnsExample() {
return (
<DataTable
withTableBorder
columns={[
{ accessor: 'firstName', noWrap: true, pinned: 'left' },
{ accessor: 'lastName', noWrap: true, pinned: 'left' },
{ accessor: 'department.name', title: 'Department' },
{ accessor: 'department.company.name', title: 'Company', noWrap: true },
{ accessor: 'department.company.city', title: 'City', noWrap: true },
{ accessor: 'department.company.state', title: 'State' },
{ accessor: 'department.company.streetAddress', title: 'Address', noWrap: true },
{ accessor: 'department.company.missionStatement', title: 'Mission statement', noWrap: true },
]}
records={records}
/>
);
}
// example-end
// example-start left-and-right-pinned
export function PinLeftAndRightColumnsExample() {
const [selectedRecords, setSelectedRecords] = useState<Employee[]>([]);
return (
<DataTable
withTableBorder
columns={[
{ accessor: 'firstName', noWrap: true, pinned: 'left' },
{ accessor: 'lastName', noWrap: true, pinned: 'left' },
{ accessor: 'department.name', title: 'Department' },
{ accessor: 'department.company.name', title: 'Company', noWrap: true },
{ accessor: 'department.company.city', title: 'City', noWrap: true },
{ accessor: 'department.company.state', title: 'State' },
{ accessor: 'department.company.streetAddress', title: 'Address', noWrap: true },
{ accessor: 'department.company.missionStatement', title: 'Mission statement', noWrap: true },
{
accessor: 'actions',
title: <Box mr={6}>Row actions</Box>,
textAlign: 'right',
pinned: 'right',
render: () => (
<Group gap={4} justify="right" wrap="nowrap">
<ActionIcon size="sm" variant="subtle" color="green">
<IconEye size={16} />
</ActionIcon>
<ActionIcon size="sm" variant="subtle" color="blue">
<IconEdit size={16} />
</ActionIcon>
<ActionIcon size="sm" variant="subtle" color="red">
<IconTrash size={16} />
</ActionIcon>
</Group>
),
},
]}
records={records}
selectedRecords={selectedRecords}
onSelectedRecordsChange={setSelectedRecords}
/>
);
}
// example-end
// example-start interactive-pinning
export function InteractivePinningExample() {
const key = 'interactive-pinning-example';
const [direction, setDirection] = useState<'ltr' | 'rtl'>('ltr');
const { effectiveColumns, resetColumnsPinning } = useDataTableColumns<Employee>({
key,
columns: [
{ accessor: 'firstName', noWrap: true, pinnable: true, pinned: 'left' },
{ accessor: 'lastName', noWrap: true, pinnable: true },
{ accessor: 'department.name', title: 'Department', pinnable: true },
{ accessor: 'department.company.name', title: 'Company', noWrap: true, pinnable: true },
{ accessor: 'department.company.city', title: 'City', noWrap: true, pinnable: true },
{ accessor: 'department.company.state', title: 'State', pinnable: true },
{ accessor: 'department.company.streetAddress', title: 'Address', noWrap: true, pinnable: true },
{ accessor: 'department.company.missionStatement', title: 'Mission statement', noWrap: true, pinnable: true },
],
});
return (
<Stack gap="xs">
<Group justify="space-between">
<Group gap="xs">
<Text size="sm" fw={500}>
Direction:
</Text>
<SegmentedControl
size="xs"
value={direction}
onChange={(value) => setDirection(value as 'ltr' | 'rtl')}
data={[
{ label: 'LTR', value: 'ltr' },
{ label: 'RTL', value: 'rtl' },
]}
/>
</Group>
<Button size="xs" variant="light" onClick={resetColumnsPinning}>
Reset pinning
</Button>
</Group>
<DirectionProvider key={`interactive-pinning-${direction}`} initialDirection={direction} detectDirection={false}>
<Box dir={direction}>
<DataTable withTableBorder storeColumnsKey={key} columns={effectiveColumns} records={records} />
</Box>
</DirectionProvider>
</Stack>
);
}
// example-end
// example-start pinning-and-toggling
export function PinningAndTogglingExample() {
const key = 'pinning-and-toggling-example';
const { effectiveColumns, resetColumnsPinning, resetColumnsToggle } = useDataTableColumns<Employee>({
key,
columns: [
{ accessor: 'firstName', noWrap: true, pinnable: true, toggleable: true, pinned: 'left' },
{ accessor: 'lastName', noWrap: true, pinnable: true, toggleable: true },
{ accessor: 'email', noWrap: true, pinnable: true, toggleable: true },
{ accessor: 'birthDate', title: 'Birth date', pinnable: true, toggleable: true },
{ accessor: 'department.name', title: 'Department', pinnable: true, toggleable: true },
{ accessor: 'department.company.name', title: 'Company', noWrap: true, pinnable: true, toggleable: true },
{ accessor: 'department.company.city', title: 'City', noWrap: true, pinnable: true, toggleable: true },
{ accessor: 'department.company.state', title: 'State', pinnable: true, toggleable: true },
{
accessor: 'department.company.streetAddress',
title: 'Address',
noWrap: true,
pinnable: true,
toggleable: true,
},
{
accessor: 'department.company.missionStatement',
title: 'Mission statement',
noWrap: true,
width: 300,
pinnable: true,
toggleable: true,
},
],
});
return (
<>
<DataTable withTableBorder storeColumnsKey={key} columns={effectiveColumns} records={records} />
<Group justify="right" mt="xs">
<Button size="xs" variant="light" onClick={resetColumnsToggle}>
Reset visibility
</Button>
<Button size="xs" variant="light" onClick={resetColumnsPinning}>
Reset pinning
</Button>
</Group>
</>
);
}
// example-end
// example-start pinning-with-column-groups
export function PinningWithColumnGroupsExample() {
const key = 'pinning-with-column-groups-example';
const { effectiveColumns, resetColumnsPinning } = useDataTableColumns<Employee>({
key,
columns: [
{ accessor: 'firstName', noWrap: true, pinnable: true, pinned: 'left' },
{ accessor: 'lastName', noWrap: true, pinnable: true, pinned: 'left' },
{ accessor: 'department.name', title: 'Department', pinnable: true },
{ accessor: 'department.company.name', title: 'Company', noWrap: true, pinnable: true },
{ accessor: 'department.company.city', title: 'City', noWrap: true, pinnable: true },
{ accessor: 'department.company.state', title: 'State', pinnable: true },
{ accessor: 'department.company.streetAddress', title: 'Address', noWrap: true, pinnable: true },
{ accessor: 'department.company.missionStatement', title: 'Mission statement', noWrap: true, pinnable: true },
],
});
// Build groups from effectiveColumns, preserving pinning state
const employeeColumns = effectiveColumns.filter(
(c) => c.accessor === 'firstName' || c.accessor === 'lastName'
);
const workplaceColumns = effectiveColumns.filter(
(c) =>
c.accessor === 'department.name' ||
c.accessor === 'department.company.name' ||
c.accessor === 'department.company.city' ||
c.accessor === 'department.company.state' ||
c.accessor === 'department.company.streetAddress' ||
c.accessor === 'department.company.missionStatement'
);
return (
<Stack gap="xs">
<DataTable
withTableBorder
withColumnBorders
storeColumnsKey={key}
groups={[
{ id: 'employee', title: 'Employee', columns: employeeColumns },
{ id: 'workplace', title: 'Workplace', columns: workplaceColumns },
]}
records={records}
/>
<Group justify="right">
<Button size="xs" variant="light" onClick={resetColumnsPinning}>
Reset pinning
</Button>
</Group>
</Stack>
);
}
// example-end