forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.tsx
More file actions
192 lines (177 loc) · 5.91 KB
/
Copy pathTable.tsx
File metadata and controls
192 lines (177 loc) · 5.91 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
// src/components/ui/Table.tsx
"use client"
// TODO: Add support for CVA
import { useAutoAnimate } from "@formkit/auto-animate/react"
import clsx from "clsx"
import type { CSSProperties, ReactNode } from "react"
import { useCallback, useState } from "react"
import { Card } from "./Card"
type TableSurface = "inset" | "raised" | "flat"
type SortDirection = "asc" | "desc"
interface Column<T> {
key: string
header: string
align?: "left" | "right"
width?: number | string
sortable?: boolean
sortValue?: (item: T) => number | string
render: (item: T, index: number) => ReactNode
}
interface TableProps<T> {
columns: Column<T>[]
data: T[]
keyExtractor: (item: T) => string | number
emptyMessage?: string
surface?: TableSurface
trackerColor?: string
className?: string
onRowClick?: (item: T) => void
rowStyle?: (item: T) => CSSProperties | undefined
defaultSortKey?: string
defaultSortDirection?: SortDirection
fixedLayout?: boolean
compact?: boolean
maxHeight?: number
alwaysShowScrollbar?: boolean
animated?: boolean
noHorizontalScroll?: boolean
}
const thBase = "px-5 py-3 text-xs font-sans font-medium text-secondary uppercase tracking-wider"
const thSortable = "cursor-pointer hover:text-primary transition-colors duration-150 select-none"
const surfaceClasses: Record<TableSurface, string> = {
inset: "nm-inset-sm",
raised: "nm-raised-sm",
flat: "",
}
function Table<T>({
columns,
data,
keyExtractor,
emptyMessage = "No data",
surface = "inset",
trackerColor,
className,
onRowClick,
rowStyle,
defaultSortKey,
defaultSortDirection = "desc",
fixedLayout = false,
compact = false,
maxHeight,
alwaysShowScrollbar = false,
animated = false,
noHorizontalScroll = false,
}: TableProps<T>) {
const [animateRef] = useAutoAnimate({ duration: 250, easing: "ease-out" })
const [sortKey, setSortKey] = useState<string | null>(defaultSortKey ?? null)
const [sortDir, setSortDir] = useState<SortDirection>(defaultSortDirection)
const hasSorting = columns.some((c) => c.sortable)
const handleSort = useCallback(
(key: string) => {
if (key === sortKey) {
setSortDir((d) => (d === "asc" ? "desc" : "asc"))
} else {
setSortKey(key)
setSortDir("desc")
}
},
[sortKey]
)
const sortedData = (() => {
if (!hasSorting || !sortKey) return data
const col = columns.find((c) => c.key === sortKey)
if (!col?.sortValue) return data
const sortFn = col.sortValue
return [...data].sort((a, b) => {
const aVal = sortFn(a)
const bVal = sortFn(b)
const cmp =
typeof aVal === "string" && typeof bVal === "string"
? aVal.localeCompare(bVal)
: (aVal as number) - (bVal as number)
return sortDir === "asc" ? cmp : -cmp
})
})()
return (
<Card
elevation="raised"
trackerColor={trackerColor}
className={clsx(
"p-0! overflow-hidden",
sortedData.length === 0 && "flex flex-col",
className
)}
>
<div
className={clsx(
surfaceClasses[surface],
"overflow-hidden rounded-nm-lg",
!noHorizontalScroll && "overflow-x-auto",
noHorizontalScroll && "pr-1",
maxHeight && "overflow-y-auto overscroll-contain",
maxHeight && (alwaysShowScrollbar ? "styled-scrollbar-visible" : "styled-scrollbar"),
sortedData.length === 0 && "flex-1 flex items-center justify-center"
)}
style={maxHeight ? { maxHeight } : undefined}
>
{sortedData.length > 0 ? (
<table className={clsx("w-full text-left", fixedLayout && "table-fixed")}>
<thead className={maxHeight ? "sticky top-0 z-10 bg-elevated" : undefined}>
<tr className="border-b border-border-emphasis">
{columns.map((col) => (
<th
key={col.key}
className={clsx(
compact
? "px-3 py-2.5 text-xs font-sans font-medium text-secondary uppercase tracking-wider"
: thBase,
col.align === "right" && "text-right",
col.sortable && thSortable
)}
style={col.width ? { width: col.width } : undefined}
onClick={col.sortable ? () => handleSort(col.key) : undefined}
>
{col.header}
{col.sortable && sortKey === col.key && (
<span className="ml-1 text-accent">{sortDir === "asc" ? "▲" : "▼"}</span>
)}
</th>
))}
</tr>
</thead>
<tbody ref={animated ? animateRef : undefined}>
{sortedData.map((item, i) => (
<tr
key={keyExtractor(item)}
className={clsx(
i < sortedData.length - 1 && "border-b border-border",
onRowClick && "cursor-pointer hover:bg-elevated transition-colors duration-150"
)}
style={rowStyle?.(item)}
onClick={onRowClick ? () => onRowClick(item) : undefined}
>
{columns.map((col) => (
<td
key={col.key}
className={clsx(
compact ? "px-3 py-2.5" : "px-5 py-3",
col.align === "right" && "text-right",
fixedLayout && "overflow-hidden"
)}
>
{col.render(item, i)}
</td>
))}
</tr>
))}
</tbody>
</table>
) : (
<p className="text-sm font-mono text-muted py-8">{emptyMessage}</p>
)}
</div>
</Card>
)
}
export type { Column, SortDirection, TableProps, TableSurface }
export { Table }