diff --git a/src/pages/ModelerFields.test.tsx b/src/pages/ModelerFields.test.tsx new file mode 100644 index 0000000..82436a5 --- /dev/null +++ b/src/pages/ModelerFields.test.tsx @@ -0,0 +1,66 @@ +import { describe, expect, it, vi, beforeEach } from 'vitest'; +import { render, screen, fireEvent } from '@testing-library/react'; +import { useState } from 'react'; +import { buildAttribute, FieldTable, type FieldRow } from './ModelerFields'; + +vi.mock('../api/meta', () => ({ listEntities: vi.fn(), listEntityColumns: vi.fn() })); + +describe('buildAttribute 映射', () => { + it('文本→string,带 length/display/mandatory', () => { + const r: FieldRow = { column: 'TITLE', display: '标题', type: '文本', length: 200, mandatory: true }; + expect(buildAttribute(r)).toEqual({ column: 'TITLE', type: 'string', length: 200, display: '标题', mandatory: true }); + }); + it('整数→integer,无 length', () => { + const r: FieldRow = { column: 'QTY', display: '数量', type: '整数' }; + expect(buildAttribute(r)).toEqual({ column: 'QTY', type: 'integer', display: '数量' }); + }); + it('小数→number / 日期→date / 日期时间→datetime / 布尔→boolean', () => { + expect(buildAttribute({ column: 'A', display: 'a', type: '小数' }).type).toBe('number'); + expect(buildAttribute({ column: 'B', display: 'b', type: '日期' }).type).toBe('date'); + expect(buildAttribute({ column: 'C', display: 'c', type: '日期时间' }).type).toBe('datetime'); + expect(buildAttribute({ column: 'D', display: 'd', type: '布尔' }).type).toBe('boolean'); + }); + it('引用→reference,带 referenceTable', () => { + const r: FieldRow = { column: 'DEPARTID', display: '部门', type: '引用', referenceTable: 'SYS_DEPART' }; + expect(buildAttribute(r)).toEqual({ column: 'DEPARTID', type: 'reference', display: '部门', referenceTable: 'SYS_DEPART' }); + }); + it('mandatory=false 不输出该键', () => { + const r: FieldRow = { column: 'X', display: 'x', type: '文本', length: 50, mandatory: false }; + expect('mandatory' in buildAttribute(r)).toBe(false); + }); +}); + +function Harness() { + const [rows, setRows] = useState([]); + return ( +
+ +
{rows.length}
+
+ ); +} + +describe('FieldTable 交互', () => { + beforeEach(() => { /* no-op */ }); + + it('加字段:填列名/显示名/选类型→提交→出现在表里', () => { + render(); + fireEvent.click(screen.getByTestId('field-add')); + fireEvent.change(screen.getByTestId('field-column'), { target: { value: 'TITLE' } }); + fireEvent.change(screen.getByTestId('field-display'), { target: { value: '标题' } }); + fireEvent.change(screen.getByTestId('field-type'), { target: { value: '文本' } }); + fireEvent.change(screen.getByTestId('field-length'), { target: { value: '200' } }); + fireEvent.click(screen.getByTestId('field-submit')); + expect(screen.getByText('TITLE')).toBeInTheDocument(); + expect(screen.getByTestId('count').textContent).toBe('1'); + }); + + it('文本类型显示 length 输入,整数类型隐藏', () => { + render(); + fireEvent.click(screen.getByTestId('field-add')); + fireEvent.change(screen.getByTestId('field-type'), { target: { value: '文本' } }); + expect(screen.getByTestId('field-length')).toBeInTheDocument(); + fireEvent.change(screen.getByTestId('field-type'), { target: { value: '整数' } }); + expect(screen.queryByTestId('field-length')).not.toBeInTheDocument(); + }); +}); diff --git a/src/pages/ModelerFields.tsx b/src/pages/ModelerFields.tsx index 3cd7219..3d87118 100644 --- a/src/pages/ModelerFields.tsx +++ b/src/pages/ModelerFields.tsx @@ -1,6 +1,10 @@ -// 占位(Task 16 用 TDD 实现字段表 + buildAttribute + 引用列选择器)。 +// 字段表 + 加字段 Dialog + UI 类型↔api 类型映射。 +// 纯 HTML (本仓无可复用表格组件)+ 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 = { + 文本: '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> }) { - return null; +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} + + ); } diff --git a/src/pages/ReferencePicker.tsx b/src/pages/ReferencePicker.tsx new file mode 100644 index 0000000..89a9cd8 --- /dev/null +++ b/src/pages/ReferencePicker.tsx @@ -0,0 +1,11 @@ +// 占位(Task 17 实现引用列选择器)。 +export function ReferencePicker(props: { value: string; onChange: (t: string) => void }) { + return ( + props.onChange(e.target.value)} + placeholder="目标表(Task 17 改为下拉)" + /> + ); +}