// 字段表 + 加字段 Dialog + UI 类型↔api 类型映射。 // 纯 HTML (本仓无可复用表格组件)+ components/ui/Dialog。 import { useState } from 'react'; import type { Dispatch, SetStateAction } from 'react'; import { Dialog } from '../components/ui/Dialog'; import type { EntityAttr, AttrType } from '../api/meta'; import { ReferencePicker } from './ReferencePicker'; export type UiType = '文本' | '整数' | '小数' | '日期' | '日期时间' | '布尔' | '引用'; export interface FieldRow { column: string; display: string; type: UiType; length?: number; mandatory?: boolean; referenceTable?: string; } const UI_TYPES: UiType[] = ['文本', '整数', '小数', '日期', '日期时间', '布尔', '引用']; const TYPE_MAP: Record = { 文本: 'string', 整数: 'integer', 小数: 'number', 日期: 'date', 日期时间: 'datetime', 布尔: 'boolean', 引用: 'reference', }; /** UI 行 → api EntityAttr。length 仅文本有意义;mandatory=false 不输出;引用带 referenceTable。 */ export function buildAttribute(row: FieldRow): EntityAttr { const attr: EntityAttr = { column: row.column, type: TYPE_MAP[row.type] }; if (row.display) attr.display = row.display; if (row.type === '文本' && typeof row.length === 'number') attr.length = row.length; if (row.mandatory) attr.mandatory = true; if (row.type === '引用' && row.referenceTable) attr.referenceTable = row.referenceTable; return attr; } interface FieldTableProps { rows: FieldRow[]; onChange: Dispatch>; } export function FieldTable({ rows, onChange }: FieldTableProps) { const [adding, setAdding] = useState(false); // 加字段草稿 const [column, setColumn] = useState(''); const [display, setDisplay] = useState(''); const [type, setType] = useState('文本'); const [length, setLength] = useState('200'); const [mandatory, setMandatory] = useState(false); const [refTable, setRefTable] = useState(''); function openAdd() { setColumn(''); setDisplay(''); setType('文本'); setLength('200'); setMandatory(false); setRefTable(''); setAdding(true); } const columnValid = /^[A-Z][A-Z0-9_]{0,40}$/.test(column); const refOk = type !== '引用' || !!refTable; const canAdd = columnValid && !!display && refOk; function submitField() { const row: FieldRow = { column, display, type }; if (type === '文本') row.length = Number(length) || undefined; if (mandatory) row.mandatory = true; if (type === '引用') row.referenceTable = refTable; onChange((prev) => [...prev, row]); setAdding(false); } function removeRow(i: number) { onChange((prev) => prev.filter((_, idx) => idx !== i)); } return (
字段(标准列由后端自动追加,无需在此添加 ID/创建人等)
{rows.length === 0 ? ( ) : ( rows.map((r, i) => ( )) )}
列名 显示名 类型 必填
尚无字段
{r.column} {r.display} {r.type}{r.type === '引用' && r.referenceTable ? `→${r.referenceTable}` : ''} {r.mandatory ? '是' : ''} removeRow(i)}>删除
{adding ? ( setAdding(false)} foot={ <> } >
{type === '文本' ? ( ) : null} {type === '引用' ? ( ) : null}
) : null} ); }