@passui/tanstack 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -2
  3. package/package.json +65 -5
  4. package/src/auth-shell/auth-shell.tsx +26 -0
  5. package/src/auth-shell/index.ts +1 -0
  6. package/src/document/compose-viewport.tsx +96 -0
  7. package/src/document/index.ts +1 -0
  8. package/src/form-fields/fields.tsx +192 -0
  9. package/src/form-fields/index.ts +2 -0
  10. package/src/form-fields/selection.tsx +149 -0
  11. package/src/form-fields/submit.tsx +40 -0
  12. package/src/forms/contexts.ts +8 -0
  13. package/src/forms/elements/form-alert.tsx +30 -0
  14. package/src/forms/elements/submit.tsx +1 -0
  15. package/src/forms/fields/avatar-picker.tsx +399 -0
  16. package/src/forms/fields/avatar.tsx +299 -0
  17. package/src/forms/fields/checkbox.tsx +1 -0
  18. package/src/forms/fields/index.ts +32 -0
  19. package/src/forms/fields/input.tsx +1 -0
  20. package/src/forms/fields/multiselect.tsx +1 -0
  21. package/src/forms/fields/otp.tsx +1 -0
  22. package/src/forms/fields/radio-card.tsx +1 -0
  23. package/src/forms/fields/switch.tsx +1 -0
  24. package/src/forms/fields/textarea.tsx +1 -0
  25. package/src/forms/index.ts +30 -0
  26. package/src/mutation/index.ts +1 -0
  27. package/src/mutation/use-app-mutation.ts +160 -0
  28. package/src/route-boundary/default-boundary.tsx +61 -0
  29. package/src/route-boundary/default-loader.tsx +19 -0
  30. package/src/route-boundary/index.ts +3 -0
  31. package/src/route-boundary/not-founder.tsx +50 -0
  32. package/src/settings/index.ts +1 -0
  33. package/src/settings/setting-list.tsx +252 -0
  34. package/src/styles.css +12 -0
  35. package/src/tables/columns.tsx +39 -0
  36. package/src/tables/data-table.tsx +617 -0
  37. package/src/tables/filter-row.tsx +260 -0
  38. package/src/tables/index.ts +3 -0
  39. package/src/tables/pagination.tsx +93 -0
  40. package/src/tables/utils.ts +35 -0
