@momoi-labs/kiso-react 0.8.1 → 0.10.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 +80 -2
- package/dist/app-shell.d.ts +11 -1
- package/dist/app-shell.js +33 -8
- package/dist/chip-input.d.ts +2 -2
- package/dist/chip-input.js +77 -16
- package/dist/command-palette.d.ts +1 -1
- package/dist/command-palette.js +18 -4
- package/dist/empty-state.js +2 -2
- package/dist/filter-expression.d.ts +49 -0
- package/dist/filter-expression.js +264 -0
- package/dist/filter-input.d.ts +14 -0
- package/dist/filter-input.js +251 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.js +10 -0
- package/dist/page-header.d.ts +2 -1
- package/dist/page-header.js +3 -3
- package/dist/sidebar.d.ts +3 -1
- package/dist/sidebar.js +2 -2
- package/dist/split.js +3 -1
- package/dist/status-badge.d.ts +8 -0
- package/dist/status-badge.js +8 -0
- package/dist/styles.css +43 -3
- package/dist/theme-selector.d.ts +2 -1
- package/dist/theme-selector.js +32 -3
- package/package.json +2 -2
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
const textOperators = ["=", "!=", "IN", "CONTAINS"];
|
|
2
|
+
const numberOperators = ["=", "!=", ">", ">=", "<", "<=", "IN"];
|
|
3
|
+
export function filterOperators(field) {
|
|
4
|
+
const supported = field.type === "number" ? numberOperators : textOperators;
|
|
5
|
+
const allowed = field.nullable ? [...supported, "IS NULL"] : supported;
|
|
6
|
+
return field.operators ? field.operators.filter(operator => allowed.includes(operator)) : allowed;
|
|
7
|
+
}
|
|
8
|
+
export function formatFilterScalar(value) {
|
|
9
|
+
return typeof value === "number" || /^[\w.-]+$/.test(value) && !/^(AND|OR)$/i.test(value)
|
|
10
|
+
? String(value) : JSON.stringify(value);
|
|
11
|
+
}
|
|
12
|
+
export function formatFilterValue(condition) {
|
|
13
|
+
return condition.value === null ? "" : Array.isArray(condition.value)
|
|
14
|
+
? `(${condition.value.map(formatFilterScalar).join(", ")})`
|
|
15
|
+
: formatFilterScalar(condition.value);
|
|
16
|
+
}
|
|
17
|
+
export function serializeFilterNode(node) {
|
|
18
|
+
return node.kind === "group" ? `(${serializeFilterExpression(node.children)})`
|
|
19
|
+
: `${node.field} ${node.operator} ${formatFilterValue(node)}`.trim();
|
|
20
|
+
}
|
|
21
|
+
export function serializeFilterExpression(nodes) {
|
|
22
|
+
return nodes.map((node, index) => `${index ? `${node.join} ` : ""}${serializeFilterNode(node)}`).join(" ");
|
|
23
|
+
}
|
|
24
|
+
function tokens(source, incomplete = false) {
|
|
25
|
+
const result = [];
|
|
26
|
+
let index = 0;
|
|
27
|
+
while (index < source.length) {
|
|
28
|
+
if (/\s/.test(source[index])) {
|
|
29
|
+
index++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const start = index;
|
|
33
|
+
const quote = source[index];
|
|
34
|
+
if (quote === '"' || quote === "'") {
|
|
35
|
+
let text = "";
|
|
36
|
+
index++;
|
|
37
|
+
while (index < source.length && source[index] !== quote) {
|
|
38
|
+
if (source[index] === "\\") {
|
|
39
|
+
index++;
|
|
40
|
+
if (index === source.length)
|
|
41
|
+
break;
|
|
42
|
+
const escape = source[index++];
|
|
43
|
+
if (escape === "u") {
|
|
44
|
+
const hex = source.slice(index, index + 4);
|
|
45
|
+
if (!/^[0-9a-f]{4}$/i.test(hex)) {
|
|
46
|
+
throw new Error("Complete the Unicode escape with four hexadecimal digits.");
|
|
47
|
+
}
|
|
48
|
+
text += String.fromCharCode(parseInt(hex, 16));
|
|
49
|
+
index += 4;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
text += { n: "\n", r: "\r", t: "\t", b: "\b", f: "\f" }[escape] ?? escape;
|
|
53
|
+
}
|
|
54
|
+
else
|
|
55
|
+
text += source[index++];
|
|
56
|
+
}
|
|
57
|
+
if (source[index] !== quote && !incomplete)
|
|
58
|
+
throw new Error("Close the quoted value before confirming.");
|
|
59
|
+
if (source[index] === quote)
|
|
60
|
+
index++;
|
|
61
|
+
result.push({ text, quoted: true, start, end: index });
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const match = /^(>=|<=|!=|[=<>:~(),\[\]]|[^\s=<>!:~(),\[\]"']+)/.exec(source.slice(index));
|
|
65
|
+
if (!match) {
|
|
66
|
+
// A final "!" may become "!="; any other unexpected character stays an error.
|
|
67
|
+
if (incomplete && source.slice(index) === "!") {
|
|
68
|
+
result.push({ text: "!", quoted: false, start, end: ++index });
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
throw new Error(`Unexpected character "${source[index]}". Choose a suggested operator.`);
|
|
72
|
+
}
|
|
73
|
+
index += match[0].length;
|
|
74
|
+
result.push({ text: match[0], quoted: false, start, end: index });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
/** Empty input is an empty filter. Incomplete input returns an error without partial results. */
|
|
80
|
+
export function parseFilterExpression(source, fields, options = {}) {
|
|
81
|
+
try {
|
|
82
|
+
const input = tokens(source);
|
|
83
|
+
let index = 0;
|
|
84
|
+
const is = (text) => !input[index]?.quoted && input[index]?.text.toUpperCase() === text;
|
|
85
|
+
function take(message) {
|
|
86
|
+
const token = input[index++];
|
|
87
|
+
if (!token)
|
|
88
|
+
throw new Error(message);
|
|
89
|
+
return token;
|
|
90
|
+
}
|
|
91
|
+
function sequence(nested = false, depth = 0) {
|
|
92
|
+
// Bound recursion for pasted or generated input; this is not a UI nesting limit.
|
|
93
|
+
if (depth > 64)
|
|
94
|
+
throw new Error("Too many nested groups. Use fewer than 65 levels.");
|
|
95
|
+
const nodes = [];
|
|
96
|
+
while (index < input.length && !(nested && is(")"))) {
|
|
97
|
+
let join = "AND";
|
|
98
|
+
if (is("AND") || is("OR")) {
|
|
99
|
+
if (!nodes.length && !(options.allowLeadingJoin && !nested))
|
|
100
|
+
throw new Error("Add a condition before AND or OR.");
|
|
101
|
+
join = take("").text.toUpperCase();
|
|
102
|
+
}
|
|
103
|
+
if (is("(")) {
|
|
104
|
+
index++;
|
|
105
|
+
const children = sequence(true, depth + 1);
|
|
106
|
+
if (!is(")"))
|
|
107
|
+
throw new Error("Close the filter group with ).");
|
|
108
|
+
index++;
|
|
109
|
+
nodes.push({ kind: "group", children, join });
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (is(")"))
|
|
113
|
+
throw new Error("This ) has no matching filter group.");
|
|
114
|
+
const name = take("Type a field after the connector.");
|
|
115
|
+
const field = fields.find(field => field.key.toLowerCase() === name.text.toLowerCase());
|
|
116
|
+
if (!field || name.quoted)
|
|
117
|
+
throw new Error(`Unknown field "${name.text}". Choose a suggested field.`);
|
|
118
|
+
let operator = take(`Choose an operator for ${field.label ?? field.key}.`).text.toUpperCase();
|
|
119
|
+
if (operator === ":")
|
|
120
|
+
operator = "=";
|
|
121
|
+
if (operator === "~")
|
|
122
|
+
operator = "CONTAINS";
|
|
123
|
+
if (operator === "IS") {
|
|
124
|
+
if (!is("NULL"))
|
|
125
|
+
throw new Error("Complete IS NULL, or choose another operator.");
|
|
126
|
+
index++;
|
|
127
|
+
operator = "IS NULL";
|
|
128
|
+
}
|
|
129
|
+
if (!filterOperators(field).includes(operator)) {
|
|
130
|
+
throw new Error(`${field.label ?? field.key} accepts ${filterOperators(field).join(", ")}.`);
|
|
131
|
+
}
|
|
132
|
+
const scalar = () => {
|
|
133
|
+
const token = take(`Type a value for ${field.label ?? field.key}.`);
|
|
134
|
+
if (!token.quoted && /^(AND|OR|[(),\[\]])$/i.test(token.text))
|
|
135
|
+
throw new Error(`Type a value for ${field.label ?? field.key}. Quote text containing spaces or reserved words.`);
|
|
136
|
+
let value = token.text;
|
|
137
|
+
if (field.type === "number") {
|
|
138
|
+
if (!/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(token.text) || !Number.isFinite(Number(token.text)))
|
|
139
|
+
throw new Error(`${field.label ?? field.key} needs a finite number.`);
|
|
140
|
+
value = Number(token.text);
|
|
141
|
+
}
|
|
142
|
+
if (field.values && !field.values.includes(value))
|
|
143
|
+
throw new Error(`Choose a listed value for ${field.label ?? field.key}.`);
|
|
144
|
+
return value;
|
|
145
|
+
};
|
|
146
|
+
let value = null;
|
|
147
|
+
if (operator === "IN") {
|
|
148
|
+
if (!is("(") && !is("["))
|
|
149
|
+
throw new Error("Write IN (a, b), with comma-separated values.");
|
|
150
|
+
const closing = take("").text === "(" ? ")" : "]";
|
|
151
|
+
const values = [scalar()];
|
|
152
|
+
while (is(",")) {
|
|
153
|
+
index++;
|
|
154
|
+
// Completion leaves a comma ready for another value; closing ends the list.
|
|
155
|
+
if (is(closing))
|
|
156
|
+
break;
|
|
157
|
+
values.push(scalar());
|
|
158
|
+
}
|
|
159
|
+
if (!is(closing))
|
|
160
|
+
throw new Error(`Separate IN values with commas and close the list with ${closing}.`);
|
|
161
|
+
index++;
|
|
162
|
+
value = [...new Set(values)];
|
|
163
|
+
}
|
|
164
|
+
else if (operator !== "IS NULL")
|
|
165
|
+
value = scalar();
|
|
166
|
+
nodes.push({ kind: "condition", field: field.key, operator: operator, value, join });
|
|
167
|
+
}
|
|
168
|
+
if (nested && !nodes.length)
|
|
169
|
+
throw new Error("Add a condition inside the group.");
|
|
170
|
+
return nodes;
|
|
171
|
+
}
|
|
172
|
+
return { ok: true, value: sequence() };
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
return { ok: false, error: error.message };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Suggestions replace only the condition at the end of the draft, including inside groups. */
|
|
179
|
+
export function getFilterSuggestions(source, fields, existing = false) {
|
|
180
|
+
const complete = parseFilterExpression(source, fields, { allowLeadingJoin: existing });
|
|
181
|
+
if (complete.ok && complete.value.length)
|
|
182
|
+
return [];
|
|
183
|
+
let input;
|
|
184
|
+
// Never suggest past a lexical error: accepting would drop the unread suffix.
|
|
185
|
+
try {
|
|
186
|
+
input = tokens(source, true);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return [];
|
|
190
|
+
}
|
|
191
|
+
let start = 0;
|
|
192
|
+
const stack = [];
|
|
193
|
+
input.forEach((token, index) => {
|
|
194
|
+
// A complete prefix marks an implicit AND boundary, using the parser grammar.
|
|
195
|
+
if (!stack.some(frame => frame.kind === "list")) {
|
|
196
|
+
const preceding = parseFilterExpression(source.slice(start, token.start), fields, { allowLeadingJoin: true });
|
|
197
|
+
if (preceding.ok && preceding.value.length)
|
|
198
|
+
start = token.start;
|
|
199
|
+
}
|
|
200
|
+
if (token.quoted)
|
|
201
|
+
return;
|
|
202
|
+
if (token.text === "(" || token.text === "[") {
|
|
203
|
+
const list = input[index - 1]?.text.toUpperCase() === "IN" && !input[index - 1].quoted;
|
|
204
|
+
stack.push({ kind: list ? "list" : "group", start });
|
|
205
|
+
if (!list)
|
|
206
|
+
start = token.end;
|
|
207
|
+
}
|
|
208
|
+
else if (token.text === ")" || token.text === "]") {
|
|
209
|
+
const frame = stack.pop();
|
|
210
|
+
if (frame?.kind === "group")
|
|
211
|
+
start = frame.start;
|
|
212
|
+
}
|
|
213
|
+
else if (!stack.some(frame => frame.kind === "list") && /^(AND|OR)$/i.test(token.text) && /\s/.test(source[token.end] ?? ""))
|
|
214
|
+
start = token.end;
|
|
215
|
+
});
|
|
216
|
+
const prefix = source.slice(0, start);
|
|
217
|
+
const tail = source.slice(start).trimStart();
|
|
218
|
+
const leadingSpace = source.slice(start).slice(0, source.slice(start).length - tail.length);
|
|
219
|
+
const wrap = (suggestions) => suggestions.map(suggestion => ({ ...suggestion, text: prefix + leadingSpace + suggestion.text }));
|
|
220
|
+
const tailResult = parseFilterExpression(tail, fields, { allowLeadingJoin: true });
|
|
221
|
+
if (tailResult.ok && tailResult.value.length)
|
|
222
|
+
return [];
|
|
223
|
+
const condition = tokens(tail, true);
|
|
224
|
+
const first = condition[0];
|
|
225
|
+
const field = fields.find(field => field.key.toLowerCase() === first?.text.toLowerCase());
|
|
226
|
+
if (!field || condition.length === 1 && !/\s$/.test(tail)) {
|
|
227
|
+
const suggestions = fields.filter(field => field.key.toLowerCase().startsWith(tail.toLowerCase()) || field.label?.toLowerCase().startsWith(tail.toLowerCase()))
|
|
228
|
+
.map(field => ({ label: field.label ?? field.key, text: `${field.key} `, description: field.type === "number" ? "Number" : "Text" }));
|
|
229
|
+
if (existing && !prefix && /^(A|AN|AND|O|OR)$/i.test(tail)) {
|
|
230
|
+
const join = tail.toUpperCase().startsWith("A") ? "AND" : "OR";
|
|
231
|
+
suggestions.push({ label: join, text: `${join} `, description: "Combine with the next filter" });
|
|
232
|
+
}
|
|
233
|
+
return wrap(suggestions);
|
|
234
|
+
}
|
|
235
|
+
const remainder = tail.slice(first.end).trimStart();
|
|
236
|
+
const operatorMatch = /^(IS NULL|CONTAINS|IN|!=|>=|<=|[=<>:~])\s*(.*)$/i.exec(remainder);
|
|
237
|
+
if (!operatorMatch)
|
|
238
|
+
return wrap(filterOperators(field).filter(operator => operator.startsWith(remainder.toUpperCase())).map(operator => ({ label: operator, text: `${field.key} ${operator} `, description: operator === "IN" ? "Several values" : "Operator" })));
|
|
239
|
+
const operator = operatorMatch[1].toUpperCase();
|
|
240
|
+
const rawValue = operatorMatch[2];
|
|
241
|
+
if (operator === "IS NULL")
|
|
242
|
+
return [];
|
|
243
|
+
if (operator === "IN") {
|
|
244
|
+
const valueTokens = tokens(rawValue, true);
|
|
245
|
+
// Complete only "(a, b, c": members alternate with commas and the list stays open.
|
|
246
|
+
const opened = !!valueTokens[0] && !valueTokens[0].quoted && /^[(\[]$/.test(valueTokens[0].text);
|
|
247
|
+
const listTokens = opened ? valueTokens.slice(1) : valueTokens;
|
|
248
|
+
const wellFormed = listTokens.every((token, index) => index % 2
|
|
249
|
+
? token.text === "," && !token.quoted
|
|
250
|
+
: token.quoted || !/^(AND|OR|[(),\[\]])$/i.test(token.text));
|
|
251
|
+
if (!wellFormed || (!opened && listTokens.length > 1))
|
|
252
|
+
return [];
|
|
253
|
+
const lastComma = [...valueTokens].reverse().find(token => token.text === "," && !token.quoted);
|
|
254
|
+
const partial = lastComma ? rawValue.slice(lastComma.end).trim() : rawValue.replace(/^\s*[(\[]/, "").trim();
|
|
255
|
+
const committed = lastComma ? rawValue.slice(0, lastComma.end).replace(/^\s*[(\[]/, "").trim() : "";
|
|
256
|
+
const search = tokens(partial, true)[0]?.text ?? "";
|
|
257
|
+
return wrap((field.values ?? []).filter(value => String(value).toLowerCase().startsWith(search.toLowerCase())).map(value => ({ label: String(value), text: `${field.key} IN (${committed ? `${committed} ` : ""}${formatFilterScalar(value)}, `, description: "Add value; type ) to finish" })));
|
|
258
|
+
}
|
|
259
|
+
const valueTokens = tokens(rawValue, true);
|
|
260
|
+
if (valueTokens.length > 1)
|
|
261
|
+
return [];
|
|
262
|
+
const partial = valueTokens[0]?.text ?? "";
|
|
263
|
+
return wrap((field.values ?? []).filter(value => String(value).toLowerCase().startsWith(partial.toLowerCase())).map(value => ({ label: String(value), text: `${field.key} ${operator} ${formatFilterScalar(value)}`, description: "Complete condition" })));
|
|
264
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type FilterField, type FilterNode } from "./filter-expression.js";
|
|
3
|
+
export type FilterInputProps = Omit<React.ComponentProps<"div">, "onChange" | "defaultValue"> & {
|
|
4
|
+
label: string;
|
|
5
|
+
fields: readonly FilterField[];
|
|
6
|
+
value: FilterNode[];
|
|
7
|
+
onValueChange: (value: FilterNode[]) => void;
|
|
8
|
+
/** Observe uncommitted text, for example to prevent submitting an incomplete search. */
|
|
9
|
+
onDraftChange?: (draft: string) => void;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
};
|
|
13
|
+
/** Continuous typing creates segmented filters; the product owns querying and SQL. */
|
|
14
|
+
export declare function FilterInput({ id: providedId, label, fields, value, onValueChange, onDraftChange, disabled, placeholder, className, ...props }: FilterInputProps): React.JSX.Element;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { clsx as cn } from "clsx";
|
|
5
|
+
import { Button } from "./button.js";
|
|
6
|
+
import { Chip, ChipRemove } from "./chip-input.js";
|
|
7
|
+
import { filterOperators, formatFilterValue, getFilterSuggestions, parseFilterExpression, serializeFilterExpression, serializeFilterNode, } from "./filter-expression.js";
|
|
8
|
+
function nodeAt(nodes, path) {
|
|
9
|
+
let node;
|
|
10
|
+
for (const index of path) {
|
|
11
|
+
node = nodes[index];
|
|
12
|
+
nodes = node?.kind === "group" ? node.children : [];
|
|
13
|
+
}
|
|
14
|
+
return node;
|
|
15
|
+
}
|
|
16
|
+
function replaceNode(nodes, path, replacement) {
|
|
17
|
+
return nodes.flatMap((node, index) => {
|
|
18
|
+
if (index !== path[0])
|
|
19
|
+
return [node];
|
|
20
|
+
if (path.length === 1)
|
|
21
|
+
return replacement ? [replacement] : [];
|
|
22
|
+
if (node.kind !== "group")
|
|
23
|
+
return [node];
|
|
24
|
+
const children = replaceNode(node.children, path.slice(1), replacement);
|
|
25
|
+
return children.length ? [{ ...node, children }] : [];
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const controlKey = (path, segment) => `${path.join("-")}-${segment}`;
|
|
29
|
+
/** Continuous typing creates segmented filters; the product owns querying and SQL. */
|
|
30
|
+
export function FilterInput({ id: providedId, label, fields, value, onValueChange, onDraftChange, disabled = false, placeholder = "status=active OR region IN (eu, us)", className, ...props }) {
|
|
31
|
+
const generatedId = React.useId();
|
|
32
|
+
const id = providedId ?? generatedId;
|
|
33
|
+
const host = React.useRef(null);
|
|
34
|
+
const input = React.useRef(null);
|
|
35
|
+
const composing = React.useRef(false);
|
|
36
|
+
const [draft, setDraft] = React.useState("");
|
|
37
|
+
const [error, setError] = React.useState("");
|
|
38
|
+
const [message, setAnnouncement] = React.useState({ text: "", revision: 0 });
|
|
39
|
+
const [focused, setFocused] = React.useState(false);
|
|
40
|
+
const [dismissed, setDismissed] = React.useState(false);
|
|
41
|
+
const [active, setActive] = React.useState(0);
|
|
42
|
+
const [edit, setEdit] = React.useState(null);
|
|
43
|
+
const [wholeExpression, setWholeExpression] = React.useState(false);
|
|
44
|
+
const suggestions = getFilterSuggestions(draft, fields, !wholeExpression && value.length > 0);
|
|
45
|
+
const visible = !disabled && !edit && focused && !dismissed && suggestions.length > 0;
|
|
46
|
+
const selected = Math.min(active, Math.max(0, suggestions.length - 1));
|
|
47
|
+
const parsed = parseFilterExpression(draft, fields, { allowLeadingJoin: !wholeExpression && value.length > 0 });
|
|
48
|
+
React.useEffect(() => {
|
|
49
|
+
if (edit && !nodeAt(value, edit.path))
|
|
50
|
+
setEdit(null);
|
|
51
|
+
}, [value, edit]);
|
|
52
|
+
React.useEffect(() => {
|
|
53
|
+
if (visible)
|
|
54
|
+
host.current?.querySelector(`#${CSS.escape(`${id}-option-${selected}`)}`)?.scrollIntoView({ block: "nearest" });
|
|
55
|
+
}, [selected, visible, id]);
|
|
56
|
+
// Replace the live region content even when consecutive actions use the same text.
|
|
57
|
+
function setMessage(text) {
|
|
58
|
+
setAnnouncement(previous => ({ text, revision: previous.revision + 1 }));
|
|
59
|
+
}
|
|
60
|
+
function updateDraft(text) {
|
|
61
|
+
setDraft(text);
|
|
62
|
+
onDraftChange?.(text);
|
|
63
|
+
setError("");
|
|
64
|
+
setActive(0);
|
|
65
|
+
setDismissed(false);
|
|
66
|
+
}
|
|
67
|
+
function commit(text = draft, reportError = true) {
|
|
68
|
+
if (disabled || !text.trim() && !wholeExpression)
|
|
69
|
+
return false;
|
|
70
|
+
const result = parseFilterExpression(text, fields, { allowLeadingJoin: !wholeExpression && value.length > 0 });
|
|
71
|
+
if (!result.ok) {
|
|
72
|
+
if (reportError)
|
|
73
|
+
setError(result.error);
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
onValueChange(wholeExpression ? result.value : [...value, ...result.value]);
|
|
77
|
+
setWholeExpression(false);
|
|
78
|
+
updateDraft("");
|
|
79
|
+
setDismissed(true);
|
|
80
|
+
setMessage("Filters updated. Keep typing to add another condition.");
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
function typed(text, caretAtEnd) {
|
|
84
|
+
updateDraft(text);
|
|
85
|
+
if (!composing.current && !wholeExpression && caretAtEnd && /(?:\s|[)\]])$/.test(text))
|
|
86
|
+
commit(text, false);
|
|
87
|
+
}
|
|
88
|
+
function choose(index) {
|
|
89
|
+
const suggestion = suggestions[index];
|
|
90
|
+
if (!suggestion || disabled)
|
|
91
|
+
return;
|
|
92
|
+
updateDraft(suggestion.text);
|
|
93
|
+
if (!wholeExpression)
|
|
94
|
+
commit(suggestion.text, false);
|
|
95
|
+
input.current?.focus();
|
|
96
|
+
}
|
|
97
|
+
function restoreFocus(path, segment) {
|
|
98
|
+
requestAnimationFrame(() => {
|
|
99
|
+
const button = host.current?.querySelector(`[data-filter-control="${controlKey(path, segment)}"]`);
|
|
100
|
+
(button ?? input.current)?.focus();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function closeEdit() {
|
|
104
|
+
if (!edit)
|
|
105
|
+
return;
|
|
106
|
+
restoreFocus(edit.path, edit.segment);
|
|
107
|
+
setEdit(null);
|
|
108
|
+
}
|
|
109
|
+
function beginEdit(path, segment) {
|
|
110
|
+
if (disabled || edit || wholeExpression || draft.trim())
|
|
111
|
+
return;
|
|
112
|
+
const node = nodeAt(value, path);
|
|
113
|
+
if (!node)
|
|
114
|
+
return;
|
|
115
|
+
const text = node.kind === "group" ? serializeFilterExpression(node.children)
|
|
116
|
+
: segment === "field" ? node.field : segment === "operator" ? node.operator : formatFilterValue(node);
|
|
117
|
+
setEdit({ path, segment, text, error: "" });
|
|
118
|
+
setDismissed(true);
|
|
119
|
+
}
|
|
120
|
+
function saveEdit() {
|
|
121
|
+
if (!edit || disabled)
|
|
122
|
+
return;
|
|
123
|
+
const node = nodeAt(value, edit.path);
|
|
124
|
+
if (!node) {
|
|
125
|
+
closeEdit();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
let candidate = edit.text;
|
|
129
|
+
if (node.kind === "condition") {
|
|
130
|
+
let operator = edit.operator ?? node.operator;
|
|
131
|
+
let nextValue = node.value;
|
|
132
|
+
if (edit.segment === "operator") {
|
|
133
|
+
const entered = edit.text.trim().toUpperCase();
|
|
134
|
+
operator = (entered === ":" ? "=" : entered === "~" ? "CONTAINS" : entered);
|
|
135
|
+
const field = fields.find(field => field.key === node.field);
|
|
136
|
+
if (!field || !filterOperators(field).includes(operator)) {
|
|
137
|
+
setEdit({ ...edit, error: "Choose an operator supported by this field." });
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (operator === "IS NULL")
|
|
141
|
+
nextValue = null;
|
|
142
|
+
else if (nextValue === null || Array.isArray(nextValue) && operator !== "IN" && nextValue.length > 1) {
|
|
143
|
+
setEdit({ ...edit, segment: "value", operator, text: "", error: "Type a value for the new operator, then press Enter." });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
else if (operator === "IN" && !Array.isArray(nextValue))
|
|
147
|
+
nextValue = [nextValue];
|
|
148
|
+
else if (operator !== "IN" && Array.isArray(nextValue))
|
|
149
|
+
nextValue = nextValue[0];
|
|
150
|
+
}
|
|
151
|
+
const field = edit.segment === "field" ? edit.text : node.field;
|
|
152
|
+
const editedValue = edit.segment === "value"
|
|
153
|
+
? operator === "IN" && !/^\s*[(\[]/.test(edit.text) ? `(${edit.text})` : edit.text
|
|
154
|
+
: formatFilterValue({ ...node, value: nextValue });
|
|
155
|
+
candidate = `${field} ${operator} ${operator === "IS NULL" ? "" : editedValue}`;
|
|
156
|
+
}
|
|
157
|
+
const result = parseFilterExpression(candidate, fields);
|
|
158
|
+
if (!result.ok || !result.value.length || node.kind === "condition" && (result.value.length !== 1 || result.value[0].kind !== "condition")) {
|
|
159
|
+
setEdit({ ...edit, error: !result.ok ? result.error : "Enter a valid condition. Groups cannot be empty." });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const next = node.kind === "group" ? { ...node, children: result.value } : { ...result.value[0], join: node.join };
|
|
163
|
+
onValueChange(replaceNode(value, edit.path, next));
|
|
164
|
+
setMessage("Filter updated.");
|
|
165
|
+
closeEdit();
|
|
166
|
+
}
|
|
167
|
+
function remove(path) {
|
|
168
|
+
if (disabled)
|
|
169
|
+
return;
|
|
170
|
+
onValueChange(replaceNode(value, path, null));
|
|
171
|
+
setEdit(null);
|
|
172
|
+
setMessage("Filter removed.");
|
|
173
|
+
input.current?.focus();
|
|
174
|
+
}
|
|
175
|
+
function keys(event) {
|
|
176
|
+
if (composing.current || event.nativeEvent.isComposing)
|
|
177
|
+
return;
|
|
178
|
+
if ((event.key === "ArrowDown" || event.key === "ArrowUp") && suggestions.length) {
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
setDismissed(false);
|
|
181
|
+
setActive(visible ? (selected + (event.key === "ArrowDown" ? 1 : -1) + suggestions.length) % suggestions.length : event.key === "ArrowDown" ? 0 : suggestions.length - 1);
|
|
182
|
+
}
|
|
183
|
+
else if (event.key === "Tab" && !event.shiftKey && visible) {
|
|
184
|
+
event.preventDefault();
|
|
185
|
+
choose(selected);
|
|
186
|
+
}
|
|
187
|
+
else if (event.key === "Enter") {
|
|
188
|
+
event.preventDefault();
|
|
189
|
+
if (parsed.ok && (parsed.value.length || wholeExpression))
|
|
190
|
+
commit();
|
|
191
|
+
else if (visible && draft.trim())
|
|
192
|
+
choose(selected);
|
|
193
|
+
else if (draft.trim())
|
|
194
|
+
commit();
|
|
195
|
+
}
|
|
196
|
+
else if (event.key === "Escape") {
|
|
197
|
+
setDismissed(true);
|
|
198
|
+
setError("");
|
|
199
|
+
if (wholeExpression) {
|
|
200
|
+
setWholeExpression(false);
|
|
201
|
+
updateDraft("");
|
|
202
|
+
setDismissed(true);
|
|
203
|
+
setMessage("Expression edit canceled. Filters unchanged.");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (event.key === "Backspace" && !draft && !wholeExpression && value.length) {
|
|
207
|
+
event.preventDefault();
|
|
208
|
+
const last = value.at(-1);
|
|
209
|
+
onValueChange(value.slice(0, -1));
|
|
210
|
+
updateDraft(`${value.length > 1 ? `${last.join} ` : ""}${serializeFilterNode(last)}`);
|
|
211
|
+
setMessage("Last filter restored as text. Edit it and press Enter.");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function editor() {
|
|
215
|
+
if (!edit)
|
|
216
|
+
return null;
|
|
217
|
+
const node = nodeAt(value, edit.path);
|
|
218
|
+
return _jsxs("span", { className: "filter-editor", children: [_jsx("input", { autoFocus: true, disabled: disabled, className: "filter-segment-input", "aria-label": `Edit ${edit.segment} in ${node ? serializeFilterNode(node) : "filter"}`, "aria-invalid": !!edit.error, "aria-describedby": `${id}-edit-hint`, value: edit.text, size: Math.max(6, Math.min(edit.text.length + 1, 48)), onFocus: event => event.target.select(), onChange: event => setEdit({ ...edit, text: event.target.value, error: "" }), onKeyDown: event => {
|
|
219
|
+
event.stopPropagation();
|
|
220
|
+
if (event.nativeEvent.isComposing)
|
|
221
|
+
return;
|
|
222
|
+
if (event.key === "Enter") {
|
|
223
|
+
event.preventDefault();
|
|
224
|
+
saveEdit();
|
|
225
|
+
}
|
|
226
|
+
if (event.key === "Escape") {
|
|
227
|
+
event.preventDefault();
|
|
228
|
+
closeEdit();
|
|
229
|
+
}
|
|
230
|
+
} }, controlKey(edit.path, edit.segment)), _jsxs("span", { className: "filter-edit-actions", children: [_jsx(Button, { size: "xs", onClick: saveEdit, disabled: disabled, children: "Save" }), _jsx(Button, { size: "xs", variant: "ghost", onClick: closeEdit, disabled: disabled, children: "Cancel" })] })] });
|
|
231
|
+
}
|
|
232
|
+
const busy = !!edit || wholeExpression || !!draft.trim();
|
|
233
|
+
function renderNodes(nodes, parent = []) {
|
|
234
|
+
return nodes.map((node, index) => {
|
|
235
|
+
const path = [...parent, index];
|
|
236
|
+
const key = path.join("-");
|
|
237
|
+
const editingHere = edit?.path.join("-") === key;
|
|
238
|
+
const description = serializeFilterNode(node);
|
|
239
|
+
function segment(name, content) {
|
|
240
|
+
return editingHere && edit?.segment === name ? editor() : _jsx("button", { type: "button", className: `filter-segment filter-${name}`, "data-filter-control": controlKey(path, name), disabled: disabled || busy, "aria-label": `Edit ${name} in ${description}`, onClick: () => beginEdit(path, name), children: content });
|
|
241
|
+
}
|
|
242
|
+
return _jsxs(React.Fragment, { children: [index > 0 && _jsx("button", { type: "button", className: "filter-join", "data-join": node.join, disabled: disabled || busy, "aria-label": `Change ${node.join} before ${description}`, onClick: () => onValueChange(replaceNode(value, path, { ...node, join: node.join === "AND" ? "OR" : "AND" })), children: node.join }), node.kind === "group" ? _jsxs("span", { className: "filter-group", role: "group", "aria-label": `Filter group: ${serializeFilterExpression(node.children)}`, children: [_jsx("button", { type: "button", className: "filter-boundary", "data-filter-control": controlKey(path, "group"), disabled: disabled || busy, "aria-label": `Edit group ${description}`, onClick: () => beginEdit(path, "group"), children: "(" }), editingHere ? editor() : renderNodes(node.children, path), _jsx("span", { className: "filter-boundary", "aria-hidden": "true", children: ")" }), _jsx(ChipRemove, { disabled: disabled || busy, "aria-label": `Remove group ${description}`, onClick: () => remove(path) })] }) : _jsxs(Chip, { className: "filter-chip", invalid: editingHere && !!edit?.error, children: [segment("field", fields.find(field => field.key === node.field)?.label ?? node.field), segment("operator", editingHere && edit?.operator ? edit.operator : node.operator), (editingHere && edit?.operator ? edit.operator : node.operator) !== "IS NULL" && segment("value", Array.isArray(node.value)
|
|
243
|
+
? _jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", children: "(" }), node.value.map((item, i) => _jsxs(React.Fragment, { children: [i > 0 && _jsx("span", { children: "," }), _jsx("span", { className: "filter-list-value", children: String(item) })] }, i)), _jsx("span", { "aria-hidden": "true", children: ")" })] })
|
|
244
|
+
: node.value === "" ? '""' : String(node.value)), _jsx(ChipRemove, { disabled: disabled || busy, "aria-label": `Remove ${description}`, onClick: () => remove(path) })] })] }, key);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return _jsxs("div", { ...props, ref: host, "data-slot": "filter-input", className: cn("filter-input", className), children: [_jsx("label", { className: "label", htmlFor: id, children: label }), _jsxs("div", { className: "chip-input", children: [_jsxs("div", { className: "chip-input-box filter-box", "data-disabled": disabled || undefined, "aria-invalid": !!error || undefined, onMouseDown: event => { if (event.target === event.currentTarget && !disabled) {
|
|
248
|
+
event.preventDefault();
|
|
249
|
+
input.current?.focus();
|
|
250
|
+
} }, children: [!wholeExpression && renderNodes(value), _jsx("input", { ref: input, id: id, "data-slot": "filter-input-field", className: "chip-input-field filter-field-input", role: "combobox", autoComplete: "off", spellCheck: false, disabled: disabled || !!edit, "aria-autocomplete": "list", "aria-expanded": visible, "aria-controls": visible ? `${id}-list` : undefined, "aria-activedescendant": visible ? `${id}-option-${selected}` : undefined, "aria-invalid": !!error, "aria-describedby": `${id}-hint${error ? ` ${id}-error` : ""}`, placeholder: placeholder, value: draft, onChange: event => typed(event.target.value, event.target.selectionStart === event.target.value.length), onCompositionStart: () => { composing.current = true; }, onCompositionEnd: event => { composing.current = false; typed(event.currentTarget.value, event.currentTarget.selectionStart === event.currentTarget.value.length); }, onKeyDown: keys, onFocus: () => setFocused(true), onBlur: () => setFocused(false) })] }), visible && _jsx("div", { className: "menu filter-suggestions", id: `${id}-list`, role: "listbox", "aria-label": `${label} suggestions`, children: suggestions.map((suggestion, index) => _jsxs("div", { id: `${id}-option-${index}`, role: "option", "aria-selected": index === selected, className: "menu-item", onMouseDown: event => event.preventDefault(), onPointerMove: () => setActive(index), onClick: () => choose(index), children: [_jsx("span", { children: suggestion.label }), _jsx("span", { className: "muted", children: suggestion.description })] }, `${suggestion.text}-${index}`)) })] }), _jsx("p", { id: `${id}-hint`, className: "field-hint", children: "Space completes a filter. Close IN lists and groups with ). Enter also confirms. Tab completes suggestions; Escape closes them. Quote values with spaces." }), error && _jsx("p", { className: "field-error", id: `${id}-error`, role: "alert", children: error }), edit && _jsx("p", { id: `${id}-edit-hint`, className: edit.error ? "field-error" : "field-hint", role: edit.error ? "alert" : undefined, children: edit.error || "Enter saves; Escape cancels. Results use the saved filter until you confirm." }), wholeExpression && _jsx("p", { className: "field-hint", children: "Edit parentheses or conditions. Enter saves; Escape cancels. Results still use the saved filters." }), _jsxs("div", { className: "filter-actions", children: [_jsx(Button, { size: "sm", variant: "ghost", disabled: disabled || busy || !value.length, onClick: () => { updateDraft(serializeFilterExpression(value)); setWholeExpression(true); input.current?.focus(); }, children: "Edit expression" }), _jsx(Button, { size: "sm", variant: "ghost", disabled: disabled || (!value.length && !draft && !edit), onClick: () => { onValueChange([]); setEdit(null); setWholeExpression(false); updateDraft(""); setMessage("Filters cleared."); requestAnimationFrame(() => input.current?.focus()); }, children: "Clear filters" })] }), _jsx("span", { className: "filter-status", role: "status", "aria-atomic": "true", children: _jsx("span", { children: message.text }, message.revision) })] });
|
|
251
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export * from "./button.js";
|
|
|
8
8
|
export * from "./card.js";
|
|
9
9
|
export * from "./checkbox.js";
|
|
10
10
|
export * from "./chip-input.js";
|
|
11
|
+
export * from "./filter-input.js";
|
|
12
|
+
export * from "./filter-expression.js";
|
|
11
13
|
export * from "./command-palette.js";
|
|
12
14
|
export * from "./dialog.js";
|
|
13
15
|
export * from "./dot.js";
|
|
@@ -53,3 +55,5 @@ export * from "./disclosure.js";
|
|
|
53
55
|
export * from "./time-range-control.js";
|
|
54
56
|
export * from "./dashboard-grid.js";
|
|
55
57
|
export * from "./step-list.js";
|
|
58
|
+
export * from "./lifecycle.js";
|
|
59
|
+
export * from "./status-badge.js";
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,8 @@ export * from "./button.js";
|
|
|
9
9
|
export * from "./card.js";
|
|
10
10
|
export * from "./checkbox.js";
|
|
11
11
|
export * from "./chip-input.js";
|
|
12
|
+
export * from "./filter-input.js";
|
|
13
|
+
export * from "./filter-expression.js";
|
|
12
14
|
export * from "./command-palette.js";
|
|
13
15
|
export * from "./dialog.js";
|
|
14
16
|
export * from "./dot.js";
|
|
@@ -54,3 +56,5 @@ export * from "./disclosure.js";
|
|
|
54
56
|
export * from "./time-range-control.js";
|
|
55
57
|
export * from "./dashboard-grid.js";
|
|
56
58
|
export * from "./step-list.js";
|
|
59
|
+
export * from "./lifecycle.js";
|
|
60
|
+
export * from "./status-badge.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export type LifecycleProps = Omit<React.ComponentProps<"div">, "children"> & {
|
|
3
|
+
status: React.ReactNode;
|
|
4
|
+
actions?: React.ReactNode;
|
|
5
|
+
destructive?: React.ReactNode;
|
|
6
|
+
};
|
|
7
|
+
export declare function Lifecycle({ status, actions, destructive, className, ...props }: LifecycleProps): React.JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { clsx as cn } from "clsx";
|
|
3
|
+
/* The header row of a detail screen: what the thing is, what you can do to
|
|
4
|
+
it, and the one action you do not want to hit by accident. Status is read,
|
|
5
|
+
the verbs are pressed, and the destructive action sits outside both with a
|
|
6
|
+
wider gap. A screen with no verbs passes none, and the middle group is
|
|
7
|
+
dropped rather than rendered as an empty bordered box. */
|
|
8
|
+
export function Lifecycle({ status, actions, destructive, className, ...props }) {
|
|
9
|
+
return _jsxs("div", { ...props, "data-slot": "lifecycle", className: cn("lifecycle", className), children: [_jsx("div", { className: "cluster cluster-status", role: "group", "aria-label": "Status", children: status }), actions ? _jsx("div", { className: "cluster cluster-verbs", role: "group", "aria-label": "Actions", children: actions }) : null, destructive] });
|
|
10
|
+
}
|
package/dist/page-header.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
declare function PageHeader({ className, actions, children, ...props }: React.ComponentProps<"div"> & {
|
|
2
|
+
declare function PageHeader({ className, actions, children, variant, ...props }: React.ComponentProps<"div"> & {
|
|
3
3
|
actions?: React.ReactNode;
|
|
4
|
+
variant?: "default" | "editorial";
|
|
4
5
|
}): React.JSX.Element;
|
|
5
6
|
declare function PageHeaderTitle({ className, asChild, ...props }: React.ComponentProps<"h1"> & {
|
|
6
7
|
asChild?: boolean;
|
package/dist/page-header.js
CHANGED
|
@@ -3,11 +3,11 @@ import { clsx as cn } from "clsx";
|
|
|
3
3
|
import { Slot } from "radix-ui";
|
|
4
4
|
// Actions sit beside the title, so they are a prop rather than a child: the
|
|
5
5
|
// title block keeps its own grid either way.
|
|
6
|
-
function PageHeader({ className, actions, children, ...props }) {
|
|
7
|
-
const heading = (_jsx("div", { "data-slot": "page-header", className: cn("page-header", className), ...props, children: children }));
|
|
6
|
+
function PageHeader({ className, actions, children, variant, ...props }) {
|
|
7
|
+
const heading = (_jsx("div", { "data-slot": "page-header", "data-variant": variant, className: cn("page-header", className), ...props, children: children }));
|
|
8
8
|
if (!actions)
|
|
9
9
|
return heading;
|
|
10
|
-
return (_jsxs("div", { "data-slot": "page-header-row", className: "between", children: [heading, _jsx("div", { "data-slot": "page-header-actions", className: "row-wrap", children: actions })] }));
|
|
10
|
+
return (_jsxs("div", { "data-slot": "page-header-row", "data-variant": variant, className: "between", children: [heading, _jsx("div", { "data-slot": "page-header-actions", className: "row-wrap", children: actions })] }));
|
|
11
11
|
}
|
|
12
12
|
function PageHeaderTitle({ className, asChild = false, ...props }) {
|
|
13
13
|
const Comp = asChild ? Slot.Root : "h1";
|
package/dist/sidebar.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
declare function Sidebar({ className, ...props }: React.ComponentProps<"aside">
|
|
2
|
+
declare function Sidebar({ className, collapsed, ...props }: React.ComponentProps<"aside"> & {
|
|
3
|
+
collapsed?: boolean;
|
|
4
|
+
}): React.JSX.Element;
|
|
3
5
|
declare function SidebarHeader({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
|
4
6
|
declare function SidebarBody({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
|
5
7
|
declare function SidebarFooter({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
package/dist/sidebar.js
CHANGED
|
@@ -2,8 +2,8 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { clsx as cn } from "clsx";
|
|
3
3
|
// Sidebar is layout. Its destinations are Navigation children, so it holds no
|
|
4
4
|
// route, no active item, and no disclosure state of its own.
|
|
5
|
-
function Sidebar({ className, ...props }) {
|
|
6
|
-
return (_jsx("aside", { "data-slot": "sidebar", className: cn("sidebar", className), ...props }));
|
|
5
|
+
function Sidebar({ className, collapsed = false, ...props }) {
|
|
6
|
+
return (_jsx("aside", { "data-slot": "sidebar", "data-collapsed": collapsed || undefined, className: cn("sidebar", className), ...props }));
|
|
7
7
|
}
|
|
8
8
|
function SidebarHeader({ className, ...props }) {
|
|
9
9
|
return (_jsx("div", { "data-slot": "sidebar-header", className: cn("sidebar-header", className), ...props }));
|