@kronor/dtv 10.0.1 → 10.1.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.
@@ -0,0 +1,11 @@
1
+ import type { FilterControl } from '../framework/filters';
2
+ export declare const NUMBER_CONTROL_MAX_FRACTION_DIGITS = 12;
3
+ /**
4
+ * What a control shows for its value, as text. Mirrors `renderInput` in
5
+ * FilterForm.tsx: each control type formats the way its input displays, so a
6
+ * pill reads the same as the form it came from.
7
+ *
8
+ * Only a `custom` control, and a value a control cannot place (an item no
9
+ * longer in the list, say), fall back to guessing from the value's shape.
10
+ */
11
+ export declare function formatControlValue(control: FilterControl, value: unknown): string;
@@ -1,8 +1,7 @@
1
- import type { ConditionOnlyTransform, FilterControl, FilterExpr as FilterExprType, FilterField, FilterTransform } from '../framework/filters';
1
+ import type { ConditionOnlyTransform, FilterControl, FilterExpr as FilterExprType, FilterField, FilterTransform, FilterValueFormatter } from '../framework/filters';
2
2
  import type { ControlValue } from './filterControl';
3
3
  import type { FilterLeafOperator } from './filterTyping';
4
- import type { IsAny } from './typeErrors';
5
- export type { ConditionOnlyTransform, FilterField, FilterTransform };
4
+ export type { ConditionOnlyTransform, FilterField, FilterTransform, FilterValueFormatter };
6
5
  export type FilterExpr = FilterExprType;
7
6
  /**
8
7
  * A leaf built by the helpers below. Field and control are kept as given:
@@ -15,43 +14,28 @@ export type FilterExprLeaf<Type extends FilterLeafOperator, Field extends Filter
15
14
  value: Control;
16
15
  /** Shown by the applied-filter pill in place of the field path. */
17
16
  fieldLabel?: string;
17
+ /** How the applied-filter pill shows the value, in place of the control's own format. */
18
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
18
19
  };
19
20
  /** A leaf whose value passes through a transform on its way to the query. */
20
21
  export type FilterExprLeafWithTransform<Type extends FilterLeafOperator, Field extends FilterField, Control extends FilterControl, Transform extends FilterTransform<any>> = FilterExprLeaf<Type, Field, Control> & {
21
22
  transform: Transform;
22
23
  };
