|
|
|
|
@ -13,6 +13,10 @@ export interface FormSchemaEditorItem {
|
|
|
|
|
help?: string
|
|
|
|
|
/** select 等:{ label, value } */
|
|
|
|
|
options?: { label: string; value: string }[]
|
|
|
|
|
/** 报名表:条件必填依赖字段 key(入库为 required_when.field) */
|
|
|
|
|
requiredWhenField?: string
|
|
|
|
|
/** 报名表:依赖取值,每行一个(入库为 required_when.values) */
|
|
|
|
|
requiredWhenValuesLines?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 入库时为 checkbox + key commitment_accepted,仅供报名表可视化 */
|
|
|
|
|
@ -55,6 +59,8 @@ export function createEmptySchemaItem(
|
|
|
|
|
placeholder: '',
|
|
|
|
|
help: '',
|
|
|
|
|
options: [],
|
|
|
|
|
requiredWhenField: '',
|
|
|
|
|
requiredWhenValuesLines: '',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -85,6 +91,19 @@ export function schemaJsonToEditorItems(json: unknown, purpose: FormSchemaPurpos
|
|
|
|
|
purpose === 'signup' && rawKey === 'commitment_accepted' && rawType === 'checkbox'
|
|
|
|
|
? SIGNUP_COMMITMENT_TYPE
|
|
|
|
|
: rawType
|
|
|
|
|
let requiredWhenField = ''
|
|
|
|
|
let requiredWhenValuesLines = ''
|
|
|
|
|
if (purpose === 'signup') {
|
|
|
|
|
const rw = o.required_when
|
|
|
|
|
if (rw != null && typeof rw === 'object' && !Array.isArray(rw)) {
|
|
|
|
|
const rwo = rw as Record<string, unknown>
|
|
|
|
|
requiredWhenField = String(rwo.field ?? '').trim()
|
|
|
|
|
const vals = rwo.values
|
|
|
|
|
if (Array.isArray(vals)) {
|
|
|
|
|
requiredWhenValuesLines = vals.map((x) => String(x).trim()).filter(Boolean).join('\n')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
__uid: newUid(),
|
|
|
|
|
key: rawKey,
|
|
|
|
|
@ -94,12 +113,14 @@ export function schemaJsonToEditorItems(json: unknown, purpose: FormSchemaPurpos
|
|
|
|
|
placeholder: o.placeholder != null ? String(o.placeholder) : '',
|
|
|
|
|
help: o.help != null ? String(o.help) : '',
|
|
|
|
|
options: type === 'select' ? normalizeOptions(o.options) : [],
|
|
|
|
|
requiredWhenField,
|
|
|
|
|
requiredWhenValuesLines,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 写入接口的 schema_json(数组) */
|
|
|
|
|
export function editorItemsToSchemaJson(items: FormSchemaEditorItem[]): unknown[] {
|
|
|
|
|
export function editorItemsToSchemaJson(items: FormSchemaEditorItem[], purpose: FormSchemaPurpose): unknown[] {
|
|
|
|
|
return items.map((item) => {
|
|
|
|
|
const isCommitment = item.type === SIGNUP_COMMITMENT_TYPE
|
|
|
|
|
const row: Record<string, unknown> = {
|
|
|
|
|
@ -113,11 +134,25 @@ export function editorItemsToSchemaJson(items: FormSchemaEditorItem[]): unknown[
|
|
|
|
|
if (item.type === 'select' && item.options?.length) {
|
|
|
|
|
row.options = item.options.filter((o) => o.value !== '' || o.label !== '')
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
purpose === 'signup'
|
|
|
|
|
&& !isCommitment
|
|
|
|
|
&& item.requiredWhenField?.trim()
|
|
|
|
|
&& item.requiredWhenValuesLines?.trim()
|
|
|
|
|
) {
|
|
|
|
|
const values = item.requiredWhenValuesLines
|
|
|
|
|
.split('\n')
|
|
|
|
|
.map((s) => s.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
if (values.length) {
|
|
|
|
|
row.required_when = { field: item.requiredWhenField.trim(), values }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return row
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function validateEditorItems(items: FormSchemaEditorItem[]): string | null {
|
|
|
|
|
export function validateEditorItems(items: FormSchemaEditorItem[], purpose: FormSchemaPurpose): string | null {
|
|
|
|
|
const keys = new Set<string>()
|
|
|
|
|
for (const it of items) {
|
|
|
|
|
const k = it.key.trim()
|
|
|
|
|
@ -125,6 +160,14 @@ export function validateEditorItems(items: FormSchemaEditorItem[]): string | nul
|
|
|
|
|
if (keys.has(k)) return `字段 key「${k}」重复`
|
|
|
|
|
keys.add(k)
|
|
|
|
|
if (!it.label.trim()) return `字段「${k}」缺少显示标签`
|
|
|
|
|
if (
|
|
|
|
|
purpose === 'signup'
|
|
|
|
|
&& it.type !== SIGNUP_COMMITMENT_TYPE
|
|
|
|
|
&& it.requiredWhenField?.trim()
|
|
|
|
|
&& !it.requiredWhenValuesLines?.trim()
|
|
|
|
|
) {
|
|
|
|
|
return `字段「${k}」填写了条件必填依赖,但未填写「当取值」列表`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|