@kronor/dtv 10.0.1 → 10.1.1

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,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import type { FilterControl as FrameworkFilterControl, SuggestionFetcher, SuggestionItem } from '../framework/filters';
2
+ import type { CustomOperatorOption, FilterControl as FrameworkFilterControl, SuggestionFetcher, SuggestionItem } from '../framework/filters';
3
3
  import type * as FilterValue from '../framework/filterValue';
4
4
  import type { MutableTuple } from './filterTyping';
5
5
  export type { SuggestionFetcher, SuggestionItem };
@@ -33,10 +33,7 @@ export type DropdownControl<Items extends readonly ItemControlItem[]> = Omit<Con
33
33
  export type MultiselectControl<Items extends readonly ItemControlItem[]> = Omit<ControlOfType<'multiselect'>, 'items'> & {
34
34
  items: MutableTuple<Items>;
35
35
  };
36
- export type CustomOperatorControl<Operators extends readonly {
37
- label: string;
38
- value: string;
39
- }[], ValueControl extends FrameworkFilterControl> = Omit<ControlOfType<'customOperator'>, 'operators' | 'valueControl'> & {
36
+ export type CustomOperatorControl<Operators extends readonly CustomOperatorOption[], ValueControl extends FrameworkFilterControl> = Omit<ControlOfType<'customOperator'>, 'operators' | 'valueControl'> & {
40
37
  operators: MutableTuple<Operators>;
41
38
  valueControl: ValueControl;
42
39
  };
@@ -79,10 +76,7 @@ export declare const FilterControl: {
79
76
  filterable?: boolean;
80
77
  initialValue?: ControlValue<MultiselectControl<Items>>;
81
78
  }) => MultiselectControl<Items>;