23
- type Exactly<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
24
- /**
25
- * Whether a transform was actually passed.
26
- *
27
- * It cannot be read from the presence of the property, because a transform's
28
- * `input` is typed by making `Transform` a parameter constrained to
29
- * `FilterTransform<ControlValue<Control>>` — a parameter's constraint is the
30
- * only thing that contextually types a callback parameter, and an intersection
31
- * member is not. So when the property is omitted, `Transform` lands on that
32
- * constraint rather than on nothing.
33
- *
34
- * The constraint is distinguishable, though: its `toQuery` is optional and its
35
- * input is *exactly* `ControlValue<Control>`. Every real transform differs —
36
- * an inline object literal has a required `toQuery`, and one annotated
37
- * `FilterTransform` or `FilterTransform<any>` has a wider input.
38
- *
39
- * Getting this wrong in the permissive direction would silently exempt leaves
40
- * from the checks in ./filterTyping, so filterExpr.transformDetection.test.ts
41
- * pins down every way a transform can arrive.
42
- */
43
- export type IsTransformed<Transform, Control> = Transform extends {
44
- toQuery?: (input: infer Input, ...rest: never[]) => any;
45
- } ? undefined extends Transform['toQuery'] ? IsAny<Input> extends true ? true : Exactly<Input, ControlValue<Control>> extends true ? false : true : true : true;
46
- export type FilterExprLeafFor<Type extends FilterLeafOperator, Field extends FilterField, Control extends FilterControl, Transform> = IsTransformed<Transform, Control> extends true ? FilterExprLeafWithTransform<Type, Field, Control, Extract<Transform, FilterTransform<any>>> : FilterExprLeaf<Type, Field, Control>;
47
- export type FilterRangeFor<Field extends FilterField, Control extends FilterControl, Transform> = {
24
+ export type FilterRange<Field extends FilterField, Control extends FilterControl> = {
48
25
  type: 'and';
49
26
  filters: [
50
- FilterExprLeafFor<'greaterThanOrEqual', Field, Control, Transform>,
51
- FilterExprLeafFor<'lessThanOrEqual', Field, Control, Transform>
27
+ FilterExprLeaf<'greaterThanOrEqual', Field, Control>,
28
+ FilterExprLeaf<'lessThanOrEqual', Field, Control>
52
29
  ];
53
30
  };
54
- export type LeafArgs<Field extends FilterField, Control extends FilterControl, Transform extends FilterTransform<any>> = {
31
+ export type FilterRangeWithTransform<Field extends FilterField, Control extends FilterControl, Transform extends FilterTransform<any>> = {
32
+ type: 'and';
33
+ filters: [
34
+ FilterExprLeafWithTransform<'greaterThanOrEqual', Field, Control, Transform>,
35
+ FilterExprLeafWithTransform<'lessThanOrEqual', Field, Control, Transform>
36
+ ];
37
+ };
38
+ export type LeafArgs<Field extends FilterField, Control extends FilterControl> = {
55
39
  field: Field;
56
40
  control: Control;
57
41
  /**
@@ -60,31 +44,71 @@ export type LeafArgs<Field extends FilterField, Control extends FilterControl, T
60
44
  * whoever is reading the pill.
61
45
  */
62
46
  fieldLabel?: string;
63
- transform?: Transform;
47
+ /**
48
+ * How the applied-filter pill shows the value, in place of the control's
49
+ * own format — for a value only the filter knows how to read.
50
+ */
51
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
64
52
  };
65
- declare function range<const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: {
53
+ type RangeArgs<Field extends FilterField, Control extends FilterControl> = {
66
54
  field: Field;
67
55
  control: (options: {
68
56
  placeholder: string;
69
57
  }) => Control;
70
58
  fieldLabel?: string;
71
- transform?: Transform;
72
- }): FilterRangeFor<Field, Control, Transform>;
59
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
60
+ };
61
+ /**
62
+ * The `transform` of the overloads without one: a transform no one can write,
63
+ * rather than `undefined`. A transform with the wrong `input` then fails both
64
+ * overloads at its `toQuery`, so TypeScript reports the error there instead of
65
+ * on the builder's name.
66
+ */
67
+ type NoTransform = {
68
+ toQuery: never;
69
+ };
70
+ /**
71
+ * A leaf builder, with or without a transform.
72
+ *
73
+ * Whether a transform was passed decides the leaf's type — a leaf carrying one
74
+ * is exempt from the control and operator checks in ./filterTyping — so each
75
+ * case has an overload of its own. The transform's `input` is typed by its
76
+ * constraint, which is why `Transform` is a type parameter at all; one
77
+ * signature with an optional `transform` would leave `Transform` on that
78
+ * constraint when it is omitted, and an omitted transform would have to be
79
+ * told apart from a passed one by its type. Here it never has to be: without a
80
+ * transform there is no `Transform`.
81
+ */
82
+ export type LeafBuilder<Type extends FilterLeafOperator> = {
83
+ <const Field extends FilterField, const Control extends FilterControl>(args: LeafArgs<Field, Control> & {
84
+ transform?: NoTransform;
85
+ }): FilterExprLeaf<Type, Field, Control>;
86
+ <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control> & {
87
+ transform: Transform;
88
+ }): FilterExprLeafWithTransform<Type, Field, Control, Transform>;
89
+ };
90
+ declare function range<const Field extends FilterField, const Control extends FilterControl>(args: RangeArgs<Field, Control> & {
91
+ transform?: NoTransform;
92
+ }): FilterRange<Field, Control>;
93
+ declare function range<const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: RangeArgs<Field, Control> & {
94
+ transform: Transform;
95
+ }): FilterRangeWithTransform<Field, Control, Transform>;
73
96
  export declare const FilterExpr: {
74
- equals: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"equals", Field, Control, Transform>;
75
- notEquals: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"notEquals", Field, Control, Transform>;
76
- greaterThan: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"greaterThan", Field, Control, Transform>;
77
- lessThan: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"lessThan", Field, Control, Transform>;
78
- greaterThanOrEqual: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"greaterThanOrEqual", Field, Control, Transform>;
79
- lessThanOrEqual: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"lessThanOrEqual", Field, Control, Transform>;
80
- in: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"in", Field, Control, Transform>;
81
- notIn: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"notIn", Field, Control, Transform>;
82
- like: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"like", Field, Control, Transform>;
83
- iLike: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"iLike", Field, Control, Transform>;
84
- isNull: <const Field extends FilterField, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, unknown, Field>>(args: LeafArgs<Field, Control, Transform>) => FilterExprLeafFor<"isNull", Field, Control, Transform>;
97
+ equals: LeafBuilder<"equals">;
98
+ notEquals: LeafBuilder<"notEquals">;
99
+ greaterThan: LeafBuilder<"greaterThan">;
100
+ lessThan: LeafBuilder<"lessThan">;
101
+ greaterThanOrEqual: LeafBuilder<"greaterThanOrEqual">;
102
+ lessThanOrEqual: LeafBuilder<"lessThanOrEqual">;
103
+ in: LeafBuilder<"in">;
104
+ notIn: LeafBuilder<"notIn">;
105
+ like: LeafBuilder<"like">;
106
+ iLike: LeafBuilder<"iLike">;
107
+ isNull: LeafBuilder<"isNull">;
85
108
  computedCondition: <const Control extends FilterControl, const Transform extends ConditionOnlyTransform<ControlValue<Control>>>(args: {
86
109
  control: Control;
87
110
  fieldLabel?: string;
111
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
88
112
  transform: Transform;
89
113
  }) => FilterExprLeafWithTransform<"equals", {
90
114
  or: [];
@@ -20,9 +20,9 @@
20
20
  * checking more.
21
21
  */
22
22
  import type { FieldPath } from '@kronor/hasura-graphql';
23
- import type { FilterControl, FilterTransform } from '../framework/filters';
23
+ import type { FilterControl, FilterTransform, FilterValueFormatter } from '../framework/filters';
24
24
  import type { FilterField } from '../framework/filters';
25
- import { FilterExpr, type FilterExprLeaf, type FilterExprLeafWithTransform, type FilterRangeFor } from './filterExpr';
25
+ import { FilterExpr, type FilterExprLeaf, type FilterExprLeafWithTransform, type FilterRange, type FilterRangeWithTransform } from './filterExpr';
26
26
  import type { ControlValue } from './filterControl';
27
27
  import type { FilterFieldForRow } from './filters';
28
28
  import type { FilterLeafOperator, MutableTuple, ValidateControlForRow, ValidateOperatorForRow, ValidateOperatorListForRow } from './filterTyping';
@@ -51,11 +51,13 @@ type LeafBuilderForRow<Row, Type extends FilterLeafOperator> = {
51
51
  field: Field & WhenFieldKnown<Row, Field, ValidateOperatorForRow<Row, Type, Field>>;
52
52
  control: Control & WhenFieldKnown<Row, Field, ValidateControlForRow<Row, Type, Field, Control>>;
53
53
  fieldLabel?: string;
54
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
54
55
  }): FilterExprLeaf<Type, Field & FilterField, Control>;
55
56
  <const Field extends FilterFieldForRow<Row>, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, Row, Field>>(args: {
56
57
  field: Field;
57
58
  control: Control & WhenFieldKnown<Row, Field, ValidateOperatorListForRow<Row, Field, Control>>;
58
59
  fieldLabel?: string;
60
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
59
61
  }, transform: Transform): FilterExprLeafWithTransform<Type, Field & FilterField, Control, Transform>;
60
62
  };
