-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFlowObjectListField.tsx
More file actions
413 lines (398 loc) · 17.4 KB
/
Copy pathFlowObjectListField.tsx
File metadata and controls
413 lines (398 loc) · 17.4 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* FlowObjectListField — a repeatable array-of-objects editor driven by a column
* schema (e.g. a screen node's `fields`: a list of `{name,label,type,required,
* visibleWhen}` definitions).
*
* Like the sibling key/value and string-list editors, rows are held in LOCAL
* state with a STABLE id and flushed on blur / Enter / add / remove so a row
* never remounts mid-keystroke. Empty per-cell values are pruned; a row with no
* populated cells is dropped on flush; an empty list commits `undefined`.
*
* A column may itself be a *list* (`stringList` / `numberList` / `objectList`) —
* a repeater-in-repeater. Those cells hold an array and render the matching
* sibling editor inline (recursively, for `objectList`), so an engine-published
* nested-array config is editable here instead of dropping to Advanced JSON.
*/
import * as React from 'react';
import { Plus, X } from 'lucide-react';
import {
Button, Input, Label, Checkbox,
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from '@object-ui/components';
import { uniqueId } from './_shared';
import type { FlowConfigColumn } from './flow-node-config';
import { ReferenceCombobox, resolveRefKind, type FlowReferenceContext } from './FlowReferenceField';
import { FlowStringListField } from './FlowStringListField';
import { VariableTextInput } from './VariableTextInput';
import type { ScopeGroup } from './useFlowScope';
import { FlowExprIssue } from './FlowExprIssue';
/** A cell is a scalar (string/boolean) or, for a nested-list column, an array. */
type Cell = string | boolean | unknown[];
interface Row {
id: string;
values: Record<string, Cell>;
}
/** Columns whose cell holds an array (a nested repeater) rather than a scalar. */
function isListColumn(kind: FlowConfigColumn['kind']): boolean {
return kind === 'stringList' || kind === 'numberList' || kind === 'objectList';
}
function toRows(list: Array<Record<string, unknown>>, columns: FlowConfigColumn[]): Row[] {
const ids: string[] = [];
return list.map((item) => {
const id = uniqueId('ol', ids);
ids.push(id);
const values: Record<string, Cell> = {};
for (const col of columns) {
const v = item[col.key];
if (col.kind === 'boolean') values[col.key] = v === true;
else if (isListColumn(col.kind)) values[col.key] = Array.isArray(v) ? v : [];
else if (v != null) values[col.key] = String(v);
else values[col.key] = '';
}
return { id, values };
});
}
function rowsToList(rows: Row[], columns: FlowConfigColumn[]): Array<Record<string, unknown>> {
const out: Array<Record<string, unknown>> = [];
for (const row of rows) {
const obj: Record<string, unknown> = {};
let hasValue = false;
for (const col of columns) {
const v = row.values[col.key];
if (col.kind === 'boolean') {
if (v === true) {
obj[col.key] = true;
hasValue = true;
}
} else if (isListColumn(col.kind)) {
// A nested list commits its own already-normalized array (string[] /
// number[] / object[]); an empty nested list drops the key entirely.
if (Array.isArray(v) && v.length > 0) {
obj[col.key] = v;
hasValue = true;
}
} else if (typeof v === 'string' && v.trim() !== '') {
obj[col.key] = v.trim();
hasValue = true;
}
}
if (hasValue) out.push(obj);
}
return out;
}
export interface FlowObjectListFieldProps {
label: string;
columns: FlowConfigColumn[];
value: unknown;
onCommit: (value: Array<Record<string, unknown>> | undefined) => void;
disabled?: boolean;
addLabel: string;
removeLabel: string;
emptyLabel: string;
/** Per-item placeholder for nested `stringList` / `numberList` columns. */
itemLabel?: string;
/** Draft + node context so `reference` columns can resolve their options. */
context?: FlowReferenceContext;
/** In-scope variable references for `expression` columns (#1934). */
scopeGroups?: ScopeGroup[];
/**
* #3447: picker groups for approval `expression` approver cells — the
* closed current/trigger/vars root set. Regular flow scopeGroups must NOT
* be offered there (record.* / bare-field spellings are rejected at
* runtime), which is why this rides as its own prop.
*/
approvalScopeGroups?: ScopeGroup[];
}
export function FlowObjectListField({
label,
columns,
value,
onCommit,
disabled,
addLabel,
removeLabel,
emptyLabel,
itemLabel,
context,
scopeGroups,
approvalScopeGroups,
}: FlowObjectListFieldProps) {
const external = React.useMemo(
() =>
Array.isArray(value)
? (value.filter((v) => v && typeof v === 'object') as Array<Record<string, unknown>>)
: [],
[value],
);
const [rows, setRows] = React.useState<Row[]>(() => toRows(external, columns));
const lastCommitted = React.useRef(JSON.stringify(external));
React.useEffect(() => {
const next = JSON.stringify(external);
if (next !== lastCommitted.current) {
setRows(toRows(external, columns));
lastCommitted.current = next;
}
}, [external, columns]);
const flush = (nextRows: Row[]) => {
const list = rowsToList(nextRows, columns);
lastCommitted.current = JSON.stringify(list);
onCommit(list.length ? list : undefined);
};
const setCell = (id: string, key: string, v: Cell) => {
setRows((rs) => rs.map((r) => (r.id === id ? { ...r, values: { ...r.values, [key]: v } } : r)));
};
// Set a cell AND flush — used by controls with no blur to flush on (checkbox,
// select) and by the nested-list editors, which each commit a whole array on
// their own blur/add/remove. The flush stays inside the `setRows` updater
// (the accepted idiom for the sibling scalar controls) so the `lastCommitted`
// ref is touched only there, never in the render body.
const commitCell = (id: string, key: string, v: Cell) => {
setRows((rs) => {
const next = rs.map((r) => (r.id === id ? { ...r, values: { ...r.values, [key]: v } } : r));
flush(next);
return next;
});
};
const addRow = () => {
const values: Record<string, Cell> = {};
for (const col of columns) values[col.key] = col.kind === 'boolean' ? false : isListColumn(col.kind) ? [] : '';
setRows((rs) => [...rs, { id: uniqueId('ol', rs.map((r) => r.id)), values }]);
};
const removeRow = (id: string) => {
setRows((rs) => {
const next = rs.filter((r) => r.id !== id);
flush(next);
return next;
});
};
return (
<div className="space-y-1.5">
<Label className="text-xs text-muted-foreground">{label}</Label>
<div className="space-y-2">
{rows.length === 0 && (
<p className="text-[11px] italic text-muted-foreground">{emptyLabel}</p>
)}
{rows.map((row) => (
<div key={row.id} className="rounded border bg-muted/30 p-2">
<div className="mb-1 flex justify-end">
<Button
type="button"
variant="ghost"
size="sm"
className="h-6 w-6 p-0 text-muted-foreground"
onClick={() => removeRow(row.id)}
disabled={disabled}
aria-label={removeLabel}
title={removeLabel}
>
<X className="h-3.5 w-3.5" />
</Button>
</div>
<div className="space-y-1.5">
{columns.map((col) => {
// A nested-list column (repeater-in-repeater) renders full-width
// as its own list editor, set off with a left rule. `stringList` /
// `numberList` reuse FlowStringListField; `objectList` recurses.
if (col.kind === 'stringList' || col.kind === 'numberList') {
const raw = row.values[col.key];
const arr = Array.isArray(raw) ? raw : [];
return (
<div key={col.key} className="border-l-2 border-muted/60 pl-2">
<FlowStringListField
label={col.label}
value={col.kind === 'numberList' ? arr.map((n) => String(n)) : arr}
onCommit={(v) => {
if (col.kind === 'numberList') {
const nums = (v ?? [])
.map((s) => Number(String(s).trim()))
.filter((n) => Number.isFinite(n));
commitCell(row.id, col.key, nums);
} else {
commitCell(row.id, col.key, v ?? []);
}
}}
disabled={disabled}
addLabel={addLabel}
itemLabel={itemLabel ?? col.label}
removeLabel={removeLabel}
emptyLabel={emptyLabel}
/>
</div>
);
}
if (col.kind === 'objectList') {
const raw = row.values[col.key];
const arr = (Array.isArray(raw) ? raw : []) as Array<Record<string, unknown>>;
return (
<div key={col.key} className="border-l-2 border-muted/60 pl-2">
<FlowObjectListField
label={col.label}
columns={col.columns ?? []}
value={arr}
onCommit={(v) => commitCell(row.id, col.key, v ?? [])}
disabled={disabled}
addLabel={addLabel}
removeLabel={removeLabel}
emptyLabel={emptyLabel}
itemLabel={itemLabel}
context={context}
scopeGroups={scopeGroups}
/>
</div>
);
}
return (
<div key={col.key} className="flex items-center gap-2">
<Label className="w-24 shrink-0 text-[11px] text-muted-foreground">
{col.label}
</Label>
{col.kind === 'boolean' ? (
<Checkbox
checked={row.values[col.key] === true}
onCheckedChange={(c) => commitCell(row.id, col.key, c === true)}
disabled={disabled}
/>
) : col.kind === 'reference' ? (
(() => {
const resolved = resolveRefKind(col.ref, (k) => row.values[k]);
const disc = col.ref?.kindFrom
? String(row.values[col.ref.kindFrom] ?? '')
: '';
// #3447: `expression` is a discriminator value, not a
// reference kind — an approver whose sibling `type` is
// 'expression' authors a CEL expression over the approval
// roots (current/trigger/vars). Render the expression
// input (mono + syntax check) instead of a dead free-text
// reference box, with the APPROVAL scope groups — never
// the regular flow scopeGroups, whose record.x /
// bare-field spellings the runtime rejects. The same
// groups feed FlowExprIssue, so an out-of-contract root
// warns inline with a "did you mean" before os lint /
// the node-entry pre-check reject it server-side.
if (resolved === undefined && disc === 'expression') {
const raw = typeof row.values[col.key] === 'string' ? (row.values[col.key] as string) : '';
return (
<div className="flex-1 space-y-1">
<VariableTextInput
mode="expression"
mono
value={raw}
onValueChange={(v) => setCell(row.id, col.key, v)}
onBlur={() => flush(rows)}
onKeyDown={(e) => {
if (e.key === 'Enter') (e.target as HTMLInputElement).blur();
}}
groups={approvalScopeGroups ?? []}
placeholder={col.placeholder ?? 'current.<field> · trigger.<field> · vars.<node>.<key>'}
disabled={disabled}
/>
<FlowExprIssue value={raw} role="value" scopeGroups={approvalScopeGroups} />
</div>
);
}
return (
<div className="flex-1">
<ReferenceCombobox
resolved={resolved}
value={typeof row.values[col.key] === 'string' ? (row.values[col.key] as string) : ''}
onCommit={(v) => setCell(row.id, col.key, typeof v === 'string' ? v : '')}
onBlur={() => flush(rows)}
placeholder={col.placeholder}
disabled={disabled}
context={context}
showHint={false}
/>
</div>
);
})()
) : col.kind === 'select' ? (
(() => {
const current =
typeof row.values[col.key] === 'string' ? (row.values[col.key] as string) : '';
const opts = col.options ?? [];
// A stored value dropped from the options (a deprecated
// enum member, e.g. the `role` approver type per
// ADR-0090 D3) must still render, or editing a legacy row
// would silently blank it. Surface it as selectable but
// flag it — it is not offered to fresh rows.
const shown =
current && !opts.some((o) => o.value === current)
? [...opts, { value: current, label: `${current} (deprecated)` }]
: opts;
return (
<div className="flex-1">
<Select
value={current || undefined}
onValueChange={(v) => commitCell(row.id, col.key, v)}
disabled={disabled}
>
<SelectTrigger className="h-8 w-full text-xs">
<SelectValue placeholder={col.placeholder ?? '—'} />
</SelectTrigger>
<SelectContent>
{shown.map((o) => (
<SelectItem key={o.value} value={o.value}>
{o.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
);
})()
) : col.kind === 'expression' ? (
<div className="flex-1 space-y-1">
<VariableTextInput
mode="expression"
mono
value={typeof row.values[col.key] === 'string' ? (row.values[col.key] as string) : ''}
onValueChange={(v) => setCell(row.id, col.key, v)}
onBlur={() => flush(rows)}
onKeyDown={(e) => {
if (e.key === 'Enter') (e.target as HTMLInputElement).blur();
}}
groups={scopeGroups ?? []}
placeholder={col.placeholder}
disabled={disabled}
/>
<FlowExprIssue
value={typeof row.values[col.key] === 'string' ? (row.values[col.key] as string) : ''}
role="predicate"
scopeGroups={scopeGroups}
/>
</div>
) : (
<Input
value={typeof row.values[col.key] === 'string' ? (row.values[col.key] as string) : ''}
onChange={(e) => setCell(row.id, col.key, e.target.value)}
onBlur={() => flush(rows)}
onKeyDown={(e) => {
if (e.key === 'Enter') (e.target as HTMLInputElement).blur();
}}
placeholder={col.placeholder}
disabled={disabled}
className="h-8 flex-1 text-xs"
/>
)}
</div>
);
})}
</div>
</div>
))}
</div>
<Button
type="button"
variant="outline"
size="sm"
className="h-7 w-full text-xs"
onClick={addRow}
disabled={disabled}
>
<Plus className="mr-1 h-3.5 w-3.5" />
{addLabel}
</Button>
</div>
);
}