feat(modeler): field table + add-field dialog + ui-type to api-type mapping (buildAttribute)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-20 09:38:14 +08:00
parent 915443f585
commit fef5e33a7a
3 changed files with 227 additions and 6 deletions

View File

@@ -1,6 +1,10 @@
// 占位(Task 16 用 TDD 实现字段表 + buildAttribute + 引用列选择器)。
// 字段表 + 加字段 Dialog + UI 类型↔api 类型映射。
// 纯 HTML <table>(本仓无可复用表格组件)+ components/ui/Dialog。
import { useState } from 'react';
import type { Dispatch, SetStateAction } from 'react';
import type { EntityAttr } from '../api/meta';
import { Dialog } from '../components/ui/Dialog';
import type { EntityAttr, AttrType } from '../api/meta';
import { ReferencePicker } from './ReferencePicker';
export type UiType = '文本' | '整数' | '小数' | '日期' | '日期时间' | '布尔' | '引用';
@@ -13,10 +17,150 @@ export interface FieldRow {
referenceTable?: string;
}
export function buildAttribute(_row: FieldRow): EntityAttr {
throw new Error('not implemented (Task 16)');
const UI_TYPES: UiType[] = ['文本', '整数', '小数', '日期', '日期时间', '布尔', '引用'];
const TYPE_MAP: Record<UiType, AttrType> = {
文本: '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;
}
export function FieldTable(_props: { rows: FieldRow[]; onChange: Dispatch<SetStateAction<FieldRow[]>> }) {
return null;
interface FieldTableProps {
rows: FieldRow[];
onChange: Dispatch<SetStateAction<FieldRow[]>>;
}
export function FieldTable({ rows, onChange }: FieldTableProps) {
const [adding, setAdding] = useState(false);
// 加字段草稿
const [column, setColumn] = useState('');
const [display, setDisplay] = useState('');
const [type, setType] = useState<UiType>('文本');
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 (
<div>
<div style={{ display: 'flex', alignItems: 'center', marginBottom: 6 }}>
<span className="sub-text" style={{ flex: 1 }}>字段(标准列由后端自动追加,无需在此添加 ID/创建人等)</span>
<button className="btn btn-sm" type="button" data-testid="field-add" onClick={openAdd}>加字段</button>
</div>
<table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 'var(--fs)' }}>
<thead>
<tr style={{ textAlign: 'left', color: 'var(--text-3)' }}>
<th style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>列名</th>
<th style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>显示名</th>
<th style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>类型</th>
<th style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>必填</th>
<th style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }} />
</tr>
</thead>
<tbody>
{rows.length === 0 ? (
<tr><td colSpan={5} style={{ padding: '10px', color: 'var(--text-3)' }}>尚无字段</td></tr>
) : (
rows.map((r, i) => (
<tr key={i} data-testid="field-row">
<td style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>{r.column}</td>
<td style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>{r.display}</td>
<td style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>
{r.type}{r.type === '引用' && r.referenceTable ? `→${r.referenceTable}` : ''}
</td>
<td style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>{r.mandatory ? '是' : ''}</td>
<td style={{ padding: '6px 10px', borderBottom: '1px solid var(--line)' }}>
<span className="link" style={{ fontSize: 11 }} onClick={() => removeRow(i)}>删除</span>
</td>
</tr>
))
)}
</tbody>
</table>
{adding ? (
<Dialog
title="加字段"
width={520}
onClose={() => setAdding(false)}
foot={
<>
<button className="btn" type="button" onClick={() => setAdding(false)}>取消</button>
<button className="btn btn-primary" type="button" data-testid="field-submit" disabled={!canAdd} onClick={submitField}>添加</button>
</>
}
>
<div style={{ display: 'grid', gap: 10 }}>
<label style={{ display: 'grid', gap: 4 }}>
<span className="sub-text">列名(大写字母开头)</span>
<input data-testid="field-column" value={column} onChange={(e) => setColumn(e.target.value.toUpperCase())} placeholder="TITLE" />
</label>
<label style={{ display: 'grid', gap: 4 }}>
<span className="sub-text">显示名</span>
<input data-testid="field-display" value={display} onChange={(e) => setDisplay(e.target.value)} placeholder="标题" />
</label>
<label style={{ display: 'grid', gap: 4 }}>
<span className="sub-text">类型</span>
<select data-testid="field-type" value={type} onChange={(e) => setType(e.target.value as UiType)}>
{UI_TYPES.map((t) => <option key={t} value={t}>{t}</option>)}
</select>
</label>
{type === '文本' ? (
<label style={{ display: 'grid', gap: 4 }}>
<span className="sub-text">长度</span>
<input data-testid="field-length" type="number" value={length} onChange={(e) => setLength(e.target.value)} />
</label>
) : null}
{type === '引用' ? (
<ReferencePicker value={refTable} onChange={setRefTable} />
) : null}
<label style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<input data-testid="field-mandatory" type="checkbox" checked={mandatory} onChange={(e) => setMandatory(e.target.checked)} />
<span className="sub-text">必填</span>
</label>
</div>
</Dialog>
) : null}
</div>
);
}