ConditionEditor.tsx
6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* 条件组编辑器:若干条「左边 比较 右边」,用「全部满足 / 任一满足」连起来,可以再嵌一层子条件组
* (如:等级是 A 且(金额 > 5000 或 是大客户))。比较方式随左边的类型变化(数字有大于小于,列表有条数,是否只有为是 / 为否)。
*
* 两种左边:
* - 数据:左边是上游的一份数据(条件节点、循环的筛选);
* - 属性:左边是某类对象的一个属性(查数据的条件,fields 给出可选属性)。
* 也可以切到「高级」直接写表达式。
*/
import { Code2, Plus, Trash2 } from "lucide-react";
import type { CondGroup, CondItem, DataSchema, FlowCond } from "@/lib/onto";
import { Select } from "@/components/kit";
import { cn } from "@/lib/utils";
import { ExprInput, RefPicker, useScope, ValueInput } from "./data";
import { isGroup, OP_LABEL, refType, UNARY } from "./model";
const OPS_BY_TYPE: Record<string, string[]> = {
number: ["eq", "ne", "gt", "ge", "lt", "le", "in", "empty", "not_empty"],
integer: ["eq", "ne", "gt", "ge", "lt", "le", "in", "empty", "not_empty"],
date: ["eq", "ne", "gt", "ge", "lt", "le", "empty", "not_empty"],
boolean: ["is_true", "is_false", "empty"],
array: ["len_gt", "len_eq", "empty", "not_empty", "contains"],
string: ["eq", "ne", "contains", "not_contains", "startswith", "in", "not_in", "empty", "not_empty"],
};
const QUERY_OPS = ["eq", "ne", "gt", "ge", "lt", "le", "contains", "not_contains", "startswith", "in", "not_in", "empty", "not_empty"];
const opsFor = (t: string, query: boolean) => (query ? QUERY_OPS : OPS_BY_TYPE[t] || [...OPS_BY_TYPE.string, "gt", "ge", "lt", "le"]);
function ItemRow({ it, onChange, onRemove, fields, disabled }: {
it: CondItem; onChange: (x: CondItem) => void; onRemove: () => void; fields?: DataSchema[]; disabled?: boolean;
}) {
const sc = useScope();
const query = !!fields;
const field = query ? fields!.find((f) => f.name === it.left) : undefined;
const t = query ? (field?.type || "string") : refType(it.left, [...(sc.extra || []), ...sc.outputs]);
const ops = opsFor(t, query);
return (
<div className="space-y-1 rounded-md border border-line/70 bg-surface p-1.5">
<div className="flex items-center gap-1.5">
<div className="min-w-0 flex-1">
{query ? (
<Select className="input h-8 w-full" disabled={disabled} value={it.left} onChange={(e) => onChange({ ...it, left: e.target.value })}>
<option value="">选属性</option>
{fields!.map((f) => <option key={f.name} value={f.name}>{f.label || f.name}</option>)}
</Select>
) : <RefPicker value={it.left} disabled={disabled} onChange={(v) => onChange({ ...it, left: v })} placeholder="选数据" />}
</div>
<Select className="input h-8 w-[6.5rem] shrink-0" disabled={disabled} value={it.op} onChange={(e) => onChange({ ...it, op: e.target.value })}>
{(ops.includes(it.op) ? ops : [it.op, ...ops]).map((o) => <option key={o} value={o}>{OP_LABEL[o] || o}</option>)}
</Select>
{!disabled && <button className="btn btn-ghost btn-sm shrink-0 px-1.5" title="删掉这条" onClick={onRemove}><Trash2 className="h-3.5 w-3.5" /></button>}
</div>
{!UNARY.includes(it.op) && (
<ValueInput value={it.right} disabled={disabled} onChange={(v) => onChange({ ...it, right: v })}
type={["len_gt", "len_eq"].includes(it.op) ? "number" : t === "array" ? "string" : t}
options={field?.options} placeholder={it.op === "in" || it.op === "not_in" ? "多个用逗号隔开" : "比较的值"} />
)}
</div>
);
}
function Group({ g, onChange, fields, disabled, depth }: {
g: CondGroup; onChange: (g: CondGroup | null) => void; fields?: DataSchema[]; disabled?: boolean; depth: number;
}) {
const set = (i: number, x: CondItem | CondGroup | null) => {
const items = x === null ? g.items.filter((_, j) => j !== i) : g.items.map((y, j) => (j === i ? x : y));
onChange(items.length ? { ...g, items } : null);
};
return (
<div className={cn("space-y-1.5", depth > 0 && "rounded-lg border border-dashed border-line bg-ink/[0.015] p-2")}>
{g.items.length > 1 && (
<div className="flex items-center gap-1.5 text-caption text-muted">
<Select className="input h-7 w-auto py-0 text-caption" disabled={disabled} value={g.mode}
onChange={(e) => onChange({ ...g, mode: e.target.value as "all" | "any" })}>
<option value="all">下面全部满足</option><option value="any">下面任一满足</option>
</Select>
</div>
)}
{g.items.map((it, i) => (isGroup(it)
? <Group key={i} g={it} depth={depth + 1} fields={fields} disabled={disabled} onChange={(x) => set(i, x)} />
: <ItemRow key={i} it={it} fields={fields} disabled={disabled} onChange={(x) => set(i, x)} onRemove={() => set(i, null)} />))}
{!disabled && (
<div className="flex gap-1.5">
<button className="btn btn-sm" onClick={() => onChange({ ...g, items: [...g.items, { left: "", op: "eq", right: "" }] })}>
<Plus className="h-3.5 w-3.5" />加一条
</button>
{depth === 0 && (
<button className="btn btn-ghost btn-sm" title="一组「或者」:如 金额 > 5000 或 是大客户"
onClick={() => onChange({ ...g, items: [...g.items, { mode: g.mode === "all" ? "any" : "all", items: [{ left: "", op: "eq", right: "" }] }] })}>
<Plus className="h-3.5 w-3.5" />加一组
</button>
)}
</div>
)}
</div>
);
}
export function ConditionEditor({ value, onChange, fields, disabled, emptyHint = "没有条件" }: {
value: FlowCond | undefined; onChange: (v: FlowCond) => void; fields?: DataSchema[]; disabled?: boolean; emptyHint?: string;
}) {
if (value && "expr" in value) {
return (
<div className="space-y-1.5">
<ExprInput value={value.expr} disabled={disabled} onChange={(v) => onChange({ expr: v })} />
{!disabled && <button className="btn btn-ghost btn-sm" onClick={() => onChange(null)}>改回逐条选择</button>}
</div>
);
}
const g: CondGroup = value || { mode: "all", items: [] };
return (
<div className="space-y-1.5">
{g.items.length === 0 && <p className="text-caption text-muted">{emptyHint}</p>}
<Group g={g} depth={0} fields={fields} disabled={disabled} onChange={(x) => onChange(x)} />
{!disabled && !fields && (
<button className="btn btn-ghost btn-sm text-muted" title="直接写表达式(高级)" onClick={() => onChange({ expr: "" })}>
<Code2 className="h-3.5 w-3.5" />写表达式
</button>
)}
</div>
);
}