82
- customOperator: <const Operators extends readonly {
83
- label: string;
84
- value: string;
85
- }[], const ValueControl extends FrameworkFilterControl>(options: {
79
+ customOperator: <const Operators extends readonly CustomOperatorOption[], const ValueControl extends FrameworkFilterControl>(options: {
86
80
  label?: string;
87
81
  operators: Operators;
88
82
  valueControl: ValueControl;
@@ -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 };
@@ -65,7 +65,8 @@ export declare function makeFilterFormStateSerializable(node: FilterFormState):
65
65
  /**
66
66
  * Schema-aware emptiness check for a FilterFormState tree.
67
67
  * A leaf is considered empty when its primitive value is '' | null | [] (if array).
68
- * For customOperator controls we look at the nested `value` field inside { operator, value }.
68
+ * For customOperator controls we look at the nested `value` field inside { operator, value },
69
+ * unless the operator takes no value (`is empty`): such a leaf is set.
69
70
  */
70
71
  export declare function isFilterEmpty(state: FilterFormState, schemaExpr: FilterExpr): boolean;
71
72
  /**
@@ -91,8 +91,29 @@ 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;
106
+ };
107
+ /**
108
+ * An operator of a `customOperator` control. `takesValue: false` marks one
109
+ * that filters without a value (`is empty`): the form hides the value control
110
+ * for it, and the filter counts as set although its value is empty. The
111
+ * filter's own transform builds its condition; `hasuraCustomOperator` does not.
112
+ */
113
+ export type CustomOperatorOption = {
114
+ label: string;
115
+ value: string;
116
+ takesValue?: boolean;
96
117
  };
97
118
  export type FilterControl = {
98
119
  type: 'text';
@@ -131,10 +152,7 @@ export type FilterControl = {
131
152
  } | {
132
153
  type: 'customOperator';
133
154
  label?: string;
134
- operators: {
135
- label: string;
136
- value: string;
137
- }[];
155
+ operators: CustomOperatorOption[];
138
156
  valueControl: FilterControl;
139
157
  initialValue?: any;
140
158
  } | {
@@ -158,73 +176,85 @@ export type FilterControl = {
158
176
  * `fieldLabel` is what the applied-filter pill shows in place of the field
159
177
  * path. Without it a filter whose transform owns the query has no way to read
160
178
  * nicely in the pill except by putting a display string in `field`, which the
161
- * row-typed checks then have to reject.
179
+ * row-typed checks then have to reject. `formatValue` does the same for the
180
+ * value, which otherwise reads as the control shows it.
162
181
  */
163
182
  export type FilterExpr = {
164
183
  type: 'equals';
165
184
  field: FilterField;
166
185
  value: FilterControl;
167
186
  fieldLabel?: string;
187
+ formatValue?: FilterValueFormatter<any>;
168
188
  transform?: FilterTransform<any>;
169
189
  } | {
170
190
  type: 'notEquals';
171
191
  field: FilterField;
172
192
  value: FilterControl;
173
193
  fieldLabel?: string;
194
+ formatValue?: FilterValueFormatter<any>;
174
195
  transform?: FilterTransform<any>;
175
196
  } | {
176
197
  type: 'greaterThan';
177
198
  field: FilterField;
178
199
  value: FilterControl;
179
200
  fieldLabel?: string;
201
+ formatValue?: FilterValueFormatter<any>;
180
202
  transform?: FilterTransform<any>;
181
203
  } | {
182
204
  type: 'lessThan';
183
205
  field: FilterField;
184
206
  value: FilterControl;
185
207
  fieldLabel?: string;
208
+ formatValue?: FilterValueFormatter<any>;
186
209
  transform?: FilterTransform<any>;
187
210
  } | {
188
211
  type: 'greaterThanOrEqual';
189
212
  field: FilterField;
190
213
  value: FilterControl;
191
214
  fieldLabel?: string;
215
+ formatValue?: FilterValueFormatter<any>;
192
216
  transform?: FilterTransform<any>;
193
217
  } | {
194
218
  type: 'lessThanOrEqual';
195
219
  field: FilterField;
196
220
  value: FilterControl;
197
221
  fieldLabel?: string;
222
+ formatValue?: FilterValueFormatter<any>;
198
223
  transform?: FilterTransform<any>;
199
224
  } | {
200
225
  type: 'in';
201
226
  field: FilterField;
202
227
  value: FilterControl;
203
228
  fieldLabel?: string;
229
+ formatValue?: FilterValueFormatter<any>;
204
230
  transform?: FilterTransform<any>;
205
231
  } | {
206
232
  type: 'notIn';
207
233
  field: FilterField;
208
234
  value: FilterControl;
209
235
  fieldLabel?: string;
236
+ formatValue?: FilterValueFormatter<any>;
210
237
  transform?: FilterTransform<any>;
211
238
  } | {
212
239
  type: 'like';
213
240
  field: FilterField;
214
241
  value: FilterControl;
215
242
  fieldLabel?: string;
243
+ formatValue?: FilterValueFormatter<any>;
216
244
  transform?: FilterTransform<any>;
217
245
  } | {
218
246
  type: 'iLike';
219
247
  field: FilterField;
220
248
  value: FilterControl;
221
249
  fieldLabel?: string;
250
+ formatValue?: FilterValueFormatter<any>;
222
251
  transform?: FilterTransform<any>;
223
252
  } | {
224
253
  type: 'isNull';
225
254
  field: FilterField;
226
255
  value: FilterControl;
227
256
  fieldLabel?: string;
257
+ formatValue?: FilterValueFormatter<any>;
228
258
  transform?: FilterTransform<any>;
229
259
  } | {
230
260
  type: 'and';
@@ -240,6 +270,8 @@ export declare const SUPPORTED_OPERATORS: {
240
270
  label: string;
241
271
  value: string;
242
272
  }[];
273
+ export declare function customOperatorTakesValue(operators: readonly CustomOperatorOption[], operator: unknown): boolean;
274
+ export declare function defaultCustomOperator(operators: readonly CustomOperatorOption[]): string | undefined;
243
275
  export type SuggestionItem = {
244
276
  label: string;
245
277
  };
@@ -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;
@@ -50,6 +50,7 @@ export type FilterControlJson = {
50
50
  operators: {
51
51
  label: string;
52
52
  value: string;
53
+ takesValue?: boolean;
53
54
  }[];
54
55
  valueControl: FilterControlJson;
55
56
  initialValue?: any;
@@ -80,66 +81,77 @@ export type FilterExprJson = {
80
81
  field: FilterFieldJson;
81
82
  value: FilterControlJson;
82
83
  fieldLabel?: string;
84
+ formatValue?: RuntimeReference;
83
85
  transform?: RuntimeReference;
84
86
  } | {
85
87
  type: 'notEquals';
86
88
  field: FilterFieldJson;
87
89
  value: FilterControlJson;
88
90
  fieldLabel?: string;
91
+ formatValue?: RuntimeReference;
89
92
  transform?: RuntimeReference;
90
93
  } | {
91
94
  type: 'greaterThan';
92
95
  field: FilterFieldJson;
93
96
  value: FilterControlJson;
94
97
  fieldLabel?: string;
98
+ formatValue?: RuntimeReference;
95
99
  transform?: RuntimeReference;
96
100
  } | {
97
101
  type: 'lessThan';
98
102
  field: FilterFieldJson;
99
103
  value: FilterControlJson;
100
104
  fieldLabel?: string;
105
+ formatValue?: RuntimeReference;
101
106
  transform?: RuntimeReference;
102
107
  } | {
103
108
  type: 'greaterThanOrEqual';
104
109
  field: FilterFieldJson;
105
110
  value: FilterControlJson;
106
111
  fieldLabel?: string;
112
+ formatValue?: RuntimeReference;
107
113
  transform?: RuntimeReference;
108
114
  } | {
109
115
  type: 'lessThanOrEqual';
110
116
  field: FilterFieldJson;
111
117
  value: FilterControlJson;
112
118
  fieldLabel?: string;
119
+ formatValue?: RuntimeReference;
113
120
  transform?: RuntimeReference;
114
121
  } | {
115
122
  type: 'in';
116
123
  field: FilterFieldJson;
117
124
  value: FilterControlJson;
118
125
  fieldLabel?: string;
126
+ formatValue?: RuntimeReference;
119
127
  transform?: RuntimeReference;
120
128
  } | {
121
129
  type: 'notIn';
122
130
  field: FilterFieldJson;
123
131
  value: FilterControlJson;
124
132
  fieldLabel?: string;
133
+ formatValue?: RuntimeReference;
125
134
  transform?: RuntimeReference;
126
135
  } | {
127
136
  type: 'like';
128
137
  field: FilterFieldJson;
129
138
  value: FilterControlJson;
130
139
  fieldLabel?: string;
140
+ formatValue?: RuntimeReference;
131
141
  transform?: RuntimeReference;
132
142
  } | {
133
143
  type: 'iLike';
134
144
  field: FilterFieldJson;
135
145
  value: FilterControlJson;
136
146
  fieldLabel?: string;
147
+ formatValue?: RuntimeReference;
137
148
  transform?: RuntimeReference;
138
149
  } | {
139
150
  type: 'isNull';
140
151
  field: FilterFieldJson;
141
152
  value: FilterControlJson;
142
153
  fieldLabel?: string;
154
+ formatValue?: RuntimeReference;
143
155
  transform?: RuntimeReference;
144
156
  } | {
145
157
  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 { CustomOperatorOption, 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
@@ -342,6 +367,23 @@ Annotate a hand-written one `ConditionOnlyTransform` — a transform typed
342
367
  `FilterTransform` may return either, which is not enough. A transform object
343
368
  with no `toQuery` at all is rejected too; it would do nothing at query time.
344
369
 
370
+ An operator marked `takesValue: false` filters without a value, as `is empty`
371
+ does. The form hides the value control while it is chosen, the filter counts as
372
+ set with an empty value, and the pill reads `Comment is empty`. A new filter
373
+ starts with the first operator that takes a value. The filter's own transform
374
+ gets `{ operator, value }` with an empty `value` and builds the condition;
375
+ `hasuraCustomOperator` has no condition for an empty value.
376
+
377
+ ```ts
378
+ FilterControl.customOperator({
379
+ operators: [
380
+ { label: 'contains', value: 'contains' },
381
+ { label: 'is empty', value: 'isEmpty', takesValue: false }
382
+ ],
383
+ valueControl: FilterControl.text()
384
+ });
385
+ ```
386
+
345
387
  ### Row-scoped builders
346
388
 
347
389
  `FilterExpr` accepts any field name and is checked when the expression reaches
@@ -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.1",
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",