multiple-select-value.js 3.77 KB
/** multipleSelect 字段值归一化 — @see packageDynamicForm/docs/ARCHITECTURE.md */
import { flattenFields } from './visibility.js'
import { listSubforms, getSubformRowTemplate } from './field-types.js'
import { getGroupRows } from './group-values.js'

const REMOTE_MULTIPLE_SELECT_OPTIONS_KEYS = new Set([
	'4sStaffList',
	'storeStaffList',
	'orderList'
])

function normalizeMultipleSelectItem(item) {
	if (item == null || item === '') {
		return null
	}
	if (typeof item === 'object') {
		return item
	}
	return String(item)
}

/** 将 multipleSelect 原始值强制为数组(支持 JSON 字符串、对象数组、单值) */
export function coerceMultipleSelectValue(rawValue) {
	if (rawValue == null || rawValue === '') {
		return rawValue
	}
	if (Array.isArray(rawValue)) {
		return rawValue.map(normalizeMultipleSelectItem).filter(Boolean)
	}
	if (typeof rawValue === 'string') {
		const trimmed = rawValue.trim()
		if (trimmed.startsWith('[')) {
			try {
				const parsed = JSON.parse(trimmed)
				if (Array.isArray(parsed)) {
					return parsed.map(normalizeMultipleSelectItem).filter(Boolean)
				}
			} catch (error) {
				// fall through to single value
			}
		}
		return trimmed ? [trimmed] : []
	}
	return [String(rawValue)]
}

function isRemoteMultipleSelectField(field = {}) {
	return REMOTE_MULTIPLE_SELECT_OPTIONS_KEYS.has(field.optionsKey)
}

function getAliases(value) {
	if (value == null || value === '') return []
	if (typeof value !== 'object') return [String(value)]
	return [...new Set([value.value, value.id, value.name, value.label]
		.filter(alias => alias != null && alias !== '')
		.map(String))]
}

/** 静态 multipleSelect 统一使用 option.value;历史 label/id 会映射到新 value。 */
export function resolveMultipleSelectOptionValue(field = {}, rawValue) {
	const values = coerceMultipleSelectValue(rawValue)
	if (!Array.isArray(values) || isRemoteMultipleSelectField(field)) {
		return values
	}
	const options = Array.isArray(field.options) ? field.options : []
	if (!options.length) return values
	return values.map(item => {
		const aliases = new Set(getAliases(item))
		const matched = options.find(option =>
			getAliases(option).some(alias => aliases.has(alias))
		)
		return matched ? (matched.value ?? matched.id ?? item) : item
	})
}

function normalizeMultipleSelectFieldValue(field, rawValue) {
	if (rawValue === undefined) return undefined
	return resolveMultipleSelectOptionValue(field, rawValue)
}

/** 批量归一化 values 中所有 multipleSelect 字段 */
export function normalizeMultipleSelectValuesInMap(list = [], values = {}) {
	const multipleSelectFields = flattenFields(list).filter(
		field => field.type === 'multipleSelect'
	)
	if (!multipleSelectFields.length) {
		return values
	}
	const nextValues = { ...values }
	multipleSelectFields.forEach(field => {
		if (nextValues[field.key] === undefined) {
			return
		}
		nextValues[field.key] = normalizeMultipleSelectFieldValue(field, nextValues[field.key])
	})

	listSubforms(list).forEach(subform => {
		const childMultipleSelects = getSubformRowTemplate(subform).filter(
			child => child.type === 'multipleSelect'
		)
		if (!childMultipleSelects.length) return
		const rows = getGroupRows(nextValues, subform.key)
		if (!rows.length) return
		nextValues[subform.key] = rows.map(row => {
			const nextRow = { ...row }
			childMultipleSelects.forEach(child => {
				if (nextRow[child.key] === undefined) return
				nextRow[child.key] = normalizeMultipleSelectFieldValue(child, nextRow[child.key])
			})
			return nextRow
		})
	})
	return nextValues
}

if (typeof module !== 'undefined' && module.exports) {
	const displayHelpers = require('./multiple-select-display.helpers.js')
	module.exports = {
		coerceMultipleSelectValue,
		resolveMultipleSelectOptionValue,
		normalizeMultipleSelectValuesInMap,
		...displayHelpers
	}
}