61
63
  type RangeBuilderForRow<Row> = {
@@ -65,14 +67,16 @@ type RangeBuilderForRow<Row> = {
65
67
  placeholder: string;
66
68
  }) => Control & WhenFieldKnown<Row, Field, ValidateControlForRow<Row, 'greaterThanOrEqual', Field, Control>>;
67
69
  fieldLabel?: string;
68
- }): FilterRangeFor<Field & FilterField, Control, never>;
70
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
71
+ }): FilterRange<Field & FilterField, Control>;
69
72
  <const Field extends FilterFieldForRow<Row>, const Control extends FilterControl, const Transform extends FilterTransform<ControlValue<Control>, Row, Field>>(args: {
70
73
  field: Field;
71
74
  control: (options: {
72
75
  placeholder: string;
73
76
  }) => Control;
74
77
  fieldLabel?: string;
75
- }, transform: Transform): FilterRangeFor<Field & FilterField, Control, Transform>;
78
+ formatValue?: FilterValueFormatter<ControlValue<Control>>;
79
+ }, transform: Transform): FilterRangeWithTransform<Field & FilterField, Control, Transform>;
76
80
  };
77
81
  export type FilterExprBuilderForRow<Row> = {
78
82
  equals: LeafBuilderForRow<Row, 'equals'>;
@@ -87,7 +87,7 @@ type ItemValueError<Path extends string, Value, Items> = IsTuple<Items> extends
87
87
  value: infer ItemValue;
88
88
  }[] ? IsAny<ItemValue> extends true ? never : Exclude<ItemValue, ItemTarget<Value>> extends infer Rejected ? [Rejected] extends [never] ? never : `item value ${ShowValue<Rejected>} is not a possible value of field '${Path}'` : never : never;