@@ -0,0 +1,260 @@
1
+ import { IconButton } from "@passui/react/icon-button"
2
+ import { useMemo } from "react"
3
+ import type { Column, RowData } from "@tanstack/react-table"
4
+ import { HugeiconsIcon } from "@hugeicons/react"
5
+ import { Cancel01Icon } from "@hugeicons/core-free-icons"
6
+ import { Button } from "@passui/react/button"
7
+ import { Toggle } from "@passui/react/toggle"
8
+ import type { DataTableFeatures } from "./data-table"
9
+ import { getColumnLabel } from "./utils"
10
+
11
+ /**
12
+ * The quick-filter checkbox list's own candidate values. For a plain scalar
13
+ * column, one value per distinct cell. For an array-valued column, the
14
+ * individual items across every row's array are what should show up as
15
+ * checkboxes.
16
+ */
17
+ function useQuickFilterValues<TData>(
18
+ data: TData[],
19
+ colId: string,
20
+ exclude: string[],
21
+ isArrayColumn: boolean
22
+ ) {
23
+ return useMemo(() => {
24
+ if (exclude.includes(colId)) return []
25
+ if (isArrayColumn) {
26
+ const flattened = data.flatMap((row) => {
27
+ const value = (
28
+ row as Record<string, unknown>
29
+ )[colId]
30
+ return Array.isArray(value) ? value : []
31
+ })
32
+ const unique = [
33
+ ...new Set(
34
+ flattened.filter(
35
+ (v) =>
36
+ v !== null &&
37
+ v !== undefined &&
38
+ v !== ""
39
+ )
40
+ ),
41
+ ]
42
+ if (unique.length < 2 || unique.length > 20)
43
+ return []
44
+ return unique.map((v) => String(v))
45
+ }
46
+ const raw = data.map(
47
+ (row) => (row as Record<string, unknown>)[colId]
48
+ )
49
+ const unique = [
50
+ ...new Set(
51
+ raw.filter(
52
+ (v) =>
53
+ v !== null &&
54
+ v !== undefined &&
55
+ v !== "" &&
56
+ typeof v !== "object"
57
+ )
58
+ ),
59
+ ]
60
+ if (unique.length < 2 || unique.length > 12) return []
61
+ return unique.map((v) => String(v))
62
+ }, [data, colId, exclude, isArrayColumn])
63
+ }
64
+
65
+ export function FilterRow<TData extends RowData>({
66
+ column,
67
+ data,
68
+ skip,
69
+ isArrayColumn,
70
+ isDateColumn,
71
+ }: {
72
+ column: Column<DataTableFeatures, TData, unknown>
73
+ data: TData[]
74
+ skip: string[]
75
+ isArrayColumn: boolean
76
+ isDateColumn: boolean
77
+ }) {
78
+ const uniqueValues = useQuickFilterValues(
79
+ data,
80
+ column.id,
81
+ skip,
82
+ isArrayColumn
83
+ )
84
+ const label = getColumnLabel(column)
85
+
86
+ // A date column never gets the enumerated-values treatment below —
87
+ // every row realistically has its own distinct timestamp — a from/to
88
+ // range is what's actually useful.
89
+ if (isDateColumn) {
90
+ const [from, to] =
91
+ (column.getFilterValue() as
92
+ | [string?, string?]
93
+ | undefined) ?? []
94
+ return (
95
+ <div className="flex items-center gap-3">
96
+ <span className="w-20 shrink-0 text-[11px] font-medium text-muted-foreground">
97
+ {label}
98
+ </span>
99
+ <div className="flex items-center gap-2">
100
+ <input
101
+ type="date"
102
+ value={from ?? ""}
103
+ onChange={(e) =>
104
+ column.setFilterValue([
105
+ e.target.value || undefined,
106
+ to,
107
+ ])
108
+ }
109
+ className="h-7 rounded-none border border-dashed border-input/40 bg-background px-2 text-xs outline-none"
110
+ />
111
+ <span className="text-[11px] text-muted-foreground">
112
+ to
113
+ </span>
114
+ <input
115
+ type="date"
116
+ value={to ?? ""}
117
+ onChange={(e) =>
118
+ column.setFilterValue([
119
+ from,
120
+ e.target.value || undefined,
121
+ ])
122
+ }
123
+ className="h-7 rounded-none border border-dashed border-input/40 bg-background px-2 text-xs outline-none"
124
+ />
125
+ {(from || to) && (
126
+ <IconButton
127
+ label="Clear filter"
128
+ type="button"
129
+ size="icon-sm"
130
+ onClick={() =>
131
+ column.setFilterValue(
132
+ undefined
133
+ )
134
+ }
135
+ className="text-muted-foreground hover:text-foreground"
136
+ >
137
+ <HugeiconsIcon
138
+ icon={Cancel01Icon}
139
+ className="size-3"
140
+ />
141
+ </IconButton>
142
+ )}
143
+ </div>
144
+ </div>
145
+ )
146
+ }
147
+
148
+ // `activeFilter` is a bare value for a plain scalar column, or a
149
+ // one-element array for an array-valued column (array-contains matching
150
+ // needs an array of values to check for overlap, even when only one is
151
+ // ever picked here).
152
+ const activeFilter = column.getFilterValue() as
153
+ | string
154
+ | string[]
155
+ | undefined
156
+ const activeValue = isArrayColumn
157
+ ? Array.isArray(activeFilter)
158
+ ? activeFilter[0]
159
+ : undefined
160
+ : (activeFilter as string | undefined)
161
+
162
+ if (uniqueValues.length > 0) {
163
+ return (
164
+ <div className="flex flex-col gap-1">
165
+ <span className="text-xs">{label}</span>
166
+ <div className="flex flex-wrap items-center gap-3">
167
+ {uniqueValues.map((value) => {
168
+ const active = activeValue === value
169
+ const setActive = () =>
170
+ column.setFilterValue(
171
+ active
172
+ ? undefined
173
+ : isArrayColumn
174
+ ? [value]
175
+ : value
176
+ )
177
+ return (
178
+ <div
179
+ key={value}
180
+ className="flex cursor-pointer items-center gap-2"
181
+ onClick={setActive}
182
+ >
183
+ <Toggle
184
+ style="checkbox"
185
+ labelHidden
186
+ label={value}
187
+ checked={active}
188
+ onChange={setActive}
189
+ />
190
+ <span className="text-xs">
191
+ {value}
192
+ </span>
193
+ </div>
194
+ )
195
+ })}
196
+ {activeValue && (
197
+ <Button
198
+ type="button"
199
+ variant="ghost"
200
+ size="sm"
201
+ onClick={() =>
202
+ column.setFilterValue(
203
+ undefined
204
+ )
205
+ }
206
+ className="text-muted-foreground hover:text-foreground"
207
+ >
208
+ <HugeiconsIcon
209
+ icon={Cancel01Icon}
210
+ className="size-3"
211
+ />
212
+ Clear
213
+ </Button>
214
+ )}
215
+ </div>
216
+ </div>
217
+ )
218
+ }
219
+
220
+ return (
221
+ <div className="flex items-center gap-3">
222
+ <span className="w-20 shrink-0 text-[11px] font-medium text-muted-foreground capitalize">
223
+ {label}
224
+ </span>
225
+ <div className="relative max-w-xs flex-1">
226
+ <input
227
+ type="text"
228
+ placeholder={`Filter ${label.toLowerCase()}…`}
229
+ value={activeValue ?? ""}
230
+ onChange={(e) =>
231
+ column.setFilterValue(
232
+ e.target.value
233
+ ? isArrayColumn
234
+ ? [e.target.value]
235
+ : e.target.value
236
+ : undefined
237
+ )
238
+ }
239
+ className="h-7 w-full rounded-none border border-dashed border-input/40 bg-background px-2.5 text-xs outline-none placeholder:text-muted-foreground/40"
240
+ />
241
+ {activeValue && (
242
+ <IconButton
243
+ label="Clear filter"
244
+ type="button"
245
+ size="icon-sm"
246
+ onClick={() =>
247
+ column.setFilterValue(undefined)
248
+ }
249
+ className="absolute top-1/2 right-1 -translate-y-1/2 text-muted-foreground hover:text-foreground"
250
+ >
251
+ <HugeiconsIcon
252
+ icon={Cancel01Icon}
253
+ className="size-3"
254
+ />
255
+ </IconButton>
256
+ )}
257
+ </div>
258
+ </div>
259
+ )
260
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./data-table"
2
+ export * from "./pagination"
3
+ export * from "./columns"
@@ -0,0 +1,93 @@
1
+ import { IconButton } from "@passui/react/icon-button"
2
+ import { HugeiconsIcon } from "@hugeicons/react"
3
+ import {
4
+ ChevronFirstIcon,
5
+ ChevronLastIcon,
6
+ ChevronLeftIcon,
7
+ ChevronRightIcon,
8
+ } from "@hugeicons/core-free-icons"
9
+ import type {
10
+ ReactTable,
11
+ RowData,
12
+ } from "@tanstack/react-table"
13
+ import { Picker } from "@passui/react"
14
+ import type { DataTableFeatures } from "./data-table"
15
+
16
+ type DataTablePaginationProps<TData extends RowData> = {
17
+ table: ReactTable<DataTableFeatures, TData>
18
+ pageSizeOptions?: number[]
19
+ }
20
+
21
+ export function DataTablePagination<TData extends RowData>({
22
+ table,
23
+ pageSizeOptions = [10, 20, 30, 50],
24
+ }: DataTablePaginationProps<TData>) {
25
+ const { pageIndex, pageSize } = table.state.pagination
26
+ const pageCount = table.getPageCount()
27
+
28
+ return (
29
+ <div className="flex flex-wrap items-center gap-x-4 gap-y-2">
30
+ <p className="shrink-0 whitespace-nowrap text-footnote text-label-secondary">
31
+ Page {pageCount ? pageIndex + 1 : 0} of{" "}
32
+ {pageCount}
33
+ </p>
34
+
35
+ <div className="flex items-center gap-1">
36
+ <IconButton
37
+ label="First page"
38
+ type="button"
39
+ size="icon-sm"
40
+ disabled={!table.getCanPreviousPage()}
41
+ onClick={() => table.setPageIndex(0)}
42
+ >
43
+ <HugeiconsIcon icon={ChevronFirstIcon} />
44
+ </IconButton>
45
+ <IconButton
46
+ label="Previous page"
47
+ type="button"
48
+ size="icon-sm"
49
+ disabled={!table.getCanPreviousPage()}
50
+ onClick={() => table.previousPage()}
51
+ >
52
+ <HugeiconsIcon icon={ChevronLeftIcon} />
53
+ </IconButton>
54
+ <IconButton
55
+ label="Next page"
56
+ type="button"
57
+ size="icon-sm"
58
+ disabled={!table.getCanNextPage()}
59
+ onClick={() => table.nextPage()}
60
+ >
61
+ <HugeiconsIcon icon={ChevronRightIcon} />
62
+ </IconButton>
63
+ <IconButton
64
+ label="Last page"
65
+ type="button"
66
+ size="icon-sm"
67
+ disabled={!table.getCanNextPage()}
68
+ onClick={() =>
69
+ table.setPageIndex(pageCount - 1)
70
+ }
71
+ >
72
+ <HugeiconsIcon icon={ChevronLastIcon} />
73
+ </IconButton>
74
+ </div>
75
+
76
+ <div className="w-20 shrink-0">
77
+ <Picker
78
+ className="w-full"
79
+ label="Rows per page"
80
+ value={String(pageSize)}
81
+ onChange={(value) =>
82
+ table.setPageSize(Number(value))
83
+ }
84
+ options={pageSizeOptions.map((size) => ({
85
+ value: String(size),
86
+ label: String(size),
87
+ }))}
88
+ searchable={false}
89
+ />
90
+ </div>
91
+ </div>
92
+ )
93
+ }
@@ -0,0 +1,35 @@
1
+ import type { RowData } from "@tanstack/react-table"
2
+ import type { DataTableColumnDef } from "./data-table"
3
+
4
+ /** Whether any row actually holds an array for this column, checked against real data rather than requiring a caller to declare it. */
5
+ export function isArrayValuedColumn<TData>(
6
+ data: TData[],
7
+ colId: string
8
+ ): boolean {
9
+ return data.some((row) =>
10
+ Array.isArray((row as Record<string, unknown>)[colId])
11
+ )
12
+ }
13
+
14
+ export function columnIdOf<TData extends RowData>(
15
+ col: DataTableColumnDef<TData>
16
+ ): string | undefined {
17
+ if ("id" in col && col.id) return col.id
18
+ if (
19
+ "accessorKey" in col &&
20
+ typeof col.accessorKey === "string"
21
+ )
22
+ return col.accessorKey
23
+ return undefined
24
+ }
25
+
26
+ export function getColumnLabel(column: {
27
+ columnDef: { header?: unknown }
28
+ id: string
29
+ }) {
30
+ const header = column.columnDef.header
31
+ if (typeof header === "string") return header
32
+ return column.id
33
+ .replace(/([A-Z])/g, " $1")
34
+ .replace(/^./, (s) => s.toUpperCase())
35
+ }