@sincpro/web-ui 2.6.0 → 2.8.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 +59 -1
- package/dist/components/search/CriteriaBuilder.d.ts +14 -2
- package/dist/components/search/CriteriaBuilder.js +26 -6
- package/dist/components/search/FieldPath.js +16 -7
- package/dist/components/search/FilterBuilder.d.ts +72 -5
- package/dist/components/search/FilterBuilder.js +395 -50
- package/dist/components/search/RuleValue.d.ts +21 -0
- package/dist/components/search/RuleValue.js +143 -0
- package/dist/components/search/SearchBar.d.ts +25 -2
- package/dist/components/search/SearchBar.js +397 -39
- package/dist/components/search/context.d.ts +49 -0
- package/dist/components/search/context.js +28 -0
- package/dist/components/search/criteria.d.ts +20 -0
- package/dist/components/search/index.d.ts +7 -4
- package/dist/components/search/index.js +9 -3
- package/dist/components/search/model.d.ts +16 -1
- package/dist/components/search/model.js +5 -0
- package/dist/components/search/query.d.ts +88 -4
- package/dist/components/search/query.js +262 -62
- package/dist/components/search/records.d.ts +91 -0
- package/dist/components/search/records.js +115 -0
- package/dist/foundations/copy.d.ts +21 -0
- package/dist/foundations/copy.es.js +12 -0
- package/dist/foundations/copy.js +12 -0
- package/package.json +1 -1
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { choiceLabel } from "./model.js";
|
|
4
|
+
import { useUiCopy } from "../../foundations/copy.js";
|
|
5
|
+
import { Picker } from "../../primitives/picker/Picker.js";
|
|
6
|
+
import { TagsField } from "../../primitives/picker/TagsField.js";
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
/* ==================================================================== *
|
|
9
|
+
* THE VALUE OF A RULE — written when it is written, CHOSEN when it is chosen.
|
|
10
|
+
*
|
|
11
|
+
* Context: the builder asked every value in a text box, so «Cliente es …» wanted an id
|
|
12
|
+
* nobody knows and «Estado es …» a raw value («paid») nobody reads. The control now follows
|
|
13
|
+
* the field and the comparison:
|
|
14
|
+
*
|
|
15
|
+
* a relation es / no es one record, picked by name (Picker)
|
|
16
|
+
* está en / no … several, as tags (TagsField)
|
|
17
|
+
* an enum es / no es its words (select)
|
|
18
|
+
* está en / no … several of its words (TagsField)
|
|
19
|
+
* yes / no es sí · no (select)
|
|
20
|
+
* anything else what is typed (input)
|
|
21
|
+
*
|
|
22
|
+
* What is read is the name; what travels is the key — `values` keeps both. An application
|
|
23
|
+
* that wants a field drawn its own way answers it from `valueEditor` before any of this.
|
|
24
|
+
* ==================================================================== */
|
|
25
|
+
/** The comparisons that take several values. */
|
|
26
|
+
export function takesMany(operator) {
|
|
27
|
+
return operator === "in" || operator === "not in";
|
|
28
|
+
}
|
|
29
|
+
/** How long the control waits for the typing to stop before asking. */
|
|
30
|
+
const DELAY = 200;
|
|
31
|
+
/**
|
|
32
|
+
* The records of a relation as a control's options: asked of the application's search as
|
|
33
|
+
* the person types, the older question cancelled. It asks nothing until the list is opened —
|
|
34
|
+
* a filter with five rules does not fetch five lists nobody looks at.
|
|
35
|
+
*/
|
|
36
|
+
function useRecordOptions(search, target, path) {
|
|
37
|
+
const [text, setText] = useState(null);
|
|
38
|
+
const [found, setFound] = useState({
|
|
39
|
+
loading: false,
|
|
40
|
+
options: [],
|
|
41
|
+
});
|
|
42
|
+
const key = target === undefined || text === null ? "" : `${path.join(".")}\u0000${text}`;
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (search === undefined || target === undefined || text === null)
|
|
45
|
+
return;
|
|
46
|
+
const controller = new AbortController();
|
|
47
|
+
setFound((before) => ({ loading: true, options: before.options }));
|
|
48
|
+
const timer = window.setTimeout(() => {
|
|
49
|
+
search({ relation: target.relation, path, text, signal: controller.signal }).then((options) => {
|
|
50
|
+
if (!controller.signal.aborted)
|
|
51
|
+
setFound({ loading: false, options: [...options] });
|
|
52
|
+
}, () => {
|
|
53
|
+
if (!controller.signal.aborted)
|
|
54
|
+
setFound({ loading: false, options: [] });
|
|
55
|
+
});
|
|
56
|
+
}, DELAY);
|
|
57
|
+
return () => {
|
|
58
|
+
window.clearTimeout(timer);
|
|
59
|
+
controller.abort();
|
|
60
|
+
};
|
|
61
|
+
// Asked again only when WHAT is searched changes.
|
|
62
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
63
|
+
}, [key]);
|
|
64
|
+
return { ...found, search: (next) => setText(next) };
|
|
65
|
+
}
|
|
66
|
+
/** A relation's records: one, picked by name, or several as tags. */
|
|
67
|
+
function RecordValue({ props, target, searchRecords, browseRecords, relationLabel, }) {
|
|
68
|
+
const { values, onValues, path, operator, label } = props;
|
|
69
|
+
/* «Buscar más…»: the application's chooser over the relation —its own list, its own bar—,
|
|
70
|
+
on what was typed. Several add to what is there; one replaces it. */
|
|
71
|
+
const browse = browseRecords === undefined
|
|
72
|
+
? undefined
|
|
73
|
+
: (text, many) => browseRecords({
|
|
74
|
+
relation: target.relation,
|
|
75
|
+
path,
|
|
76
|
+
label: relationLabel,
|
|
77
|
+
text,
|
|
78
|
+
many,
|
|
79
|
+
chosen: values,
|
|
80
|
+
choose: (records) => onValues(many
|
|
81
|
+
? [
|
|
82
|
+
...values,
|
|
83
|
+
...records.filter((one) => !values.some((each) => String(each.id) === String(one.id))),
|
|
84
|
+
]
|
|
85
|
+
: records.slice(0, 1)),
|
|
86
|
+
});
|
|
87
|
+
const found = useRecordOptions(searchRecords, target, path);
|
|
88
|
+
/* A record read back has no name until the builder asks for it (`useRecordNames`). */
|
|
89
|
+
const shown = (one) => (one.name === "" ? `#${one.id}` : one.name);
|
|
90
|
+
if (takesMany(operator)) {
|
|
91
|
+
const known = new Map();
|
|
92
|
+
for (const one of [...values, ...found.options])
|
|
93
|
+
known.set(String(one.id), one);
|
|
94
|
+
return (_jsx("span", { className: "min-w-0 flex-1 basis-56", children: _jsx(TagsField, { mode: "edit", "aria-label": label, value: values.map((one) => String(one.id)), options: [...known.values()].map((one) => ({
|
|
95
|
+
value: String(one.id),
|
|
96
|
+
label: shown(one),
|
|
97
|
+
...(one.meta === undefined ? {} : { meta: one.meta }),
|
|
98
|
+
})), onSearch: found.search, ...(browse === undefined
|
|
99
|
+
? {}
|
|
100
|
+
: { onBrowseAll: (text) => browse(text, true) }), onChange: (next) => onValues(next
|
|
101
|
+
.map((id) => known.get(id))
|
|
102
|
+
.filter((one) => one !== undefined)) }) }));
|
|
103
|
+
}
|
|
104
|
+
const chosen = values[0];
|
|
105
|
+
return (_jsx("span", { className: "min-w-0 flex-1 basis-56", children: _jsx(Picker, { mode: "edit", "aria-label": label, items: found.options.map((one) => ({
|
|
106
|
+
id: String(one.id),
|
|
107
|
+
name: one.name,
|
|
108
|
+
...(one.meta === undefined ? {} : { meta: one.meta }),
|
|
109
|
+
})), loading: found.loading && found.options.length === 0, ...(chosen === undefined
|
|
110
|
+
? {}
|
|
111
|
+
: { value: String(chosen.id), valueLabel: shown(chosen) }), onSearch: found.search, ...(browse === undefined
|
|
112
|
+
? {}
|
|
113
|
+
: { onBrowseAll: (text) => browse(text, false) }), onChange: (id) => {
|
|
114
|
+
const one = found.options.find((each) => String(each.id) === id);
|
|
115
|
+
if (one !== undefined)
|
|
116
|
+
onValues([one]);
|
|
117
|
+
}, onClear: () => onValues([]) }) }));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The builder's own value control for a rule — what is drawn when the application's
|
|
121
|
+
* `valueEditor` does not answer for the field.
|
|
122
|
+
*/
|
|
123
|
+
export function RuleValue({ props, target, searchRecords, browseRecords, relationLabel, box, inputType, }) {
|
|
124
|
+
const copy = useUiCopy();
|
|
125
|
+
const { field, name, model, operator, value, onValue, values, onValues, label } = props;
|
|
126
|
+
if (target !== undefined && searchRecords !== undefined) {
|
|
127
|
+
return (_jsx(RecordValue, { props: props, target: target, searchRecords: searchRecords, ...(browseRecords === undefined ? {} : { browseRecords }), relationLabel: relationLabel ?? props.name }));
|
|
128
|
+
}
|
|
129
|
+
const choices = (field.choices ?? []).map((one) => String(one));
|
|
130
|
+
const word = (one) => model === undefined ? one : choiceLabel(model, name, one, copy.locale);
|
|
131
|
+
if (choices.length > 0 && takesMany(operator)) {
|
|
132
|
+
return (_jsx("span", { className: "min-w-0 flex-1 basis-56", children: _jsx(TagsField, { mode: "edit", "aria-label": label, value: values.map((one) => String(one.id)), options: choices.map((one) => ({ value: one, label: word(one) })), onChange: (next) => onValues(next.map((one) => ({ id: one, name: word(one) }))) }) }));
|
|
133
|
+
}
|
|
134
|
+
if (choices.length > 0) {
|
|
135
|
+
return (_jsxs("select", { "aria-label": label, value: value, onChange: (event) => onValue(event.target.value), className: `min-w-0 flex-1 basis-40 rounded-control border px-2 py-1 text-meta ${box}`, children: [_jsx("option", { value: "", children: copy.builder.chooseValue }), choices.map((one) => (_jsx("option", { value: one, children: word(one) }, one)))] }));
|
|
136
|
+
}
|
|
137
|
+
const type = field.type.toLowerCase();
|
|
138
|
+
if (type === "boolean" || type === "bool") {
|
|
139
|
+
/* Sí / no travel as what they are —`true`, `false`—, not as the words. */
|
|
140
|
+
return (_jsxs("select", { "aria-label": label, value: props.raw === true ? "true" : props.raw === false ? "false" : "", onChange: (event) => props.onRaw(event.target.value === "" ? undefined : event.target.value === "true"), className: `min-w-0 flex-1 basis-40 rounded-control border px-2 py-1 text-meta ${box}`, children: [_jsx("option", { value: "", children: copy.builder.chooseValue }), _jsx("option", { value: "true", children: copy.table.yes }), _jsx("option", { value: "false", children: copy.table.no })] }));
|
|
141
|
+
}
|
|
142
|
+
return (_jsx("input", { "aria-label": label, type: inputType, value: value, onChange: (event) => onValue(event.target.value), placeholder: copy.builder.value, className: `min-w-0 flex-1 basis-40 rounded-control border px-2 py-1 text-meta ${box}` }));
|
|
143
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { type BuiltCondition, type BuiltFilter, type CriteriaJoin, type CriteriaPath, type SearchCriteria } from "./criteria.js";
|
|
1
|
+
import { type BuiltCondition, type BuiltFilter, type CriteriaJoin, type CriteriaPath, type RecordOption, type SearchCriteria } from "./criteria.js";
|
|
2
2
|
import type { ModelDefinition } from "./model.js";
|
|
3
|
+
import { type QueryField } from "./query.js";
|
|
4
|
+
import type { BrowseRecords, SearchRecords } from "./records.js";
|
|
3
5
|
import type { Surface } from "../../foundations/types.js";
|
|
4
6
|
import type { MenuGroup } from "../../primitives/menu/Menu.js";
|
|
5
7
|
import { type ReactNode } from "react";
|
|
@@ -80,6 +82,27 @@ export type SearchBarProps = {
|
|
|
80
82
|
* enum's value, a number a numeric field answers): then that goes first and this after it.
|
|
81
83
|
*/
|
|
82
84
|
literal?: (text: string) => BuiltCondition | BuiltFilter | null;
|
|
85
|
+
/**
|
|
86
|
+
* Searches the records of a relation — the application's, the SAME function the builder
|
|
87
|
+
* takes (see `records.ts`). With it a relation's row unfolds (▸, or →) into the records that
|
|
88
|
+
* match what was typed, and choosing one filters by THAT record, sent by its key and read by
|
|
89
|
+
* its name: «Cliente: Andrés Pérez». A second one of the same relation lands in the same chip
|
|
90
|
+
* — «… o Andrés Gutiérrez». A relation that cannot compare a text (its foreign key, a
|
|
91
|
+
* many2many) is offered only this way; `#cliente` goes straight to its records.
|
|
92
|
+
*
|
|
93
|
+
* Absent, the one of the nearest `SearchProvider`.
|
|
94
|
+
*/
|
|
95
|
+
searchRecords?: SearchRecords;
|
|
96
|
+
/** «Buscar más…» under a relation's records: the application's chooser with the whole list
|
|
97
|
+
* —`List onChoose` with its own search bar—, on what was typed. What it answers lands in the
|
|
98
|
+
* relation's chip. Absent, the one of the nearest `SearchProvider`. */
|
|
99
|
+
browseRecords?: BrowseRecords;
|
|
100
|
+
/** How a record found reads in the dropdown — a face, a NIT, a balance —, for one that does
|
|
101
|
+
* not fit «name + meta». The row around it stays the bar's: keys, focus, choosing. */
|
|
102
|
+
renderRecord?: (record: RecordOption, state: {
|
|
103
|
+
field: QueryField;
|
|
104
|
+
query: string;
|
|
105
|
+
}) => ReactNode;
|
|
83
106
|
/** What goes stuck to the field, on its right. */
|
|
84
107
|
trailing?: ReactNode;
|
|
85
108
|
/** How many facets the bar shows before folding the rest into «+N»; the grouping chain
|
|
@@ -88,4 +111,4 @@ export type SearchBarProps = {
|
|
|
88
111
|
/** By default it inherits it from the surface that wraps it. */
|
|
89
112
|
surface?: Surface;
|
|
90
113
|
};
|
|
91
|
-
export declare function SearchBar({ facets, model, criteria, onCriteriaChange, placeholder, filters, grouping, onOpenGroup, favorites, onFilterSelect, onFavoriteSelect, onOpenBuilder, literal, trailing, collapseAfter, surface, }: SearchBarProps): import("react").JSX.Element;
|
|
114
|
+
export declare function SearchBar({ facets, model, criteria, onCriteriaChange, placeholder, filters, grouping, onOpenGroup, favorites, onFilterSelect, onFavoriteSelect, onOpenBuilder, literal, searchRecords: ownSearchRecords, browseRecords: ownBrowseRecords, renderRecord, trailing, collapseAfter, surface, }: SearchBarProps): import("react").JSX.Element;
|