89
89
  type ToQueryResult<Transform> = Transform extends {
90
- toQuery?: (input: any, context: any) => infer Result;
90
+ toQuery: (input: any, context: any) => infer Result;
91
91
  } ? Result : never;
92
92
  /**
93
93
  * A `customOperator` control stores the operator alongside the value, which
@@ -100,16 +100,6 @@ type CustomOperatorTransformError<Leaf, Control> = Control extends {
100
100
  } ? Leaf extends {
101
101
  transform: infer Transform;
102
102
  } ? [ToQueryResult<Transform>] extends [TransformConditionResult] ? never : "a 'customOperator' control needs a query transform that returns a condition" : "a 'customOperator' control needs a query transform" : never;
103
- /**
104
- * `toQuery` is optional on `FilterTransform` — which is what lets the leaf
105
- * builders tell an omitted transform from a real one, see
106
- * filterExpr's `IsTransformed` — so a transform object carrying nothing at all
107
- * type-checks while doing nothing at query time, and would take the checks
108
- * below down with it.
109
- */
110
- type EmptyTransformError<Leaf> = Leaf extends {
111
- transform: infer Transform;
112
- } ? 'toQuery' extends keyof Transform ? never : 'a transform needs a toQuery function' : never;
113
103
  type OperatorError<Type extends FilterLeafOperator, Path extends string, Value> = LeafHasuraOperator[Type] extends keyof HasuraOperatorFor<Value> ? never : `operator '${Type}' is not available for field '${Path}' (type ${DescribeType<Value>})`;
