@izumisy-tailor/tailor-data-viewer 0.2.33 → 0.3.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/README.md +31 -18
- package/package.json +1 -1
- package/src/component/collection/collection-provider.tsx +16 -30
- package/src/component/collection/use-collection.test.ts +65 -76
- package/src/component/collection/use-collection.ts +67 -89
- package/src/component/collection/use-collection.typetest.ts +157 -293
- package/src/component/data-table/data-table.tsx +8 -6
- package/src/component/data-table/i18n.ts +12 -6
- package/src/component/data-table/pagination.tsx +3 -3
- package/src/component/data-table/search-filter-form.tsx +3 -3
- package/src/component/data-table/use-data-table.ts +6 -2
- package/src/component/index.ts +6 -9
- package/src/component/types.ts +145 -276
- package/src/tests/helpers.tsx +9 -5
|
@@ -1,36 +1,19 @@
|
|
|
1
1
|
import { useCallback, useMemo, useState } from "react";
|
|
2
2
|
import type { TableMetadata } from "../../generator/metadata-generator";
|
|
3
3
|
import type {
|
|
4
|
+
BuildQueryVariables,
|
|
5
|
+
CollectionVariables,
|
|
4
6
|
Filter,
|
|
5
7
|
FilterOperator,
|
|
6
8
|
TableMetadataFilter,
|
|
7
9
|
PageInfo,
|
|
8
|
-
|
|
10
|
+
PaginationVariables,
|
|
9
11
|
SortState,
|
|
10
12
|
UseCollectionOptions,
|
|
11
13
|
UseCollectionReturn,
|
|
12
|
-
ExtractQueryVariables,
|
|
13
|
-
ValidateCollectionQuery,
|
|
14
14
|
} from "../types";
|
|
15
15
|
import type { TableFieldName, TableOrderableFieldName } from "../types";
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Resolves the variables type for `toQueryArgs()` return value.
|
|
19
|
-
*
|
|
20
|
-
* When the query document carries gql-tada type information
|
|
21
|
-
* (`ExtractQueryVariables` resolves to a concrete type), uses that type
|
|
22
|
-
* so the result is directly compatible with `useQuery()` from urql.
|
|
23
|
-
* Otherwise falls back to `QueryVariables`.
|
|
24
|
-
*
|
|
25
|
-
* Note: The runtime value is always `QueryVariables`, but the gql-tada
|
|
26
|
-
* variables type is structurally compatible (a supertype), so the cast
|
|
27
|
-
* is safe.
|
|
28
|
-
*/
|
|
29
|
-
type ResolveVariables<TQuery> =
|
|
30
|
-
ExtractQueryVariables<TQuery> extends never
|
|
31
|
-
? QueryVariables
|
|
32
|
-
: ExtractQueryVariables<TQuery>;
|
|
33
|
-
|
|
34
17
|
// -----------------------------------------------------------------------------
|
|
35
18
|
// Overload signatures
|
|
36
19
|
// -----------------------------------------------------------------------------
|
|
@@ -39,38 +22,42 @@ type ResolveVariables<TQuery> =
|
|
|
39
22
|
* Hook for managing collection query parameters (filters, sort, pagination)
|
|
40
23
|
* with metadata-based field name typing and automatic `fieldType` detection.
|
|
41
24
|
*
|
|
42
|
-
* `
|
|
43
|
-
*
|
|
25
|
+
* Returns `variables` with `query`, `order`, and `pagination` sub-properties
|
|
26
|
+
* that can be mapped to GraphQL query variables.
|
|
44
27
|
*
|
|
45
28
|
* @example
|
|
46
29
|
* ```tsx
|
|
47
30
|
* import { tableMetadata } from "./generated/data-viewer-metadata.generated";
|
|
48
31
|
*
|
|
49
|
-
* const
|
|
32
|
+
* const { variables } = useCollectionVariables({
|
|
50
33
|
* tableMetadata: tableMetadata.task,
|
|
51
|
-
* query: GET_TASKS,
|
|
52
34
|
* params: { pageSize: 20 },
|
|
53
35
|
* });
|
|
54
|
-
* const
|
|
36
|
+
* const { query, order, pagination } = variables;
|
|
37
|
+
* const [result] = useQuery({
|
|
38
|
+
* query: GET_TASKS,
|
|
39
|
+
* variables: { ...pagination, query, order },
|
|
40
|
+
* });
|
|
55
41
|
* ```
|
|
56
42
|
*/
|
|
57
|
-
export function
|
|
43
|
+
export function useCollectionVariables<const TTable extends TableMetadata>(
|
|
58
44
|
options: UseCollectionOptions<
|
|
59
45
|
TableFieldName<TTable>,
|
|
60
46
|
TableMetadataFilter<TTable>
|
|
61
47
|
> & {
|
|
62
48
|
tableMetadata: TTable;
|
|
63
|
-
query: ValidateCollectionQuery<
|
|
64
|
-
TQuery,
|
|
65
|
-
TableFieldName<TTable>,
|
|
66
|
-
TableOrderableFieldName<TTable>
|
|
67
|
-
>;
|
|
68
49
|
},
|
|
69
50
|
): UseCollectionReturn<
|
|
70
51
|
TableFieldName<TTable>,
|
|
71
52
|
{
|
|
72
|
-
query:
|
|
73
|
-
|
|
53
|
+
query: BuildQueryVariables<TTable> | undefined;
|
|
54
|
+
order:
|
|
55
|
+
| {
|
|
56
|
+
field: TableOrderableFieldName<TTable>;
|
|
57
|
+
direction: "Asc" | "Desc";
|
|
58
|
+
}[]
|
|
59
|
+
| undefined;
|
|
60
|
+
pagination: PaginationVariables;
|
|
74
61
|
},
|
|
75
62
|
TableMetadataFilter<TTable>
|
|
76
63
|
>;
|
|
@@ -78,38 +65,34 @@ export function useCollection<const TTable extends TableMetadata, TQuery>(
|
|
|
78
65
|
/**
|
|
79
66
|
* Hook for managing collection query parameters (filters, sort, pagination).
|
|
80
67
|
*
|
|
81
|
-
*
|
|
82
|
-
* to
|
|
83
|
-
*
|
|
84
|
-
* `toQueryArgs()` returns `{ query, variables }` so the result can be spread
|
|
85
|
-
* directly into `useQuery()`.
|
|
68
|
+
* Returns `variables` with `query`, `order`, and `pagination` sub-properties
|
|
69
|
+
* that can be mapped to GraphQL query variables.
|
|
86
70
|
*
|
|
87
71
|
* @example
|
|
88
72
|
* ```tsx
|
|
89
|
-
* const
|
|
90
|
-
* const
|
|
73
|
+
* const { variables } = useCollectionVariables({ params: { pageSize: 20 } });
|
|
74
|
+
* const { query, order, pagination } = variables;
|
|
75
|
+
* const [result] = useQuery({
|
|
76
|
+
* query: GET_ORDERS,
|
|
77
|
+
* variables: { ...pagination, query, order },
|
|
78
|
+
* });
|
|
91
79
|
* ```
|
|
92
80
|
*/
|
|
93
|
-
export function
|
|
81
|
+
export function useCollectionVariables(
|
|
94
82
|
options: UseCollectionOptions & {
|
|
95
|
-
query: TQuery;
|
|
96
83
|
tableMetadata?: never;
|
|
97
84
|
},
|
|
98
|
-
): UseCollectionReturn<
|
|
99
|
-
string,
|
|
100
|
-
{ query: TQuery; variables: ResolveVariables<TQuery> }
|
|
101
|
-
>;
|
|
85
|
+
): UseCollectionReturn<string, CollectionVariables>;
|
|
102
86
|
|
|
103
87
|
// -----------------------------------------------------------------------------
|
|
104
88
|
// Implementation
|
|
105
89
|
// -----------------------------------------------------------------------------
|
|
106
|
-
export function
|
|
90
|
+
export function useCollectionVariables(
|
|
107
91
|
options: UseCollectionOptions & {
|
|
108
92
|
tableMetadata?: TableMetadata;
|
|
109
|
-
query: unknown;
|
|
110
93
|
},
|
|
111
|
-
): UseCollectionReturn<string,
|
|
112
|
-
const { params = {}
|
|
94
|
+
): UseCollectionReturn<string, CollectionVariables> {
|
|
95
|
+
const { params = {} } = options;
|
|
113
96
|
const {
|
|
114
97
|
initialFilters = [],
|
|
115
98
|
initialSort = [],
|
|
@@ -270,56 +253,51 @@ export function useCollection(
|
|
|
270
253
|
: currentPageInfo.hasNextPage;
|
|
271
254
|
|
|
272
255
|
// ---------------------------------------------------------------------------
|
|
273
|
-
// Build
|
|
256
|
+
// Build collection variables (Tailor Platform format)
|
|
274
257
|
// ---------------------------------------------------------------------------
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
vars.first = pageSize;
|
|
281
|
-
if (cursor) {
|
|
282
|
-
vars.after = cursor;
|
|
283
|
-
}
|
|
284
|
-
} else {
|
|
285
|
-
vars.last = pageSize;
|
|
286
|
-
if (cursor) {
|
|
287
|
-
vars.before = cursor;
|
|
288
|
-
}
|
|
258
|
+
const queryVars = useMemo(() => {
|
|
259
|
+
if (filters.length === 0) return undefined;
|
|
260
|
+
const filterQuery: Record<string, Record<string, unknown>> = {};
|
|
261
|
+
for (const filter of filters) {
|
|
262
|
+
filterQuery[filter.field] = { [filter.operator]: filter.value };
|
|
289
263
|
}
|
|
264
|
+
return filterQuery;
|
|
265
|
+
}, [filters]);
|
|
290
266
|
|
|
291
|
-
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
267
|
+
const orderVars = useMemo(() => {
|
|
268
|
+
if (sortStates.length === 0) return undefined;
|
|
269
|
+
return sortStates.map((s) => ({
|
|
270
|
+
field: s.field,
|
|
271
|
+
direction: s.direction,
|
|
272
|
+
}));
|
|
273
|
+
}, [sortStates]);
|
|
299
274
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
275
|
+
const paginationVars = useMemo<PaginationVariables>(() => {
|
|
276
|
+
const p: PaginationVariables = {};
|
|
277
|
+
if (paginationDirection === "forward") {
|
|
278
|
+
p.first = pageSize;
|
|
279
|
+
if (cursor) p.after = cursor;
|
|
280
|
+
} else {
|
|
281
|
+
p.last = pageSize;
|
|
282
|
+
if (cursor) p.before = cursor;
|
|
306
283
|
}
|
|
284
|
+
return p;
|
|
285
|
+
}, [pageSize, cursor, paginationDirection]);
|
|
307
286
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}, [queryDocument, variables]);
|
|
287
|
+
const variables = useMemo<CollectionVariables>(
|
|
288
|
+
() => ({
|
|
289
|
+
query: queryVars,
|
|
290
|
+
order: orderVars,
|
|
291
|
+
pagination: paginationVars,
|
|
292
|
+
}),
|
|
293
|
+
[queryVars, orderVars, paginationVars],
|
|
294
|
+
);
|
|
317
295
|
|
|
318
296
|
// ---------------------------------------------------------------------------
|
|
319
297
|
// Return
|
|
320
298
|
// ---------------------------------------------------------------------------
|
|
321
299
|
return {
|
|
322
|
-
|
|
300
|
+
variables,
|
|
323
301
|
filters,
|
|
324
302
|
// Cast needed: implementation accepts FilterOperator (widest),
|
|
325
303
|
// but overload signatures narrow via OperatorForField<TFilter, F>.
|