selectPopup.js
5.56 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// packageWorkspace/pages/performanceRating/components/selectPopup/selectPopup.js
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/regional-assistant/'
Component({
options: {
addGlobalClass: true
},
properties: {
show: {
type: Boolean,
value: false
},
title: {
type: String,
value: ''
},
// 原始选项列表,支持任意字段名,通过 labelKey/valueKey 映射
options: {
type: Array,
value: []
},
// 是否多选
multiple: {
type: Boolean,
value: false
},
// 单选初始值
value: {
type: null,
value: null
},
// 多选初始值
values: {
type: Array,
value: []
},
// 字段映射
labelKey: {
type: String,
value: 'name'
},
valueKey: {
type: String,
value: 'value'
},
// 多选最大数量(不限制则设为 0 或不传)
max: {
type: Number,
value: 0
},
// 单选时点击即确认
confirmOnSelect: {
type: Boolean,
value: false
},
height: {
type: String
},
allowSearch: {
type: Boolean
}
},
data: {
IMG_URL,
displayOptions: [], // 带 checked 的渲染选项
selectedValues: [] // 选中的值列表(单选也统一用数组)
},
observers: {
'options, multiple, value, values': function(options, multiple, value, values) {
this.initDisplay(options || [], multiple, value, values)
},
show(n) {
// 进出场时保持数据一致
if (n) {
this.setData({
searchValue: ''
})
this.initDisplay(this.properties.options || [], this.properties.multiple, this.properties.value, this.properties.values)
}
}
},
methods: {
initDisplay(options = [], multiple = false, value = null, values = []) {
const vKey = this.properties.valueKey
// 初始选中值统一转为数组
const initSelected = multiple ? (Array.isArray(values) ? values.slice() : []) : (value !== null && value !== undefined ? value : [])
const display = (options || []).map(item => {
const val = item[vKey]
const checked = initSelected?.some(v => v === val)
return { ...item, checked }
})
this.setData({
displayOptions: display,
copyDisplayOptions: wx.$util.deepClone(display),
selectedValues: initSelected
})
},
toggle(e) {
const { index, item } = e.currentTarget.dataset
const { multiple, max, valueKey } = this.properties
let { displayOptions, copyDisplayOptions } = this.data
if (!item || item.disabled) return
if (multiple) {
let nextChecked = !item.checked
// 限制最大数量
if (nextChecked && max > 0) {
const selectedCount = this.data.selectedValues.length
if (selectedCount >= max) {
// 可根据项目规范提示用户,这里不做 UI 弹窗,保持组件纯净
this.triggerEvent('maxtips')
return
}
}
const selectIndex = displayOptions?.findIndex(v => v[valueKey] == item[valueKey])
this.setData({ [`displayOptions[${selectIndex}].checked`]: nextChecked })
const copySelectIndex = copyDisplayOptions?.findIndex(v => v[valueKey] == item[valueKey])
this.setData({ [`copyDisplayOptions[${copySelectIndex}].checked`]: nextChecked })
const val = item[valueKey]
let selected = this.data.selectedValues.slice()
const pos = selected.findIndex(v => v === val)
if (nextChecked && pos === -1) selected.push(val)
if (!nextChecked && pos > -1) selected.splice(pos, 1)
this.setData({ selectedValues: selected })
this.triggerEvent('change', { values: selected, items: this.getSelectedItems() })
} else {
// 单选:取消其他,选中当前
const currentVal = item[valueKey]
const display = this.data.displayOptions.map((it, idx) => ({ ...it, checked: idx === index ? !it.checked : false }))
const selected = display[index].checked ? [currentVal] : []
this.setData({ displayOptions: display, selectedValues: selected })
const copyDisplay = this.data.copyDisplayOptions.map((it, idx) => ({ ...it, checked: idx === index ? !it.checked : false }))
this.setData({ copyDisplayOptions: copyDisplay })
this.triggerEvent('change', { values: selected, items: this.getSelectedItems() })
if (this.properties.confirmOnSelect) {
this.onConfirm()
}
}
},
search(e) {
let { labelKey, copyDisplayOptions, displayOptions, selectedValues, valueKey } = this.data
let { value } = e.detail
if (!value) {
copyDisplayOptions.map(i => {
i.checked = false
if (selectedValues.includes(i[valueKey])) i.checked = true
return i
})
this.setData({
displayOptions: copyDisplayOptions
})
} else {
let arr = []
copyDisplayOptions.map(i => {
i.checked = false
if (selectedValues.includes(i[valueKey])) i.checked = true
if (i[labelKey].indexOf(value) != -1) {
arr.push(i)
}
})
this.setData({
displayOptions: arr
})
}
},
onCancel() {
this.triggerEvent('cancel')
},
onConfirm() {
const values = this.data.selectedValues
const items = this.getSelectedItems()
this.triggerEvent('confirm', { values, items })
},
getSelectedItems() {
return (this.data.copyDisplayOptions || []).filter(it => it.checked)
}
}
})