multiple-select-display.helpers.js 3.29 KB
/**
 * multipleSelect 展示名解析 — 纯函数,供组件回显与 jsonContent 编解码共用
 * @see packageDynamicForm/docs/field-options-api.md
 */

/**
 * 展示名是否实为 id(jsonContent 误把 uuid 写入 value 时不应作为 label)
 * @param {*} label 候选展示名
 * @param {*} [id] 对应选项 id
 * @returns {boolean}
 */
function isMultipleSelectIdLikeLabel(label, id = '') {
	if (label == null || label === '') {
		return true
	}
	return String(label) === String(id)
}

/**
 * 解析 multipleSelect 单选项展示文案;optionsKey 未匹配时不回退 id
 * @param {Object} [params]
 * @param {string} params.selectedKey 选中 id
 * @param {*} [params.rawItem] 引擎值中的原始项
 * @param {Object} [params.matched] 已加载 options 中的匹配项
 * @param {boolean} [params.hasOptionsKey] 是否远程 optionsKey 字段
 * @returns {string}
 */
function resolveMultipleSelectItemDisplayLabel({
	selectedKey,
	rawItem,
	matched,
	hasOptionsKey = false
} = {}) {
	if (rawItem != null && typeof rawItem === 'object') {
		const name = rawItem.name || rawItem.label
		if (name && !isMultipleSelectIdLikeLabel(name, selectedKey)) {
			return String(name)
		}
	}
	if (matched?.label) {
		return String(matched.label)
	}
	return hasOptionsKey ? '' : String(selectedKey || '')
}

/**
 * `/` 拼接展示串中剔除与 id 相同的段(保存入库前防御)
 * @param {Array<string>} [ids=[]]
 * @param {string} [displayLabel='']
 * @returns {string}
 */
function sanitizeMultipleSelectSlashLabel(ids = [], displayLabel = '') {
	if (typeof displayLabel !== 'string' || displayLabel === '') {
		return ''
	}
	const parts = displayLabel
		.trim()
		.split('/')
		.map(part => part.trim())
	const sanitized = ids
		.map((id, index) => {
			const part = parts[index]
			if (part == null || part === '' || isMultipleSelectIdLikeLabel(part, id)) {
				return ''
			}
			return part
		})
		.filter(Boolean)
	return sanitized.join('/')
}

/** App jsonContent 双 key:`value` 为 `/` 拼接展示串 */
function splitMultipleSelectDisplayLabel(value) {
	if (typeof value !== 'string' || value === '') {
		return []
	}
	const trimmed = value.trim()
	if (trimmed.startsWith('[')) {
		return []
	}
	return trimmed.split('/').map(part => part.trim()).filter(part => part !== '')
}

/** `/` 展示串是否含至少一段真实姓名(非 id) */
function hasRealMultipleSelectDisplayNames(ids = [], displayLabel = '') {
	const names = splitMultipleSelectDisplayLabel(displayLabel)
	if (!names.length) {
		return false
	}
	return names.some((name, index) => !isMultipleSelectIdLikeLabel(name, ids[index]))
}

/** id 数组 + `/` 展示串 → `{ id, name? }[]`(不把 id 当 name) */
function zipMultipleSelectIdsWithDisplayLabel(ids = [], displayLabel = '') {
	const names = splitMultipleSelectDisplayLabel(displayLabel)
	return ids.map((id, index) => {
		const idStr = String(id)
		const namePart = names[index]
		const entry = { id: idStr }
		if (
			namePart != null &&
			namePart !== '' &&
			!isMultipleSelectIdLikeLabel(namePart, idStr)
		) {
			entry.name = namePart
		}
		return entry
	})
}

module.exports = {
	isMultipleSelectIdLikeLabel,
	resolveMultipleSelectItemDisplayLabel,
	sanitizeMultipleSelectSlashLabel,
	splitMultipleSelectDisplayLabel,
	hasRealMultipleSelectDisplayNames,
	zipMultipleSelectIdsWithDisplayLabel
}