|
|
@@ -3,7 +3,8 @@
|
|
|
* 支持顶层混排叶子字段与一层 `array`(可增删卡片)。
|
|
|
* 值域约束不常驻展示;输入中/失焦/提交前校验并提示。服务端仍全量校验。
|
|
|
*/
|
|
|
-import { forwardRef, useImperativeHandle, useMemo, useState } from "react";
|
|
|
+import { forwardRef, useId, useImperativeHandle, useMemo, useState, type CSSProperties } from "react";
|
|
|
+import { ListChecks, Plus, Trash2 } from "lucide-react";
|
|
|
import { Button } from "../ui/button";
|
|
|
import { FieldHint } from "../ui/field-hint";
|
|
|
import {
|
|
|
@@ -51,12 +52,6 @@ function fieldSpan(field: FormFieldSchema, cols: number): number {
|
|
|
return field.type === "textarea" || field.type === "multiselect" ? cols : 1;
|
|
|
}
|
|
|
|
|
|
-function gridClass(cols: number): string {
|
|
|
- if (cols === 1) return " cols-1";
|
|
|
- if (cols === 3) return " cols-3";
|
|
|
- return "";
|
|
|
-}
|
|
|
-
|
|
|
function isEmpty(value: unknown): boolean {
|
|
|
return (
|
|
|
value == null ||
|
|
|
@@ -285,28 +280,27 @@ export function validateExtValues(
|
|
|
}
|
|
|
|
|
|
function FieldInput({
|
|
|
+ id,
|
|
|
field,
|
|
|
value,
|
|
|
readOnly,
|
|
|
- invalid,
|
|
|
- idPrefix,
|
|
|
+ errorId,
|
|
|
onChange,
|
|
|
onBlurValidate,
|
|
|
}: {
|
|
|
+ id: string;
|
|
|
field: FormFieldSchema;
|
|
|
value: unknown;
|
|
|
readOnly?: boolean;
|
|
|
- invalid?: boolean;
|
|
|
- idPrefix?: string;
|
|
|
+ errorId?: string;
|
|
|
onChange: (v: unknown) => void;
|
|
|
onBlurValidate: () => void;
|
|
|
}) {
|
|
|
- const id = `${idPrefix ?? "ext"}-${field.key}`;
|
|
|
const common = {
|
|
|
id,
|
|
|
disabled: readOnly,
|
|
|
- "aria-label": field.label,
|
|
|
- "aria-invalid": invalid || undefined,
|
|
|
+ "aria-invalid": Boolean(errorId) || undefined,
|
|
|
+ "aria-describedby": errorId,
|
|
|
onBlur: onBlurValidate,
|
|
|
};
|
|
|
switch (field.type) {
|
|
|
@@ -346,20 +340,22 @@ function FieldInput({
|
|
|
);
|
|
|
case "boolean":
|
|
|
return (
|
|
|
- <label className="work-order-repeater-flag">
|
|
|
+ <label className="work-order-ext-choice">
|
|
|
<input
|
|
|
{...common}
|
|
|
+ aria-labelledby={`${id}-title`}
|
|
|
type="checkbox"
|
|
|
checked={value === true}
|
|
|
onChange={(e) => onChange(e.target.checked)}
|
|
|
/>
|
|
|
- <span>{field.help || "是"}</span>
|
|
|
+ <span>是</span>
|
|
|
</label>
|
|
|
);
|
|
|
case "select":
|
|
|
return (
|
|
|
<select
|
|
|
{...common}
|
|
|
+ className={typeof value !== "string" || !value ? "is-placeholder" : undefined}
|
|
|
value={typeof value === "string" ? value : ""}
|
|
|
onChange={(e) => onChange(e.target.value)}
|
|
|
>
|
|
|
@@ -374,16 +370,17 @@ function FieldInput({
|
|
|
case "multiselect": {
|
|
|
const picked = Array.isArray(value) ? value.map(String) : [];
|
|
|
return (
|
|
|
- <div className="wide work-order-checklist">
|
|
|
+ <div className="work-order-ext-choices" role="group" aria-labelledby={`${id}-title`} aria-describedby={errorId}>
|
|
|
{(field.options ?? []).map((o) => {
|
|
|
const on = picked.includes(o.value);
|
|
|
return (
|
|
|
- <label key={o.value}>
|
|
|
+ <label key={o.value} className="work-order-ext-choice">
|
|
|
<input
|
|
|
type="checkbox"
|
|
|
disabled={readOnly}
|
|
|
checked={on}
|
|
|
- aria-invalid={invalid || undefined}
|
|
|
+ aria-invalid={Boolean(errorId) || undefined}
|
|
|
+ aria-describedby={errorId}
|
|
|
onChange={(e) => {
|
|
|
const next = e.target.checked
|
|
|
? [...picked, o.value]
|
|
|
@@ -415,6 +412,33 @@ function FieldInput({
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function ExtField({ field, value, error, cols, readOnly, onChange, onBlurValidate }: {
|
|
|
+ field: FormFieldSchema;
|
|
|
+ value: unknown;
|
|
|
+ error: string | null;
|
|
|
+ cols: number;
|
|
|
+ readOnly?: boolean;
|
|
|
+ onChange: (value: unknown) => void;
|
|
|
+ onBlurValidate: () => void;
|
|
|
+}) {
|
|
|
+ const id = useId();
|
|
|
+ const errorId = error ? `${id}-error` : undefined;
|
|
|
+ const titleId = `${id}-title`;
|
|
|
+ const title = <>{field.label}{field.required && <em> *</em>}</>;
|
|
|
+ return (
|
|
|
+ <div className="work-order-ext-field" style={{ "--ext-field-span": fieldSpan(field, cols) } as CSSProperties}>
|
|
|
+ <div className="field-label-title">
|
|
|
+ {field.type === "multiselect" || field.type === "boolean"
|
|
|
+ ? <span id={titleId}>{title}</span>
|
|
|
+ : <label htmlFor={id}>{title}</label>}
|
|
|
+ {field.help && <FieldHint label={`${field.label}说明`}>{field.help}</FieldHint>}
|
|
|
+ </div>
|
|
|
+ <FieldInput id={id} field={field} value={value} readOnly={readOnly} errorId={errorId} onChange={onChange} onBlurValidate={onBlurValidate} />
|
|
|
+ {error && <small id={errorId} className="work-order-ext-field-error" role="alert">{error}</small>}
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
function ArrayFieldBlock({
|
|
|
field,
|
|
|
value,
|
|
|
@@ -458,44 +482,59 @@ function ArrayFieldBlock({
|
|
|
: null;
|
|
|
|
|
|
return (
|
|
|
- <div
|
|
|
- className="wide work-order-repeater work-order-ext-array"
|
|
|
- style={{ gridColumn: `span ${cols}` }}
|
|
|
- data-gap-focus={`ext-${field.key}`}
|
|
|
- >
|
|
|
- <div className="work-order-repeater-head">
|
|
|
- <span>
|
|
|
- {field.label}
|
|
|
- {(field.required || minItems > 0) && <em> *</em>}
|
|
|
- {field.help ? (
|
|
|
- <FieldHint label={`${field.label}说明`}>{field.help}</FieldHint>
|
|
|
- ) : null}
|
|
|
- </span>
|
|
|
+ <div className="work-order-ext-array" data-gap-focus={`ext-${field.key}`}>
|
|
|
+ <div className="work-order-ext-array-head">
|
|
|
+ <div>
|
|
|
+ <div className="field-label-title">
|
|
|
+ <strong>{field.label}</strong>
|
|
|
+ {(field.required || minItems > 0) && <em> *</em>}
|
|
|
+ {field.help && <FieldHint label={`${field.label}说明`}>{field.help}</FieldHint>}
|
|
|
+ </div>
|
|
|
+ <small>{cards.length} / {maxItems} 条{minItems > 0 ? ` · 至少 ${minItems} 条` : ""}</small>
|
|
|
+ </div>
|
|
|
+ {!readOnly && (
|
|
|
+ <Button
|
|
|
+ type="button"
|
|
|
+ variant="ghost"
|
|
|
+ size="sm"
|
|
|
+ className="org-button org-button-compact org-button-scope"
|
|
|
+ disabled={cards.length >= maxItems}
|
|
|
+ onClick={() => {
|
|
|
+ if (cards.length >= maxItems) return;
|
|
|
+ onChangeCards([...cards, {}]);
|
|
|
+ markDirty(groupPath);
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <Plus size={14} aria-hidden="true" />添加{itemLabel}
|
|
|
+ </Button>
|
|
|
+ )}
|
|
|
</div>
|
|
|
|
|
|
+ {!cards.length && <p className="work-order-ext-array-empty">尚未添加{itemLabel}</p>}
|
|
|
{cards.map((card, i) => {
|
|
|
const cardKey = `${field.key}[${i}]`;
|
|
|
return (
|
|
|
- <div key={cardKey} className="work-order-repeater-row work-order-ext-array-card">
|
|
|
+ <div key={cardKey} className="work-order-ext-array-card">
|
|
|
<div className="work-order-ext-array-card-head">
|
|
|
- <strong>{itemLabel}{i + 1}</strong>
|
|
|
+ <strong>{itemLabel} {i + 1}</strong>
|
|
|
{!readOnly && (
|
|
|
<Button
|
|
|
type="button"
|
|
|
variant="ghost"
|
|
|
size="sm"
|
|
|
- className="work-order-ext-array-remove"
|
|
|
+ className="org-button org-button-compact org-button-danger-soft"
|
|
|
+ aria-label={`删除${itemLabel} ${i + 1}`}
|
|
|
onClick={() => {
|
|
|
const next = cards.filter((_, j) => j !== i);
|
|
|
onChangeCards(next);
|
|
|
markDirty(groupPath);
|
|
|
}}
|
|
|
>
|
|
|
- 删除
|
|
|
+ <Trash2 size={14} aria-hidden="true" />删除
|
|
|
</Button>
|
|
|
)}
|
|
|
</div>
|
|
|
- <div className={`work-order-form-grid work-order-ext-array-card-grid${gridClass(cols)}`}>
|
|
|
+ <div className="work-order-ext-grid">
|
|
|
{children.map((child) => {
|
|
|
const path = `${field.key}[${i}].${child.key}`;
|
|
|
const active = forceAll || dirty[path] || dirty[groupPath];
|
|
|
@@ -505,36 +544,25 @@ function ArrayFieldBlock({
|
|
|
? validateExtField(child, card[child.key], { phase, enforceRequired })
|
|
|
: null;
|
|
|
return (
|
|
|
- <label
|
|
|
+ <ExtField
|
|
|
key={child.key}
|
|
|
- className={err ? "is-ext-invalid" : undefined}
|
|
|
- style={{ gridColumn: `span ${fieldSpan(child, cols)}` }}
|
|
|
- >
|
|
|
- {child.label}
|
|
|
- {child.required ? <em> *</em> : null}
|
|
|
- {child.help && child.type !== "boolean" ? (
|
|
|
- <FieldHint label={`${child.label}说明`}>{child.help}</FieldHint>
|
|
|
- ) : null}
|
|
|
- <FieldInput
|
|
|
- field={child}
|
|
|
- value={card[child.key]}
|
|
|
- readOnly={readOnly}
|
|
|
- invalid={!!err}
|
|
|
- idPrefix={`ext-${field.key}-${i}`}
|
|
|
- onChange={(v) => {
|
|
|
- markDirty(path);
|
|
|
- const next = cards.map((c, j) =>
|
|
|
- j === i ? { ...c, [child.key]: v } : c,
|
|
|
- );
|
|
|
- onChangeCards(next);
|
|
|
- }}
|
|
|
- onBlurValidate={() => {
|
|
|
- markDirty(path);
|
|
|
- markCommitted(path);
|
|
|
- }}
|
|
|
- />
|
|
|
- {err ? <small className="work-order-ext-field-error" role="alert">{err}</small> : null}
|
|
|
- </label>
|
|
|
+ field={child}
|
|
|
+ value={card[child.key]}
|
|
|
+ error={err}
|
|
|
+ readOnly={readOnly}
|
|
|
+ cols={cols}
|
|
|
+ onChange={(v) => {
|
|
|
+ markDirty(path);
|
|
|
+ const next = cards.map((c, j) =>
|
|
|
+ j === i ? { ...c, [child.key]: v } : c,
|
|
|
+ );
|
|
|
+ onChangeCards(next);
|
|
|
+ }}
|
|
|
+ onBlurValidate={() => {
|
|
|
+ markDirty(path);
|
|
|
+ markCommitted(path);
|
|
|
+ }}
|
|
|
+ />
|
|
|
);
|
|
|
})}
|
|
|
</div>
|
|
|
@@ -542,28 +570,6 @@ function ArrayFieldBlock({
|
|
|
);
|
|
|
})}
|
|
|
|
|
|
- {!readOnly && (
|
|
|
- <div className="work-order-repeater-foot">
|
|
|
- <Button
|
|
|
- type="button"
|
|
|
- variant="outline"
|
|
|
- size="sm"
|
|
|
- className="work-order-ext-array-add"
|
|
|
- disabled={cards.length >= maxItems}
|
|
|
- onClick={() => {
|
|
|
- if (cards.length >= maxItems) return;
|
|
|
- onChangeCards([...cards, {}]);
|
|
|
- markDirty(groupPath);
|
|
|
- }}
|
|
|
- >
|
|
|
- 增加{itemLabel}
|
|
|
- </Button>
|
|
|
- <small>
|
|
|
- {cards.length}/{maxItems}
|
|
|
- {minItems > 0 ? `(至少 ${minItems})` : ""}
|
|
|
- </small>
|
|
|
- </div>
|
|
|
- )}
|
|
|
{groupCountErr ? (
|
|
|
<small className="work-order-ext-field-error" role="alert">{groupCountErr}</small>
|
|
|
) : null}
|
|
|
@@ -602,31 +608,21 @@ function LeafFieldBlock({
|
|
|
? validateExtField(field, value, { phase, enforceRequired })
|
|
|
: null;
|
|
|
return (
|
|
|
- <label
|
|
|
- className={err ? "is-ext-invalid" : undefined}
|
|
|
- style={{ gridColumn: `span ${fieldSpan(field, cols)}` }}
|
|
|
- >
|
|
|
- {field.label}
|
|
|
- {field.required ? <em> *</em> : null}
|
|
|
- {field.help && field.type !== "boolean" ? (
|
|
|
- <FieldHint label={`${field.label}说明`}>{field.help}</FieldHint>
|
|
|
- ) : null}
|
|
|
- <FieldInput
|
|
|
- field={field}
|
|
|
- value={value}
|
|
|
- readOnly={readOnly}
|
|
|
- invalid={!!err}
|
|
|
- onChange={(v) => {
|
|
|
- markDirty(field.key);
|
|
|
- onChange(v);
|
|
|
- }}
|
|
|
- onBlurValidate={() => {
|
|
|
- markDirty(field.key);
|
|
|
- markCommitted(field.key);
|
|
|
- }}
|
|
|
- />
|
|
|
- {err ? <small className="work-order-ext-field-error" role="alert">{err}</small> : null}
|
|
|
- </label>
|
|
|
+ <ExtField
|
|
|
+ field={field}
|
|
|
+ value={value}
|
|
|
+ error={err}
|
|
|
+ readOnly={readOnly}
|
|
|
+ cols={cols}
|
|
|
+ onChange={(v) => {
|
|
|
+ markDirty(field.key);
|
|
|
+ onChange(v);
|
|
|
+ }}
|
|
|
+ onBlurValidate={() => {
|
|
|
+ markDirty(field.key);
|
|
|
+ markCommitted(field.key);
|
|
|
+ }}
|
|
|
+ />
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -676,7 +672,7 @@ export const TicketExtForm = forwardRef<TicketExtFormHandle, Props>(function Tic
|
|
|
const cols = layoutCols(schema);
|
|
|
|
|
|
const body = (
|
|
|
- <div className={`work-order-form-grid${gridClass(cols)}`}>
|
|
|
+ <div className="work-order-ext-grid">
|
|
|
{schema.fields.map((field) => {
|
|
|
if (field.type === "array") {
|
|
|
return (
|
|
|
@@ -723,12 +719,13 @@ export const TicketExtForm = forwardRef<TicketExtFormHandle, Props>(function Tic
|
|
|
);
|
|
|
|
|
|
return (
|
|
|
- <div className="wide work-order-gap-anchor" data-gap-focus="ext">
|
|
|
- <h4 className="work-order-subhead">
|
|
|
- 扩展信息
|
|
|
- {ext?.template_version != null ? `(模板 v${ext.template_version})` : ""}
|
|
|
- </h4>
|
|
|
+ <section className="wide work-order-ext-editor" aria-label="扩展信息" data-gap-focus="ext"
|
|
|
+ style={{ "--ext-template-cols": cols } as CSSProperties}>
|
|
|
+ <header className="work-order-ext-editor-head">
|
|
|
+ <h3><ListChecks size={16} aria-hidden="true" />扩展信息</h3>
|
|
|
+ {ext?.template_version != null && <small>模板 v{ext.template_version}</small>}
|
|
|
+ </header>
|
|
|
{body}
|
|
|
- </div>
|
|
|
+ </section>
|
|
|
);
|
|
|
});
|