Commit 087635a0 by 张立顺

fix 修复子表单默认值问题

1 parent 872db3c2
# DEVLOG — packageDynamicForm
## 2026-06-04 — 保存后子表单增行 numberInput 零值丢失
- **根因**:`sanitizeOriginChildrenColumn` 对 `origin_children` 调用 `clearRowFieldForAdd`,保存/回显 jsonContent 时剥离 numberInput 模板 `value: 0`。
- **修复**:`sanitizeOriginChildrenField` 仅清 select 双 key 与选值,保留 numberInput 等模板 value;`mergeAppJsonIntoSchema` 用 `mergeOriginChildrenWithSchema` 从 schema 模板补回历史数据缺失的默认值。
- **单测**:`IT-SUBFORM-SAVE-001`~`003`。
## 2026-06-04 — 模板 numberInput `value: 0` 展示补全
- **缺口 A**:`valueMapToRowFieldsFromOrigin` 在 valueMap 缺 key 时回退 `origin_children` 列模板 `field.value`(含 `0`)。
......
......@@ -216,8 +216,51 @@ function clearRowFieldForAdd(field = {}) {
return next
}
function isEmptyTemplateValue(value) {
return value === undefined || value === null || value === ''
}
/** origin_children 列模板:清 select 双 key/选值,保留 numberInput 等模板 value(含 0) */
function sanitizeOriginChildrenField(field = {}) {
const next = { ...field }
const key = next.key
if (key != null && Object.prototype.hasOwnProperty.call(next, key)) {
delete next[key]
}
if (next.type === 'select' || next.type === 'multipleSelect') {
delete next.value
}
return next
}
function sanitizeOriginChildrenColumn(list = []) {
return cloneJson(list || []).map(clearRowFieldForAdd)
return cloneJson(list || []).map(sanitizeOriginChildrenField)
}
/** 回显时合并 saved origin_children 与 schema 模板,补回被历史保存剥离的模板默认值 */
function mergeOriginChildrenWithSchema(schemaOrigin = [], savedOrigin = []) {
const schemaList = cloneJson(schemaOrigin || [])
const savedList = cloneJson(savedOrigin || [])
if (!schemaList.length) {
return sanitizeOriginChildrenColumn(savedList)
}
if (!savedList.length) {
return sanitizeOriginChildrenColumn(schemaList)
}
const savedByKey = new Map(savedList.filter(f => f?.key).map(f => [f.key, f]))
return sanitizeOriginChildrenColumn(
schemaList.map(schemaField => {
const saved = savedByKey.get(schemaField.key)
if (!saved) {
return schemaField
}
const merged = { ...schemaField, ...saved }
if (schemaField.value !== undefined && isEmptyTemplateValue(merged.value)) {
merged.value = schemaField.value
}
return merged
})
)
}
function getStaticGroupChildren(field = {}) {
......@@ -1096,7 +1139,10 @@ export function mergeAppJsonIntoSchema(schemaRoot = {}, detail = {}) {
const rowFieldsList = getSectionRowFieldsList(section)
if (Array.isArray(section.origin_children) && section.origin_children.length) {
nextField.origin_children = sanitizeOriginChildrenColumn(section.origin_children)
nextField.origin_children = mergeOriginChildrenWithSchema(
field.origin_children || [],
section.origin_children
)
if (!getRowTemplate(field).length) {
nextField.children = [cloneJson(nextField.origin_children)]
}
......
......@@ -8,6 +8,10 @@ import {
} from '../form-engine/group-row-adapter.js'
import { buildGroupRowsCache } from '../form-engine/group-rows-cache.js'
import { getSubformRowTemplateForAdd } from '../form-engine/field-types.js'
import {
buildAppJsonContent,
mergeAppJsonIntoSchema
} from '../form-engine/app-subform-json.js'
const originChildren = [
{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
......@@ -201,3 +205,73 @@ describe('subform add row template (origin_children)', () => {
assert.equal(cacheNum.value, 0)
})
})
describe('subform origin_children jsonContent roundtrip', () => {
const originWithZero = [
{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 0 }
]
const schemaRoot = {
list: [
{
key: 'F2-9',
type: 'subform',
origin_children: originWithZero,
children: [originWithZero]
}
]
}
it('IT-SUBFORM-SAVE-001: buildAppJsonContent preserves numberInput value 0 in origin_children', () => {
const row = [
{ key: 'F2-9-1', type: 'select', optionsKey: '4sStaffList', value: 'A', 'F2-9-1': 'u-1' },
{ key: 'F2-9-2', type: 'numberInput', value: 5 }
]
const jsonList = buildAppJsonContent(schemaRoot.list, { 'F2-9': [row] })
const section = jsonList.find(f => f.key === 'F2-9')
const num = section.origin_children.find(f => f.key === 'F2-9-2')
assert.equal(num.value, 0)
})
it('IT-SUBFORM-SAVE-002: save merge roundtrip keeps value 0 for add row', () => {
const row = [
{ key: 'F2-9-1', type: 'select', optionsKey: '4sStaffList', value: 'A', 'F2-9-1': 'u-1' },
{ key: 'F2-9-2', type: 'numberInput', value: 5 }
]
const jsonList = buildAppJsonContent(schemaRoot.list, { 'F2-9': [row] })
const merged = mergeAppJsonIntoSchema(schemaRoot, {
jsonContent: JSON.stringify(jsonList)
})
const subform = merged.list.find(f => f.key === 'F2-9')
const originNum = subform.origin_children.find(f => f.key === 'F2-9-2')
assert.equal(originNum.value, 0)
const addRow = cloneRowTemplateForAdd(subform)
assert.equal(addRow.find(f => f.key === 'F2-9-2').value, 0)
})
it('IT-SUBFORM-SAVE-003: legacy jsonContent without value restores from schema template', () => {
const legacyJson = JSON.stringify([
{
key: 'F2-9',
type: 'subform',
origin_children: [
{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
],
children: [
[
{ key: 'F2-9-1', type: 'select', value: 'A', 'F2-9-1': 'u-1' },
{ key: 'F2-9-2', type: 'numberInput', value: 3 }
]
]
}
])
const merged = mergeAppJsonIntoSchema(schemaRoot, { jsonContent: legacyJson })
const subform = merged.list.find(f => f.key === 'F2-9')
const originNum = subform.origin_children.find(f => f.key === 'F2-9-2')
assert.equal(originNum.value, 0)
const addRow = cloneRowTemplateForAdd(subform)
assert.equal(addRow.find(f => f.key === 'F2-9-2').value, 0)
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!