field-submit-value.js 8.23 KB
/**
 * fieldDataList / subformDataList 提交与回显值转换
 * @see packageDynamicForm/docs/field-options-api.md
 */
import { coerceSelectValue } from './select-value.js'
import { coerceMultipleSelectValue } from './multiple-select-value.js'
import {
	normalizeCarBrandValue,
	carInfoToCascadeLevels,
	cascadeLevelsToCarInfo
} from './car-brand-value.js'

function isPlainObject(value) {
	return value != null && typeof value === 'object' && !Array.isArray(value)
}

function resolveOptionId(option = {}) {
	return option.id ?? option.orderCode ?? option.value ?? ''
}

function resolveOptionName(option = {}) {
	return option.name ?? option.orderCode ?? option.label ?? option.productName ?? ''
}

/** 将 picker option 归一化为 { id, name, ...extras } */
export function normalizeOptionRecord(option = {}) {
	if (!option || typeof option !== 'object') {
		return null
	}
	const id = resolveOptionId(option)
	const name = resolveOptionName(option)
	if (id === '' && name === '') {
		return null
	}
	const next = { id: String(id), name: String(name) }
	Object.keys(option).forEach(key => {
		if (key === 'label' || key === 'value') {
			return
		}
		if (option[key] != null && option[key] !== '') {
			next[key] = option[key]
		}
	})
	return next
}

function findStaticOption(field = {}, rawId) {
	const str = String(rawId)
	return (field.options || []).find(
		option =>
			String(option.value) === str ||
			String(option.id) === str ||
			String(option.label) === str ||
			String(option.name) === str
	)
}

function scalarToSelectObject(field = {}, rawValue) {
	const scalar = coerceSelectValue(rawValue)
	if (scalar === '' || scalar == null) {
		return undefined
	}
	const matched = findStaticOption(field, scalar)
	return {
		id: String(matched?.id ?? matched?.value ?? scalar),
		name: matched?.label ?? matched?.name ?? matched?.value ?? String(scalar)
	}
}

/** 从 subform 行 FormField 提取 fieldDataList 用的 select 完整对象 */
export function extractSelectStorageValueFromRowField(field = {}) {
	if (field.type !== 'select') {
		return undefined
	}
	const key = field.key
	const meta = key != null ? field[key] : undefined

	if (field.optionsKey) {
		if (isPlainObject(meta)) {
			return normalizeOptionRecord(meta) || undefined
		}
		const v = field.value
		if (isPlainObject(v)) {
			return normalizeOptionRecord(v) || undefined
		}
		const scalar = meta != null && meta !== '' ? meta : v
		if (scalar === undefined || scalar === null || scalar === '') {
			return undefined
		}
		const obj = scalarToSelectObject(field, scalar)
		if (
			obj &&
			typeof v === 'string' &&
			v !== '' &&
			String(v) !== String(scalar) &&
			(!obj.name || obj.name === String(scalar))
		) {
			obj.name = v
		}
		return obj
	}

	const v = field.value
	if (v === undefined || v === null || v === '') {
		return undefined
	}
	if (isPlainObject(v)) {
		return v
	}
	return scalarToSelectObject(field, v)
}

/**
 * subform 校验取值:optionsKey select 优先 field[key] / 行 map(避免 value 为空串时误报必填)
 * @param {Object} [rowField]
 * @param {string} [childKey]
 * @param {*} [rowMapValue]
 * @returns {*}
 */
export function resolveSelectValueForValidation(rowField = {}, childKey, rowMapValue) {
	if (rowField?.type !== 'select' || !rowField?.optionsKey) {
		return rowField?.value !== undefined ? rowField.value : rowMapValue
	}
	const key = childKey ?? rowField.key
	const meta = key != null ? rowField[key] : undefined
	if (isPlainObject(meta)) {
		return meta
	}
	if (meta != null && meta !== '') {
		return meta
	}
	if (isPlainObject(rowMapValue)) {
		return rowMapValue
	}
	if (rowMapValue != null && rowMapValue !== '') {
		return rowMapValue
	}
	const v = rowField.value
	if (v !== undefined && v !== null && v !== '') {
		return v
	}
	return rowMapValue
}

/** select 提交值 → { id, name, ... } */
export function buildSelectSubmitValue(field = {}, rawValue) {
	if (rawValue == null || rawValue === '') {
		return undefined
	}
	if (isPlainObject(rawValue)) {
		const normalized = normalizeOptionRecord(rawValue)
		return normalized || undefined
	}
	return scalarToSelectObject(field, rawValue)
}

