|
|
@ -26,8 +26,18 @@ export default { |
|
|
|
// 选项列表,支持多种格式: |
|
|
|
// 1. 数组格式: [{ label: '显示文本', value: '值', disabled: false }] |
|
|
|
// 2. 对象格式: { key1: 'value1', key2: 'value2' } |
|
|
|
// 3.null |
|
|
|
// 4.‘’ |
|
|
|
options: { |
|
|
|
type: [Array, Object], |
|
|
|
// type: [Array, Object], |
|
|
|
default: () => null, |
|
|
|
validator: (value) => { |
|
|
|
// 自定义验证器,允许 null、数组或非空对象 |
|
|
|
return value == null || |
|
|
|
value === '' || |
|
|
|
Array.isArray(value) || |
|
|
|
(typeof value === 'object' && value !== null) |
|
|
|
}, |
|
|
|
required: true, |
|
|
|
}, |
|
|
|
// 当前选中的值,使用 v-model 绑定 |
|
|
@ -73,16 +83,26 @@ export default { |
|
|
|
computed: { |
|
|
|
// 统一处理数组和对象格式 |
|
|
|
normalizedOptions() { |
|
|
|
// 数组 |
|
|
|
// 处理null或undefined、‘’情况 |
|
|
|
if (this.options == null || !this.options) { |
|
|
|
return []; |
|
|
|
} |
|
|
|
|
|
|
|
// 处理数组 |
|
|
|
if (Array.isArray(this.options)) { |
|
|
|
return this.options; |
|
|
|
} else if (typeof this.options === 'object' && this.options !== null) { |
|
|
|
// 将对象格式统一转换为数组 |
|
|
|
return Object.keys(this.options).map(key => ({ |
|
|
|
return this.options.length ? this.options : []; |
|
|
|
} |
|
|
|
|
|
|
|
// 处理对象 |
|
|
|
if (typeof this.options === 'object') { |
|
|
|
const keys = Object.keys(this.options); |
|
|
|
return keys.length ? keys.map(key => ({ |
|
|
|
key, |
|
|
|
value: this.options[key] |
|
|
|
})); |
|
|
|
})) : []; |
|
|
|
} |
|
|
|
|
|
|
|
// 其他意外情况返回空数组 |
|
|
|
return []; |
|
|
|
} |
|
|
|
}, |
|
|
|