|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { ActionIcon, Box, Button, DirectionProvider, Group, SegmentedControl, Stack, Text } from '@mantine/core'; |
| 4 | +import { IconColumns3, IconEdit, IconEye, IconTrash } from '@tabler/icons-react'; |
| 5 | +import { DataTable, useDataTableColumns, type DataTableSortStatus } from '__PACKAGE__'; |
| 6 | +import { useMemo, useState } from 'react'; |
| 7 | +import { companies, employees, type Employee } from '~/data'; |
| 8 | + |
| 9 | +// Example 1: Basic table without pinned columns |
| 10 | +export function RTLBasicExample({ direction }: { direction: 'ltr' | 'rtl' }) { |
| 11 | + return ( |
| 12 | + <DirectionProvider key={`basic-${direction}`} initialDirection={direction} detectDirection={false}> |
| 13 | + <Box dir={direction}> |
| 14 | + <DataTable |
| 15 | + withTableBorder |
| 16 | + withColumnBorders |
| 17 | + records={companies.slice(0, 5)} |
| 18 | + columns={[ |
| 19 | + { accessor: 'name', width: 200 }, |
| 20 | + { accessor: 'streetAddress', width: 200 }, |
| 21 | + { accessor: 'city', width: 150 }, |
| 22 | + { accessor: 'state', width: 100 }, |
| 23 | + ]} |
| 24 | + /> |
| 25 | + </Box> |
| 26 | + </DirectionProvider> |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +// Example 2: With column dragging |
| 31 | +export function RTLDraggingExample({ direction }: { direction: 'ltr' | 'rtl' }) { |
| 32 | + const key = 'rtl-dragging-example'; |
| 33 | + |
| 34 | + const { effectiveColumns, resetColumnsOrder } = useDataTableColumns({ |
| 35 | + key, |
| 36 | + columns: [ |
| 37 | + { accessor: 'name', width: 200, draggable: true }, |
| 38 | + { accessor: 'streetAddress', width: 250, draggable: true }, |
| 39 | + { accessor: 'city', width: 150, draggable: true }, |
| 40 | + { accessor: 'state', width: 100 }, |
| 41 | + ], |
| 42 | + }); |
| 43 | + |
| 44 | + return ( |
| 45 | + <DirectionProvider key={`dragging-${direction}`} initialDirection={direction} detectDirection={false}> |
| 46 | + <Box dir={direction}> |
| 47 | + <Stack gap="xs"> |
| 48 | + <Group gap="xs"> |
| 49 | + <Button size="xs" variant="light" leftSection={<IconColumns3 size={16} />} onClick={resetColumnsOrder}> |
| 50 | + Reset order |
| 51 | + </Button> |
| 52 | + </Group> |
| 53 | + <DataTable |
| 54 | + withTableBorder |
| 55 | + withColumnBorders |
| 56 | + storeColumnsKey={key} |
| 57 | + records={companies.slice(0, 8)} |
| 58 | + columns={effectiveColumns} |
| 59 | + /> |
| 60 | + </Stack> |
| 61 | + </Box> |
| 62 | + </DirectionProvider> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +// Example 3: With pinned columns, selection, sorting, pagination, and resizing |
| 67 | +export function RTLFullFeaturedExample({ direction }: { direction: 'ltr' | 'rtl' }) { |
| 68 | + const PAGE_SIZES = [5, 10, 20]; |
| 69 | + const [page, setPage] = useState(1); |
| 70 | + const [recordsPerPage, setRecordsPerPage] = useState(PAGE_SIZES[0]); |
| 71 | + const [selectedRecords, setSelectedRecords] = useState<Employee[]>([]); |
| 72 | + const [sortStatus, setSortStatus] = useState<DataTableSortStatus<Employee>>({ |
| 73 | + columnAccessor: 'firstName', |
| 74 | + direction: 'asc', |
| 75 | + }); |
| 76 | + |
| 77 | + const sortedRecords = useMemo(() => { |
| 78 | + const sorted = [...employees].sort((a, b) => { |
| 79 | + const accessor = sortStatus.columnAccessor as keyof Employee; |
| 80 | + const aValue = a[accessor]; |
| 81 | + const bValue = b[accessor]; |
| 82 | + if (typeof aValue === 'string' && typeof bValue === 'string') { |
| 83 | + return sortStatus.direction === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue); |
| 84 | + } |
| 85 | + return 0; |
| 86 | + }); |
| 87 | + return sorted; |
| 88 | + }, [sortStatus]); |
| 89 | + |
| 90 | + const paginatedRecords = useMemo(() => { |
| 91 | + const start = (page - 1) * recordsPerPage; |
| 92 | + return sortedRecords.slice(start, start + recordsPerPage); |
| 93 | + }, [sortedRecords, page, recordsPerPage]); |
| 94 | + |
| 95 | + return ( |
| 96 | + <DirectionProvider key={`full-${direction}`} initialDirection={direction} detectDirection={false}> |
| 97 | + <Box dir={direction}> |
| 98 | + <DataTable |
| 99 | + withTableBorder |
| 100 | + withColumnBorders |
| 101 | + striped |
| 102 | + highlightOnHover |
| 103 | + pinFirstColumn |
| 104 | + pinLastColumn |
| 105 | + minHeight={300} |
| 106 | + columns={[ |
| 107 | + { |
| 108 | + accessor: 'firstName', |
| 109 | + title: 'First Name', |
| 110 | + sortable: true, |
| 111 | + resizable: true, |
| 112 | + width: 120, |
| 113 | + }, |
| 114 | + { |
| 115 | + accessor: 'lastName', |
| 116 | + title: 'Last Name', |
| 117 | + sortable: true, |
| 118 | + resizable: true, |
| 119 | + width: 120, |
| 120 | + }, |
| 121 | + { |
| 122 | + accessor: 'email', |
| 123 | + sortable: true, |
| 124 | + resizable: true, |
| 125 | + width: 220, |
| 126 | + }, |
| 127 | + { |
| 128 | + accessor: 'department.name', |
| 129 | + title: 'Department', |
| 130 | + resizable: true, |
| 131 | + width: 150, |
| 132 | + }, |
| 133 | + { |
| 134 | + accessor: 'department.company.name', |
| 135 | + title: 'Company', |
| 136 | + resizable: true, |
| 137 | + width: 180, |
| 138 | + }, |
| 139 | + { |
| 140 | + accessor: 'department.company.city', |
| 141 | + title: 'City', |
| 142 | + resizable: true, |
| 143 | + width: 120, |
| 144 | + }, |
| 145 | + { |
| 146 | + accessor: 'actions', |
| 147 | + title: 'Actions', |
| 148 | + width: 100, |
| 149 | + render: () => ( |
| 150 | + <Group gap={4} justify="center" wrap="nowrap"> |
| 151 | + <ActionIcon size="sm" variant="subtle" color="blue"> |
| 152 | + <IconEye size={16} /> |
| 153 | + </ActionIcon> |
| 154 | + <ActionIcon size="sm" variant="subtle" color="green"> |
| 155 | + <IconEdit size={16} /> |
| 156 | + </ActionIcon> |
| 157 | + <ActionIcon size="sm" variant="subtle" color="red"> |
| 158 | + <IconTrash size={16} /> |
| 159 | + </ActionIcon> |
| 160 | + </Group> |
| 161 | + ), |
| 162 | + }, |
| 163 | + ]} |
| 164 | + records={paginatedRecords} |
| 165 | + totalRecords={employees.length} |
| 166 | + recordsPerPage={recordsPerPage} |
| 167 | + onRecordsPerPageChange={setRecordsPerPage} |
| 168 | + recordsPerPageOptions={PAGE_SIZES} |
| 169 | + page={page} |
| 170 | + onPageChange={setPage} |
| 171 | + sortStatus={sortStatus} |
| 172 | + onSortStatusChange={setSortStatus} |
| 173 | + selectedRecords={selectedRecords} |
| 174 | + onSelectedRecordsChange={setSelectedRecords} |
| 175 | + /> |
| 176 | + </Box> |
| 177 | + </DirectionProvider> |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +// Main example component with direction toggle |
| 182 | +export function RTLSupportExample() { |
| 183 | + const [direction, setDirection] = useState<'ltr' | 'rtl'>('ltr'); |
| 184 | + |
| 185 | + return ( |
| 186 | + <Stack gap="xl"> |
| 187 | + <Group justify="center"> |
| 188 | + <Text size="sm" fw={700}> |
| 189 | + Direction: |
| 190 | + </Text> |
| 191 | + <SegmentedControl |
| 192 | + value={direction} |
| 193 | + onChange={(value) => setDirection(value as 'ltr' | 'rtl')} |
| 194 | + data={[ |
| 195 | + { label: 'LTR (Left to Right)', value: 'ltr' }, |
| 196 | + { label: 'RTL (Right to Left)', value: 'rtl' }, |
| 197 | + ]} |
| 198 | + /> |
| 199 | + </Group> |
| 200 | + |
| 201 | + <Stack gap="md"> |
| 202 | + <Text fw={700}>Basic table:</Text> |
| 203 | + <RTLBasicExample direction={direction} /> |
| 204 | + </Stack> |
| 205 | + |
| 206 | + <Stack gap="md"> |
| 207 | + <Text fw={700}>Column dragging:</Text> |
| 208 | + <RTLDraggingExample direction={direction} /> |
| 209 | + </Stack> |
| 210 | + |
| 211 | + <Stack gap="md"> |
| 212 | + <Text fw={700}>Full-featured: pinned columns, selection, sorting, pagination, resizing:</Text> |
| 213 | + <RTLFullFeaturedExample direction={direction} /> |
| 214 | + </Stack> |
| 215 | + </Stack> |
| 216 | + ); |
| 217 | +} |
0 commit comments