From a875437d77f470e4c71d47cf2202391bcc78c8b0 Mon Sep 17 00:00:00 2001 From: ssw Date: Wed, 24 Jun 2026 14:36:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(field-builder):=20BS-54=20select/relationF?= =?UTF-8?q?orm=20=E9=BB=98=E8=AE=A4=20+=20=E6=8A=BD=20field-defaults.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TDD 红绿重构: - 红: tests/unit/field-builder-bs54.test.ts 6 FAIL - 绿: FieldBuilder.build() select/relationForm 自动补默认字段 - select: dataType='static', dictionaryType='', propsUrl='', options=[2项占位], props={label, value} - relationForm: modelId='' 占位 (用户必填) - 重构: 抽通用占位 → src/schema/field-defaults.ts (避免应用特定信息入 dacli/src, jxcj 36 套模板/vmodel→enCode 仍留在 docs/jxcj/scripts/) 字段形状来源: web componentMap.ts:324-376 (select) + 1363-1406 (relationForm) Co-Authored-By: Claude --- src/schema/FieldBuilder.ts | 20 +++++++++ src/schema/field-defaults.ts | 30 +++++++++++++ tests/unit/field-builder-bs54.test.ts | 64 +++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/schema/field-defaults.ts create mode 100644 tests/unit/field-builder-bs54.test.ts diff --git a/src/schema/FieldBuilder.ts b/src/schema/FieldBuilder.ts index 2951c56..4b822ee 100644 --- a/src/schema/FieldBuilder.ts +++ b/src/schema/FieldBuilder.ts @@ -20,6 +20,7 @@ import { readFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' +import { DEFAULT_SELECT_OPTIONS, DEFAULT_SELECT_PROPS, PLACEHOLDER_MODEL_ID } from './field-defaults.ts' // ----- JSON 数据类型 ----- @@ -238,6 +239,25 @@ export class FieldBuilder { if (jnpfKey === 'cascader') defaults.defaultValue = [] if (jnpfKey === 'rate' || jnpfKey === 'slider') defaults.defaultValue = 0 + // BS-54 (2026-06-24): select/relationForm 默认字段 + // 根因: 用户不传 dataType/options/modelId 时, 浏览器层 select 下拉框空白, + // relationForm 弹窗直接 return (RelationForm.vue:201 if (!props.modelId) return). + // 修复: FieldBuilder 加通用占位默认值, 业务特定语义 (36 套模板/vmodel→enCode 映射) 由应用脚本补. + // 字段形状来源: web componentMap.ts:324-376 (select) + 1363-1406 (relationForm). + if (jnpfKey === 'select') { + finalConfig.dataType = 'static' + finalConfig.dictionaryType = '' + finalConfig.propsUrl = '' + if (!('options' in props) || !Array.isArray(props.options)) { + defaults.options = DEFAULT_SELECT_OPTIONS + } + defaults.props = DEFAULT_SELECT_PROPS + } + if (jnpfKey === 'relationForm') { + // modelId 默认空字符串, 用户必须显式填目标 form 标识 (enCode 或数字 id) + finalConfig.modelId = userConfig.modelId ?? PLACEHOLDER_MODEL_ID + } + const { __config__: _drop, ...restUser } = props return { ...defaults, diff --git a/src/schema/field-defaults.ts b/src/schema/field-defaults.ts new file mode 100644 index 0000000..f1b1b2a --- /dev/null +++ b/src/schema/field-defaults.ts @@ -0,0 +1,30 @@ +/** + * schema/field-defaults.ts — 通用字段默认值 (BS-54, 2026-06-24) + * + * 设计: 仅放通用占位, 不放应用特定语义. + * - 应用特定 (jxcj 36 套 select 模板 + vmodel→enCode 映射) → docs//scripts/ + * - 通用占位 (空 modelId / 2 项 select) → dacli/src (本文件) + * + * 数据来源: web componentMap.ts:324-376 (select) + 1363-1406 (relationForm) + * - select 默认 'static' + 2 项占位 (选项一/选项二) + * - relationForm 默认空 modelId (用户必填) + */ +export interface SelectOption { + fullName: string + id: string +} + +/** select 字段默认 options (2 项占位, 用户必改) */ +export const DEFAULT_SELECT_OPTIONS: SelectOption[] = [ + { fullName: '选项一', id: '1' }, + { fullName: '选项二', id: '2' }, +] + +/** select 字段默认 props.label / props.value 映射 */ +export const DEFAULT_SELECT_PROPS = { + label: 'fullName', + value: 'id', +} as const + +/** relationForm 字段默认 modelId (空字符串, 用户必填目标 form enCode/id) */ +export const PLACEHOLDER_MODEL_ID = '' \ No newline at end of file diff --git a/tests/unit/field-builder-bs54.test.ts b/tests/unit/field-builder-bs54.test.ts new file mode 100644 index 0000000..2ecaa40 --- /dev/null +++ b/tests/unit/field-builder-bs54.test.ts @@ -0,0 +1,64 @@ +/** + * tests/unit/field-builder-bs54.test.ts — BS-54 闭环 unit 测试 (v0.10+) + * + * 根因: jxcj v1.0 浏览器层 select 下拉框空白 (无 options) + + * relationForm 弹窗不显示 (无 modelId). FieldBuilder.build() + * 没补 dataType/options/modelId 默认值, 需用户手工写. + * + * 修复: FieldBuilder.build() 对 select/relationForm 字段自动补默认字段 + * (dataType='static' + inline options; modelId='' 占位) + * + * 测试: + * 1. select 字段 build() 后 __config__.dataType 必须存在 (默认 'static') + * 2. select 字段 build() 后 options 数组必须有 ≥1 项 (默认 2 项占位) + * 3. select 字段 build() 后 props.label/value 映射必须存在 (默认 fullName/id) + * 4. relationForm 字段 build() 后 __config__.modelId key 必须存在 (空字符串占位) + * 5. 用户传 --modelId 时, 该值透传 (不被默认覆盖) + */ +import { describe, it, expect } from 'vitest' +import { FieldBuilder } from '../../src/schema/FieldBuilder.ts' + +describe('BS-54 FieldBuilder.build() select/relationForm 默认字段', () => { + const fb = new FieldBuilder() + + it('select 字段: __config__.dataType 默认 = "static"', () => { + const f = fb.build('select', { label: '状态下拉框' }) + expect(f.__config__.dataType).toBe('static') + }) + + it('select 字段: 不传 options 时, 自动补 ≥1 项 inline options', () => { + const f = fb.build('select', { label: '类型' }) + const opts = f.options as Array<{ id: string; fullName: string }> | undefined + expect(opts).toBeDefined() + expect(Array.isArray(opts)).toBe(true) + expect(opts!.length).toBeGreaterThan(0) + }) + + it('select 字段: 自动补 props.label="fullName", props.value="id"', () => { + const f = fb.build('select', { label: '类型' }) + const props = f.props as { label: string; value: string } | undefined + expect(props).toBeDefined() + expect(props!.label).toBe('fullName') + expect(props!.value).toBe('id') + }) + + it('select 字段: 用户传的 options 优先, 不被默认覆盖', () => { + const custom = [{ fullName: 'A', id: 'a' }, { fullName: 'B', id: 'b' }, { fullName: 'C', id: 'c' }] + const f = fb.build('select', { label: '类型', options: custom }) + expect(f.options).toEqual(custom) + }) + + it('relationForm 字段: __config__.modelId key 必须存在 (默认空字符串)', () => { + const f = fb.build('relationForm', { label: '关联大赛' }) + expect('modelId' in f.__config__).toBe(true) + expect(f.__config__.modelId).toBe('') + }) + + it('relationForm 字段: 用户传 modelId 时, 透传', () => { + const f = fb.build('relationForm', { + label: '关联大赛', + __config__: { modelId: 'jxcj_competition_info' }, + }) + expect(f.__config__.modelId).toBe('jxcj_competition_info') + }) +}) \ No newline at end of file