picker-value.js
1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/** form-picker-popup 选中态纯函数 — 供组件与单测共用 */
/** 将单项(标量或 { id, value } 对象)解析为 option.value 比对用的字符串 key */
function resolvePickerItemKey(item) {
if (item != null && typeof item === 'object') {
const key = item.id ?? item.value
return key == null || key === '' ? '' : String(key)
}
return item == null || item === '' ? '' : String(item)
}
/** 将 value 规范为字符串 id 数组,供 draft 与 option 比对(支持详情回显对象数组) */
function normalizePickerValues(value) {
if (!Array.isArray(value)) {
if (value == null || value === '') {
return []
}
const key = resolvePickerItemKey(value)
return key ? [key] : []
}
return value.map(resolvePickerItemKey).filter(Boolean)
}
/** 根据 options 与当前 value 生成带 active 的行列表 */
function buildPickerOptionRows(options = [], value) {
const draftValues = normalizePickerValues(value)
return (options || []).map(option => ({
label: option.label,
value: option.value,
active: draftValues.includes(String(option.value))
}))
}
module.exports = {
resolvePickerItemKey,
normalizePickerValues,
buildPickerOptionRows
}