/** * 表单渲染器:agentlab.form/1(应用平台 §3.3,X2「只有一个表单渲染器」)。 * * 用在四处:画布和版本页的发起表单(替换 RunControl 的 FieldsForm)、人工节点、待办、应用入口。 * 前端只校验目录规定的子集(lib/schemaForm.ts),后端复核、以后端为准;两边的错误格式都是 {path, code, message}, * 按 path 挂到字段下面。对不上任何字段的错误(服务端说的是整张表,或者字段名对不上)照实列在表单顶上,不吞掉。 * * 值的类型和表单声明的一致:数字字段存数(输入途中的「1.」这类半截写法留在框里,不回写成别的数),日期时间存带本地 * 时区的 RFC 3339,多选存字符串数组,文件存上传后拿回的引用(FileInput)。没填的不进值(undefined),必填由校验拦。 * 文件要先上传:上传的上下文(应用的入口、待办、蓝图)由用表单的地方给(fileContext),不给的地方不能上传。 */ import { useEffect, useId, useState } from 'react' import type { CSSProperties, KeyboardEvent as ReactKeyboardEvent, ReactNode } from 'react' import clsx from 'clsx' import type { CandidateSource, FormError, FormField, FormSchema } from '../typesApp' import { FORM_TEXT } from '../lib/termsApp' import { fieldOfPath, fieldOrder, fileLimitsOf, localToRfc3339, optionsOf, rfc3339ToLocal, widgetOf, } from '../lib/schemaForm' import { matchShortcut } from '../lib/keys' import { useAtLeast } from '../store/auth' import { JsonInput, isComposing } from './ui' import { PrincipalPicker } from './PrincipalPicker' import { FileInput } from './FileInput' export interface SchemaFormProps { schema: FormSchema value: Record onChange: (value: Record) => void /** 服务端(422)或本地校验出的错误,按 path 挂到字段下面 */ errors?: FormError[] readOnly?: boolean /** Mod+Enter 提交 */ onSubmit?: () => void /** 控件 id 的前缀,同一页有多份表单时避免撞 id */ idPrefix?: string /** 文件控件上传的上下文:{app_id, entry}(入口)、{task_id}(待办)、{blueprint_id}(试运行)。不给就不能上传 */ fileContext?: Record } export function SchemaForm({ schema, value, onChange, errors, readOnly, onSubmit, idPrefix, fileContext }: SchemaFormProps) { const auto = useId().replace(/[^a-zA-Z0-9_-]/g, '') const prefix = idPrefix ?? `sf${auto}` const order = fieldOrder(schema) const required = new Set(schema.required ?? []) const byField = new Map() const loose: FormError[] = [] for (const e of errors ?? []) { const key = fieldOfPath(e.path) if (key && schema.properties[key]) { if (!byField.has(key)) byField.set(key, e) } else loose.push(e) } const set = (key: string, v: unknown) => { const next = { ...value } if (v === undefined) delete next[key] else next[key] = v onChange(next) } const onKeyDown = (e: ReactKeyboardEvent) => { if (!onSubmit || isComposing(e) || !matchShortcut(e, 'Mod+Enter')) return e.preventDefault() onSubmit() } return (
{schema.title &&
{schema.title}
} {loose.length > 0 && (
{FORM_TEXT.errorsHead(loose.length)}
    {loose.map((e, i) => (
  • {e.path ? `${FORM_TEXT.unknownField(e.path)}${e.message}` : e.message}
  • ))}
)} {order.map((key) => ( set(key, v)} fileContext={fileContext} /> ))}
) } /** * 选人控件的候选从哪个池取:表单用在入口(app_id + entry)、待办(task_id)上时,按这个字段问服务端(05-已知遗留 (b)); * 蓝图试运行之类没有应用的地方不给(搭建者用整份名录) */ function principalSource(context: Record | undefined, field: string): CandidateSource | undefined { if (context?.task_id) return { purpose: 'form', task_id: context.task_id, field } if (context?.app_id && context.entry) return { purpose: 'form', app_id: context.app_id, entry: context.entry, field } return undefined } function FieldRow({ id, name, field, required, value, error, readOnly, onChange, fileContext }: { id: string; name: string; field: FormField; required: boolean; value: unknown; error?: FormError readOnly: boolean; onChange: (v: unknown) => void; fileContext?: Record }) { const widget = widgetOf(field) const title = field.title || name const hintId = `${id}-hint` const errorId = `${id}-error` const describedBy = error ? errorId : field.description ? hintId : undefined const aria = { id, 'aria-describedby': describedBy, 'aria-invalid': error ? true : undefined, 'aria-required': required || undefined } if (widget === 'note') { return (
{field.title &&
{field.title}
} {field.description &&
{field.description}
}
) } // 单选组、多选组、单个勾选框自带文字,用 fieldset / label 包起来;其余是「标题 + 一个控件」 const group = widget === 'radio' || widget === 'multiselect' const label: ReactNode = ( <> {title} {required && *} ) const foot = error ?
{error.message}
: field.description ?
{field.description}
: null if (group) { return (
{label} {widget === 'radio' ? : } {foot}
) } if (widget === 'principal') { // 选人控件自己有输入框和标签,外层不再用 label 指着一个 id return (
{label}
{readOnly ?
{typeof value === 'string' && value ? value : '—'}
: onChange(list[0]?.name || undefined)} />} {foot}
) } if (widget === 'file') { // 文件控件自己有按钮和链接:标题指着它的主按钮(id),限制和「谁能下载」写在控件下面 return (
{foot}
) } if (widget === 'checkbox') { return (
{foot}
) } return (
{foot}
) } type Aria = { id: string; 'aria-describedby'?: string; 'aria-invalid'?: boolean; 'aria-required'?: boolean } function Control({ widget, field, value, readOnly, aria, invalid, onChange }: { widget: string; field: FormField; value: unknown; readOnly: boolean; aria: Aria; invalid: boolean onChange: (v: unknown) => void }) { const builder = useAtLeast('builder') const style = invalid ? { borderColor: 'var(--err)' } : undefined const text = (v: string) => onChange(v === '' ? undefined : v) switch (widget) { case 'textarea': return