114
104
  /**
115
105
  * A multiselect yields a list of values, which only `in`/`notIn` can consume —
@@ -143,7 +133,7 @@ type LeafErrors<Row, Leaf> = Leaf extends {
143
133
  type: infer Type extends FilterLeafOperator;
144
134
  field: infer Field;
145
135
  value: infer Control extends FilterControl;
146
- } ? PathErrors<Row, Field> | OperatorListErrors<Row, Field, Control> | CustomOperatorTransformError<Leaf, Control> | EmptyTransformError<Leaf> | (HasTransform<Leaf> extends true ? never : ControlErrors<Row, Type, Field, Control> | OperatorErrors<Row, Type, Field>) : never;
136
+ } ? PathErrors<Row, Field> | OperatorListErrors<Row, Field, Control> | CustomOperatorTransformError<Leaf, Control> | (HasTransform<Leaf> extends true ? never : ControlErrors<Row, Type, Field, Control> | OperatorErrors<Row, Type, Field>) : never;
147
137
  /**
148
138
  * `NestingDepth` bounds how far into and/or/not the walk goes. Real filter
149
139
  * trees are a few levels deep; the bound exists because during inference the
@@ -12,5 +12,6 @@ export declare function cellRendererRef(key: string): RuntimeReference;
12
12
  export declare function noRowsComponentRef(key: string): RuntimeReference;
13
13
  export declare function customFilterComponentRef(key: string): RuntimeReference;
14
14
  export declare function queryTransformRef(key: string): RuntimeReference;
15
+ export declare function valueFormatterRef(key: string): RuntimeReference;
15
16
  export declare function initialValueRef(key: string): RuntimeReference;
16
17
  export type { RuntimeReference };
@@ -91,8 +91,18 @@ export type QueryTransformContext<Row = unknown, Field extends FilterField = Fil
91
91
  export type ConditionOnlyTransform<Input = unknown, Row = any, Field extends FilterField = any> = {
92
92
  toQuery: (input: Input, context: QueryTransformContext<Row, Field>) => TransformConditionResult;
93
93
  };
94
+ /**
95
+ * How a leaf's value reads outside the form, as in the applied-filter pills:
96
+ * the text after the field and the operator. `input` is the value the control
97
+ * holds, which is never empty here; `context.format` formats a value the way
98
+ * the leaf's control does by default, so a formatter can build on it.
99
+ */
100
+ export type FilterValueFormatter<Input = unknown> = (input: Input, context: FilterValueFormatContext) => string;
101
+ export type FilterValueFormatContext = {
102
+ format: (value: unknown) => string;
103
+ };
94
104
  export type FilterTransform<Input = unknown, Row = any, Field extends FilterField = any> = {
95
- toQuery?: (input: Input, context: QueryTransformContext<Row, Field>) => TransformResult;
105
+ toQuery: (input: Input, context: QueryTransformContext<Row, Field>) => TransformResult;
96
106
  };
