forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-table.tsx
152 lines (146 loc) · 4.17 KB
/
data-table.tsx
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
import {
ColumnDef,
Row,
SortingState,
flexRender,
getCoreRowModel,
getSortedRowModel,
useReactTable,
} from '@tanstack/react-table';
import React from 'react';
import { ActivityIndicator, Dimensions, RefreshControl } from 'react-native';
import Animated, { FadeInUp, FadeOutUp } from 'react-native-reanimated';
import { cn } from '~/lib/utils';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
TableRowsFlashListProps,
TableRowsList,
} from './table';
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
onRowPress?: (row: Row<TData>) => void;
estimatedItemSize?: number;
ListEmptyComponent?: TableRowsFlashListProps<TData>['ListEmptyComponent'];
ListFooterComponent?: TableRowsFlashListProps<TData>['ListFooterComponent'];
isRefreshing?: boolean;
onRefresh?: () => void;
}
/**
* @docs https://tanstack.com/table
*/
export function DataTable<TData, TValue>({
columns,
data,
onRowPress,
estimatedItemSize = 45,
ListEmptyComponent,
ListFooterComponent,
isRefreshing = false,
onRefresh,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = React.useState<SortingState>([]);
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
state: {
sorting,
},
});
return (
<>
{isRefreshing && (
<Animated.View
entering={FadeInUp}
exiting={FadeOutUp}
className='h-14 top-16 absolute items-center justify-center w-screen'
>
<ActivityIndicator size='small' className='text-foreground' />
</Animated.View>
)}
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead
key={header.id}
width={getColumnWidth(header.getSize(), columns.length)}
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
<TableRowsList
data={table.getRowModel().rows}
estimatedItemSize={estimatedItemSize}
ListEmptyComponent={ListEmptyComponent}
ListFooterComponent={ListFooterComponent}
refreshControl={
<RefreshControl
refreshing={isRefreshing}
onRefresh={onRefresh}
style={{ opacity: 0 }}
/>
}
renderItem={({ item: row, index }) => {
return (
<TableRow
className={cn(
'active:opacity-70',
index % 2 && 'bg-zinc-100/50 dark:bg-zinc-900/50'
)}
onPress={
onRowPress
? () => {
onRowPress(row);
}
: undefined
}
>
{row.getVisibleCells().map((cell) => (
<TableCell
key={cell.id}
width={getColumnWidth(
cell.column.getSize(),
columns.length
)}
>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
);
}}
/>
</TableBody>
</Table>
</>
);
}
const { width } = Dimensions.get('window');
function getColumnWidth(size: number, length: number) {
const evenWidth = width / length;
return evenWidth > size ? evenWidth : size;
}