|
|
@@ -3,7 +3,7 @@
|
|
|
* 支持顶层混排叶子字段与一层 `array`(可增删卡片)。
|
|
|
* 值域约束不常驻展示;输入中/失焦/提交前校验并提示。服务端仍全量校验。
|
|
|
*/
|
|
|
-import { forwardRef, useImperativeHandle, useMemo, useState } from "react";
|
|
|
+import { forwardRef, useImperativeHandle, useMemo, useState, type CSSProperties } from "react";
|
|
|
import { Button } from "../ui/button";
|
|
|
import { FieldHint } from "../ui/field-hint";
|
|
|
import {
|
|
|
@@ -87,6 +87,42 @@ function asCardList(value: unknown): Record<string, unknown>[] {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+function readOnlyValue(field: FormFieldSchema, value: unknown): string | null {
|
|
|
+ if (field.type === "boolean") return typeof value === "boolean" ? (value ? "是" : "否") : null;
|
|
|
+ const optionLabel = (code: string) => field.options?.find((option) => option.value === code)?.label ?? code;
|
|
|
+ if (field.type === "multiselect") {
|
|
|
+ return Array.isArray(value)
|
|
|
+ ? value.filter((item): item is string => typeof item === "string" && item.trim() !== "").map(optionLabel).join("、") || null
|
|
|
+ : null;
|
|
|
+ }
|
|
|
+ if (isEmpty(value) || (typeof value !== "string" && typeof value !== "number")) return null;
|
|
|
+ return field.type === "select" ? optionLabel(String(value)) : String(value);
|
|
|
+}
|
|
|
+
|
|
|
+function ReadOnlyFields({ fields, values, cols }: {
|
|
|
+ fields: FormFieldSchema[]; values: Record<string, unknown>; cols: number;
|
|
|
+}) {
|
|
|
+ return <dl className="work-order-ext-detail-fields" style={{ "--ext-template-cols": cols } as CSSProperties}>
|
|
|
+ {fields.map((field) => {
|
|
|
+ const value = values[field.key];
|
|
|
+ if (field.type === "array") {
|
|
|
+ const cards = asCardList(value);
|
|
|
+ return <div key={field.key} className="work-order-ext-detail-group">
|
|
|
+ <dt>{field.label}{cards.length > 0 && <small>{cards.length} 条</small>}</dt>
|
|
|
+ <dd>{cards.length ? cards.map((card, index) => <div key={index} className="work-order-ext-detail-card">
|
|
|
+ <h4>{field.itemLabel || "条目"} {index + 1}</h4>
|
|
|
+ <ReadOnlyFields fields={field.fields ?? []} values={card} cols={cols} />
|
|
|
+ </div>) : <span className="table-placeholder">暂无记录</span>}</dd>
|
|
|
+ </div>;
|
|
|
+ }
|
|
|
+ return <div key={field.key} className="work-order-ext-detail-field" style={{ "--ext-field-span": fieldSpan(field, cols) } as CSSProperties}>
|
|
|
+ <dt>{field.label}{field.help && <FieldHint label={`${field.label}说明`}>{field.help}</FieldHint>}</dt>
|
|
|
+ <dd>{readOnlyValue(field, value) ?? <span className="table-placeholder">—</span>}</dd>
|
|
|
+ </div>;
|
|
|
+ })}
|
|
|
+ </dl>;
|
|
|
+}
|
|
|
+
|
|
|
function formatOk(s: string, rule: string): boolean {
|
|
|
switch (rule) {
|
|
|
case "ipv4": {
|
|
|
@@ -287,7 +323,6 @@ export function validateExtValues(
|
|
|
function FieldInput({
|
|
|
field,
|
|
|
value,
|
|
|
- readOnly,
|
|
|
invalid,
|
|
|
idPrefix,
|
|
|
onChange,
|
|
|
@@ -295,7 +330,6 @@ function FieldInput({
|
|
|
}: {
|
|
|
field: FormFieldSchema;
|
|
|
value: unknown;
|
|
|
- readOnly?: boolean;
|
|
|
invalid?: boolean;
|
|
|
idPrefix?: string;
|
|
|
onChange: (v: unknown) => void;
|
|
|
@@ -304,7 +338,6 @@ function FieldInput({
|
|
|
const id = `${idPrefix ?? "ext"}-${field.key}`;
|
|
|
const common = {
|
|
|
id,
|
|
|
- disabled: readOnly,
|
|
|
"aria-label": field.label,
|
|
|
"aria-invalid": invalid || undefined,
|
|
|
onBlur: onBlurValidate,
|
|
|
@@ -381,7 +414,6 @@ function FieldInput({
|
|
|
<label key={o.value}>
|
|
|
<input
|
|
|
type="checkbox"
|
|
|
- disabled={readOnly}
|
|
|
checked={on}
|
|
|
aria-invalid={invalid || undefined}
|
|
|
onChange={(e) => {
|
|
|
@@ -419,7 +451,6 @@ function ArrayFieldBlock({
|
|
|
field,
|
|
|
value,
|
|
|
cols,
|
|
|
- readOnly,
|
|
|
forceAll,
|
|
|
dirty,
|
|
|
committed,
|
|
|
@@ -431,7 +462,6 @@ function ArrayFieldBlock({
|
|
|
field: FormFieldSchema;
|
|
|
value: unknown;
|
|
|
cols: number;
|
|
|
- readOnly?: boolean;
|
|
|
forceAll: boolean;
|
|
|
dirty: Record<string, boolean>;
|
|
|
committed: Record<string, boolean>;
|
|
|
@@ -479,21 +509,19 @@ function ArrayFieldBlock({
|
|
|
<div key={cardKey} className="work-order-repeater-row work-order-ext-array-card">
|
|
|
<div className="work-order-ext-array-card-head">
|
|
|
<strong>{itemLabel}{i + 1}</strong>
|
|
|
- {!readOnly && (
|
|
|
- <Button
|
|
|
- type="button"
|
|
|
- variant="ghost"
|
|
|
- size="sm"
|
|
|
- className="work-order-ext-array-remove"
|
|
|
- onClick={() => {
|
|
|
- const next = cards.filter((_, j) => j !== i);
|
|
|
- onChangeCards(next);
|
|
|
- markDirty(groupPath);
|
|
|
- }}
|
|
|
- >
|
|
|
- 删除
|
|
|
- </Button>
|
|
|
- )}
|
|
|
+ <Button
|
|
|
+ type="button"
|
|
|
+ variant="ghost"
|
|
|
+ size="sm"
|
|
|
+ className="work-order-ext-array-remove"
|
|
|
+ onClick={() => {
|
|
|
+ const next = cards.filter((_, j) => j !== i);
|
|
|
+ onChangeCards(next);
|
|
|
+ markDirty(groupPath);
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ 删除
|
|
|
+ </Button>
|
|
|
</div>
|
|
|
<div className={`work-order-form-grid work-order-ext-array-card-grid${gridClass(cols)}`}>
|
|
|
{children.map((child) => {
|
|
|
@@ -518,7 +546,6 @@ function ArrayFieldBlock({
|
|
|
<FieldInput
|
|
|
field={child}
|
|
|
value={card[child.key]}
|
|
|
- readOnly={readOnly}
|
|
|
invalid={!!err}
|
|
|
idPrefix={`ext-${field.key}-${i}`}
|
|
|
onChange={(v) => {
|
|
|
@@ -542,28 +569,26 @@ 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>
|
|
|
- )}
|
|
|
+ <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}
|
|
|
@@ -575,7 +600,6 @@ function LeafFieldBlock({
|
|
|
field,
|
|
|
value,
|
|
|
cols,
|
|
|
- readOnly,
|
|
|
forceAll,
|
|
|
dirty,
|
|
|
committed,
|
|
|
@@ -587,7 +611,6 @@ function LeafFieldBlock({
|
|
|
field: FormFieldSchema;
|
|
|
value: unknown;
|
|
|
cols: number;
|
|
|
- readOnly?: boolean;
|
|
|
forceAll: boolean;
|
|
|
dirty: Record<string, boolean>;
|
|
|
committed: Record<string, boolean>;
|
|
|
@@ -614,7 +637,6 @@ function LeafFieldBlock({
|
|
|
<FieldInput
|
|
|
field={field}
|
|
|
value={value}
|
|
|
- readOnly={readOnly}
|
|
|
invalid={!!err}
|
|
|
onChange={(v) => {
|
|
|
markDirty(field.key);
|
|
|
@@ -675,6 +697,14 @@ export const TicketExtForm = forwardRef<TicketExtFormHandle, Props>(function Tic
|
|
|
if (!schema) return null;
|
|
|
const cols = layoutCols(schema);
|
|
|
|
|
|
+ if (readOnly) return <section className="work-order-ext-detail" aria-label="扩展信息">
|
|
|
+ <header className="work-order-content-heading">
|
|
|
+ <h3 className="work-order-panel-title">扩展信息</h3>
|
|
|
+ {ext?.template_version != null && <small>模板 v{ext.template_version}</small>}
|
|
|
+ </header>
|
|
|
+ <ReadOnlyFields fields={schema.fields} values={current} cols={cols} />
|
|
|
+ </section>;
|
|
|
+
|
|
|
const body = (
|
|
|
<div className={`work-order-form-grid${gridClass(cols)}`}>
|
|
|
{schema.fields.map((field) => {
|
|
|
@@ -685,7 +715,6 @@ export const TicketExtForm = forwardRef<TicketExtFormHandle, Props>(function Tic
|
|
|
field={field}
|
|
|
value={current[field.key]}
|
|
|
cols={cols}
|
|
|
- readOnly={readOnly}
|
|
|
forceAll={forceAll}
|
|
|
dirty={dirty}
|
|
|
committed={committed}
|
|
|
@@ -705,7 +734,6 @@ export const TicketExtForm = forwardRef<TicketExtFormHandle, Props>(function Tic
|
|
|
field={field}
|
|
|
value={current[field.key]}
|
|
|
cols={cols}
|
|
|
- readOnly={readOnly}
|
|
|
forceAll={forceAll}
|
|
|
dirty={dirty}
|
|
|
committed={committed}
|