@mdbase-dev/ui 0.2.1 → 0.2.3
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 +119 -0
- package/css/controls.css +37 -0
- package/css/date-picker.css +176 -0
- package/css/filter-builder.css +336 -0
- package/css/properties.css +398 -0
- package/dist/AnchoredPopover.d.ts +20 -0
- package/dist/AnchoredPopover.js +100 -0
- package/dist/Combo.d.ts +41 -0
- package/dist/Combo.js +82 -0
- package/dist/DatePicker.d.ts +69 -0
- package/dist/DatePicker.js +317 -0
- package/dist/Menu.d.ts +3 -1
- package/dist/Menu.js +3 -2
- package/dist/QuickCreate.js +1 -16
- package/dist/date-picker-model.d.ts +31 -0
- package/dist/date-picker-model.js +97 -0
- package/dist/filter-builder/AddFilter.d.ts +13 -0
- package/dist/filter-builder/AddFilter.js +295 -0
- package/dist/filter-builder/FilterBuilder.d.ts +36 -0
- package/dist/filter-builder/FilterBuilder.js +225 -0
- package/dist/filter-builder/FilterPill.d.ts +17 -0
- package/dist/filter-builder/FilterPill.js +494 -0
- package/dist/filter-builder/context.d.ts +40 -0
- package/dist/filter-builder/context.js +67 -0
- package/dist/filter-builder/editors.d.ts +31 -0
- package/dist/filter-builder/editors.js +294 -0
- package/dist/filter-builder/filter-ast.d.ts +13 -0
- package/dist/filter-builder/filter-ast.js +382 -0
- package/dist/filter-builder/labels.d.ts +99 -0
- package/dist/filter-builder/labels.js +555 -0
- package/dist/filter-builder/menu.d.ts +21 -0
- package/dist/filter-builder/menu.js +60 -0
- package/dist/filter-builder/operators.d.ts +30 -0
- package/dist/filter-builder/operators.js +192 -0
- package/dist/filter-builder/tree.d.ts +12 -0
- package/dist/filter-builder/tree.js +61 -0
- package/dist/filter-builder/types.d.ts +69 -0
- package/dist/filter-builder/types.js +48 -0
- package/dist/filter-builder.d.ts +13 -0
- package/dist/filter-builder.js +10 -0
- package/dist/glyphs.d.ts +32 -0
- package/dist/glyphs.js +143 -0
- package/dist/locales/de/messages.json +1 -1
- package/dist/locales/en/messages.json +1 -1
- package/dist/locales/es/messages.json +1 -1
- package/dist/locales/fr/messages.json +1 -1
- package/dist/locales/it/messages.json +1 -1
- package/dist/locales/ja/messages.json +1 -1
- package/dist/locales/ko/messages.json +1 -1
- package/dist/locales/pt/messages.json +1 -1
- package/dist/locales/ru/messages.json +1 -1
- package/dist/locales/zh/messages.json +1 -1
- package/dist/popover.d.ts +5 -0
- package/dist/popover.js +15 -0
- package/dist/properties/PropertyList.d.ts +31 -0
- package/dist/properties/PropertyList.js +233 -0
- package/dist/properties/PropertyValue.d.ts +42 -0
- package/dist/properties/PropertyValue.js +304 -0
- package/dist/properties/fields.d.ts +20 -0
- package/dist/properties/fields.js +81 -0
- package/dist/properties/labels.d.ts +29 -0
- package/dist/properties/labels.js +127 -0
- package/dist/properties/model.d.ts +52 -0
- package/dist/properties/model.js +195 -0
- package/dist/properties.d.ts +13 -0
- package/dist/properties.js +9 -0
- package/package.json +26 -3
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useLayoutEffect } from "react";
|
|
3
|
+
import { Glyph } from "../glyphs.js";
|
|
4
|
+
import { Menu } from "../Menu.js";
|
|
5
|
+
/**
|
|
6
|
+
* One of the bar's menus: the shared menu beside the pill segment that opened it. Rows with
|
|
7
|
+
* `checked` set are a single choice; the menu opens on the chosen one.
|
|
8
|
+
*/
|
|
9
|
+
export function FilterMenu({
|
|
10
|
+
open,
|
|
11
|
+
anchor,
|
|
12
|
+
label,
|
|
13
|
+
items,
|
|
14
|
+
onClose
|
|
15
|
+
}) {
|
|
16
|
+
const id = useId();
|
|
17
|
+
// The shared menu opens on its first row; a single choice opens on the row that is chosen.
|
|
18
|
+
useLayoutEffect(() => {
|
|
19
|
+
if (open) document.getElementById(id)?.querySelector('[role="menuitemradio"][aria-checked="true"]')?.focus({
|
|
20
|
+
preventScroll: true
|
|
21
|
+
});
|
|
22
|
+
}, [open, id]);
|
|
23
|
+
return _jsx(Menu, {
|
|
24
|
+
open: open,
|
|
25
|
+
id: id,
|
|
26
|
+
label: label,
|
|
27
|
+
className: "mdbase-menu mdbase-filter-menu",
|
|
28
|
+
triggerRef: anchor,
|
|
29
|
+
align: "start",
|
|
30
|
+
onClose: refocus => {
|
|
31
|
+
if (refocus) anchor.current?.focus({
|
|
32
|
+
preventScroll: true
|
|
33
|
+
});
|
|
34
|
+
onClose(refocus);
|
|
35
|
+
},
|
|
36
|
+
children: items.map((item, index) => item === "separator" ? _jsx("div", {
|
|
37
|
+
className: "mdbase-menu-divider",
|
|
38
|
+
role: "separator"
|
|
39
|
+
}, `separator-${String(index)}`) : _jsxs("button", {
|
|
40
|
+
type: "button",
|
|
41
|
+
className: item.danger ? "mdbase-menu-item is-danger" : "mdbase-menu-item",
|
|
42
|
+
role: item.checked === undefined ? "menuitem" : "menuitemradio",
|
|
43
|
+
"aria-checked": item.checked,
|
|
44
|
+
onClick: item.onSelect,
|
|
45
|
+
children: [item.icon, _jsx("span", {
|
|
46
|
+
className: "mdbase-filter-menu-label",
|
|
47
|
+
children: item.label
|
|
48
|
+
}), item.hint && _jsx("span", {
|
|
49
|
+
className: "mdbase-filter-hint",
|
|
50
|
+
children: item.hint
|
|
51
|
+
}), item.checked && _jsx("span", {
|
|
52
|
+
className: "mdbase-filter-menu-check",
|
|
53
|
+
children: _jsx(Glyph, {
|
|
54
|
+
name: "check",
|
|
55
|
+
size: 12
|
|
56
|
+
})
|
|
57
|
+
})]
|
|
58
|
+
}, item.label))
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FilterLabels } from "./labels.js";
|
|
2
|
+
import type { FilterContextVar, FilterFieldKind, FilterFieldSpec, FilterOperator, FilterRule, FilterValue } from "./types.js";
|
|
3
|
+
/** An operator as a menu shows it. */
|
|
4
|
+
export type OperatorOption = {
|
|
5
|
+
op: FilterOperator;
|
|
6
|
+
label: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const UNARY: ReadonlySet<FilterOperator>;
|
|
9
|
+
export declare const MULTI: ReadonlySet<FilterOperator>;
|
|
10
|
+
export declare const labelOf: (field: FilterFieldSpec) => string;
|
|
11
|
+
/** The operator a new rule on a field of `kind` starts with. */
|
|
12
|
+
export declare const defaultOperator: (kind: FilterFieldKind) => FilterOperator;
|
|
13
|
+
export declare function operatorLabel(labels: FilterLabels, kind: FilterFieldKind, op: FilterOperator): string;
|
|
14
|
+
/** The operator menu for `rule`: a rule holding several values shows `is any of` and `is none of` in place of equality. */
|
|
15
|
+
export declare function operatorMenu(labels: FilterLabels, kind: FilterFieldKind, rule: FilterRule): OperatorOption[];
|
|
16
|
+
/** The kind of value a rule compares against: `year is` takes a number and text operators take text. */
|
|
17
|
+
export declare function valueKindOf(kind: FilterFieldKind, op: FilterOperator): FilterFieldKind;
|
|
18
|
+
/** A field the query mentions but the host does not describe; its kind follows the rule. */
|
|
19
|
+
export declare function inferField(rule: FilterRule): FilterFieldSpec;
|
|
20
|
+
/** The `this.*` bindings a value of `kind` can compare against. */
|
|
21
|
+
export declare const varsFor: (vars: FilterContextVar[], kind: FilterFieldKind) => FilterContextVar[];
|
|
22
|
+
/** `rule` with a single `value`, or as a draft without one. */
|
|
23
|
+
export declare function withValue(rule: FilterRule, value?: FilterValue, valueKind?: "literal" | "context"): FilterRule;
|
|
24
|
+
/** The literals an equality rule matches: its `values`, or its one literal `value`. */
|
|
25
|
+
export declare function selection(rule: FilterRule): FilterValue[];
|
|
26
|
+
export declare const isNegated: (rule: FilterRule) => boolean;
|
|
27
|
+
/** An equality rule matching `values`: one value is `eq`/`neq`, several are `in`/`not_in`, none is a draft. */
|
|
28
|
+
export declare function withSelection(rule: FilterRule, values: FilterValue[], negated: boolean): FilterRule;
|
|
29
|
+
/** `rule` under operator `op`, keeping whatever value still fits. */
|
|
30
|
+
export declare function changeOperator(rule: FilterRule, field: FilterFieldSpec, op: FilterOperator): FilterRule;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
const EMPTY = [{
|
|
2
|
+
op: "is_empty",
|
|
3
|
+
name: "isEmpty"
|
|
4
|
+
}, {
|
|
5
|
+
op: "is_not_empty",
|
|
6
|
+
name: "isNotEmpty"
|
|
7
|
+
}];
|
|
8
|
+
const TEXT = [{
|
|
9
|
+
op: "contains",
|
|
10
|
+
name: "contains"
|
|
11
|
+
}, {
|
|
12
|
+
op: "not_contains",
|
|
13
|
+
name: "doesNotContain"
|
|
14
|
+
}, {
|
|
15
|
+
op: "starts_with",
|
|
16
|
+
name: "startsWith"
|
|
17
|
+
}, {
|
|
18
|
+
op: "ends_with",
|
|
19
|
+
name: "endsWith"
|
|
20
|
+
}];
|
|
21
|
+
const NUMERIC = [{
|
|
22
|
+
op: "eq",
|
|
23
|
+
symbol: "="
|
|
24
|
+
}, {
|
|
25
|
+
op: "neq",
|
|
26
|
+
symbol: "≠"
|
|
27
|
+
}, {
|
|
28
|
+
op: "gt",
|
|
29
|
+
symbol: ">"
|
|
30
|
+
}, {
|
|
31
|
+
op: "gte",
|
|
32
|
+
symbol: "≥"
|
|
33
|
+
}, {
|
|
34
|
+
op: "lt",
|
|
35
|
+
symbol: "<"
|
|
36
|
+
}, {
|
|
37
|
+
op: "lte",
|
|
38
|
+
symbol: "≤"
|
|
39
|
+
}];
|
|
40
|
+
const IS = [{
|
|
41
|
+
op: "eq",
|
|
42
|
+
name: "is"
|
|
43
|
+
}, {
|
|
44
|
+
op: "neq",
|
|
45
|
+
name: "isNot"
|
|
46
|
+
}];
|
|
47
|
+
const IS_ANY = {
|
|
48
|
+
op: "in",
|
|
49
|
+
name: "isAnyOf"
|
|
50
|
+
};
|
|
51
|
+
const IS_NONE = {
|
|
52
|
+
op: "not_in",
|
|
53
|
+
name: "isNoneOf"
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Operators each field kind offers, in menu order; the first is the default
|
|
57
|
+
* for a new rule. `in` and `not_in` are not listed: equality turns into them
|
|
58
|
+
* when a rule holds several values (see `operatorMenu`).
|
|
59
|
+
*/
|
|
60
|
+
const OPERATORS = {
|
|
61
|
+
string: [...IS, ...TEXT, ...EMPTY],
|
|
62
|
+
number: [...NUMERIC, ...EMPTY],
|
|
63
|
+
date: [...IS, {
|
|
64
|
+
op: "gt",
|
|
65
|
+
name: "after"
|
|
66
|
+
}, {
|
|
67
|
+
op: "gte",
|
|
68
|
+
name: "onOrAfter"
|
|
69
|
+
}, {
|
|
70
|
+
op: "lt",
|
|
71
|
+
name: "before"
|
|
72
|
+
}, {
|
|
73
|
+
op: "lte",
|
|
74
|
+
name: "onOrBefore"
|
|
75
|
+
}, {
|
|
76
|
+
op: "year_eq",
|
|
77
|
+
name: "yearIs"
|
|
78
|
+
}, ...EMPTY],
|
|
79
|
+
enum: [...IS, ...EMPTY],
|
|
80
|
+
boolean: [...IS, ...EMPTY],
|
|
81
|
+
formula: [...NUMERIC, ...TEXT, ...EMPTY]
|
|
82
|
+
};
|
|
83
|
+
export const UNARY = new Set(["is_empty", "is_not_empty"]);
|
|
84
|
+
export const MULTI = new Set(["in", "not_in"]);
|
|
85
|
+
const EQUALITY = new Set(["eq", "neq", "in", "not_in"]);
|
|
86
|
+
const NEGATED = new Set(["neq", "not_in"]);
|
|
87
|
+
const TEXT_OPS = new Set(TEXT.map(item => item.op));
|
|
88
|
+
export const labelOf = field => field.label ?? field.key;
|
|
89
|
+
/** The operator a new rule on a field of `kind` starts with. */
|
|
90
|
+
export const defaultOperator = kind => OPERATORS[kind][0]?.op ?? "eq";
|
|
91
|
+
const option = (labels, entry) => ({
|
|
92
|
+
op: entry.op,
|
|
93
|
+
label: "symbol" in entry ? entry.symbol : labels[entry.name]
|
|
94
|
+
});
|
|
95
|
+
export function operatorLabel(labels, kind, op) {
|
|
96
|
+
const entry = [...OPERATORS[kind], IS_ANY, IS_NONE, ...NUMERIC, ...TEXT, ...EMPTY].find(item => item.op === op);
|
|
97
|
+
return entry ? option(labels, entry).label : labels.yearIs;
|
|
98
|
+
}
|
|
99
|
+
/** The operator menu for `rule`: a rule holding several values shows `is any of` and `is none of` in place of equality. */
|
|
100
|
+
export function operatorMenu(labels, kind, rule) {
|
|
101
|
+
const multi = MULTI.has(rule.op);
|
|
102
|
+
const menu = OPERATORS[kind].map(item => option(labels, !multi ? item : item.op === "eq" ? IS_ANY : item.op === "neq" ? IS_NONE : item));
|
|
103
|
+
return menu.some(item => item.op === rule.op) ? menu : [{
|
|
104
|
+
op: rule.op,
|
|
105
|
+
label: operatorLabel(labels, kind, rule.op)
|
|
106
|
+
}, ...menu];
|
|
107
|
+
}
|
|
108
|
+
/** The kind of value a rule compares against: `year is` takes a number and text operators take text. */
|
|
109
|
+
export function valueKindOf(kind, op) {
|
|
110
|
+
if (op === "year_eq") return "number";
|
|
111
|
+
if (TEXT_OPS.has(op)) return "string";
|
|
112
|
+
return kind;
|
|
113
|
+
}
|
|
114
|
+
/** A field the query mentions but the host does not describe; its kind follows the rule. */
|
|
115
|
+
export function inferField(rule) {
|
|
116
|
+
const {
|
|
117
|
+
op,
|
|
118
|
+
value
|
|
119
|
+
} = rule;
|
|
120
|
+
const sample = value ?? rule.values?.[0];
|
|
121
|
+
const kind = op === "year_eq" ? "date" : typeof sample === "number" ? "number" : typeof sample === "boolean" ? "boolean" : "string";
|
|
122
|
+
return {
|
|
123
|
+
key: rule.field,
|
|
124
|
+
kind
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const textual = kind => kind === "string" || kind === "enum" || kind === "formula";
|
|
128
|
+
/** The `this.*` bindings a value of `kind` can compare against. */
|
|
129
|
+
export const varsFor = (vars, kind) => vars.filter(item => item.kind === kind || textual(item.kind) && textual(kind) || kind === "formula" && item.kind === "number");
|
|
130
|
+
/** `rule` with a single `value`, or as a draft without one. */
|
|
131
|
+
export function withValue(rule, value, valueKind = "literal") {
|
|
132
|
+
const {
|
|
133
|
+
kind,
|
|
134
|
+
id,
|
|
135
|
+
field,
|
|
136
|
+
op
|
|
137
|
+
} = rule;
|
|
138
|
+
return value === undefined ? {
|
|
139
|
+
kind,
|
|
140
|
+
id,
|
|
141
|
+
field,
|
|
142
|
+
op
|
|
143
|
+
} : {
|
|
144
|
+
kind,
|
|
145
|
+
id,
|
|
146
|
+
field,
|
|
147
|
+
op,
|
|
148
|
+
value,
|
|
149
|
+
valueKind
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/** The literals an equality rule matches: its `values`, or its one literal `value`. */
|
|
153
|
+
export function selection(rule) {
|
|
154
|
+
if (MULTI.has(rule.op)) return rule.values ?? [];
|
|
155
|
+
return EQUALITY.has(rule.op) && rule.valueKind !== "context" && rule.value !== undefined ? [rule.value] : [];
|
|
156
|
+
}
|
|
157
|
+
export const isNegated = rule => NEGATED.has(rule.op);
|
|
158
|
+
/** An equality rule matching `values`: one value is `eq`/`neq`, several are `in`/`not_in`, none is a draft. */
|
|
159
|
+
export function withSelection(rule, values, negated) {
|
|
160
|
+
const {
|
|
161
|
+
kind,
|
|
162
|
+
id,
|
|
163
|
+
field
|
|
164
|
+
} = rule;
|
|
165
|
+
if (values.length > 1) return {
|
|
166
|
+
kind,
|
|
167
|
+
id,
|
|
168
|
+
field,
|
|
169
|
+
op: negated ? "not_in" : "in",
|
|
170
|
+
values
|
|
171
|
+
};
|
|
172
|
+
return withValue({
|
|
173
|
+
kind,
|
|
174
|
+
id,
|
|
175
|
+
field,
|
|
176
|
+
op: negated ? "neq" : "eq"
|
|
177
|
+
}, values[0]);
|
|
178
|
+
}
|
|
179
|
+
/** `rule` under operator `op`, keeping whatever value still fits. */
|
|
180
|
+
export function changeOperator(rule, field, op) {
|
|
181
|
+
if (op === rule.op) return rule;
|
|
182
|
+
const values = selection(rule);
|
|
183
|
+
if (EQUALITY.has(op) && values.length) return withSelection(rule, values, NEGATED.has(op));
|
|
184
|
+
const keep = !UNARY.has(op) && !MULTI.has(rule.op) && rule.value !== undefined && valueKindOf(field.kind, op) === valueKindOf(field.kind, rule.op);
|
|
185
|
+
return keep ? withValue({
|
|
186
|
+
...rule,
|
|
187
|
+
op
|
|
188
|
+
}, rule.value, rule.valueKind) : withValue({
|
|
189
|
+
...rule,
|
|
190
|
+
op
|
|
191
|
+
});
|
|
192
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FilterGroup, FilterNode } from "./types.js";
|
|
2
|
+
/** An id no parsed node uses (`parseWhere` numbers its nodes `n0`, `n1`, …). */
|
|
3
|
+
export declare const newId: () => string;
|
|
4
|
+
/** `node` and its descendants under fresh ids, so it can join another tree without clashing. */
|
|
5
|
+
export declare function reid(node: FilterNode): FilterNode;
|
|
6
|
+
export declare function findNode(group: FilterGroup, id: string): FilterNode | null;
|
|
7
|
+
/** `root` with the node `id` replaced by `next`, or removed when `next` is null. The root itself is replaced, never removed. */
|
|
8
|
+
export declare function replaceNode(root: FilterGroup, id: string, next: FilterNode | null): FilterGroup;
|
|
9
|
+
/** `root` with `node` replacing the child of the same id in group `groupId`, or appended there. A missing group means the root. */
|
|
10
|
+
export declare function upsertChild(root: FilterGroup, groupId: string, node: FilterNode): FilterGroup;
|
|
11
|
+
/** `root` joined with `node` by `&&`, keeping the root's own combinator inside its own group. */
|
|
12
|
+
export declare function appendAll(root: FilterGroup, node: FilterNode): FilterGroup;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
let created = 0;
|
|
2
|
+
/** An id no parsed node uses (`parseWhere` numbers its nodes `n0`, `n1`, …). */
|
|
3
|
+
export const newId = () => `u${++created}`;
|
|
4
|
+
/** `node` and its descendants under fresh ids, so it can join another tree without clashing. */
|
|
5
|
+
export function reid(node) {
|
|
6
|
+
return node.kind === "group" ? {
|
|
7
|
+
...node,
|
|
8
|
+
id: newId(),
|
|
9
|
+
children: node.children.map(reid)
|
|
10
|
+
} : {
|
|
11
|
+
...node,
|
|
12
|
+
id: newId()
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function findNode(group, id) {
|
|
16
|
+
for (const child of group.children) {
|
|
17
|
+
if (child.id === id) return child;
|
|
18
|
+
const found = child.kind === "group" ? findNode(child, id) : null;
|
|
19
|
+
if (found) return found;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
/** `root` with the node `id` replaced by `next`, or removed when `next` is null. The root itself is replaced, never removed. */
|
|
24
|
+
export function replaceNode(root, id, next) {
|
|
25
|
+
if (root.id === id) return next?.kind === "group" ? next : {
|
|
26
|
+
...root,
|
|
27
|
+
children: []
|
|
28
|
+
};
|
|
29
|
+
const walk = group => ({
|
|
30
|
+
...group,
|
|
31
|
+
children: group.children.flatMap(child => {
|
|
32
|
+
if (child.id === id) return next ? [next] : [];
|
|
33
|
+
return child.kind === "group" ? [walk(child)] : [child];
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
return walk(root);
|
|
37
|
+
}
|
|
38
|
+
/** `root` with `node` replacing the child of the same id in group `groupId`, or appended there. A missing group means the root. */
|
|
39
|
+
export function upsertChild(root, groupId, node) {
|
|
40
|
+
const target = root.id === groupId ? root : findNode(root, groupId);
|
|
41
|
+
const group = target?.kind === "group" ? target : root;
|
|
42
|
+
const children = group.children.some(child => child.id === node.id) ? group.children.map(child => child.id === node.id ? node : child) : [...group.children, node];
|
|
43
|
+
return replaceNode(root, group.id, {
|
|
44
|
+
...group,
|
|
45
|
+
children
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** `root` joined with `node` by `&&`, keeping the root's own combinator inside its own group. */
|
|
49
|
+
export function appendAll(root, node) {
|
|
50
|
+
if (root.combinator === "all" || !root.children.length) return {
|
|
51
|
+
...root,
|
|
52
|
+
combinator: "all",
|
|
53
|
+
children: [...root.children, node]
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
kind: "group",
|
|
57
|
+
id: newId(),
|
|
58
|
+
combinator: "all",
|
|
59
|
+
children: [root, node]
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** How a field's values are compared and edited. `formula` fields hold computed values of either text or number. */
|
|
2
|
+
export type FilterFieldKind = "string" | "number" | "boolean" | "date" | "enum" | "formula";
|
|
3
|
+
/** A record field the builder can filter on. */
|
|
4
|
+
export interface FilterFieldSpec {
|
|
5
|
+
/** Frontmatter key, as written in the `where` expression. */
|
|
6
|
+
key: string;
|
|
7
|
+
/** Display name; defaults to `key`. */
|
|
8
|
+
label?: string;
|
|
9
|
+
kind: FilterFieldKind;
|
|
10
|
+
/** Allowed values for `enum` fields, offered as choices. */
|
|
11
|
+
enumValues?: FilterValue[];
|
|
12
|
+
}
|
|
13
|
+
/** A `this.*` context binding a rule can compare against. */
|
|
14
|
+
export interface FilterContextVar {
|
|
15
|
+
/** The CEL expression, e.g. `this.fiscal_year`. */
|
|
16
|
+
expr: string;
|
|
17
|
+
/** Display name, e.g. `fiscal_year`. */
|
|
18
|
+
label: string;
|
|
19
|
+
/** Kind of the bound value; decides which rules offer it. */
|
|
20
|
+
kind: FilterFieldKind;
|
|
21
|
+
/** The current value, shown as a hint, e.g. `2026`. */
|
|
22
|
+
preview?: string;
|
|
23
|
+
}
|
|
24
|
+
export type FilterValue = string | number | boolean;
|
|
25
|
+
/**
|
|
26
|
+
* `year_eq` compares the year of a date field (`date.year() == 2026`).
|
|
27
|
+
* `in` and `not_in` match any or none of several literals (`values`).
|
|
28
|
+
* `is_empty` and `is_not_empty` take no value.
|
|
29
|
+
*/
|
|
30
|
+
export type FilterOperator = "eq" | "neq" | "in" | "not_in" | "gt" | "gte" | "lt" | "lte" | "contains" | "not_contains" | "starts_with" | "ends_with" | "year_eq" | "is_empty" | "is_not_empty";
|
|
31
|
+
/**
|
|
32
|
+
* One visual condition. A rule without a `value` (or, for `in` and `not_in`,
|
|
33
|
+
* without `values`), other than a unary one, is a draft: it is shown but
|
|
34
|
+
* compiles to nothing until a value is chosen.
|
|
35
|
+
*/
|
|
36
|
+
export interface FilterRule {
|
|
37
|
+
kind: "rule";
|
|
38
|
+
id: string;
|
|
39
|
+
field: string;
|
|
40
|
+
op: FilterOperator;
|
|
41
|
+
/** A literal, or the context expression (`this.foo`) when `valueKind` is `context`. */
|
|
42
|
+
value?: FilterValue;
|
|
43
|
+
valueKind?: "literal" | "context";
|
|
44
|
+
/** The literals `in` and `not_in` compare against, in order. */
|
|
45
|
+
values?: FilterValue[];
|
|
46
|
+
}
|
|
47
|
+
/** A condition kept as CEL text, for anything a rule cannot express. Empty text compiles to nothing. */
|
|
48
|
+
export interface FilterExpr {
|
|
49
|
+
kind: "expr";
|
|
50
|
+
id: string;
|
|
51
|
+
expression: string;
|
|
52
|
+
}
|
|
53
|
+
/** `all` joins children with `&&`, `any` with `||`, and `none` negates their disjunction. */
|
|
54
|
+
export type FilterCombinator = "all" | "any" | "none";
|
|
55
|
+
export interface FilterGroup {
|
|
56
|
+
kind: "group";
|
|
57
|
+
id: string;
|
|
58
|
+
combinator: FilterCombinator;
|
|
59
|
+
children: FilterNode[];
|
|
60
|
+
}
|
|
61
|
+
export type FilterNode = FilterGroup | FilterRule | FilterExpr;
|
|
62
|
+
/**
|
|
63
|
+
* Derives filterable fields from a type's JSON Schema (`_types/<type>.md`).
|
|
64
|
+
*
|
|
65
|
+
* Scalar schema properties come first, in declaration order; objects, arrays
|
|
66
|
+
* and untyped properties are skipped. Columns the schema does not describe
|
|
67
|
+
* follow as `string` fields, so a schema-less table still gets a field list.
|
|
68
|
+
*/
|
|
69
|
+
export declare function fieldsFromJsonSchema(schema: Record<string, unknown> | null, fallbackColumns: string[], columnLabels?: Record<string, string>): FilterFieldSpec[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const isRecord = value => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
const isEnumValue = value => typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
3
|
+
function kindOf(property) {
|
|
4
|
+
if (Array.isArray(property.enum) && property.enum.length && property.enum.every(isEnumValue)) return "enum";
|
|
5
|
+
const types = Array.isArray(property.type) ? property.type.filter(type => type !== "null") : [property.type];
|
|
6
|
+
if (types.length !== 1) return null;
|
|
7
|
+
switch (types[0]) {
|
|
8
|
+
case "string":
|
|
9
|
+
return property.format === "date" || property.format === "date-time" ? "date" : "string";
|
|
10
|
+
case "number":
|
|
11
|
+
case "integer":
|
|
12
|
+
return "number";
|
|
13
|
+
case "boolean":
|
|
14
|
+
return "boolean";
|
|
15
|
+
default:
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Derives filterable fields from a type's JSON Schema (`_types/<type>.md`).
|
|
21
|
+
*
|
|
22
|
+
* Scalar schema properties come first, in declaration order; objects, arrays
|
|
23
|
+
* and untyped properties are skipped. Columns the schema does not describe
|
|
24
|
+
* follow as `string` fields, so a schema-less table still gets a field list.
|
|
25
|
+
*/
|
|
26
|
+
export function fieldsFromJsonSchema(schema, fallbackColumns, columnLabels = {}) {
|
|
27
|
+
const properties = schema && isRecord(schema.properties) ? schema.properties : {};
|
|
28
|
+
const spec = (key, kind, enumValues) => ({
|
|
29
|
+
key,
|
|
30
|
+
kind,
|
|
31
|
+
...(columnLabels[key] && {
|
|
32
|
+
label: columnLabels[key]
|
|
33
|
+
}),
|
|
34
|
+
...(enumValues && {
|
|
35
|
+
enumValues
|
|
36
|
+
})
|
|
37
|
+
});
|
|
38
|
+
const fields = [];
|
|
39
|
+
for (const [key, property] of Object.entries(properties)) {
|
|
40
|
+
if (!isRecord(property)) continue;
|
|
41
|
+
const kind = kindOf(property);
|
|
42
|
+
if (kind) fields.push(spec(key, kind, kind === "enum" ? property.enum : undefined));
|
|
43
|
+
}
|
|
44
|
+
for (const column of fallbackColumns) {
|
|
45
|
+
if (!Object.hasOwn(properties, column)) fields.push(spec(column, "string"));
|
|
46
|
+
}
|
|
47
|
+
return fields;
|
|
48
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@mdbase-dev/ui/filter-builder`: an inline filter bar over a CEL `where` expression. Its
|
|
3
|
+
* filters are pills in nested all/any/none groups; a pill is a rule on a field (with one value
|
|
4
|
+
* or several) or a raw expression. Import `@mdbase-dev/ui/filter-builder.css` and
|
|
5
|
+
* `@mdbase-dev/ui/controls.css` once.
|
|
6
|
+
*/
|
|
7
|
+
export { FilterBuilder } from "./filter-builder/FilterBuilder.js";
|
|
8
|
+
export type { FilterBuilderProps } from "./filter-builder/FilterBuilder.js";
|
|
9
|
+
export { compileWhere, countActive, parseWhere, ruleToExpression, tryExpressionToRule } from "./filter-builder/filter-ast.js";
|
|
10
|
+
export { filterLabels } from "./filter-builder/labels.js";
|
|
11
|
+
export type { FilterLabels } from "./filter-builder/labels.js";
|
|
12
|
+
export { fieldsFromJsonSchema } from "./filter-builder/types.js";
|
|
13
|
+
export type { FilterCombinator, FilterContextVar, FilterExpr, FilterFieldKind, FilterFieldSpec, FilterGroup, FilterNode, FilterOperator, FilterRule, FilterValue, } from "./filter-builder/types.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@mdbase-dev/ui/filter-builder`: an inline filter bar over a CEL `where` expression. Its
|
|
3
|
+
* filters are pills in nested all/any/none groups; a pill is a rule on a field (with one value
|
|
4
|
+
* or several) or a raw expression. Import `@mdbase-dev/ui/filter-builder.css` and
|
|
5
|
+
* `@mdbase-dev/ui/controls.css` once.
|
|
6
|
+
*/
|
|
7
|
+
export { FilterBuilder } from "./filter-builder/FilterBuilder.js";
|
|
8
|
+
export { compileWhere, countActive, parseWhere, ruleToExpression, tryExpressionToRule } from "./filter-builder/filter-ast.js";
|
|
9
|
+
export { filterLabels } from "./filter-builder/labels.js";
|
|
10
|
+
export { fieldsFromJsonSchema } from "./filter-builder/types.js";
|
package/dist/glyphs.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { JSX } from "react";
|
|
2
|
+
/** The small line drawings the record controls share, on a 16 unit grid. */
|
|
3
|
+
declare const glyphs: {
|
|
4
|
+
readonly plus: JSX.Element;
|
|
5
|
+
readonly close: JSX.Element;
|
|
6
|
+
readonly check: JSX.Element;
|
|
7
|
+
readonly down: JSX.Element;
|
|
8
|
+
readonly up: JSX.Element;
|
|
9
|
+
readonly left: JSX.Element;
|
|
10
|
+
readonly right: JSX.Element;
|
|
11
|
+
readonly filter: JSX.Element;
|
|
12
|
+
readonly search: JSX.Element;
|
|
13
|
+
readonly group: JSX.Element;
|
|
14
|
+
readonly swap: JSX.Element;
|
|
15
|
+
readonly convert: JSX.Element;
|
|
16
|
+
readonly remove: JSX.Element;
|
|
17
|
+
readonly enter: JSX.Element;
|
|
18
|
+
readonly text: JSX.Element;
|
|
19
|
+
readonly number: JSX.Element;
|
|
20
|
+
readonly date: JSX.Element;
|
|
21
|
+
readonly boolean: JSX.Element;
|
|
22
|
+
readonly choice: JSX.Element;
|
|
23
|
+
readonly list: JSX.Element;
|
|
24
|
+
readonly link: JSX.Element;
|
|
25
|
+
readonly formula: JSX.Element;
|
|
26
|
+
};
|
|
27
|
+
export type GlyphName = keyof typeof glyphs;
|
|
28
|
+
export declare function Glyph({ name, size }: {
|
|
29
|
+
readonly name: GlyphName;
|
|
30
|
+
readonly size?: number;
|
|
31
|
+
}): JSX.Element;
|
|
32
|
+
export {};
|
package/dist/glyphs.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
const dot = (cx, cy, r = 0.75) => _jsx("circle", {
|
|
3
|
+
cx: cx,
|
|
4
|
+
cy: cy,
|
|
5
|
+
r: r,
|
|
6
|
+
fill: "currentColor",
|
|
7
|
+
stroke: "none"
|
|
8
|
+
});
|
|
9
|
+
/** The small line drawings the record controls share, on a 16 unit grid. */
|
|
10
|
+
const glyphs = {
|
|
11
|
+
plus: _jsx("path", {
|
|
12
|
+
d: "M8 3.5v9M3.5 8h9"
|
|
13
|
+
}),
|
|
14
|
+
close: _jsx("path", {
|
|
15
|
+
d: "m4 4 8 8M12 4l-8 8"
|
|
16
|
+
}),
|
|
17
|
+
check: _jsx("path", {
|
|
18
|
+
d: "m3.5 8.5 3 3 6-7"
|
|
19
|
+
}),
|
|
20
|
+
down: _jsx("path", {
|
|
21
|
+
d: "m4 6 4 4 4-4"
|
|
22
|
+
}),
|
|
23
|
+
up: _jsx("path", {
|
|
24
|
+
d: "m4 10 4-4 4 4"
|
|
25
|
+
}),
|
|
26
|
+
left: _jsx("path", {
|
|
27
|
+
d: "M10 4 6 8l4 4"
|
|
28
|
+
}),
|
|
29
|
+
right: _jsx("path", {
|
|
30
|
+
d: "m6 4 4 4-4 4"
|
|
31
|
+
}),
|
|
32
|
+
filter: _jsx("path", {
|
|
33
|
+
d: "M2.5 4h11M4.5 8h7M6.5 12h3"
|
|
34
|
+
}),
|
|
35
|
+
search: _jsxs(_Fragment, {
|
|
36
|
+
children: [_jsx("circle", {
|
|
37
|
+
cx: "7",
|
|
38
|
+
cy: "7",
|
|
39
|
+
r: "4.25"
|
|
40
|
+
}), _jsx("path", {
|
|
41
|
+
d: "m10.2 10.2 3.3 3.3"
|
|
42
|
+
})]
|
|
43
|
+
}),
|
|
44
|
+
group: _jsxs(_Fragment, {
|
|
45
|
+
children: [_jsx("rect", {
|
|
46
|
+
x: "2",
|
|
47
|
+
y: "6.25",
|
|
48
|
+
width: "3.5",
|
|
49
|
+
height: "3.5",
|
|
50
|
+
rx: "0.75"
|
|
51
|
+
}), _jsx("rect", {
|
|
52
|
+
x: "10.5",
|
|
53
|
+
y: "2.75",
|
|
54
|
+
width: "3.5",
|
|
55
|
+
height: "3.5",
|
|
56
|
+
rx: "0.75"
|
|
57
|
+
}), _jsx("rect", {
|
|
58
|
+
x: "10.5",
|
|
59
|
+
y: "9.75",
|
|
60
|
+
width: "3.5",
|
|
61
|
+
height: "3.5",
|
|
62
|
+
rx: "0.75"
|
|
63
|
+
}), _jsx("path", {
|
|
64
|
+
d: "M5.5 8H8M8 4.5v7M8 4.5h2.5M8 11.5h2.5"
|
|
65
|
+
})]
|
|
66
|
+
}),
|
|
67
|
+
swap: _jsx("path", {
|
|
68
|
+
d: "M2.5 5.5h11M11 3l2.5 2.5L11 8M13.5 10.5h-11M5 8l-2.5 2.5L5 13"
|
|
69
|
+
}),
|
|
70
|
+
convert: _jsx("path", {
|
|
71
|
+
d: "m3 13 6.5-6.5M11.5 2.5v3M10 4h3M12.5 9v2M11.5 10h2M5 3v2M4 4h2"
|
|
72
|
+
}),
|
|
73
|
+
remove: _jsx("path", {
|
|
74
|
+
d: "M3 4.5h10M6 4.5V3h4v1.5M4.5 4.5l.6 8.5h5.8l.6-8.5M6.75 7v3.5M9.25 7v3.5"
|
|
75
|
+
}),
|
|
76
|
+
enter: _jsx("path", {
|
|
77
|
+
d: "M12.5 3.5V9H4M6.5 6.5 4 9l2.5 2.5"
|
|
78
|
+
}),
|
|
79
|
+
text: _jsx("path", {
|
|
80
|
+
d: "M3 4.5h10M3 8h10M3 11.5h6"
|
|
81
|
+
}),
|
|
82
|
+
number: _jsx("path", {
|
|
83
|
+
d: "m6.5 2.5-1.5 11M11 2.5l-1.5 11M3 6h10.5M2.5 10H13"
|
|
84
|
+
}),
|
|
85
|
+
date: _jsxs(_Fragment, {
|
|
86
|
+
children: [_jsx("rect", {
|
|
87
|
+
x: "2.5",
|
|
88
|
+
y: "3.5",
|
|
89
|
+
width: "11",
|
|
90
|
+
height: "10",
|
|
91
|
+
rx: "1.5"
|
|
92
|
+
}), _jsx("path", {
|
|
93
|
+
d: "M2.5 6.5h11M5.5 2v3M10.5 2v3"
|
|
94
|
+
})]
|
|
95
|
+
}),
|
|
96
|
+
boolean: _jsxs(_Fragment, {
|
|
97
|
+
children: [_jsx("rect", {
|
|
98
|
+
x: "1.5",
|
|
99
|
+
y: "4.5",
|
|
100
|
+
width: "13",
|
|
101
|
+
height: "7",
|
|
102
|
+
rx: "3.5"
|
|
103
|
+
}), _jsx("circle", {
|
|
104
|
+
cx: "5.25",
|
|
105
|
+
cy: "8",
|
|
106
|
+
r: "1.5"
|
|
107
|
+
})]
|
|
108
|
+
}),
|
|
109
|
+
choice: _jsxs(_Fragment, {
|
|
110
|
+
children: [_jsx("path", {
|
|
111
|
+
d: "M2.5 8.2V3.5a1 1 0 0 1 1-1h4.7l5.3 5.3a1 1 0 0 1 0 1.4l-4.3 4.3a1 1 0 0 1-1.4 0z"
|
|
112
|
+
}), dot(5.5, 5.5)]
|
|
113
|
+
}),
|
|
114
|
+
list: _jsxs(_Fragment, {
|
|
115
|
+
children: [_jsx("path", {
|
|
116
|
+
d: "M6 4.5h7.5M6 8h7.5M6 11.5h7.5"
|
|
117
|
+
}), dot(3, 4.5), dot(3, 8), dot(3, 11.5)]
|
|
118
|
+
}),
|
|
119
|
+
link: _jsx("path", {
|
|
120
|
+
d: "M7 9a2.5 2.5 0 0 0 3.5 0l2-2A2.5 2.5 0 0 0 9 3.5l-.75.75M9 7a2.5 2.5 0 0 0-3.5 0l-2 2A2.5 2.5 0 0 0 7 12.5l.75-.75"
|
|
121
|
+
}),
|
|
122
|
+
formula: _jsx("path", {
|
|
123
|
+
d: "M11 3.2c-1.6-.6-2.7.1-3 1.8L6.7 12c-.3 1.5-1.3 2-2.5 1.5M5 7h5"
|
|
124
|
+
})
|
|
125
|
+
};
|
|
126
|
+
export function Glyph({
|
|
127
|
+
name,
|
|
128
|
+
size = 14
|
|
129
|
+
}) {
|
|
130
|
+
return _jsx("svg", {
|
|
131
|
+
className: "mdbase-glyph",
|
|
132
|
+
viewBox: "0 0 16 16",
|
|
133
|
+
width: size,
|
|
134
|
+
height: size,
|
|
135
|
+
fill: "none",
|
|
136
|
+
stroke: "currentColor",
|
|
137
|
+
strokeWidth: "1.5",
|
|
138
|
+
strokeLinecap: "round",
|
|
139
|
+
strokeLinejoin: "round",
|
|
140
|
+
"aria-hidden": "true",
|
|
141
|
+
children: glyphs[name]
|
|
142
|
+
});
|
|
143
|
+
}
|