@sincpro/web-ui 2.6.0 → 2.7.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 CHANGED
@@ -267,7 +267,8 @@ of classes that change with the next refactor:
267
267
  The parts: `button`, `icon-button`, `control`, `popover`, `menu`, `menu.trigger`,
268
268
  `menu.section`, `menu.item`, `menu.fold`, `dialog`, `block`, `record-card`, `board.tile`,
269
269
  `board.stat`, `board.stat.value`, `activity.changes`, `file-list`, `file-list.file`, `metric`, `metric.detail`, `tag`, `tags.item`, `search`, `search.field`, `search.chip`,
270
- `search.panel`, `search.suggestions`, `search-entry`, `search-entry.keys`, `table`,
270
+ `search.panel`, `search.suggestions`, `search.suggestion`, `search.record`, `search.advanced`,
271
+ `search.keys`, `search-entry`, `search-entry.keys`, `table`,
271
272
  `table.head`, `topbar`, `topbar.brand`, `topbar.menus`, `topbar.actions`, `topbar.user`,
272
273
  `frame`, `frame.bar`, `frame.sidebar`, `frame.main`, `sidebar`, `sidebar.brand`,
273
274
  `sidebar.user`, `sidebar.search`, `sidebar.nav`, `sidebar.account`, `navigation`,
