|
@@ -25,7 +25,7 @@ import {
|
|
|
import { classifyTicketError } from "../../lib/ticket";
|
|
import { classifyTicketError } from "../../lib/ticket";
|
|
|
import "../../styles/work-orders.css";
|
|
import "../../styles/work-orders.css";
|
|
|
|
|
|
|
|
-const FIELD_TYPES: { code: string; label: string }[] = [
|
|
|
|
|
|
|
+const LEAF_FIELD_TYPES: { code: string; label: string }[] = [
|
|
|
{ code: "text", label: "单行文本" },
|
|
{ code: "text", label: "单行文本" },
|
|
|
{ code: "textarea", label: "多行文本" },
|
|
{ code: "textarea", label: "多行文本" },
|
|
|
{ code: "number", label: "数字" },
|
|
{ code: "number", label: "数字" },
|
|
@@ -36,6 +36,14 @@ const FIELD_TYPES: { code: string; label: string }[] = [
|
|
|
{ code: "material_ref", label: "材料引用" },
|
|
{ code: "material_ref", label: "材料引用" },
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
+const FIELD_TYPES: { code: string; label: string }[] = [
|
|
|
|
|
+ ...LEAF_FIELD_TYPES,
|
|
|
|
|
+ { code: "array", label: "可增删卡片" },
|
|
|
|
|
+];
|
|
|
|
|
+
|
|
|
|
|
+const MAX_ARRAY_ITEM_FIELDS = 10;
|
|
|
|
|
+const DEFAULT_MAX_ITEMS = 20;
|
|
|
|
|
+
|
|
|
function isTextish(type: string): boolean {
|
|
function isTextish(type: string): boolean {
|
|
|
return type === "text" || type === "textarea" || type === "material_ref";
|
|
return type === "text" || type === "textarea" || type === "material_ref";
|
|
|
}
|
|
}
|
|
@@ -44,9 +52,32 @@ function maxLengthCap(type: string): number {
|
|
|
return type === "textarea" ? 2000 : 200;
|
|
return type === "textarea" ? 2000 : 200;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function clampInt(n: number | undefined, lo: number, hi: number, fallback: number): number {
|
|
|
|
|
+ if (n == null || !Number.isFinite(n)) return fallback;
|
|
|
|
|
+ return Math.min(Math.max(lo, Math.trunc(n)), hi);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/** 切类型时清掉不适用的值域,避免草稿带着非法约束被后端拒。 */
|
|
/** 切类型时清掉不适用的值域,避免草稿带着非法约束被后端拒。 */
|
|
|
function constraintsForType(type: string, prev: FormFieldSchema): Partial<FormFieldSchema> {
|
|
function constraintsForType(type: string, prev: FormFieldSchema): Partial<FormFieldSchema> {
|
|
|
const next: Partial<FormFieldSchema> = { type };
|
|
const next: Partial<FormFieldSchema> = { type };
|
|
|
|
|
+ if (type === "array") {
|
|
|
|
|
+ return {
|
|
|
|
|
+ type,
|
|
|
|
|
+ options: undefined,
|
|
|
|
|
+ maxLength: undefined,
|
|
|
|
|
+ format: undefined,
|
|
|
|
|
+ min: undefined,
|
|
|
|
|
+ max: undefined,
|
|
|
|
|
+ step: undefined,
|
|
|
|
|
+ span: undefined,
|
|
|
|
|
+ minItems: prev.minItems ?? 0,
|
|
|
|
|
+ maxItems: prev.maxItems ?? DEFAULT_MAX_ITEMS,
|
|
|
|
|
+ itemLabel: prev.itemLabel ?? "",
|
|
|
|
|
+ fields: prev.type === "array" && Array.isArray(prev.fields) && prev.fields.length > 0
|
|
|
|
|
+ ? prev.fields
|
|
|
|
|
+ : [emptyField()],
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
if (!isTextish(type)) {
|
|
if (!isTextish(type)) {
|
|
|
next.maxLength = undefined;
|
|
next.maxLength = undefined;
|
|
|
next.format = undefined;
|
|
next.format = undefined;
|
|
@@ -61,9 +92,33 @@ function constraintsForType(type: string, prev: FormFieldSchema): Partial<FormFi
|
|
|
if (type !== "select" && type !== "multiselect") {
|
|
if (type !== "select" && type !== "multiselect") {
|
|
|
next.options = undefined;
|
|
next.options = undefined;
|
|
|
}
|
|
}
|
|
|
|
|
+ next.minItems = undefined;
|
|
|
|
|
+ next.maxItems = undefined;
|
|
|
|
|
+ next.itemLabel = undefined;
|
|
|
|
|
+ next.fields = undefined;
|
|
|
return next;
|
|
return next;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/** 归一卡内叶子(保存/预览共用)。 */
|
|
|
|
|
+function normalizeLeafForSave(f: FormFieldSchema, columns: number): FormFieldSchema | null {
|
|
|
|
|
+ const key = f.key.trim();
|
|
|
|
|
+ const label = f.label.trim();
|
|
|
|
|
+ if (!key || !label) return null;
|
|
|
|
|
+ if (f.type === "array") return null;
|
|
|
|
|
+ const span = Math.min(Math.max(1, Math.trunc(Number(f.span ?? 1)) || 1), columns);
|
|
|
|
|
+ const out: FormFieldSchema = {
|
|
|
|
|
+ ...f,
|
|
|
|
|
+ key,
|
|
|
|
|
+ label,
|
|
|
|
|
+ span: columns === 1 ? undefined : span,
|
|
|
|
|
+ minItems: undefined,
|
|
|
|
|
+ maxItems: undefined,
|
|
|
|
|
+ itemLabel: undefined,
|
|
|
|
|
+ fields: undefined,
|
|
|
|
|
+ };
|
|
|
|
|
+ return out;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function parseOptionalNumber(raw: string): number | undefined {
|
|
function parseOptionalNumber(raw: string): number | undefined {
|
|
|
const t = raw.trim();
|
|
const t = raw.trim();
|
|
|
if (!t) return undefined;
|
|
if (!t) return undefined;
|
|
@@ -84,6 +139,14 @@ function emptyField(): FormFieldSchema {
|
|
|
return { key: "", label: "", type: "text", required: false };
|
|
return { key: "", label: "", type: "text", required: false };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function cloneField(f: FormFieldSchema): FormFieldSchema {
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...f,
|
|
|
|
|
+ options: f.options ? f.options.map((o) => ({ ...o })) : undefined,
|
|
|
|
|
+ fields: f.fields ? f.fields.map(cloneField) : undefined,
|
|
|
|
|
+ };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function schemaFrom(raw: FormSchema | string | null | undefined): FormSchema {
|
|
function schemaFrom(raw: FormSchema | string | null | undefined): FormSchema {
|
|
|
const parsed: FormSchema | null | undefined =
|
|
const parsed: FormSchema | null | undefined =
|
|
|
typeof raw === "string"
|
|
typeof raw === "string"
|
|
@@ -93,7 +156,7 @@ function schemaFrom(raw: FormSchema | string | null | undefined): FormSchema {
|
|
|
: raw;
|
|
: raw;
|
|
|
return {
|
|
return {
|
|
|
fields: Array.isArray(parsed?.fields)
|
|
fields: Array.isArray(parsed?.fields)
|
|
|
- ? parsed!.fields.map((f) => ({ ...f, options: f.options ? [...f.options] : undefined }))
|
|
|
|
|
|
|
+ ? parsed!.fields.map(cloneField)
|
|
|
: [],
|
|
: [],
|
|
|
layout: { columns: colCount(parsed?.layout) },
|
|
layout: { columns: colCount(parsed?.layout) },
|
|
|
};
|
|
};
|
|
@@ -108,16 +171,38 @@ function colCount(raw: { columns?: number } | undefined | null): number {
|
|
|
/** 保存前丢掉未填完的空行,并按当前列数夹紧 span,避免被后端拒。 */
|
|
/** 保存前丢掉未填完的空行,并按当前列数夹紧 span,避免被后端拒。 */
|
|
|
function schemaForSave(schema: FormSchema): FormSchema {
|
|
function schemaForSave(schema: FormSchema): FormSchema {
|
|
|
const columns = colCount(schema.layout);
|
|
const columns = colCount(schema.layout);
|
|
|
- return {
|
|
|
|
|
- layout: { columns },
|
|
|
|
|
- fields: schema.fields
|
|
|
|
|
- .map((f) => ({ ...f, key: f.key.trim(), label: f.label.trim() }))
|
|
|
|
|
- .filter((f) => f.key && f.label)
|
|
|
|
|
- .map((f) => {
|
|
|
|
|
- const span = Math.min(Math.max(1, Math.trunc(Number(f.span ?? 1)) || 1), columns);
|
|
|
|
|
- return { ...f, span: columns === 1 ? undefined : span };
|
|
|
|
|
- }),
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ const fields: FormFieldSchema[] = [];
|
|
|
|
|
+ for (const raw of schema.fields) {
|
|
|
|
|
+ const key = raw.key.trim();
|
|
|
|
|
+ const label = raw.label.trim();
|
|
|
|
|
+ if (!key || !label) continue;
|
|
|
|
|
+ if (raw.type === "array") {
|
|
|
|
|
+ let minItems = clampInt(raw.minItems, 0, DEFAULT_MAX_ITEMS, 0);
|
|
|
|
|
+ let maxItems = clampInt(raw.maxItems, 1, DEFAULT_MAX_ITEMS, DEFAULT_MAX_ITEMS);
|
|
|
|
|
+ if (raw.required) minItems = Math.max(minItems, 1);
|
|
|
|
|
+ if (minItems > maxItems) maxItems = minItems;
|
|
|
|
|
+ const itemFields = (raw.fields ?? [])
|
|
|
|
|
+ .map((c) => normalizeLeafForSave(c, columns))
|
|
|
|
|
+ .filter((c): c is FormFieldSchema => !!c)
|
|
|
|
|
+ .slice(0, MAX_ARRAY_ITEM_FIELDS);
|
|
|
|
|
+ if (itemFields.length === 0) continue;
|
|
|
|
|
+ fields.push({
|
|
|
|
|
+ key,
|
|
|
|
|
+ label,
|
|
|
|
|
+ type: "array",
|
|
|
|
|
+ required: !!raw.required,
|
|
|
|
|
+ help: raw.help?.trim() || undefined,
|
|
|
|
|
+ minItems,
|
|
|
|
|
+ maxItems,
|
|
|
|
|
+ itemLabel: raw.itemLabel?.trim() || undefined,
|
|
|
|
|
+ fields: itemFields,
|
|
|
|
|
+ });
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ const leaf = normalizeLeafForSave(raw, columns);
|
|
|
|
|
+ if (leaf) fields.push(leaf);
|
|
|
|
|
+ }
|
|
|
|
|
+ return { layout: { columns }, fields };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/** 预览用:补齐空 key/空标签,避免 React key 冲突与空白标题。 */
|
|
/** 预览用:补齐空 key/空标签,避免 React key 冲突与空白标题。 */
|
|
@@ -125,12 +210,36 @@ function previewSchema(schema: FormSchema): FormSchema {
|
|
|
const columns = colCount(schema.layout);
|
|
const columns = colCount(schema.layout);
|
|
|
return {
|
|
return {
|
|
|
layout: { columns },
|
|
layout: { columns },
|
|
|
- fields: schema.fields.map((f, i) => ({
|
|
|
|
|
- ...f,
|
|
|
|
|
- key: f.key.trim() || `__draft_${i}`,
|
|
|
|
|
- label: f.label.trim() || f.key.trim() || `字段 ${i + 1}`,
|
|
|
|
|
- span: columns === 1 ? undefined : Math.min(Math.max(1, Number(f.span ?? 1) || 1), columns),
|
|
|
|
|
- })),
|
|
|
|
|
|
|
+ fields: schema.fields.map((f, i) => {
|
|
|
|
|
+ if (f.type === "array") {
|
|
|
|
|
+ const children = (f.fields ?? []).map((c, j) => ({
|
|
|
|
|
+ ...c,
|
|
|
|
|
+ key: c.key.trim() || `__draft_${i}_${j}`,
|
|
|
|
|
+ label: c.label.trim() || c.key.trim() || `子字段 ${j + 1}`,
|
|
|
|
|
+ type: c.type === "array" ? "text" : c.type,
|
|
|
|
|
+ span: columns === 1 ? undefined : Math.min(Math.max(1, Number(c.span ?? 1) || 1), columns),
|
|
|
|
|
+ fields: undefined,
|
|
|
|
|
+ minItems: undefined,
|
|
|
|
|
+ maxItems: undefined,
|
|
|
|
|
+ itemLabel: undefined,
|
|
|
|
|
+ }));
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...f,
|
|
|
|
|
+ key: f.key.trim() || `__draft_${i}`,
|
|
|
|
|
+ label: f.label.trim() || f.key.trim() || `卡片组 ${i + 1}`,
|
|
|
|
|
+ span: undefined,
|
|
|
|
|
+ minItems: clampInt(f.minItems, 0, DEFAULT_MAX_ITEMS, 0),
|
|
|
|
|
+ maxItems: clampInt(f.maxItems, 1, DEFAULT_MAX_ITEMS, DEFAULT_MAX_ITEMS),
|
|
|
|
|
+ fields: children.length > 0 ? children : [{ key: `__draft_${i}_0`, label: "子字段", type: "text" }],
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...f,
|
|
|
|
|
+ key: f.key.trim() || `__draft_${i}`,
|
|
|
|
|
+ label: f.label.trim() || f.key.trim() || `字段 ${i + 1}`,
|
|
|
|
|
+ span: columns === 1 ? undefined : Math.min(Math.max(1, Number(f.span ?? 1) || 1), columns),
|
|
|
|
|
+ };
|
|
|
|
|
+ }),
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -187,6 +296,18 @@ export function FormTemplateAdminCard({ canEdit }: { canEdit: boolean }) {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ function patchNestedField(parent: number, child: number, patch: Partial<FormFieldSchema>) {
|
|
|
|
|
+ setSchema((prev) => {
|
|
|
|
|
+ const fields = prev.fields.map((f, idx) => {
|
|
|
|
|
+ if (idx !== parent || f.type !== "array") return f;
|
|
|
|
|
+ const nested = [...(f.fields ?? [])];
|
|
|
|
|
+ nested[child] = { ...nested[child], ...patch };
|
|
|
|
|
+ return { ...f, fields: nested };
|
|
|
|
|
+ });
|
|
|
|
|
+ return { ...prev, fields };
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async function saveDraft() {
|
|
async function saveDraft() {
|
|
|
setTip("");
|
|
setTip("");
|
|
|
const toSave = schemaForSave(schema);
|
|
const toSave = schemaForSave(schema);
|
|
@@ -314,6 +435,14 @@ export function FormTemplateAdminCard({ canEdit }: { canEdit: boolean }) {
|
|
|
...s,
|
|
...s,
|
|
|
layout: { columns },
|
|
layout: { columns },
|
|
|
fields: s.fields.map((f) => {
|
|
fields: s.fields.map((f) => {
|
|
|
|
|
+ if (f.type === "array") {
|
|
|
|
|
+ const nested = (f.fields ?? []).map((c) => {
|
|
|
|
|
+ if (columns === 1) return { ...c, span: undefined };
|
|
|
|
|
+ const span = Math.min(Math.max(1, Number(c.span ?? 1) || 1), columns);
|
|
|
|
|
+ return { ...c, span };
|
|
|
|
|
+ });
|
|
|
|
|
+ return { ...f, span: undefined, fields: nested };
|
|
|
|
|
+ }
|
|
|
if (columns === 1) return { ...f, span: undefined };
|
|
if (columns === 1) return { ...f, span: undefined };
|
|
|
const span = Math.min(Math.max(1, Number(f.span ?? 1) || 1), columns);
|
|
const span = Math.min(Math.max(1, Number(f.span ?? 1) || 1), columns);
|
|
|
return { ...f, span };
|
|
return { ...f, span };
|
|
@@ -345,20 +474,23 @@ export function FormTemplateAdminCard({ canEdit }: { canEdit: boolean }) {
|
|
|
|
|
|
|
|
{schema.fields.map((f, i) => {
|
|
{schema.fields.map((f, i) => {
|
|
|
const columns = colCount(schema.layout);
|
|
const columns = colCount(schema.layout);
|
|
|
|
|
+ const isArray = f.type === "array";
|
|
|
const showHelp = !!expanded[i];
|
|
const showHelp = !!expanded[i];
|
|
|
const showOptions = f.type === "select" || f.type === "multiselect";
|
|
const showOptions = f.type === "select" || f.type === "multiselect";
|
|
|
const showTextDomain = isTextish(f.type);
|
|
const showTextDomain = isTextish(f.type);
|
|
|
const showNumberDomain = f.type === "number";
|
|
const showNumberDomain = f.type === "number";
|
|
|
- const showSpan = columns > 1;
|
|
|
|
|
- const showExtra = showHelp || showOptions || showTextDomain || showNumberDomain || showSpan;
|
|
|
|
|
|
|
+ const showSpan = !isArray && columns > 1;
|
|
|
|
|
+ const showArrayExtra = isArray;
|
|
|
|
|
+ const showExtra =
|
|
|
|
|
+ showHelp || showOptions || showTextDomain || showNumberDomain || showSpan || showArrayExtra;
|
|
|
return (
|
|
return (
|
|
|
- <div key={i} className="work-order-settings-field-row">
|
|
|
|
|
|
|
+ <div key={i} className={`work-order-settings-field-row${isArray ? " is-array" : ""}`}>
|
|
|
<div className="work-order-settings-field-main">
|
|
<div className="work-order-settings-field-main">
|
|
|
<span className="work-order-settings-field-idx">{i + 1}</span>
|
|
<span className="work-order-settings-field-idx">{i + 1}</span>
|
|
|
<input
|
|
<input
|
|
|
disabled={!canEdit || busy}
|
|
disabled={!canEdit || busy}
|
|
|
value={f.key}
|
|
value={f.key}
|
|
|
- placeholder="network_vendor"
|
|
|
|
|
|
|
+ placeholder={isArray ? "devices" : "network_vendor"}
|
|
|
aria-label={`字段 ${i + 1} key`}
|
|
aria-label={`字段 ${i + 1} key`}
|
|
|
onChange={(e) => patchField(i, { key: e.target.value })}
|
|
onChange={(e) => patchField(i, { key: e.target.value })}
|
|
|
/>
|
|
/>
|
|
@@ -384,7 +516,17 @@ export function FormTemplateAdminCard({ canEdit }: { canEdit: boolean }) {
|
|
|
type="checkbox"
|
|
type="checkbox"
|
|
|
disabled={!canEdit || busy}
|
|
disabled={!canEdit || busy}
|
|
|
checked={!!f.required}
|
|
checked={!!f.required}
|
|
|
- onChange={(e) => patchField(i, { required: e.target.checked })}
|
|
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const required = e.target.checked;
|
|
|
|
|
+ if (isArray && required) {
|
|
|
|
|
+ patchField(i, {
|
|
|
|
|
+ required,
|
|
|
|
|
+ minItems: Math.max(1, Number(f.minItems ?? 0) || 0),
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ patchField(i, { required });
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
/>
|
|
/>
|
|
|
</label>
|
|
</label>
|
|
|
<div className="work-order-settings-field-ops">
|
|
<div className="work-order-settings-field-ops">
|
|
@@ -435,6 +577,245 @@ export function FormTemplateAdminCard({ canEdit }: { canEdit: boolean }) {
|
|
|
</select>
|
|
</select>
|
|
|
</label>
|
|
</label>
|
|
|
)}
|
|
)}
|
|
|
|
|
+ {showArrayExtra && (
|
|
|
|
|
+ <div className="work-order-settings-array-meta" role="group" aria-label={`字段 ${i + 1} 卡片组`}>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>最少</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ min={f.required ? 1 : 0}
|
|
|
|
|
+ max={DEFAULT_MAX_ITEMS}
|
|
|
|
|
+ value={f.minItems ?? 0}
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const n = clampInt(parseOptionalNumber(e.target.value), f.required ? 1 : 0, DEFAULT_MAX_ITEMS, f.required ? 1 : 0);
|
|
|
|
|
+ const maxItems = Math.max(n, Number(f.maxItems ?? DEFAULT_MAX_ITEMS) || DEFAULT_MAX_ITEMS);
|
|
|
|
|
+ patchField(i, { minItems: n, maxItems: Math.min(maxItems, DEFAULT_MAX_ITEMS) });
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>最多</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ min={1}
|
|
|
|
|
+ max={DEFAULT_MAX_ITEMS}
|
|
|
|
|
+ value={f.maxItems ?? DEFAULT_MAX_ITEMS}
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const n = clampInt(parseOptionalNumber(e.target.value), 1, DEFAULT_MAX_ITEMS, DEFAULT_MAX_ITEMS);
|
|
|
|
|
+ const minItems = Math.min(n, Number(f.minItems ?? 0) || 0);
|
|
|
|
|
+ patchField(i, { maxItems: n, minItems });
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>卡头</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ value={f.itemLabel ?? ""}
|
|
|
|
|
+ placeholder="设备"
|
|
|
|
|
+ onChange={(e) => patchField(i, { itemLabel: e.target.value })}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ {showArrayExtra && (
|
|
|
|
|
+ <div className="work-order-settings-nested" role="group" aria-label={`字段 ${i + 1} 卡内字段`}>
|
|
|
|
|
+ <div className="work-order-settings-nested-head">
|
|
|
|
|
+ <span>卡内字段</span>
|
|
|
|
|
+ {canEdit && (
|
|
|
|
|
+ <Button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ disabled={busy || (f.fields?.length ?? 0) >= MAX_ARRAY_ITEM_FIELDS}
|
|
|
|
|
+ onClick={() => {
|
|
|
|
|
+ const nested = [...(f.fields ?? []), emptyField()];
|
|
|
|
|
+ patchField(i, { fields: nested });
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ 增加子字段
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ {(f.fields ?? []).length === 0 && (
|
|
|
|
|
+ <p className="work-order-settings-meta">至少需要 1 个子字段。</p>
|
|
|
|
|
+ )}
|
|
|
|
|
+ {(f.fields ?? []).map((child, j) => {
|
|
|
|
|
+ const childOptions = child.type === "select" || child.type === "multiselect";
|
|
|
|
|
+ const childText = isTextish(child.type);
|
|
|
|
|
+ const childNumber = child.type === "number";
|
|
|
|
|
+ const childSpan = columns > 1;
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div key={j} className="work-order-settings-nested-row">
|
|
|
|
|
+ <div className="work-order-settings-field-main nested">
|
|
|
|
|
+ <span className="work-order-settings-field-idx">{j + 1}</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ value={child.key}
|
|
|
|
|
+ placeholder="sn"
|
|
|
|
|
+ aria-label={`字段 ${i + 1} 子字段 ${j + 1} key`}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, { key: e.target.value })}
|
|
|
|
|
+ />
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ value={child.label}
|
|
|
|
|
+ placeholder="标签"
|
|
|
|
|
+ aria-label={`字段 ${i + 1} 子字段 ${j + 1} 标签`}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, { label: e.target.value })}
|
|
|
|
|
+ />
|
|
|
|
|
+ <select
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ value={child.type}
|
|
|
|
|
+ aria-label={`字段 ${i + 1} 子字段 ${j + 1} 类型`}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, constraintsForType(e.target.value === "array" ? "text" : e.target.value, child))}
|
|
|
|
|
+ >
|
|
|
|
|
+ {LEAF_FIELD_TYPES.map((t) => (
|
|
|
|
|
+ <option key={t.code} value={t.code}>{t.label}</option>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ <label className="work-order-settings-field-req" title="卡内必填">
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="checkbox"
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ checked={!!child.required}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, { required: e.target.checked })}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ {canEdit && (
|
|
|
|
|
+ <Button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ disabled={busy}
|
|
|
|
|
+ onClick={() => {
|
|
|
|
|
+ const nested = (f.fields ?? []).filter((_, k) => k !== j);
|
|
|
|
|
+ patchField(i, { fields: nested });
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ 删
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ {(childOptions || childText || childNumber || childSpan) && (
|
|
|
|
|
+ <div className="work-order-settings-field-extra nested">
|
|
|
|
|
+ {childSpan && (
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>跨列</span>
|
|
|
|
|
+ <select
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ value={Math.min(Math.max(1, Number(child.span ?? 1) || 1), columns)}
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const n = Math.min(Math.max(1, Number(e.target.value) || 1), columns);
|
|
|
|
|
+ patchNestedField(i, j, { span: n });
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ {Array.from({ length: columns }, (_, k) => k + 1).map((n) => (
|
|
|
|
|
+ <option key={n} value={n}>
|
|
|
|
|
+ {n === 1 ? "占 1 列" : `跨 ${n} 列`}
|
|
|
|
|
+ </option>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ )}
|
|
|
|
|
+ {childText && (
|
|
|
|
|
+ <div className="work-order-settings-domain">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>上限</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ min={1}
|
|
|
|
|
+ max={maxLengthCap(child.type)}
|
|
|
|
|
+ value={child.maxLength ?? ""}
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const n = parseOptionalNumber(e.target.value);
|
|
|
|
|
+ patchNestedField(i, j, {
|
|
|
|
|
+ maxLength: n == null ? undefined : Math.min(Math.max(1, Math.trunc(n)), maxLengthCap(child.type)),
|
|
|
|
|
+ });
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>格式</span>
|
|
|
|
|
+ <select
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ value={child.format ?? ""}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, { format: e.target.value || undefined })}
|
|
|
|
|
+ >
|
|
|
|
|
+ <option value="">不限</option>
|
|
|
|
|
+ {FORM_FIELD_FORMATS.map((code) => (
|
|
|
|
|
+ <option key={code} value={code}>{FORM_FIELD_FORMAT_NAMES[code]}</option>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ {childNumber && (
|
|
|
|
|
+ <div className="work-order-settings-domain">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>最小</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ value={child.min ?? ""}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, { min: parseOptionalNumber(e.target.value) })}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>最大</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ value={child.max ?? ""}
|
|
|
|
|
+ onChange={(e) => patchNestedField(i, j, { max: parseOptionalNumber(e.target.value) })}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>步长</span>
|
|
|
|
|
+ <input
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ step="any"
|
|
|
|
|
+ value={child.step ?? ""}
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const n = parseOptionalNumber(e.target.value);
|
|
|
|
|
+ patchNestedField(i, j, { step: n != null && n > 0 ? n : undefined });
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ {childOptions && (
|
|
|
|
|
+ <label>
|
|
|
|
|
+ <span>选项</span>
|
|
|
|
|
+ <textarea
|
|
|
|
|
+ rows={2}
|
|
|
|
|
+ disabled={!canEdit || busy}
|
|
|
|
|
+ placeholder={"每行 value|label"}
|
|
|
|
|
+ value={(child.options ?? []).map((o) => `${o.value}|${o.label}`).join("\n")}
|
|
|
|
|
+ onChange={(e) => {
|
|
|
|
|
+ const options = e.target.value
|
|
|
|
|
+ .split("\n")
|
|
|
|
|
+ .map((line) => line.trim())
|
|
|
|
|
+ .filter(Boolean)
|
|
|
|
|
+ .map((line) => {
|
|
|
|
|
+ const [value, ...rest] = line.split("|");
|
|
|
|
|
+ return { value: value.trim(), label: (rest.join("|") || value).trim() };
|
|
|
|
|
+ });
|
|
|
|
|
+ patchNestedField(i, j, { options });
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </label>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ );
|
|
|
|
|
+ })}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
{showTextDomain && (
|
|
{showTextDomain && (
|
|
|
<div className="work-order-settings-domain" role="group" aria-label={`字段 ${i + 1} 值域`}>
|
|
<div className="work-order-settings-domain" role="group" aria-label={`字段 ${i + 1} 值域`}>
|
|
|
<span className="work-order-settings-domain-label">值域</span>
|
|
<span className="work-order-settings-domain-label">值域</span>
|
|
@@ -583,7 +964,7 @@ export function FormTemplateAdminCard({ canEdit }: { canEdit: boolean }) {
|
|
|
{tip && <p role="alert" className="work-order-form-error">{tip}</p>}
|
|
{tip && <p role="alert" className="work-order-form-error">{tip}</p>}
|
|
|
{ackOpen && (
|
|
{ackOpen && (
|
|
|
<div role="dialog" className="work-order-settings-ack">
|
|
<div role="dialog" className="work-order-settings-ack">
|
|
|
- <p>新版本新增或改动了必填字段,在途工单将按新模板校验。确认继续发布?</p>
|
|
|
|
|
|
|
+ <p>新版本新增或改动了必填(含卡片组下限/卡内必填),在途工单将按新模板校验。确认继续发布?</p>
|
|
|
<div className="ops-form-actions">
|
|
<div className="ops-form-actions">
|
|
|
<Button type="button" size="sm" disabled={busy} onClick={() => void publish(true)}>确认并发布</Button>
|
|
<Button type="button" size="sm" disabled={busy} onClick={() => void publish(true)}>确认并发布</Button>
|
|
|
<Button type="button" variant="outline" size="sm" disabled={busy} onClick={() => setAckOpen(false)}>取消</Button>
|
|
<Button type="button" variant="outline" size="sm" disabled={busy} onClick={() => setAckOpen(false)}>取消</Button>
|