97
107
  export type FilterControl = {
98
108
  type: 'text';
@@ -158,73 +168,85 @@ export type FilterControl = {
158
168
  * `fieldLabel` is what the applied-filter pill shows in place of the field
159
169
  * path. Without it a filter whose transform owns the query has no way to read
160
170
  * nicely in the pill except by putting a display string in `field`, which the
161
- * row-typed checks then have to reject.
171
+ * row-typed checks then have to reject. `formatValue` does the same for the
172
+ * value, which otherwise reads as the control shows it.
162
173
  */
163
174
  export type FilterExpr = {
164
175
  type: 'equals';
165
176
  field: FilterField;
166
177
  value: FilterControl;
167
178
  fieldLabel?: string;
179
+ formatValue?: FilterValueFormatter<any>;
168
180
  transform?: FilterTransform<any>;
169
181
  } | {
170
182
  type: 'notEquals';
171
183
  field: FilterField;
172
184
  value: FilterControl;
173
185
  fieldLabel?: string;
186
+ formatValue?: FilterValueFormatter<any>;
174
187
  transform?: FilterTransform<any>;
175
188
  } | {
176
189
  type: 'greaterThan';
177
190
  field: FilterField;
178
191
  value: FilterControl;
179
192
  fieldLabel?: string;
193
+ formatValue?: FilterValueFormatter<any>;
180
194
  transform?: FilterTransform<any>;
181
195
  } | {
182
196
  type: 'lessThan';
183
197
  field: FilterField;
184
198
  value: FilterControl;
185
199
  fieldLabel?: string;
200
+ formatValue?: FilterValueFormatter<any>;
186
201
  transform?: FilterTransform<any>;
187
202
  } | {
188
203
  type: 'greaterThanOrEqual';
189
204
  field: FilterField;
190
205
  value: FilterControl;
191
206
  fieldLabel?: string;
207
+ formatValue?: FilterValueFormatter<any>;
192
208
  transform?: FilterTransform<any>;
193
209
  } | {
194
210
  type: 'lessThanOrEqual';
195
211
  field: FilterField;
196
212
  value: FilterControl;
197
213
  fieldLabel?: string;
214
+ formatValue?: FilterValueFormatter<any>;
198
215
  transform?: FilterTransform<any>;
199
216
  } | {
200
217
  type: 'in';
201
218
  field: FilterField;
202
219
  value: FilterControl;
203
220
  fieldLabel?: string;
221
+ formatValue?: FilterValueFormatter<any>;
204
222
  transform?: FilterTransform<any>;
205
223
  } | {
206
224
  type: 'notIn';
207
225
  field: FilterField;
208
226
  value: FilterControl;
209
227
  fieldLabel?: string;
228
+ formatValue?: FilterValueFormatter<any>;
210
229
  transform?: FilterTransform<any>;
211
230
  } | {
212
231
  type: 'like';
213
232
  field: FilterField;
214
233
  value: FilterControl;
215
234
  fieldLabel?: string;
235
+ formatValue?: FilterValueFormatter<any>;
216
236
  transform?: FilterTransform<any>;
217
237
  } | {
218
238
  type: 'iLike';
219
239
  field: FilterField;
220
240
  value: FilterControl;
221
241
  fieldLabel?: string;
242
+ formatValue?: FilterValueFormatter<any>;
222
243
  transform?: FilterTransform<any>;
223
244
  } | {
224
245
  type: 'isNull';
225
246
  field: FilterField;
226
247
  value: FilterControl;
227
248
  fieldLabel?: string;
249
+ formatValue?: FilterValueFormatter<any>;
228
250
  transform?: FilterTransform<any>;
229
251
  } | {
230
252
  type: 'and';
@@ -1,13 +1,14 @@
1
1
  import React from "react";
2
2
  import { CellRenderer, TableColumnDefinitionFooter } from "./column-definition";
3
3
  import { NoRowsComponent, RowExpansionRuntimeEntry } from "./view";
4
- import { QueryTransformContext, TransformResult, SuggestionFetcher } from "./filters";
4
+ import { QueryTransformContext, TransformResult, SuggestionFetcher, FilterValueFormatter } from "./filters";
5
5
  import type { ViewMigrationChain } from "./view-migrations";
6
6
  export type Runtime = {
7
7
  cellRenderers: Record<string, CellRenderer>;
8
8
  queryTransforms: Record<string, {
9
9
  toQuery: (input: any, context: QueryTransformContext) => TransformResult;
10
10
  }>;
11
+ valueFormatters?: Record<string, FilterValueFormatter<any>>;
11
12
  noRowsComponents: Record<string, NoRowsComponent>;
12
13
  rowExpansions?: Record<string, RowExpansionRuntimeEntry>;
13
14
  columnFooters?: Record<string, TableColumnDefinitionFooter>;
@@ -5,7 +5,7 @@ import type { Runtime } from './runtime';
5
5
  import type { HasuraFilterExpression, HasuraOrderBy } from './graphql';
6
6
  import { type OrderDirection } from './order-direction';
7
7
  export type RuntimeReference = {
8
- section: 'cellRenderers' | 'noRowsComponents' | 'rowExpansions' | 'columnFooters' | 'customFilterComponents' | 'queryTransforms' | 'initialValues' | 'suggestionFetchers' | 'viewMigrations';
8
+ section: 'cellRenderers' | 'noRowsComponents' | 'rowExpansions' | 'columnFooters' | 'customFilterComponents' | 'queryTransforms' | 'valueFormatters' | 'initialValues' | 'suggestionFetchers' | 'viewMigrations';
9
9
  key: string;
10
10
  };
11
11
  export declare function resolveRuntimeReference<T>(reference: RuntimeReference, externalRuntime: Runtime | undefined, builtInRuntime: Runtime): T;
@@ -80,66 +80,77 @@ export type FilterExprJson = {
80
80
  field: FilterFieldJson;
81
81
  value: FilterControlJson;
82
82
  fieldLabel?: string;
83
+ formatValue?: RuntimeReference;
83
84
  transform?: RuntimeReference;
84
85
  } | {
85
86
  type: 'notEquals';
86
87
  field: FilterFieldJson;
87
88
  value: FilterControlJson;
88
89
  fieldLabel?: string;
90
+ formatValue?: RuntimeReference;
89
91
  transform?: RuntimeReference;
90
92
  } | {
91
93
  type: 'greaterThan';
92
94
  field: FilterFieldJson;
93
95
  value: FilterControlJson;
94
96
  fieldLabel?: string;
97
+ formatValue?: RuntimeReference;
95
98
  transform?: RuntimeReference;
96
99
  } | {
97
100
  type: 'lessThan';
98
101
  field: FilterFieldJson;
99
102
  value: FilterControlJson;
100
103
  fieldLabel?: string;
104
+ formatValue?: RuntimeReference;
101
105
  transform?: RuntimeReference;
102
106
  } | {
103
107
  type: 'greaterThanOrEqual';
104
108
  field: FilterFieldJson;
105
109
  value: FilterControlJson;
106
110
  fieldLabel?: string;
111
+ formatValue?: RuntimeReference;
107
112
  transform?: RuntimeReference;
108
113
  } | {
109
114
  type: 'lessThanOrEqual';
110
115
  field: FilterFieldJson;
111
116
  value: FilterControlJson;
112
117
  fieldLabel?: string;
118
+ formatValue?: RuntimeReference;
113
119
  transform?: RuntimeReference;
114
120
  } | {
115
121
  type: 'in';
116
122
  field: FilterFieldJson;
117
123
  value: FilterControlJson;
118
124
  fieldLabel?: string;
125
+ formatValue?: RuntimeReference;
119
126
  transform?: RuntimeReference;
120
127
  } | {
121
128
  type: 'notIn';
122
129
  field: FilterFieldJson;
123
130
  value: FilterControlJson;
124
131
  fieldLabel?: string;
132
+ formatValue?: RuntimeReference;
125
133
  transform?: RuntimeReference;
126
134
  } | {
127
135
  type: 'like';
128
136
  field: FilterFieldJson;
129
137
  value: FilterControlJson;
130
138
  fieldLabel?: string;
139
+ formatValue?: RuntimeReference;
131
140
  transform?: RuntimeReference;
132
141
  } | {
133
142
  type: 'iLike';
134
143
  field: FilterFieldJson;
135
144
  value: FilterControlJson;
136
145
  fieldLabel?: string;
146
+ formatValue?: RuntimeReference;
137
147
  transform?: RuntimeReference;
138
148
  } | {
139
149
  type: 'isNull';
140
150
  field: FilterFieldJson;
141
151
  value: FilterControlJson;
142
152
  fieldLabel?: string;
153
+ formatValue?: RuntimeReference;
143
154
  transform?: RuntimeReference;
144
155
  } | {
145
156
  type: 'and';
@@ -16,7 +16,7 @@ export type { Runtime } from '../framework/runtime';
16
16
  export type { ColumnDefinition } from '../framework/column-definition';
17
17
  export type { FieldQuery } from '../framework/column-definition';
18
18
  export type { FilterState } from '../framework/state';
19
- export type { FilterSchema, FilterField, QueryTransformContext, SuggestionFetcher, TransformConditionResult } from '../framework/filters';
19
+ export type { FilterSchema, FilterField, FilterValueFormatter, FilterValueFormatContext, QueryTransformContext, SuggestionFetcher, TransformConditionResult } from '../framework/filters';
20
20
  export { TransformResult } from '../framework/filters';
21
21
  export * as FilterValue from '../framework/filterValue';
22
22
  export { FilterExpr } from '../dsl/filterExpr';
@@ -323,6 +323,31 @@ FilterExpr.in({
323
323
  Without a `fieldLabel` the pill falls back to the field path, as before. A range
324
324
  gives the same label to both of its bounds.
325
325
 
326
+ ### How a value reads in the pill
327
+
328
+ The pill shows the value the user picked, the way its control shows it — never
329
+ what a transform sends to the query. `formatValue` replaces that format for one
330
+ leaf, for a value only the filter knows how to read. Its `input` is typed by the
331
+ control, like a transform's, and `context.format` is the control's own format
332
+ to build on:
333
+
334
+ ```ts
335
+ FilterExpr.equals({
336
+ field: 'amount',
337
+ control: FilterControl.number(),
338
+ formatValue: (input, { format }) => `${format(input)} SEK`,
339
+ // input: number | null
340
+ transform: toCents
341
+ });
342
+ ```
343
+
344
+ Unlike a transform it changes nothing at query time, so the leaf stays checked.
345
+ It is per leaf, so it works at any depth of an `and`/`or`/`not` tree, and a
346
+ range gives it to both of its bounds. On a `customOperator` control the result
347
+ replaces both the operator and the value. A formatter that throws falls back to
348
+ the control's format. A JSON view references one from the runtime's
349
+ `valueFormatters`.
350
+
326
351
  ### customOperator
327
352
 
328
353
  A `customOperator` control stores the chosen operator beside the value, and only
@@ -7,6 +7,7 @@ It is defined in `src/framework/runtime.ts`:
7
7
  export type Runtime = {
8
8
  cellRenderers: Record<string, CellRenderer | React.ComponentType<any>>;
9
9
  queryTransforms: Record<string, { toQuery: (input: any) => TransformResult }>;
10
+ valueFormatters?: Record<string, (input: any, context: { format: (value: unknown) => string }) => string>;
10
11
  noRowsComponents: Record<string, NoRowsComponent | React.ComponentType<any>>;
11
12
  customFilterComponents: Record<string, React.ComponentType<any>>;
12
13
  initialValues: Record<string, any>;
@@ -19,6 +20,7 @@ export type Runtime = {
19
20
  |---------|---------|-----------------|------------|
20
21
  | `cellRenderers` | Render table cells | Column `cellRenderer` | `(props) => ReactNode` |
21
22
  | `queryTransforms` | Preprocess raw filter form values into GraphQL-ready values | Filter expression `transform` | `{ toQuery(input) => { value, operator?, ... } }` |
23
+ | `valueFormatters` | Show a filter leaf's value in the applied-filter pills (optional) | Filter expression `formatValue` | `(input, { format }) => string` |
22
24
  | `noRowsComponents` | UI when a data query yields zero rows | View `noRowsComponent` | `(props) => ReactNode` |
23
25
  | `customFilterComponents` | Custom input widgets inside filter forms | Filter control `{ type: 'custom' }` | React component |
24
26
  | `initialValues` | Dynamic initial values resolved at parse time | Any `initialValue` field | any |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kronor/dtv",
3
- "version": "10.0.1",
3
+ "version": "10.1.0",
4
4
  "description": "Schema-driven, declarative table views with advanced filtering, data fetching and AI-assisted filter generation.",
5
5
  "repository": {
6
6
  "type": "git",