@@ -296,6 +297,49 @@ written out under it on the left, the control on the right —, every `FormSecti
296
297
  the rail. The layout (`stacked` / `horizontal`) is still the screen's: stacked, the help and the
297
298
  control go under the label. Without the provider nothing changes.
298
299
 
300
+ #### Searching the records of a relation — and extending the search
301
+
302
+ Typing «andres» in the bar offers «Cliente con «andres» ▸», and the ▸ (or →) unfolds the
303
+ CLIENTS that match — the way Odoo's «Search Contact for: …» does. Choosing one filters by that
304
+ record: what travels is its key, what reads is its name («Cliente: Andrés Pérez»), and a second
305
+ one lands in the same chip («… o Andrés Gutiérrez», sent as `in`). An enum unfolds into its
306
+ words («Pagada», sent as `paid`), and the builder asks the same way: «Cliente es [Andrés Pérez]»
307
+ with a picker, «está en» with tags, «está vacío» with nothing.
308
+
309
+ web-ui does not know where the records live. The application gives **one** search, by prop or
310
+ once for the whole app — web-framework wires it to the relation's literal search, the one a
311
+ many2one already asks:
312
+
313
+ ```tsx
314
+ const searchRecords: SearchRecords = ({ relation, path, text, keys, signal }) =>
315
+ keys !== undefined
316
+ ? api.names(relation, keys, { signal }) // name what a filter read back carries
317
+ : api.literalSearch(relation, text, { limit: 8, signal }); // what was typed
318
+
319
+ <SearchProvider searchRecords={searchRecords} valueEditor={myValueEditor}>
320
+ <App /> {/* every SearchBar, FilterBuilder and CriteriaBuilder inside */}
321
+ </SearchProvider>;
322
+ ```
323
+
324
+ `RecordOption.id` is what the relation compares: for a many2one, what its identifying column
325
+ (`identifiedBy`, its foreign key) holds; for a to-many relation, the key of the record on the
326
+ other side (`relationKey`, `id` by default) — the filter crosses it with `through`. An enum's
327
+ words come from `translations.choices`.
328
+
329
+ What can be extended, and how:
330
+
331
+ | You want | Use |
332
+ | ------------------------------------------------ | -------------------------------------------------------------------------------------------- |
333
+ | The records of a relation, from your server | `searchRecords` (prop, or `SearchProvider` once) |
334
+ | A record drawn your way in the dropdown | `SearchBar renderRecord` |
335
+ | «Buscar más…» under the records: your chooser | `SearchBar onBrowseRecords(field, text)` + `withValue` |
336
+ | A field's value drawn your way in the builder | `valueEditor` (prop or `SearchProvider`): answer `undefined` to keep ours |
337
+ | A list that opens filtered (defaults, a link) | `queryField` + `valuesCriteria` / `conditionCriteria` / `toggleNamed` |
338
+ | One more value into a chip, by code | `withValue(criteria, field, record)` |
339
+ | The filter said with names, for your own chip | `describeFilter(selection, model, modelAt, names)`, `FilterBuilder onApply(selection, said)` |
340
+ | Your own builder | `SelectionTree`, `RuleValue`, `liftRecords`, `recordsFilter`, `recordOps` |
341
+ | The words of an operator («no está establecido») | the copy table: `ops` |
342
+
299
343
  #### A guided tour: the screen says what each thing is
300
344
 
301
345
  `Guide` walks a person through a screen: the rest dims, the thing being explained is cut out,
@@ -1,5 +1,7 @@
1
+ import { type ValueEditor } from "./context.js";
1
2
  import type { BuiltFilter } from "./criteria.js";
2
3
  import { type ModelDefinition } from "./model.js";
4
+ import type { SearchRecords } from "./records.js";
3
5
  export interface BuiltSort {
4
6
  field: string;
5
7
  descending?: boolean;
@@ -53,10 +55,15 @@ export interface BuiltCriteria {
53
55
  grouping?: BuiltGrouping;
54
56
  specification?: Record<string, Record<string, never>>;
55
57
  }
56
- export declare function CriteriaBuilder({ model, initial, focus, onApply, onCancel, }: {
58
+ export declare function CriteriaBuilder({ model, initial, focus, onApply, onCancel, searchRecords: ownSearchRecords, valueEditor: ownValueEditor, }: {
57
59
  model: ModelDefinition;
58
60
  /** The criteria that was already set. Reopening has to continue where it was left. */
59
61
  initial?: BuiltCriteria;
62
+ /** Searches the records of a relation: a rule can then be about a relation itself, its
63
+ * records picked by name (see `records.ts`). Absent, the `SearchProvider`'s. */
64
+ searchRecords?: SearchRecords;
65
+ /** The application's own value controls (see `ValueEditor`). Absent, the provider's. */
66
+ valueEditor?: ValueEditor;
60
67
  /**
61
68
  * The condition's field to show first: the chip somebody
62
69
  * touched in the bar.
@@ -66,6 +73,8 @@ export declare function CriteriaBuilder({ model, initial, focus, onApply, onCanc
66
73
  * edit the wrong one.
67
74
  */
68
75
  focus?: string;
69
- onApply: (criteria: BuiltCriteria | undefined) => void;
76
+ /** The criteria applied, and how its filter reads —with the names of the records chosen—
77
+ * for the chip that shows it. */
78
+ onApply: (criteria: BuiltCriteria | undefined, said?: string) => void;
70
79
  onCancel?: () => void;
71
80
  }): import("react").JSX.Element;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import { blankGroup, ExpressionView, SelectionTree, treeFromFilter, treeToFilter, } from "./FilterBuilder.js";
3
+ import { useSearchSettings, } from "./context.js";
4
+ import { blankGroup, describeFilter, ExpressionView, liftRecords, namesOf, SelectionTree, treeFromFilter, treeToFilter, useRecordNames, } from "./FilterBuilder.js";
4
5
  import { LazyJsonEditor } from "./LazyJsonEditor.js";
5
6
  import { filterableNames, } from "./model.js";
6
7
  import { fillCopy, useUiCopy } from "../../foundations/copy.js";
@@ -71,14 +72,26 @@ function nextGrain(grain) {
71
72
  const at = GRAINS.indexOf(grain ?? null);
72
73
  return GRAINS[(at + 1) % GRAINS.length] ?? null;
73
74
  }
74
- export function CriteriaBuilder({ model, initial, focus, onApply, onCancel, }) {
75
+ export function CriteriaBuilder({ model, initial, focus, onApply, onCancel, searchRecords: ownSearchRecords, valueEditor: ownValueEditor, }) {
75
76
  const surface = useSurface();
77
+ const settings = useSearchSettings({
78
+ ...(ownSearchRecords === undefined ? {} : { searchRecords: ownSearchRecords }),
79
+ ...(ownValueEditor === undefined ? {} : { valueEditor: ownValueEditor }),
80
+ });
81
+ /* A filter read back carries keys; with records searchable, the rules about records are
82
+ drawn as such again — by name. */
83
+ const read = (filter) => {
84
+ const node = treeFromFilter(filter);
85
+ return node === null || settings.searchRecords === undefined
86
+ ? node
87
+ : liftRecords(node, model);
88
+ };
76
89
  const names = Object.keys(model.fields).sort();
77
90
  /* Never a relational nor a value-object: neither has a comparison of its own
78
91
  (empty `ops`), so neither serves as a target for a filter, an order or a level. */
79
92
  const scalarNames = filterableNames(model);
80
93
  const sortable = names.filter((name) => model.fields[name]?.sortable === true);
81
- const decomposed = initial?.selection === undefined ? null : treeFromFilter(initial.selection);
94
+ const decomposed = initial?.selection === undefined ? null : read(initial.selection);
82
95
  const [tree, setTree] = useState(() => decomposed !== null && decomposed.kind === "group"
83
96
  ? decomposed
84
97
  : decomposed !== null
@@ -110,6 +123,7 @@ export function CriteriaBuilder({ model, initial, focus, onApply, onCancel, }) {
110
123
  const [fields, setFields] = useState(Object.keys(initial?.specification ?? {}));
111
124
  const [rawText, setRawText] = useState(() => initial === undefined ? "" : JSON.stringify(initial, null, 2));
112
125
  const [rawError, setRawError] = useState(null);
126
+ useRecordNames(tree, settings.searchRecords, (named) => setTree((current) => named(current)));
113
127
  /** What the controls say, together: it is what is applied and what the JSON shows. */
114
128
  function assemble(next) {
115
129
  const selection = next.locked ?? lockedSelection ?? treeToFilter(next.tree ?? tree);
@@ -187,7 +201,7 @@ export function CriteriaBuilder({ model, initial, focus, onApply, onCancel, }) {
187
201
  return;
188
202
  }
189
203
  setRawError(null);
190
- const node = parsed.selection === undefined ? null : treeFromFilter(parsed.selection);
204
+ const node = parsed.selection === undefined ? null : read(parsed.selection);
191
205
  if (node !== null) {
192
206
  setTree(node.kind === "group" ? node : { kind: "group", join: "all", children: [node] });
193
207
  setLockedSelection(null);
@@ -211,7 +225,7 @@ export function CriteriaBuilder({ model, initial, focus, onApply, onCancel, }) {
211
225
  ? "border-chrome-ink/20 bg-chrome text-chrome-ink"
212
226
  : "border-ink/15 bg-surface text-ink";
213
227
  const select = `min-w-0 flex-1 basis-40 rounded-control border px-2 py-1 text-meta ${box}`;
214
- return (_jsxs("div", { className: "flex min-w-0 flex-col gap-3 p-3", children: [_jsxs("section", { className: "flex min-w-0 flex-col gap-2", children: [_jsx(Rule, { label: copy.builder.filter }), _jsx("div", { className: "max-h-72 min-w-0 overflow-auto", children: lockedSelection !== null ? (_jsxs(_Fragment, { children: [_jsx("p", { className: "mb-2 text-meta text-muted", children: copy.search.unreadable }), _jsx(ExpressionView, { node: lockedSelection, model: model })] })) : (_jsx(SelectionTree, { model: model, group: tree, onChange: (next) => push({ tree: next }), ...(focus === undefined ? {} : { focus }) })) })] }), _jsxs("section", { className: "flex min-w-0 flex-col gap-2", children: [_jsx(Rule, { label: copy.builder.order }), _jsxs("div", { className: "flex min-w-0 flex-wrap items-center gap-1", children: [_jsx(Tags, { items: order.map((sort, index) => ({
228
+ return (_jsxs("div", { className: "flex min-w-0 flex-col gap-3 p-3", children: [_jsxs("section", { className: "flex min-w-0 flex-col gap-2", children: [_jsx(Rule, { label: copy.builder.filter }), _jsx("div", { className: "max-h-72 min-w-0 overflow-auto", children: lockedSelection !== null ? (_jsxs(_Fragment, { children: [_jsx("p", { className: "mb-2 text-meta text-muted", children: copy.search.unreadable }), _jsx(ExpressionView, { node: lockedSelection, model: model })] })) : (_jsx(SelectionTree, { model: model, group: tree, onChange: (next) => push({ tree: next }), ...settings, ...(focus === undefined ? {} : { focus }) })) })] }), _jsxs("section", { className: "flex min-w-0 flex-col gap-2", children: [_jsx(Rule, { label: copy.builder.order }), _jsxs("div", { className: "flex min-w-0 flex-wrap items-center gap-1", children: [_jsx(Tags, { items: order.map((sort, index) => ({
215
229
  id: `${sort.field}-${index}`,
216
230
  label: sort.field,
217
231
  note: sort.descending === true ? "↓" : "↑",
@@ -285,5 +299,10 @@ export function CriteriaBuilder({ model, initial, focus, onApply, onCancel, }) {
285
299
  return (_jsx("button", { type: "button", onClick: () => push({
286
300
  fields: chosen ? fields.filter((one) => one !== name) : [...fields, name],
287
301
  }), className: `rounded-pill border px-2 py-0.5 text-micro ${chosen ? chosenTone("canvas") : `text-muted ${box}`}`, children: name }, name));
288
- }) })] }), _jsx(LazyJsonEditor, { value: rawText, onChange: updateRaw, error: rawError, height: "14rem", label: copy.builder.json }), _jsxs("div", { className: "flex gap-2", children: [_jsx(Button, { role: "primary", disabled: rawError !== null, onClick: () => onApply(rawText.trim() === "" ? undefined : JSON.parse(rawText)), children: copy.builder.apply }), onCancel === undefined ? null : (_jsx(Button, { role: "ghost", onClick: onCancel, children: copy.builder.cancel }))] })] }));
302
+ }) })] }), _jsx(LazyJsonEditor, { value: rawText, onChange: updateRaw, error: rawError, height: "14rem", label: copy.builder.json }), _jsxs("div", { className: "flex gap-2", children: [_jsx(Button, { role: "primary", disabled: rawError !== null, onClick: () => {
303
+ const built = rawText.trim() === "" ? undefined : JSON.parse(rawText);
304
+ onApply(built, built?.selection === undefined
305
+ ? undefined
306
+ : describeFilter(built.selection, model, undefined, namesOf(tree)));
307
+ }, children: copy.builder.apply }), onCancel === undefined ? null : (_jsx(Button, { role: "ghost", onClick: onCancel, children: copy.builder.cancel }))] })] }));
289
308
  }
@@ -1,6 +1,8 @@
1
- import type { BuiltFilter, CriteriaJoin, ThroughMatch } from "./criteria.js";
1
+ import { type ValueEditor } from "./context.js";
2
+ import type { BuiltFilter, CriteriaJoin, RecordKey, RecordOption, ThroughMatch } from "./criteria.js";
2
3
  import { type ModelDefinition } from "./model.js";
3
4
  import { type LoadModel, type ModelCache } from "./paths.js";
5
+ import { type RecordTarget, type SearchRecords } from "./records.js";
4
6
  /** Whether this is one of the shapes the filter recognises — condition, or `all`/`any`/
5
7
  * `negate` group. A valid JSON that is none of these —`{}`, `{"foo": 1}`, a loose array—
6
8
  * is not a filter this component knows how to draw, and is treated as a shape error instead of
@@ -27,6 +29,16 @@ export interface TreeCondition {
27
29
  /** The other end, for the operators that compare against two —today `between`—. Empty in
28
30
  * all the others, where it means nothing. */
29
31
  upper?: string;
32
+ /** The value as it travels when it is not the text: `true`, `7`, a list read back — kept so
33
+ * a rule read from a filter goes back out the same, not as the string it is drawn with.
34
+ * Writing in the box drops it: what is written is then the value. */
35
+ raw?: unknown;
36
+ /** The values CHOSEN, with the names they are read by — records of a relation, words of an
37
+ * enum, «está en» a list. What travels is their `id`. */
38
+ values?: RecordOption[];
39
+ /** The rule asks about RECORDS of a relation: `field` is the relation itself, chosen by
40
+ * name and compared by key (`recordsFilter`). */
41
+ target?: RecordTarget;
30
42
  }
31
43
  export interface TreeGroup {
32
44
  kind: "group";
@@ -70,6 +82,36 @@ export declare function treeToFilter(node: TreeNode): BuiltFilter | undefined;
70
82
  * shows it read-only while still letting you touch it as JSON.
71
83
  */
72
84
  export declare function treeFromFilter(filter: unknown): TreeNode | null;
85
+ /** The operators a rule about records offers: one record or several, in or out — and, through
86
+ * a column that admits it, «está vacío». A to-many relation is asked by some of its records
87
+ * or none of them. */
88
+ export declare function recordOps(target: RecordTarget): string[];
89
+ /**
90
+ * The rules about RECORDS of a relation, recognised again in a tree read back.
91
+ *
92
+ * A filter carries keys, never which relation they were chosen from: `customer_id in [7, 9]`
93
+ * is, for the tree, a column compared to two numbers. The model says that `customer_id` is
94
+ * how «Cliente» is compared, so the row is drawn as «Cliente es alguno de [Andrés Pérez,
95
+ * Andrés Gutiérrez]» — the names asked of `searchRecords` by key. The same for a to-many
96
+ * crossed to its key: `{through: "tags", selection: {field: "id", operator: "in", …}}` is
97
+ * «Etiquetas es alguno de …».
98
+ *
99
+ * in {field: "customer_id", operator: "in", value: [7, 9]} (customer_id names Cliente)
100
+ * out {field: "customer", target: Cliente, operator: "in", values: [#7, #9]}
101
+ *
102
+ * Only on the record's own fields: a relation of a related record needs its model, and a row
103
+ * that stays a plain condition still filters the same.
104
+ */
105
+ export declare function liftRecords(node: TreeNode, model: ModelDefinition): TreeNode;
106
+ /** The names of the records a tree has chosen, by relation and key: what says them in a
107
+ * sentence (`describeFilter`) once the tree is applied. */
108
+ export declare function namesOf(node: TreeNode): RecordNames;
109
+ /**
110
+ * Names the records a tree read back carries only by key — ONE question per relation, for the
111
+ * whole tree. Per row, five rows answered at five moments and each wrote its answer over a tree
112
+ * that no longer had the others'. Until it answers, a record reads «#7».
113
+ */
114
+ export declare function useRecordNames(tree: TreeNode, searchRecords: SearchRecords | undefined, apply: (named: (tree: TreeNode) => TreeNode) => void): void;
73
115
  /** The filter said in one line, for a chip or a label.
74
116
  *
75
117
  * in {field: "row_count", operator: ">", value: 1000}, with the model
@@ -95,7 +137,12 @@ export declare function treeFromFilter(filter: unknown): TreeNode | null;
95
137
  export declare function describeFilter(node: BuiltFilter, model?: ModelDefinition,
96
138
  /** The model at the end of a path of relations, when it already arrived: it names the
97
139
  * fields of a `through`. Without it they keep their raw names. */
98
- modelAt?: (path: readonly string[]) => ModelDefinition | undefined): string;
140
+ modelAt?: (path: readonly string[]) => ModelDefinition | undefined,
141
+ /** The names of the records a filter asks for, by relation and key: «Cliente es Andrés
142
+ * Pérez» instead of «customer_id es 7». The builder hands its own (`namesOf`). */
143
+ names?: RecordNames): string;
144
+ /** A record's name by the relation it was chosen from and its key, when it is known. */
145
+ export type RecordNames = (relation: string, key: RecordKey) => string | undefined;
99
146
  /** The filter drawn as it is, recursive: a condition is a line, `all`/`any` are a
100
147
  * group with its label and its children inside, `negate` is «no» wrapping the one it negates.
101
148
  *
@@ -112,11 +159,16 @@ export declare function ExpressionView({ node, model, depth, modelAt, at, }: {
112
159
  /** The path up to here (through the outer `through`s). */
113
160
  at?: readonly string[];
114
161
  }): import("react").JSX.Element;
115
- export declare function SelectionTree({ model, group, onChange, depth, focus, loadModel, models: shared, }: {
162
+ export declare function SelectionTree({ model, group, onChange, depth, focus, loadModel, models: shared, searchRecords: ownSearchRecords, valueEditor: ownValueEditor, }: {
116
163
  model: ModelDefinition;
117
164
  group: TreeGroup;
118
165
  onChange: (next: TreeGroup) => void;
119
166
  depth?: number;
167
+ /** Searches the records of a relation: with it a rule can be about a relation ITSELF,
168
+ * its records picked by name (see `records.ts`). Absent, the `SearchProvider`'s. */
169
+ searchRecords?: SearchRecords;
170
+ /** The application's own value controls (see `ValueEditor`). Absent, the provider's. */
171
+ valueEditor?: ValueEditor;
120
172
  /**
121
173
  * The condition's field to show FIRST: the one somebody touched in
122
174
  * the bar.
@@ -142,7 +194,7 @@ export declare function SelectionTree({ model, group, onChange, depth, focus, lo
142
194
  /** The model cache the rules share. The outer tree builds it. */
143
195
  models?: ModelCache;
144
196
  }): import("react").JSX.Element;
145
- export declare function FilterBuilder({ model, initial, onApply, onCancel, loadModel, models: shared, }: {
197
+ export declare function FilterBuilder({ model, initial, onApply, onCancel, loadModel, models: shared, searchRecords: ownSearchRecords, valueEditor: ownValueEditor, }: {
146
198
  model: ModelDefinition;
147
199
  /** The model at the end of a path of relations. With this, a rule can ask about
148
200
  * a field of a related record: «alguna línea con algún impuesto con monto > 10».
@@ -154,6 +206,12 @@ export declare function FilterBuilder({ model, initial, onApply, onCancel, loadM
154
206
  /** The filter that was already set, if there was one. Reopening the builder with this inside
155
207
  * has to continue where it was left, not start blank. */
156
208
  initial?: BuiltFilter;
157
- onApply: (selection: BuiltFilter | undefined) => void;
209
+ /** The filter applied, and how it reads —with the names of the records chosen, «Cliente es
210
+ * Andrés Pérez»— for the chip that shows it. */
211
+ onApply: (selection: BuiltFilter | undefined, said?: string) => void;
158
212
  onCancel?: () => void;
213
+ /** Searches the records of a relation (see `records.ts`). Absent, the `SearchProvider`'s. */
214
+ searchRecords?: SearchRecords;
215
+ /** The application's own value controls (see `ValueEditor`). Absent, the provider's. */
216
+ valueEditor?: ValueEditor;
159
217
  }): import("react").JSX.Element;