next-data-kit 11.0.0 → 12.0.0
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.
- package/MIGRATION.md +58 -0
- package/README.md +227 -126
- package/dist/{chunk-5D747NEB.js → chunk-A4ILIJJR.js} +4 -4
- package/dist/chunk-A4ILIJJR.js.map +1 -0
- package/dist/chunk-BERW2A2B.js +123 -0
- package/dist/chunk-BERW2A2B.js.map +1 -0
- package/dist/index-j8_iNoy6.d.ts +120 -0
- package/dist/index.d.ts +12 -110
- package/dist/index.js +5 -116
- package/dist/index.js.map +1 -1
- package/dist/server/drizzle.d.ts +3 -3
- package/dist/server/drizzle.js +4 -4
- package/dist/server/drizzle.js.map +1 -1
- package/dist/server.d.ts +63 -44
- package/dist/server.js +119 -66
- package/dist/server.js.map +1 -1
- package/dist/{source--l-z9HHB.d.ts → source-BPO-9lNK.d.ts} +16 -6
- package/dist/style.css +1 -0
- package/dist/ui.d.ts +401 -0
- package/dist/ui.js +696 -0
- package/dist/ui.js.map +1 -0
- package/package.json +36 -9
- package/registry/r/data-kit.json +59 -23
- package/registry/r/registry.json +45 -15
- package/dist/chunk-5D747NEB.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# next-data-kit
|
|
2
2
|
|
|
3
|
-
Server-driven data tables for
|
|
3
|
+
Server-driven data tables for Next.js: **one server function, one component.** You declare which columns can be filtered and sorted; the URL holds the table's state; the toolbar builds itself; any database plugs in.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
// ** server
|
|
7
7
|
export const tableUser = dataKit({
|
|
8
|
-
source:
|
|
8
|
+
source: dataKitSourceMongoose({ model: ModelUser }),
|
|
9
9
|
column: {
|
|
10
|
-
name: { filter: 'text'
|
|
11
|
-
role: { filter: 'multiSelect', option: ['admin', 'user'] },
|
|
12
|
-
createdAt: { filter: 'dateRange', preset: ['7d', '30d']
|
|
10
|
+
name: { sort: true, filter: { variant: 'text' } },
|
|
11
|
+
role: { filter: { variant: 'multiSelect', option: ['admin', 'user'] } },
|
|
12
|
+
createdAt: { sort: true, filter: { variant: 'dateRange', preset: ['7d', '30d'] } },
|
|
13
13
|
},
|
|
14
14
|
search: ['name', 'email'],
|
|
15
15
|
row: (props) => ({ id: String(props.row._id), name: props.row.name, role: props.row.role }),
|
|
@@ -18,21 +18,20 @@ export const tableUser = dataKit({
|
|
|
18
18
|
|
|
19
19
|
```tsx
|
|
20
20
|
// ** client
|
|
21
|
-
<DataKit data={data} columns={[{
|
|
21
|
+
<DataKit data={data} columns={[{ id: 'name', header: 'Name' }, { id: 'role', header: 'Role' }]} />
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
- **Secure by default**: only declared columns can be filtered or sorted, values are validated and capped, and the browser never names a database field.
|
|
25
|
-
- **
|
|
26
|
-
- **
|
|
25
|
+
- **Any data source**: Mongoose, Prisma, Drizzle (any SQL dialect), REST and in-memory built in; a new database is one `dataKitSourceDefine` call. Sources work like Refine data providers.
|
|
26
|
+
- **Two ready UIs**: a styled `<DataKit>` from `next-data-kit/ui` (plain HTML and one small stylesheet, no shadcn needed), or a shadcn registry item built from your own components. Both run on the same logic hook.
|
|
27
27
|
- **Server-rendered**: the first page is in the HTML; paging, sorting and filtering change the URL, so reload, back and shared links just work.
|
|
28
|
-
- **
|
|
29
|
-
- **Small**: no runtime dependencies (about 1.3 kB client, 3 kB server, brotli).
|
|
28
|
+
- **No runtime dependencies**: about 1.4 kB client core, 3.3 kB server, 5.9 kB UI and 1.5 kB CSS (brotli).
|
|
30
29
|
|
|
31
30
|
## Contents
|
|
32
31
|
|
|
33
32
|
- [Install](#install)
|
|
34
|
-
- [Quick start](#quick-start)
|
|
35
|
-
- [Guides](#guides): [filters](#filters) · [search](#search) · [sorting](#sorting) · [page size and counting](#page-size-and-counting) · [scope and auth](#scope-and-auth) · [custom filters](#custom-filters) · [the row shape](#the-row-shape) · [selection and bulk actions](#selection-and-bulk-actions) · [row actions](#row-actions) · [your own controls](#your-own-controls) · [your own layout](#your-own-layout) · [infinite scroll](#infinite-scroll) · [several tables on one page](#several-tables-on-one-page) · [exports](#exports) · [translations](#translations) · [
|
|
33
|
+
- [Quick start](#quick-start): [without shadcn](#without-shadcn) · [with shadcn](#with-shadcn)
|
|
34
|
+
- [Guides](#guides): [columns](#columns) · [filters](#filters) · [search](#search) · [sorting](#sorting) · [page size and counting](#page-size-and-counting) · [scope and auth](#scope-and-auth) · [custom filters](#custom-filters) · [the row shape](#the-row-shape) · [selection and bulk actions](#selection-and-bulk-actions) · [row actions](#row-actions) · [your own controls](#your-own-controls) · [your own layout](#your-own-layout) · [infinite scroll](#infinite-scroll) · [several tables on one page](#several-tables-on-one-page) · [exports](#exports) · [translations](#translations) · [theming the default UI](#theming-the-default-ui)
|
|
36
35
|
- [Sources](#sources): [Mongoose](#mongoose) · [Prisma](#prisma) · [Drizzle](#drizzle) · [REST](#rest) · [memory](#memory) · [your own](#your-own-source)
|
|
37
36
|
- [Reference](#reference)
|
|
38
37
|
- [FAQ](#faq)
|
|
@@ -44,36 +43,32 @@ export const tableUser = dataKit({
|
|
|
44
43
|
npm install next-data-kit
|
|
45
44
|
```
|
|
46
45
|
|
|
47
|
-
Peers: `react` 19, `react-dom` 19, `zod` 4; `mongoose` 8–9 or `drizzle-orm` only if you use those sources.
|
|
46
|
+
Peers: `react` 19, `react-dom` 19, `zod` 4; `next` 15+ for the `next-data-kit/ui` entry; `mongoose` 8–9 or `drizzle-orm` only if you use those sources.
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
npx shadcn add @ndk/data-kit
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
That adds `components/ui/ndk/data-kit/` plus any shadcn component it needs that you don't have yet (`table`, `popover`, `command`, `calendar`, …) and `@tanstack/react-table` 8. If shadcn asks to overwrite one of your components, answer **No**. Works with shadcn's Radix and Base UI styles.
|
|
48
|
+
| Entry | What's in it |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `next-data-kit/server` | `dataKit`, `dataKitFilterCustom`, the `dataKitSource*` sources, `dataKitUrlDecode` |
|
|
51
|
+
| `next-data-kit/server/drizzle` | `dataKitSourceDrizzle` (needs `drizzle-orm`) |
|
|
52
|
+
| `next-data-kit` | `useDataKit` (the state hook, framework-free), `dataKitUrlEncode`, types |
|
|
53
|
+
| `next-data-kit/ui` | `useDataKitTable` (all the view logic) and the ready UI: `DataKit`, `DataKitTable`, `DataKitToolbar`, `DataKitPagination`, `DataKitInfinite`, `DataKitActionBar`, labels |
|
|
54
|
+
| `next-data-kit/style.css` | the default UI's stylesheet |
|
|
60
55
|
|
|
61
56
|
## Quick start
|
|
62
57
|
|
|
63
58
|
**1. Declare the table** once, on the server (`controllers/user.ts`):
|
|
64
59
|
|
|
65
60
|
```ts
|
|
66
|
-
import { dataKit,
|
|
61
|
+
import { dataKit, dataKitSourceMongoose } from 'next-data-kit/server';
|
|
67
62
|
|
|
68
63
|
export const tableUser = dataKit({
|
|
69
|
-
source:
|
|
64
|
+
source: dataKitSourceMongoose({ model: ModelUser }),
|
|
70
65
|
column: {
|
|
71
|
-
name: { filter: 'text'
|
|
66
|
+
name: { sort: true, filter: { variant: 'text' } },
|
|
72
67
|
email: { sort: true },
|
|
73
|
-
role: { filter: 'multiSelect', option: ['admin', 'user', 'guest'] },
|
|
74
|
-
age: { filter: 'range'
|
|
75
|
-
active: { filter: 'boolean' },
|
|
76
|
-
createdAt: { filter: 'dateRange', preset: ['7d', '30d']
|
|
68
|
+
role: { filter: { variant: 'multiSelect', option: ['admin', 'user', 'guest'] } },
|
|
69
|
+
age: { sort: true, filter: { variant: 'range' } },
|
|
70
|
+
active: { filter: { variant: 'boolean' } },
|
|
71
|
+
createdAt: { sort: true, filter: { variant: 'dateRange', preset: ['7d', '30d'] } },
|
|
77
72
|
},
|
|
78
73
|
search: ['name', 'email'],
|
|
79
74
|
sort: 'createdAt.desc',
|
|
@@ -89,50 +84,114 @@ const Page = async (props: { searchParams: Promise<Record<string, string | strin
|
|
|
89
84
|
export default Page;
|
|
90
85
|
```
|
|
91
86
|
|
|
92
|
-
**3. Render it** (client component)
|
|
87
|
+
**3. Render it** (client component), with either UI.
|
|
88
|
+
|
|
89
|
+
### Without shadcn
|
|
90
|
+
|
|
91
|
+
Import the stylesheet once. With Tailwind, put it in your global CSS inside the `components` layer, so your utility classes still win over it:
|
|
92
|
+
|
|
93
|
+
```css
|
|
94
|
+
@import 'tailwindcss';
|
|
95
|
+
@import 'next-data-kit/style.css' layer(components);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Without Tailwind, import it in your root layout: `import 'next-data-kit/style.css';`.
|
|
93
99
|
|
|
94
100
|
```tsx
|
|
95
101
|
'use client';
|
|
96
102
|
|
|
97
|
-
import type {
|
|
98
|
-
import
|
|
99
|
-
import { DataKit } from '@/components/ui/ndk/data-kit/data-kit';
|
|
103
|
+
import type { TDataKitTableData } from 'next-data-kit';
|
|
104
|
+
import { DataKit } from 'next-data-kit/ui';
|
|
100
105
|
import type { tableUser } from '@/controllers/user';
|
|
101
106
|
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
export const ViewUser = (props: { data: TDataKitTableData<typeof tableUser> }) => (
|
|
108
|
+
<DataKit
|
|
109
|
+
data={props.data}
|
|
110
|
+
columns={[
|
|
111
|
+
{ id: 'name', header: 'Name' },
|
|
112
|
+
{ id: 'email', header: 'Email' },
|
|
113
|
+
{ id: 'role', header: 'Role', filter: { option: { admin: 'Admin', user: 'User', guest: 'Guest' } } },
|
|
114
|
+
]}
|
|
115
|
+
pagination={{ size: [10, 20, 50] }}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### With shadcn
|
|
121
|
+
|
|
122
|
+
Add the registry to `components.json`, then the components:
|
|
123
|
+
|
|
124
|
+
```json
|
|
125
|
+
{ "registries": { "@ndk": "https://unpkg.com/next-data-kit@12/registry/r/{name}.json" } }
|
|
126
|
+
```
|
|
107
127
|
|
|
108
|
-
|
|
128
|
+
```bash
|
|
129
|
+
npx shadcn add @ndk/data-kit
|
|
109
130
|
```
|
|
110
131
|
|
|
111
|
-
That
|
|
132
|
+
That adds `components/ui/ndk/data-kit/` (markup only, from your `table`, `popover`, `command`, `calendar`, … components, installing any you don't have yet). If shadcn asks to overwrite one of your own components, answer **No**. It works with shadcn's Radix and Base UI styles. The view is the same as above, importing `DataKit` from `@/components/ui/ndk/data-kit` instead of `next-data-kit/ui`.
|
|
133
|
+
|
|
134
|
+
Either way that's the whole table: a search box and one filter per declared column, sortable headers (`aria-sort`), pagination with real links and page sizes.
|
|
112
135
|
|
|
113
136
|
## Guides
|
|
114
137
|
|
|
138
|
+
### Columns
|
|
139
|
+
|
|
140
|
+
Columns are plain objects, typed from `data` (no generics to write):
|
|
141
|
+
|
|
142
|
+
```tsx
|
|
143
|
+
columns={[
|
|
144
|
+
{ id: 'name', header: 'Name' }, // ** shows row.name
|
|
145
|
+
{ id: 'role', header: 'Role', cell: ({ row }) => <Badge>{row.role}</Badge> },
|
|
146
|
+
{ id: 'age', header: 'Age', className: 'text-right tabular-nums' }, // ** on the header and the cells
|
|
147
|
+
{ id: 'status', table: false, filter: { label: 'Status', option: { online: 'Online' } } }, // ** names a filter, no table column
|
|
148
|
+
]}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- `id` is the server column key: that's how a header knows it can sort and a filter finds its label. A column without `cell` shows the row's field with the same id as text.
|
|
152
|
+
- `filter.label` names the column's filter in the toolbar (it falls back to `header` when that's a string); `filter.option` names its option values.
|
|
153
|
+
- Columns hold functions, so write them in a client component.
|
|
154
|
+
|
|
115
155
|
### Filters
|
|
116
156
|
|
|
117
|
-
Each column's `filter
|
|
157
|
+
Each column's `filter.variant` fixes the operator, the URL encoding and the toolbar control, so the client never chooses how to query:
|
|
118
158
|
|
|
119
159
|
| Variant | URL | Condition | Toolbar |
|
|
120
160
|
|---|---|---|---|
|
|
121
161
|
| `text` | `?name=ada` | contains (case-insensitive) | input |
|
|
122
162
|
| `number` | `?age=30` | equals | number input |
|
|
123
|
-
| `
|
|
163
|
+
| `numberCompare` | `?stock=gt.5` | the picked operator (`eq ne gt gte lt lte`) | operator + number |
|
|
164
|
+
| `range` | `?age=18-30`, `?age=18-`, `?age=-30`, `?temp=-5--2` | between / ≥ / ≤ | min–max inputs |
|
|
165
|
+
| `slider` | `?price=20-450` | between / ≥ / ≤, inside `bound` | two-thumb slider + min–max |
|
|
124
166
|
| `select` | `?role=admin` | equals one of `option` | single-choice list |
|
|
125
167
|
| `multiSelect` | `?role=admin,user` | in `option` | multi-choice list |
|
|
168
|
+
| `tags` | `?sku=A-1,B-2` | in the values (or contains any) | chip input |
|
|
126
169
|
| `boolean` | `?active=true` | equals | yes / no |
|
|
127
|
-
| `
|
|
128
|
-
| `
|
|
170
|
+
| `exists` | `?note=true` | has a value / is empty | has value / is empty |
|
|
171
|
+
| `date` | `?createdAt=<ms>-<ms>` | between (that day) | date picker |
|
|
172
|
+
| `dateRange` | `?createdAt=<ms>-<ms>` or `?createdAt=7d` | between, or since a `preset` | presets + from / to |
|
|
173
|
+
| `dateTimeRange` | `?at=<ms>-<ms>` or `?at=24h` | between, or since a `preset` | presets + from / to with times |
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
column: {
|
|
177
|
+
price: { sort: true, filter: { variant: 'slider', bound: { min: 0, max: 500, step: 5 } } }, // ** bound can be async, per request
|
|
178
|
+
stock: { filter: { variant: 'numberCompare', compare: ['gt', 'lte'] } }, // ** compare: the operators offered (default all six)
|
|
179
|
+
sku: { filter: { variant: 'tags', match: 'contains', schema: z.string().max(20) } }, // ** match: 'exact' (default) or 'contains'; schema checks each value
|
|
180
|
+
note: { filter: { variant: 'exists' } },
|
|
181
|
+
updatedAt: { filter: { variant: 'dateTimeRange', preset: ['1h', '24h', '7d'] } },
|
|
182
|
+
}
|
|
183
|
+
```
|
|
129
184
|
|
|
130
|
-
- Dates are sent as the browser's own day boundaries, so the server never guesses a time zone. `preset: ['7d', '24h']` stays relative in a shared link.
|
|
131
|
-
- `option` can be a function, resolved per request, e.g. ids from another table: `option: async () => (await ModelCollection.find()).map((row) => row.id)`. Name the values on the client with
|
|
132
|
-
- A
|
|
133
|
-
-
|
|
134
|
-
-
|
|
135
|
-
-
|
|
185
|
+
- Dates are sent as the browser's own day (or minute) boundaries, so the server never guesses a time zone; the datetime inputs show which zone they're in. `preset: ['7d', '24h']` stays relative in a shared link. A reversed range is dropped.
|
|
186
|
+
- `option` can be a function, resolved per request, e.g. ids from another table: `option: async () => (await ModelCollection.find()).map((row) => row.id)`. Name the values on the client with the column's `filter.option`.
|
|
187
|
+
- A slider snaps to its `step` and stays inside its `bound`; dragging back to an end leaves that side open, and the full range means no filter. It applies once you let go.
|
|
188
|
+
- Tags: Enter or a comma adds one, a pasted list adds them all, Backspace in the empty field removes the last. A value `schema` rejects is dropped.
|
|
189
|
+
- A different database field than the key: `username: { field: 'username.data', filter: { variant: 'text' } }`. Two filters can share one field (`cost: { field: 'price', filter: { variant: 'numberCompare' } }`).
|
|
190
|
+
- Filterable from the URL but not shown in the toolbar (a link from another page): `filter: { variant: 'text', hidden: true }`.
|
|
191
|
+
- Invalid values are dropped as if unset; text is capped at 200 characters and lists at 100 values. A misconfigured variant (a slider without `bound`, `compare` on a text filter, …) throws when the table is defined.
|
|
192
|
+
- In the toolbar, an active filter moves to the front, gets a solid border and shows its value as a badge; its ⓧ clears just that filter, and **Reset** clears them all. While you're using a filter the order holds, so its popover stays open and focus stays put.
|
|
193
|
+
- Typed values apply after a short pause (or Enter / blur), lists on click, sliders on release, dates on change. No Apply button is needed.
|
|
194
|
+
- Not built in yet: a select that searches the server as you type (for thousands of options), and counts next to options. Use `dataKitFilterCustom` with your own control in `toolbar.control` meanwhile.
|
|
136
195
|
|
|
137
196
|
### Search
|
|
138
197
|
|
|
@@ -140,11 +199,11 @@ Each column's `filter` is a variant. The variant fixes the operator, the URL enc
|
|
|
140
199
|
|
|
141
200
|
### Sorting
|
|
142
201
|
|
|
143
|
-
Columns with `sort: true` get a sortable header (ascending → descending → off). `sort: 'createdAt.desc'` (or a list) is the default
|
|
202
|
+
Columns with `sort: true` get a sortable header (ascending → descending → off; the default column toggles between its two directions). `sort: 'createdAt.desc'` (or a list) is the default, and anything else in `?sort=` falls back to it. Sources add a unique tie-break, so equal values keep a stable order across pages.
|
|
144
203
|
|
|
145
204
|
### Page size and counting
|
|
146
205
|
|
|
147
|
-
`page: { size: 20 }` (the max defaults to 100) or `page: { size: { default: 25, max: 25 } }`. `<DataKit
|
|
206
|
+
`page: { size: 20 }` (the max defaults to 100) or `page: { size: { default: 25, max: 25 } }`. `<DataKit pagination={{ size: [10, 25, 50] }} />` adds a rows-per-page select. A page past the end shows the last one.
|
|
148
207
|
|
|
149
208
|
Counting every match costs a query. `page: { count: 'none' }` skips it: `total` is `null` and pagination shows previous / next only.
|
|
150
209
|
|
|
@@ -165,19 +224,19 @@ It returns field → value pairs (each an equality) or a list of conditions.
|
|
|
165
224
|
When a variant's condition isn't the one you need, keep the variant (it still drives the toolbar and validation) and give it a `query` that returns your source's native condition:
|
|
166
225
|
|
|
167
226
|
```ts
|
|
168
|
-
type: { filter: 'select', option: ['user', 'admin'], query: (props) => (props.value === 'admin' ? { type: 'admin' } : { type: { $ne: 'admin' } }) },
|
|
169
|
-
status: { filter: 'select', option: ['online'], query: async () => ({ _id: { $in: await ModelSession.distinct('userId', { expiresAt: { $gt: new Date() } }) } }) },
|
|
227
|
+
type: { filter: { variant: 'select', option: ['user', 'admin'], query: (props) => (props.value === 'admin' ? { type: 'admin' } : { type: { $ne: 'admin' } }) } },
|
|
228
|
+
status: { filter: { variant: 'select', option: ['online'], query: async () => ({ _id: { $in: await ModelSession.distinct('userId', { expiresAt: { $gt: new Date() } }) } }) } },
|
|
170
229
|
```
|
|
171
230
|
|
|
172
|
-
For a filter with no toolbar control at all, `
|
|
231
|
+
For a filter with no toolbar control at all, `dataKitFilterCustom` takes a zod schema and types `value` from it:
|
|
173
232
|
|
|
174
233
|
```ts
|
|
175
|
-
user: { filter:
|
|
234
|
+
user: { filter: dataKitFilterCustom({ schema: z.string().regex(/^[a-f\d]{24}$/i), query: (props) => ({ userId: props.value }) }) },
|
|
176
235
|
```
|
|
177
236
|
|
|
178
237
|
### The row shape
|
|
179
238
|
|
|
180
|
-
`row` maps one database row to what the browser gets: plain JSON with a string `id`, nothing secret. Load less with `select` on the source (`
|
|
239
|
+
`row` maps one database row to what the browser gets: plain JSON with a string `id`, nothing secret. Load less with `select` on the source (`dataKitSourceMongoose({ model, select: '-password' })`). In development a warning shows when a row has no string `id` or carries an `ObjectId`. Reuse the types with `TDataKitTableRow<typeof tableUser>` and `TDataKitTableData<typeof tableUser>`.
|
|
181
240
|
|
|
182
241
|
### Selection and bulk actions
|
|
183
242
|
|
|
@@ -185,43 +244,52 @@ user: { filter: filterCustom({ schema: z.string().regex(/^[a-f\d]{24}$/i), query
|
|
|
185
244
|
<DataKit
|
|
186
245
|
data={data}
|
|
187
246
|
columns={columns}
|
|
188
|
-
|
|
189
|
-
|
|
247
|
+
selection={{
|
|
248
|
+
enabled: ({ row }) => row.id !== currentUserId, // ** optional: which rows can be picked
|
|
249
|
+
action: (selection) => <Button onClick={() => aUsersDelete({ ids: selection.rows.map((row) => row.id) }).then(selection.clear)}>Delete</Button>,
|
|
250
|
+
}}
|
|
190
251
|
/>
|
|
191
252
|
```
|
|
192
253
|
|
|
193
|
-
While rows are selected, an action bar floats at the bottom of the screen (so it's in reach however far you scroll) with the count, a clear button and your actions. It's a labelled `role="toolbar"`
|
|
254
|
+
While rows are selected, an action bar floats at the bottom of the screen (so it's in reach however far you scroll) with the count, a clear button and your actions. It's a labelled `role="toolbar"` in a portal, the count is announced to screen readers, and **Escape** clears the selection. The header checkbox only picks rows `enabled` allows. Rows that disappear (deleted, filtered out) leave the selection on their own. Two tables' bars stack instead of overlapping.
|
|
194
255
|
|
|
195
256
|
### Row actions
|
|
196
257
|
|
|
197
|
-
|
|
258
|
+
Put a dropdown in a cell. After a server action changes data, `revalidatePath` (or `router.refresh()`) re-renders the page and the table takes the new `data`. `row={{ onClick: ({ row }) => open(row) }}` makes whole rows clickable; stop propagation in cells that hold their own buttons.
|
|
198
259
|
|
|
199
260
|
### Your own controls
|
|
200
261
|
|
|
201
|
-
`
|
|
262
|
+
`toolbar={{ control }}` adds anything to the end of the toolbar; a falsy value renders nothing:
|
|
202
263
|
|
|
203
264
|
```tsx
|
|
204
|
-
<DataKit data={data} columns={columns}
|
|
265
|
+
<DataKit data={data} columns={columns} toolbar={{ control: data.filter.user && <Link href="?">One user only ✕</Link> }} />
|
|
205
266
|
```
|
|
206
267
|
|
|
207
268
|
`toolbar={false}` and `pagination={false}` hide those parts (e.g. a "latest 5" card).
|
|
208
269
|
|
|
209
270
|
### Your own layout
|
|
210
271
|
|
|
211
|
-
`useDataKitTable`
|
|
272
|
+
`useDataKitTable` (from `next-data-kit/ui`) returns the table ready to render; every part takes it:
|
|
212
273
|
|
|
213
274
|
```tsx
|
|
214
275
|
const table = useDataKitTable({ data, columns });
|
|
215
276
|
return (
|
|
216
277
|
<Fragment>
|
|
217
278
|
<DataKitToolbar table={table} />
|
|
218
|
-
<div className="grid grid-cols-3 gap-4">{table.
|
|
279
|
+
<div className="grid grid-cols-3 gap-4">{table.row.map((item) => <Card key={item.id} user={item.row} />)}</div>
|
|
219
280
|
<DataKitPagination table={table} />
|
|
220
281
|
</Fragment>
|
|
221
282
|
);
|
|
222
283
|
```
|
|
223
284
|
|
|
224
|
-
|
|
285
|
+
| `table.` | |
|
|
286
|
+
|---|---|
|
|
287
|
+
| `header` | `{ id, content, className, sort: { can, direction, aria, toggle } }[]` |
|
|
288
|
+
| `row` | `{ id, row, selected, disabled, toggle, cell: { id, content, className }[] }[]` |
|
|
289
|
+
| `filter` | `list` (`{ id, variant, label, option, preset, state, set, clear }[]`, active first), `active`, `reset` |
|
|
290
|
+
| `selection` | `active` (rows can be picked at all), `rows`, `count`, `clear`, `page: { checked, indeterminate, toggle }` |
|
|
291
|
+
| `page` | `number`, `count`, `next`, `total`, `size: { value, set }`, `list` (`[1, 'gap', 4, 5, 6, 'gap', 20]`), `link(page, enabled?)` (real link props) |
|
|
292
|
+
| `kit` | `useDataKit`'s state: `set.filter`, `set.sort`, `reset`, `refresh`, `row.update`, … |
|
|
225
293
|
|
|
226
294
|
### Infinite scroll
|
|
227
295
|
|
|
@@ -233,8 +301,8 @@ export const aUserList = async (input: unknown) => tableUser.query({ input, cont
|
|
|
233
301
|
```
|
|
234
302
|
|
|
235
303
|
```tsx
|
|
236
|
-
const table = useDataKitTable({ data,
|
|
237
|
-
// ** …render table.
|
|
304
|
+
const table = useDataKitTable({ data, source: { fetch: aUserList }, append: true });
|
|
305
|
+
// ** …render table.row, then:
|
|
238
306
|
<DataKitInfinite table={table} />
|
|
239
307
|
```
|
|
240
308
|
|
|
@@ -242,14 +310,18 @@ Answers that arrive out of order are dropped. `source: { fetch, url: true }` kee
|
|
|
242
310
|
|
|
243
311
|
### Several tables on one page
|
|
244
312
|
|
|
313
|
+
Give each `query` its own `prefix`. It comes back in the data, so the views don't repeat it:
|
|
314
|
+
|
|
245
315
|
```tsx
|
|
246
|
-
const users = await tableUser.query({ searchParams,
|
|
247
|
-
const logs = await tableLog.query({ searchParams,
|
|
248
|
-
// **
|
|
249
|
-
<DataKit data={users}
|
|
250
|
-
<DataKit data={logs}
|
|
316
|
+
const users = await tableUser.query({ searchParams, prefix: 'user' });
|
|
317
|
+
const logs = await tableLog.query({ searchParams, prefix: 'log' });
|
|
318
|
+
// ** ?user.page=2&user.role=admin&log.sort=createdAt.desc
|
|
319
|
+
<DataKit data={users} columns={…} />
|
|
320
|
+
<DataKit data={logs} columns={…} />
|
|
251
321
|
```
|
|
252
322
|
|
|
323
|
+
Each table only reads and writes its own params and keeps everything else in the URL. A prefix uses `a-z`, `0-9` and `-`; a column key can't contain `.`. In development, two tables on one page with the same prefix (or none) log a warning.
|
|
324
|
+
|
|
253
325
|
### Exports
|
|
254
326
|
|
|
255
327
|
Reuse the definition with a server-only page size (it ignores `max`):
|
|
@@ -266,14 +338,34 @@ export const aUsersExport = action.schema(tableUser.schema).action(async ({ pars
|
|
|
266
338
|
Set every label once, near the root (for example inside your i18n provider):
|
|
267
339
|
|
|
268
340
|
```tsx
|
|
269
|
-
<DataKitLabelsProvider
|
|
341
|
+
<DataKitLabelsProvider
|
|
342
|
+
labels={{
|
|
343
|
+
pagination: { previous: t('previous'), next: t('next') },
|
|
344
|
+
filter: { reset: t('reset') },
|
|
345
|
+
date: { preset: (value) => t('lastDays', { days: value.slice(0, -1) }), format: (date) => format.dateTime(date, { dateStyle: 'medium' }) },
|
|
346
|
+
}}
|
|
347
|
+
>
|
|
348
|
+
{children}
|
|
349
|
+
</DataKitLabelsProvider>
|
|
270
350
|
```
|
|
271
351
|
|
|
272
|
-
`<DataKit labels={…}>` overrides them for one table, e.g. `pagination.summary` or `table.empty`. Column and option names come from your columns (`header`, `
|
|
352
|
+
`<DataKit labels={…}>` overrides them for one table, e.g. `pagination.summary` or `table.empty`. Column and option names come from your columns (`header`, `filter.label`, `filter.option`). Pass your i18n formatter as `date.format` (and `date.formatTime` / `date.formatClock` for date-and-time filters), so the server and the browser print a picked date the same way. The other new strings are `compare` (each operator's symbol and spoken name), `exists`, `tags` and `date.zone`. The provider and `dataKitLabels` (the English defaults) come from `next-data-kit/ui` and serve both UIs.
|
|
273
353
|
|
|
274
|
-
###
|
|
354
|
+
### Theming the default UI
|
|
275
355
|
|
|
276
|
-
|
|
356
|
+
Every part has a `data-slot` (`data-kit`, `data-kit-toolbar`, `data-kit-filter`, `data-kit-table`, `data-kit-row`, `data-kit-pagination`, `data-kit-action-bar`, …) and a `data-state` where it has one. The stylesheet only uses `:where()` selectors in its own `ndk` layer, so any style of yours wins. Colors come from variables:
|
|
357
|
+
|
|
358
|
+
```css
|
|
359
|
+
[data-slot='data-kit'],
|
|
360
|
+
[data-slot='data-kit-action-bar'] {
|
|
361
|
+
--ndk-background: #fff;
|
|
362
|
+
--ndk-foreground: #111;
|
|
363
|
+
--ndk-border: #e5e5e5;
|
|
364
|
+
--ndk-radius: 0.375rem;
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
The defaults are system colors that follow the page's `color-scheme`, so dark mode works with no extra CSS. `.dark` or `[data-theme="dark"]` on any ancestor (next-themes) also switches it.
|
|
277
369
|
|
|
278
370
|
## Sources
|
|
279
371
|
|
|
@@ -282,9 +374,9 @@ A source is the only part that talks to your data. Every source receives the sam
|
|
|
282
374
|
### Mongoose
|
|
283
375
|
|
|
284
376
|
```ts
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
377
|
+
dataKitSourceMongoose({ model: ModelUser }); // ** .lean() plain objects
|
|
378
|
+
dataKitSourceMongoose({ model: ModelUser, hydrate: true }); // ** documents with methods (row: (props) => props.row.onClient())
|
|
379
|
+
dataKitSourceMongoose({ model: ModelUser, select: '-password' });
|
|
288
380
|
```
|
|
289
381
|
|
|
290
382
|
Imports mongoose for types only, so it always uses your app's mongoose. `contains` is an escaped, case-insensitive regex.
|
|
@@ -292,8 +384,8 @@ Imports mongoose for types only, so it always uses your app's mongoose. `contain
|
|
|
292
384
|
### Prisma
|
|
293
385
|
|
|
294
386
|
```ts
|
|
295
|
-
|
|
296
|
-
|
|
387
|
+
dataKitSourcePrisma({ model: prisma.user });
|
|
388
|
+
dataKitSourcePrisma({ model: prisma.user, select: { id: true, name: true }, insensitive: false }); // ** insensitive: false on MySQL / SQLite
|
|
297
389
|
```
|
|
298
390
|
|
|
299
391
|
Any model delegate; Prisma isn't imported. Nested fields (`author.name`) become relation filters.
|
|
@@ -301,10 +393,10 @@ Any model delegate; Prisma isn't imported. Nested fields (`author.name`) become
|
|
|
301
393
|
### Drizzle
|
|
302
394
|
|
|
303
395
|
```ts
|
|
304
|
-
import {
|
|
396
|
+
import { dataKitSourceDrizzle } from 'next-data-kit/server/drizzle';
|
|
305
397
|
|
|
306
|
-
|
|
307
|
-
|
|
398
|
+
dataKitSourceDrizzle({ db, table: users }); // ** Postgres (ilike)
|
|
399
|
+
dataKitSourceDrizzle({ db, table: users, dialect: 'sqlite' }); // ** or 'mysql' (like)
|
|
308
400
|
```
|
|
309
401
|
|
|
310
402
|
Fields resolve to the table's own columns (an unknown field throws), values go as parameters, and LIKE wildcards are escaped. Tested on real SQL (PGlite).
|
|
@@ -312,24 +404,24 @@ Fields resolve to the table's own columns (an unknown field throws), values go a
|
|
|
312
404
|
### REST
|
|
313
405
|
|
|
314
406
|
```ts
|
|
315
|
-
|
|
407
|
+
dataKitSourceRest<TRepo>({ url: 'https://api.example.com/repos', request: { headers: { authorization: `Bearer ${token}` } } });
|
|
316
408
|
```
|
|
317
409
|
|
|
318
410
|
Speaks json-server / Refine simple-rest params (`_start`, `_end`, `_sort`, `_order`, `field_gte`, `field_like`, `q`) and reads `X-Total-Count`. Use `response: (props) => ({ rows, total })` for another body shape.
|
|
319
411
|
|
|
320
412
|
### Memory
|
|
321
413
|
|
|
322
|
-
`
|
|
414
|
+
`dataKitSourceMemory({ rows })`, for tests, fixtures and small static lists.
|
|
323
415
|
|
|
324
416
|
### Your own source
|
|
325
417
|
|
|
326
418
|
Map the 11 operators to your query language once; `list` runs the query:
|
|
327
419
|
|
|
328
420
|
```ts
|
|
329
|
-
import {
|
|
421
|
+
import { dataKitSourceDefine } from 'next-data-kit/server';
|
|
330
422
|
|
|
331
|
-
export const
|
|
332
|
-
|
|
423
|
+
export const dataKitSourceKysely = <TRow>(props: { db: Kysely<TDatabase>; table: keyof TDatabase }) =>
|
|
424
|
+
dataKitSourceDefine<TRow, Expression<SqlBool>>({
|
|
333
425
|
operator: {
|
|
334
426
|
eq: (props) => sql`${sql.ref(props.field)} = ${props.value}`,
|
|
335
427
|
contains: (props) => sql`${sql.ref(props.field)} ilike ${`%${String(props.value).replace(/[\\%_]/g, '\\$&')}%`}`,
|
|
@@ -346,7 +438,7 @@ export const sourceKysely = <TRow>(props: { db: Kysely<TDatabase>; table: keyof
|
|
|
346
438
|
});
|
|
347
439
|
```
|
|
348
440
|
|
|
349
|
-
To keep a built-in translation but run your own query (an aggregate, a join), spread its operators: `
|
|
441
|
+
To keep a built-in translation but run your own query (an aggregate, a join), spread its operators: `dataKitSourceDefine({ ...dataKitSourceMongooseQuery, list: async (props) => … })`. `props.where` is the already-built Mongo filter, and `props.sort` lists the fields, so a computed sort (e.g. by a `$lookup` count) is yours to handle.
|
|
350
442
|
|
|
351
443
|
Rules for any source: combine with **and**, keep values as parameters (never in a query string), map fields through known columns, return the total of all matches (or `null` when `page.count` is false), and add a unique tie-break to the sort.
|
|
352
444
|
|
|
@@ -356,30 +448,32 @@ Rules for any source: combine with **and**, keep values as parameters (never in
|
|
|
356
448
|
|
|
357
449
|
| Option | Default | |
|
|
358
450
|
|---|---|---|
|
|
359
|
-
| `source` | required | A built-in source, `
|
|
360
|
-
| `column` | required | `{ [key]: { field?,
|
|
451
|
+
| `source` | required | A built-in source, `dataKitSourceDefine(…)` or `{ list }`. |
|
|
452
|
+
| `column` | required | `{ [key]: { field?, sort?, filter?: { variant, option?, preset?, query?, hidden? } \| dataKitFilterCustom(…) } }` |
|
|
361
453
|
| `search` | none | Column keys the search box looks in. |
|
|
362
454
|
| `scope` | none | `(props: { context }) => pairs \| conditions`, always applied. |
|
|
363
455
|
| `sort` | none | `'field.direction'` or a list. |
|
|
364
456
|
| `page` | `{ size: 20 }` | `size`: number or `{ default, max }`; `count: 'exact' \| 'none'`. |
|
|
365
457
|
| `row` | required | `(props: { row }) => row` with a string `id`. |
|
|
366
458
|
|
|
367
|
-
`tableX.query({ searchParams,
|
|
459
|
+
`tableX.query({ searchParams, prefix?, context?, page? })` or `({ input, prefix?, context?, page? })` returns `{ rows, total, page, sort, filter, search, prefix, meta }`. `tableX.schema` validates an action's input. Also exported: `dataKitFilterCustom`, `dataKitSourceDefine`, `dataKitSourceMongoose`, `dataKitSourceMongooseQuery`, `dataKitSourcePrisma`, `dataKitSourceRest`, `dataKitSourceMemory`, `dataKitUrlEncode`, `dataKitUrlDecode`, and the `TDataKit*` types; `dataKitSourceDrizzle` from `next-data-kit/server/drizzle`.
|
|
460
|
+
|
|
461
|
+
### UI · `next-data-kit/ui` and `components/ui/ndk/data-kit/`
|
|
368
462
|
|
|
369
|
-
|
|
463
|
+
Both UIs export the same components with the same props.
|
|
370
464
|
|
|
371
465
|
| | Props |
|
|
372
466
|
|---|---|
|
|
373
|
-
| `DataKit` | `data`, `columns`, `source?`, `
|
|
374
|
-
| `useDataKitTable` | `data`, `columns
|
|
375
|
-
| `DataKitToolbar` · `
|
|
376
|
-
| `DataKitLabelsProvider` | `labels`, `children` |
|
|
467
|
+
| `DataKit` | `data`, `columns`, `source?`, `refetch?: { intervalMs }`, `selection?: { enabled?, action? }`, `row?: { onClick }`, `toolbar?: boolean \| { control }`, `pagination?: boolean \| { size }`, `labels?`, `className?` |
|
|
468
|
+
| `useDataKitTable` | `data`, `columns?`, `source?`, `append?`, `refetch?`, `selection?: { enabled? }` → `table` (see [your own layout](#your-own-layout)) |
|
|
469
|
+
| `DataKitToolbar` · `DataKitTable` · `DataKitPagination` · `DataKitInfinite` · `DataKitActionBar` | `table` (+ `children` / `row` / `size`) |
|
|
470
|
+
| `DataKitLabelsProvider` | `labels`, `children` (from `next-data-kit/ui`) |
|
|
377
471
|
|
|
378
|
-
Column `
|
|
472
|
+
Column: `{ id, header?, cell?, className?, filter?: { label?, option? }, table?: false }`.
|
|
379
473
|
|
|
380
474
|
### URL
|
|
381
475
|
|
|
382
|
-
`?page=2&size=20&sort=name.asc&search=ada&role=admin,user&age=18-30&createdAt=7d`. With a
|
|
476
|
+
`?page=2&size=20&sort=name.asc&search=ada&role=admin,user&age=18-30&createdAt=7d`. With a prefix, every param is prefixed (`?user.page=2`). Page 1 is left out.
|
|
383
477
|
|
|
384
478
|
## FAQ
|
|
385
479
|
|
|
@@ -389,15 +483,19 @@ Yes. Only declared columns exist; every value is validated for its variant and c
|
|
|
389
483
|
|
|
390
484
|
### Does it work with SQL databases?
|
|
391
485
|
|
|
392
|
-
Yes: `
|
|
486
|
+
Yes: `dataKitSourcePrisma`, `dataKitSourceDrizzle` (Postgres, MySQL, SQLite), or your own with `dataKitSourceDefine`. The same `dataKit` definition works with any of them.
|
|
487
|
+
|
|
488
|
+
### Do I need shadcn?
|
|
489
|
+
|
|
490
|
+
No. `next-data-kit/ui` with `next-data-kit/style.css` is a complete UI in plain HTML. The shadcn registry is the same table built from your shadcn components, if you'd rather match them.
|
|
393
491
|
|
|
394
492
|
### Does it need Next.js?
|
|
395
493
|
|
|
396
|
-
The
|
|
494
|
+
The server entries and `useDataKit` don't import Next.js. `next-data-kit/ui` (and the registry) use `next/navigation` for the default URL source. Elsewhere, use `useDataKit` with `source={{ fetch }}` and your own markup.
|
|
397
495
|
|
|
398
|
-
### Why
|
|
496
|
+
### Why don't my Tailwind classes apply to the default UI?
|
|
399
497
|
|
|
400
|
-
|
|
498
|
+
A stylesheet imported after Tailwind declares its layer later, and a later layer wins. Import it with `@import 'next-data-kit/style.css' layer(components);` in your global CSS (see [without shadcn](#without-shadcn)).
|
|
401
499
|
|
|
402
500
|
### How do I update the copied components?
|
|
403
501
|
|
|
@@ -405,29 +503,32 @@ v8's `ColumnDef` and `useReactTable` are what the shadcn data-table, tablecn and
|
|
|
405
503
|
|
|
406
504
|
## Migrating
|
|
407
505
|
|
|
408
|
-
Only a major version (
|
|
506
|
+
Only a major version (13.0.0, …) breaks your code, and every one gets a section here and in [MIGRATION.md](./MIGRATION.md). To upgrade: read the section for each major you cross, `npm install next-data-kit@<version>`, refresh the components if you use the registry, and typecheck: the definition, the data shape and the component props are typed, so most breaks show up as type errors.
|
|
409
507
|
|
|
410
|
-
###
|
|
508
|
+
### 11.x → 12.0
|
|
411
509
|
|
|
412
|
-
|
|
510
|
+
12.0 drops TanStack Table, adds a ready UI that needs no shadcn (`next-data-kit/ui` + `style.css`), puts every export under the `dataKit` name, and nests each column's filter options.
|
|
413
511
|
|
|
414
|
-
|
|
|
512
|
+
| 11.x | 12.0 |
|
|
415
513
|
|---|---|
|
|
416
|
-
| `
|
|
417
|
-
| `
|
|
418
|
-
| `
|
|
419
|
-
| `
|
|
420
|
-
| `
|
|
421
|
-
| `
|
|
422
|
-
|
|
|
423
|
-
|
|
|
424
|
-
| `
|
|
425
|
-
| `
|
|
426
|
-
| `
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
514
|
+
| `sourceMongoose`, `sourceDefine`, `sourcePrisma`, … | `dataKitSourceMongoose`, `dataKitSourceDefine`, `dataKitSourcePrisma`, … |
|
|
515
|
+
| `filterCustom`, `urlEncode`, `urlDecode` | `dataKitFilterCustom`, `dataKitUrlEncode`, `dataKitUrlDecode` |
|
|
516
|
+
| `name: { filter: 'text', option, preset, query, hidden }` | `name: { filter: { variant: 'text', option, preset, query, hidden } }` |
|
|
517
|
+
| `query({ searchParams, key })` + `<DataKit dataKey>` | `query({ searchParams, prefix })` only |
|
|
518
|
+
| `TDataKitRow` / `TDataKitDataOf` | `TDataKitTableRow` / `TDataKitTableData` |
|
|
519
|
+
| TanStack `ColumnDef`: `accessorKey`, `cell: ({ row }) => row.original.x`, `meta: { className, label, option }` | `{ id, cell: ({ row }) => row.x, className, filter: { label, option } }` |
|
|
520
|
+
| `hidden={['status']}` | `{ id: 'status', table: false }` |
|
|
521
|
+
| `selectable`, `actions` | `selection={{ enabled, action }}` |
|
|
522
|
+
| `controls`, `sizes` | `toolbar={{ control }}`, `pagination={{ size }}` |
|
|
523
|
+
| `useDataKitTable` from the registry → TanStack `table` | from `next-data-kit/ui` → `table.header`, `table.row`, `table.filter`, `table.page` |
|
|
524
|
+
| `DataKitTableView` | `DataKitTable` |
|
|
525
|
+
| `kit.set.page(n)`, `kit.set.size(n)` | `kit.set.page.number(n)`, `kit.set.page.size(n)` |
|
|
526
|
+
|
|
527
|
+
Step by step, with before/after snippets: [MIGRATION.md](./MIGRATION.md#migrating-from-11x-to-120).
|
|
528
|
+
|
|
529
|
+
### 10.x → 11.0 and 9.x → 10.0
|
|
530
|
+
|
|
531
|
+
See [MIGRATION.md](./MIGRATION.md#migrating-from-10x-to-110).
|
|
431
532
|
|
|
432
533
|
## License
|
|
433
534
|
|