form-picker-popup.js
3.61 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
/**
* 选项弹框 — single 单选 / multiple 多选
* @see packageDynamicForm/docs/field-options-api.md
*
* mode=single:点选替换 draft,confirm 返回标量
* mode=multiple:toggle 多选,confirm 返回数组
*/
const { normalizePickerValues, buildPickerOptionRows } = require('./picker-value.js')
Component({
properties: {
show: {
type: Boolean,
value: false
},
title: {
type: String,
value: '请选择'
},
height: {
type: String,
value: '70%'
},
options: {
type: Array,
value: []
},
value: {
type: null,
value: []
},
loading: {
type: Boolean,
value: false
},
mode: {
type: String,
value: 'multiple'
},
searchable: {
type: Boolean,
value: false
},
searchKeyword: {
type: String,
value: ''
}
},
data: {
optionRows: [],
draftValues: [],
isFullSucreen: false,
emptyText: '暂无可选项'
},
lifetimes: {
attached() {
this.setData({
isFullSucreen: wx.$util?.isFullSucreen?.(true) || false
})
}
},
observers: {
show: function (show) {
if (show) {
wx.hideKeyboard?.()
this.syncOptionRows()
}
},
'options, value, loading': function () {
if (this.properties.show && !this.properties.loading) {
this.syncOptionRows()
}
},
'searchable, searchKeyword': function (searchable, searchKeyword) {
if (!this.properties.show || this.properties.loading) return
this.updateEmptyText(searchable, searchKeyword)
}
},
methods: {
noScroll() {},
/** show/options/value 变化时同步 optionRows 与 draftValues 选中态 */
syncOptionRows() {
const draftValues = normalizePickerValues(this.properties.value)
const optionRows = buildPickerOptionRows(this.properties.options, this.properties.value)
this.updateEmptyText(this.properties.searchable, this.properties.searchKeyword)
this.setData({
optionRows,
draftValues
})
},
/** 搜索态空列表文案对齐 Merchant-Seles */
updateEmptyText(searchable, searchKeyword) {
const emptyText =
searchable && searchKeyword ? '暂无数据' : '暂无可选项'
if (emptyText !== this.data.emptyText) {
this.setData({ emptyText })
}
},
/** search-input 确认搜索 → 父组件带 name 重拉员工列表 */
onSearch(e) {
this.triggerEvent('search', { value: e.detail?.value || '' })
},
/** 点选行:single 替换 draft,multiple toggle 多选并刷新 active 态 */
onToggle(e) {
const index = e.currentTarget.dataset.index
const row = this.data.optionRows[index]
if (!row) return
const valueStr = String(row.value)
const isSingle = this.properties.mode === 'single'
let draftValues
if (isSingle) {
draftValues = [valueStr]
} else {
draftValues = [...this.data.draftValues]
const hitIndex = draftValues.indexOf(valueStr)
if (hitIndex === -1) {
draftValues.push(valueStr)
} else {
draftValues.splice(hitIndex, 1)
}
}
const optionRows = this.data.optionRows.map(item => ({
...item,
active: draftValues.includes(String(item.value))
}))
this.setData({
draftValues,
optionRows
})
},
/** 确认 draft:按 mode 返回标量或数组 triggerEvent('confirm') */
onConfirm() {
const values = this.data.draftValues.map(value => {
const matched = (this.properties.options || []).find(
item => String(item.value) === String(value)
)
return matched ? matched.value : value
})
if (this.properties.mode === 'single') {
this.triggerEvent('confirm', { value: values[0] })
return
}
this.triggerEvent('confirm', { value: values })
},
onCancel() {
this.triggerEvent('cancel')
}
}
})