@izumisy-tailor/tailor-data-viewer 0.2.2 → 0.2.4
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/package.json
CHANGED
|
@@ -69,8 +69,8 @@ describe("DataTable", () => {
|
|
|
69
69
|
it("renders data-bound table with auto-generated rows", () => {
|
|
70
70
|
render(
|
|
71
71
|
<DataTable.Root columns={testColumns} rows={testRows}>
|
|
72
|
-
<DataTable.Headers
|
|
73
|
-
<DataTable.Body
|
|
72
|
+
<DataTable.Headers />
|
|
73
|
+
<DataTable.Body />
|
|
74
74
|
</DataTable.Root>,
|
|
75
75
|
);
|
|
76
76
|
|
|
@@ -84,8 +84,8 @@ describe("DataTable", () => {
|
|
|
84
84
|
it("renders display columns via render function", () => {
|
|
85
85
|
render(
|
|
86
86
|
<DataTable.Root columns={testColumns} rows={testRows}>
|
|
87
|
-
<DataTable.Headers
|
|
88
|
-
<DataTable.Body
|
|
87
|
+
<DataTable.Headers />
|
|
88
|
+
<DataTable.Body />
|
|
89
89
|
</DataTable.Root>,
|
|
90
90
|
);
|
|
91
91
|
|
|
@@ -96,8 +96,8 @@ describe("DataTable", () => {
|
|
|
96
96
|
it("shows loading state", () => {
|
|
97
97
|
render(
|
|
98
98
|
<DataTable.Root columns={testColumns} rows={[]} loading>
|
|
99
|
-
<DataTable.Headers
|
|
100
|
-
<DataTable.Body
|
|
99
|
+
<DataTable.Headers />
|
|
100
|
+
<DataTable.Body />
|
|
101
101
|
</DataTable.Root>,
|
|
102
102
|
);
|
|
103
103
|
|
|
@@ -108,8 +108,8 @@ describe("DataTable", () => {
|
|
|
108
108
|
const err = new Error("Something went wrong");
|
|
109
109
|
render(
|
|
110
110
|
<DataTable.Root columns={testColumns} rows={[]} error={err}>
|
|
111
|
-
<DataTable.Headers
|
|
112
|
-
<DataTable.Body
|
|
111
|
+
<DataTable.Headers />
|
|
112
|
+
<DataTable.Body />
|
|
113
113
|
</DataTable.Root>,
|
|
114
114
|
);
|
|
115
115
|
|
|
@@ -119,8 +119,8 @@ describe("DataTable", () => {
|
|
|
119
119
|
it("shows empty state", () => {
|
|
120
120
|
render(
|
|
121
121
|
<DataTable.Root columns={testColumns} rows={[]}>
|
|
122
|
-
<DataTable.Headers
|
|
123
|
-
<DataTable.Body
|
|
122
|
+
<DataTable.Headers />
|
|
123
|
+
<DataTable.Body />
|
|
124
124
|
</DataTable.Root>,
|
|
125
125
|
);
|
|
126
126
|
|
|
@@ -129,12 +129,13 @@ describe("DataTable", () => {
|
|
|
129
129
|
|
|
130
130
|
it("renders sort indicator on sorted column", () => {
|
|
131
131
|
render(
|
|
132
|
-
<DataTable.Root
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
<DataTable.
|
|
132
|
+
<DataTable.Root
|
|
133
|
+
columns={testColumns}
|
|
134
|
+
rows={testRows}
|
|
135
|
+
sortStates={[{ field: "name", direction: "Asc" }]}
|
|
136
|
+
>
|
|
137
|
+
<DataTable.Headers />
|
|
138
|
+
<DataTable.Body />
|
|
138
139
|
</DataTable.Root>,
|
|
139
140
|
);
|
|
140
141
|
|
|
@@ -144,7 +145,7 @@ describe("DataTable", () => {
|
|
|
144
145
|
it("supports custom rendering with children", () => {
|
|
145
146
|
render(
|
|
146
147
|
<DataTable.Root columns={testColumns} rows={testRows}>
|
|
147
|
-
<DataTable.Headers
|
|
148
|
+
<DataTable.Headers />
|
|
148
149
|
<DataTable.Body>
|
|
149
150
|
<DataTable.Row>
|
|
150
151
|
<DataTable.Cell>Custom Cell</DataTable.Cell>
|
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import { createContext, useContext } from "react";
|
|
2
|
-
import type { RowOperations } from "../types";
|
|
2
|
+
import type { Column, RowOperations, SortState } from "../types";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Context value provided by `DataTable.Root`.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* Exposes row operations for optimistic updates and table state
|
|
8
|
+
* so that `DataTable.Headers` / `DataTable.Body` can read them
|
|
9
|
+
* without explicit props.
|
|
8
10
|
*/
|
|
9
11
|
export interface DataTableContextValue<TRow extends Record<string, unknown>> {
|
|
10
12
|
updateRow: RowOperations<TRow>["updateRow"];
|
|
11
13
|
deleteRow: RowOperations<TRow>["deleteRow"];
|
|
12
14
|
insertRow: RowOperations<TRow>["insertRow"];
|
|
15
|
+
|
|
16
|
+
// Table state propagated from DataTable.Root
|
|
17
|
+
columns: Column<TRow>[];
|
|
18
|
+
rows: TRow[];
|
|
19
|
+
loading: boolean;
|
|
20
|
+
error: Error | null;
|
|
21
|
+
sortStates: SortState[];
|
|
22
|
+
onSort?: (field: string, direction?: "Asc" | "Desc") => void;
|
|
13
23
|
}
|
|
14
24
|
|
|
15
25
|
// Using `any` for the context default since generic contexts need a base type.
|
|
@@ -31,7 +41,9 @@ export { DataTableContext };
|
|
|
31
41
|
* };
|
|
32
42
|
* ```
|
|
33
43
|
*/
|
|
34
|
-
export function useDataTableContext<
|
|
44
|
+
export function useDataTableContext<
|
|
45
|
+
TRow extends Record<string, unknown>,
|
|
46
|
+
>(): DataTableContextValue<TRow> {
|
|
35
47
|
const ctx = useContext(DataTableContext);
|
|
36
48
|
if (!ctx) {
|
|
37
49
|
throw new Error("useDataTableContext must be used within <DataTable.Root>");
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createElement,
|
|
3
|
+
useContext,
|
|
4
|
+
type ComponentProps,
|
|
5
|
+
type ReactNode,
|
|
6
|
+
} from "react";
|
|
2
7
|
import { cn } from "../lib/utils";
|
|
3
8
|
import { Table } from "../table";
|
|
4
|
-
import type { Column, DataTableRootProps
|
|
9
|
+
import type { Column, DataTableRootProps } from "../types";
|
|
5
10
|
import {
|
|
6
11
|
DataTableContext,
|
|
7
12
|
type DataTableContextValue,
|
|
@@ -14,23 +19,38 @@ import {
|
|
|
14
19
|
/**
|
|
15
20
|
* Internal noop row operations used when none are provided.
|
|
16
21
|
*/
|
|
17
|
-
const
|
|
22
|
+
const noopRowOps = {
|
|
18
23
|
updateRow: () => ({ rollback: () => {} }),
|
|
19
|
-
deleteRow: () => ({
|
|
24
|
+
deleteRow: () => ({
|
|
25
|
+
rollback: () => {},
|
|
26
|
+
deletedRow: {} as Record<string, unknown>,
|
|
27
|
+
}),
|
|
20
28
|
insertRow: () => ({ rollback: () => {} }),
|
|
21
29
|
};
|
|
22
30
|
|
|
23
31
|
function DataTableRoot<TRow extends Record<string, unknown>>({
|
|
32
|
+
columns = [] as Column<TRow>[],
|
|
33
|
+
rows = [] as TRow[],
|
|
34
|
+
loading = false,
|
|
35
|
+
error = null,
|
|
36
|
+
sortStates = [],
|
|
37
|
+
onSort,
|
|
24
38
|
rowOperations,
|
|
25
39
|
children,
|
|
26
40
|
}: DataTableRootProps<TRow>) {
|
|
27
|
-
const contextValue: DataTableContextValue<TRow> =
|
|
28
|
-
updateRow:
|
|
29
|
-
|
|
30
|
-
deleteRow:
|
|
31
|
-
|
|
32
|
-
insertRow:
|
|
33
|
-
|
|
41
|
+
const contextValue: DataTableContextValue<TRow> = {
|
|
42
|
+
updateRow: (rowOperations?.updateRow ??
|
|
43
|
+
noopRowOps.updateRow) as DataTableContextValue<TRow>["updateRow"],
|
|
44
|
+
deleteRow: (rowOperations?.deleteRow ??
|
|
45
|
+
noopRowOps.deleteRow) as DataTableContextValue<TRow>["deleteRow"],
|
|
46
|
+
insertRow: (rowOperations?.insertRow ??
|
|
47
|
+
noopRowOps.insertRow) as DataTableContextValue<TRow>["insertRow"],
|
|
48
|
+
columns,
|
|
49
|
+
rows,
|
|
50
|
+
loading,
|
|
51
|
+
error,
|
|
52
|
+
sortStates,
|
|
53
|
+
onSort,
|
|
34
54
|
};
|
|
35
55
|
|
|
36
56
|
return (
|
|
@@ -44,19 +64,17 @@ function DataTableRoot<TRow extends Record<string, unknown>>({
|
|
|
44
64
|
// DataTable.Headers
|
|
45
65
|
// =============================================================================
|
|
46
66
|
|
|
47
|
-
interface DataTableHeadersProps
|
|
48
|
-
columns?: Column<TRow>[];
|
|
49
|
-
sortStates?: SortState[];
|
|
50
|
-
onSort?: (field: string, direction?: "Asc" | "Desc") => void;
|
|
67
|
+
interface DataTableHeadersProps {
|
|
51
68
|
className?: string;
|
|
52
69
|
}
|
|
53
70
|
|
|
54
|
-
function DataTableHeaders
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
71
|
+
function DataTableHeaders({ className }: DataTableHeadersProps) {
|
|
72
|
+
const ctx = useContext(DataTableContext);
|
|
73
|
+
if (!ctx) {
|
|
74
|
+
throw new Error("<DataTable.Headers> must be used within <DataTable.Root>");
|
|
75
|
+
}
|
|
76
|
+
const { columns, sortStates, onSort } = ctx;
|
|
77
|
+
|
|
60
78
|
return (
|
|
61
79
|
<Table.Headers className={className}>
|
|
62
80
|
<Table.HeaderRow>
|
|
@@ -111,23 +129,18 @@ function SortIndicator({ direction }: { direction: "Asc" | "Desc" }) {
|
|
|
111
129
|
// DataTable.Body
|
|
112
130
|
// =============================================================================
|
|
113
131
|
|
|
114
|
-
interface DataTableBodyProps
|
|
115
|
-
columns?: Column<TRow>[];
|
|
116
|
-
rows?: TRow[];
|
|
117
|
-
loading?: boolean;
|
|
118
|
-
error?: Error | null;
|
|
132
|
+
interface DataTableBodyProps {
|
|
119
133
|
children?: ReactNode;
|
|
120
134
|
className?: string;
|
|
121
135
|
}
|
|
122
136
|
|
|
123
|
-
function DataTableBody
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}: DataTableBodyProps<TRow>) {
|
|
137
|
+
function DataTableBody({ children, className }: DataTableBodyProps) {
|
|
138
|
+
const ctx = useContext(DataTableContext);
|
|
139
|
+
if (!ctx) {
|
|
140
|
+
throw new Error("<DataTable.Body> must be used within <DataTable.Root>");
|
|
141
|
+
}
|
|
142
|
+
const { columns, rows, loading, error } = ctx;
|
|
143
|
+
|
|
131
144
|
// If children are provided, render them directly (custom rendering)
|
|
132
145
|
if (children) {
|
|
133
146
|
return <Table.Body className={className}>{children}</Table.Body>;
|
package/src/component/types.ts
CHANGED
|
@@ -110,10 +110,16 @@ export interface PageInfo {
|
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* GraphQL query variables in Tailor Platform format.
|
|
113
|
+
*
|
|
114
|
+
* @typeParam TFieldName - Union of allowed field name strings (default: `string`).
|
|
115
|
+
* When metadata is provided to `useCollectionParams`, this
|
|
116
|
+
* narrows `order[].field` to match the table's field names,
|
|
117
|
+
* making the output directly compatible with gql-tada's
|
|
118
|
+
* `VariablesOf<>` types.
|
|
113
119
|
*/
|
|
114
|
-
export interface QueryVariables {
|
|
120
|
+
export interface QueryVariables<TFieldName extends string = string> {
|
|
115
121
|
query?: Record<string, unknown>;
|
|
116
|
-
order?: { field:
|
|
122
|
+
order?: { field: TFieldName; direction: "Asc" | "Desc" }[];
|
|
117
123
|
first: number;
|
|
118
124
|
after?: string | null;
|
|
119
125
|
}
|
|
@@ -287,7 +293,7 @@ export interface UseCollectionParamsOptions<
|
|
|
287
293
|
*/
|
|
288
294
|
export interface UseCollectionParamsReturn<TFieldName extends string = string> {
|
|
289
295
|
/** Query variables in Tailor Platform format */
|
|
290
|
-
variables: QueryVariables
|
|
296
|
+
variables: QueryVariables<TFieldName>;
|
|
291
297
|
|
|
292
298
|
// Filter operations
|
|
293
299
|
/** Current active filters */
|