function itemToSelectObject(field = {}, item) {
	if (isPlainObject(item)) {
		const normalized = normalizeOptionRecord(item)
		if (!normalized) return null
		if (field.optionsKey && field.optionsKey !== 'options') return normalized
		const matched = findStaticOption(field, normalized.name) ||
			findStaticOption(field, normalized.id)
		if (!matched) return normalized
		return {
			...normalized,
			id: String(matched.id ?? matched.value ?? normalized.id),
			name: String(matched.label ?? matched.name ?? matched.value ?? normalized.name)
		}
	}
	return scalarToSelectObject(field, item)
}

/** multipleSelect 提交值 → [{ id, name, ... }, ...] */
export function buildMultipleSelectSubmitValue(field = {}, rawValue) {
	const coerced = coerceMultipleSelectValue(rawValue)
	if (coerced == null || coerced === '') {
		return undefined
	}
	if (!Array.isArray(coerced)) {
		return undefined
	}
	const items = coerced
		.map(item => itemToSelectObject(field, item))
		.filter(Boolean)
	return items.length ? items : undefined
}

/** carBrandSelect 提交值 → [{ id, name }×4] */
export function buildCarBrandSubmitValue(rawValue) {
	const normalized = normalizeCarBrandValue(rawValue)
	if (!normalized) {
		return undefined
	}
	const levels = carInfoToCascadeLevels(normalized)
	return levels.length ? levels : undefined
}

/** fieldDataList 回显:select → 引擎值(对象或标量) */
export function parseSelectEchoValue(rawValue, field = {}) {
	if (rawValue == null || rawValue === '') {
		return undefined
	}
	if (isPlainObject(rawValue)) {
		if (field.optionsKey) {
			return normalizeOptionRecord(rawValue) || rawValue
		}
		return rawValue.id ?? rawValue.value ?? rawValue
	}
	return rawValue
}

/** fieldDataList 回显:multipleSelect → 引擎值 */
export function parseMultipleSelectEchoValue(rawValue, field = {}) {
	if (rawValue == null || rawValue === '') {
		return undefined
	}
	const coerced = coerceMultipleSelectValue(rawValue)
	if (!Array.isArray(coerced)) {
		return coerced
	}
	if (field.optionsKey) {
		return coerced.map(item => (isPlainObject(item) ? normalizeOptionRecord(item) || item : item))
	}
	return coerced.map(item =>
		isPlainObject(item) ? item.id ?? item.value ?? item : item
	)
}

/** fieldDataList 回显:carBrandSelect → flat carInfo */
export function parseCarBrandEchoValue(rawValue) {
	if (rawValue == null || rawValue === '') {
		return undefined
	}
	if (Array.isArray(rawValue)) {
		return cascadeLevelsToCarInfo(rawValue)
	}
	if (typeof rawValue === 'string') {
		try {
			const parsed = JSON.parse(rawValue)
			if (Array.isArray(parsed)) {
				return cascadeLevelsToCarInfo(parsed)
			}
			return normalizeCarBrandValue(parsed)
		} catch (error) {
			return normalizeCarBrandValue(rawValue)
		}
	}
	return normalizeCarBrandValue(rawValue)
}

/** 按 schema 字段类型将 fieldDataList 条目转为引擎值 */
export function parseFieldDataEchoValue(field = {}, coercedValue) {
	if (coercedValue === undefined) {
		return undefined
	}
	if (field.type === 'select') {
		return parseSelectEchoValue(coercedValue, field)
	}
	if (field.type === 'multipleSelect') {
		return parseMultipleSelectEchoValue(coercedValue, field)
	}
	if (field.type === 'carBrandSelect') {
		return parseCarBrandEchoValue(coercedValue)
	}
	return coercedValue
}

export default {
	normalizeOptionRecord,
	extractSelectStorageValueFromRowField,
	resolveSelectValueForValidation,
	buildSelectSubmitValue,
	buildMultipleSelectSubmitValue,
	buildCarBrandSubmitValue,
	parseSelectEchoValue,
	parseMultipleSelectEchoValue,
	parseCarBrandEchoValue,
	parseFieldDataEchoValue
}

if (typeof module !== 'undefined' && module.exports) {
	module.exports = {
		normalizeOptionRecord,
		extractSelectStorageValueFromRowField,
		resolveSelectValueForValidation,
		buildSelectSubmitValue,
		buildMultipleSelectSubmitValue,
		buildCarBrandSubmitValue,
		parseSelectEchoValue,
		parseMultipleSelectEchoValue,
		parseCarBrandEchoValue,
		parseFieldDataEchoValue,
		default: {
			normalizeOptionRecord,
			extractSelectStorageValueFromRowField,
			buildSelectSubmitValue,
			buildMultipleSelectSubmitValue,
			buildCarBrandSubmitValue,
			parseSelectEchoValue,
			parseMultipleSelectEchoValue,
			parseCarBrandEchoValue,
			parseFieldDataEchoValue
		}
	}
}