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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @Date: 2020-05-26 15:31
* @author jiabinbin
* @Email 425605679@qq.com
* @Description:
*/
import S from 'ant-design-vue/es/select'
import { isPromise } from '../../../utils/utils' // 判断是否是promise
import { getDefaultProps } from '../../_utils' // 参见upload组件中的getDefaultProps

const BSelect = {
name: 'BSelect',
props: Object.assign({}, S.props, {
loadOptions: { // a-select-option数组对象
type: Array | Promise,
required: true
},
optionKey: { // selectOption的key
type: String,
default: 'id'
},
optionValue: { // selectOption的value
type: String,
default: 'value'
},
keyValue: { // 有时候需要展示值为 00-完成 01-未完成 02-待确定。开启之后 option的value和label同时展示
type: Boolean,
default: false
},
keyValueSplit: { // 开启keyValue之后,keyValue的连接符
type: String,
default: '-'
}
}),
model: {
prop: 'value',
event: 'change'
},
data: () => ({
optionList: []
}),
created () {
this.loadData()
},
watch: {
loadOptions: {
handler (c, n) {
this.loadData()
}
},
immediate: true
},
methods: {
loadData () {
if (isPromise(this.loadOptions)) {
this.loadOptions.then(({ data }) => {
this.optionList = data
})
} else {
this.optionList = this.loadOptions
}
},
renderOptions () {
const { optionList } = this
const { optionKey, optionValue, keyValue, keyValueSplit } = this
return (
optionList.map(item => {
const key = item[optionKey]
const value = item[optionValue]
const title = keyValue ? `${key} ${keyValueSplit} ${value}` : `${value}`
return (
!keyValue ? (
<a-select-option key={key} title={value} value={key}>{value}</a-select-option>
) : (
<a-select-option key={key} title={title} value={key}>{key} {keyValueSplit} {value}</a-select-option>
)
)
})
)
}
},
render () {
// props
const props = getDefaultProps(S.props, this)
const options = this.renderOptions()
return (
<a-select {...{ props }} {...{ on: this.$listeners }} value={this.model}>
{options}
</a-select>
)
}
}

export default BSelect