FilterBar.js
8.12 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// components/FilterBar/FilterBar.js
const IMG_URL =
"https://img02.glsx.com.cn/weapp/resource/miniprogram/regional-assistant/";
const date = new Date();
const years = [];
const months = [];
for (let i = 2020; i <= date.getFullYear() + 5; i++) {
years.push(i);
}
for (let i = 1; i <= 12; i++) {
months.push(i);
}
Component({
properties: {
height: {
type: String,
value: "40%",
},
searchPlaceholder: {
type: String,
value: "门店名称",
},
searchObj: {
type: Object,
value: {},
},
searchKey: {
type: String,
value: "keyword",
},
// Array of filter objects: { type: 'month'|'select', label: 'Label', key: 'key', optionList: [], value: '' }
filters: {
type: Array,
value: [],
},
},
data: {
IMG_URL,
years: years,
months: months,
searchValue: "",
formData: {},
// Popup State
showPopup: false,
activeFilterIndex: -1, // Index of the filter currently being edited
// Temp Values for Popups
tempDateIndex: [years.indexOf(date.getFullYear()), date.getMonth()],
tempDateValue: new Date().getTime(),
pickerViewValue: new Date().getTime(),
minDate: new Date(2020, 0, 1).getTime(),
maxDate: new Date(new Date().getFullYear() + 5, 11, 31).getTime(),
tempSelectIndex: 0,
},
methods: {
// Search Handlers
onSearch(e) {
if (e.detail && e.detail.value !== undefined) {
this.setData({ searchValue: e.detail.value });
}
this.triggerEvent("filterChange", this.getFilterData());
},
onSearchInput(e) {
this.setData({ searchValue: e.detail.value });
},
clearSearch() {
this.setData({ searchValue: "" });
this.triggerEvent("filterChange", this.getFilterData());
},
// Filter Tag Click Handler
onFilterTagTap(e) {
const index = e.currentTarget.dataset.index;
const filter = this.properties.filters[index];
const currentValue = this.data.formData[filter.key];
this.setData({
activeFilterIndex: index,
showPopup: true,
formData: this.data.searchObj,
});
if (filter.type === "month") {
this.initDatePicker(currentValue, filter.pickerType);
} else if (filter.type === "select") {
this.initSelectPicker(currentValue);
}
},
// Popup Logic
closePopup() {
this.setData({ showPopup: false, activeFilterIndex: -1 });
},
confirmPopup() {
const index = this.data.activeFilterIndex;
if (index === -1) return;
const filter = this.properties.filters[index];
let newValue = "";
if (filter.type === "month") {
newValue = this.getDateFromPicker(filter.pickerType);
} else if (filter.type === "select") {
newValue = this.data.formData[filter.key];
}
// Update the specific filter's value
const key = `formData.${filter.key}`;
this.setData({
[key]: newValue,
showPopup: false,
activeFilterIndex: -1,
});
this.triggerEvent("filterChange", this.getFilterData());
},
// Date Logic
initDatePicker(currentValue, pickerType = "year-month") {
let val = new Date().getTime();
if (pickerType === "time") {
val = currentValue || "00:00";
} else {
if (currentValue) {
let str = currentValue;
// Handle YYYY-MM for year-month type
if (pickerType === "year-month" && /^\d{4}-\d{2}$/.test(str)) {
str += "-01";
}
val = new Date(str.replace(/-/g, "/")).getTime();
if (isNaN(val)) val = new Date().getTime();
}
}
this.setData({ tempDateValue: val });
},
onDatePickChange(e) {
this.setData({ tempDateValue: e.detail });
},
getDateFromPicker(pickerType = "year-month") {
const val = this.data.tempDateValue;
if (pickerType === "time") return val;
const date = new Date(val);
const y = date.getFullYear();
const m = (date.getMonth() + 1).toString().padStart(2, "0");
const d = date.getDate().toString().padStart(2, "0");
if (pickerType === "date") return `${y}-${m}-${d}`;
if (pickerType === "year-month") return `${y}-${m}`;
return `${y}-${m}-${d}`; // default fallback
},
// Select Logic
getSelectOptions(filter) {
let optionList = filter.optionList || [];
if (!filter.hideAllOption) {
optionList = [{ confId: '', confLabel: '全部' }, ...optionList];
}
return optionList;
},
initSelectPicker(currentValue) {
const filter = this.properties.filters[this.data.activeFilterIndex];
const optionList = this.getSelectOptions(filter);
let index = 0;
if (currentValue) {
index = optionList.findIndex(item => item.confId == currentValue);
if (index == -1) index = 0;
} else {
// If currentValue is empty, it might mean "All" is selected (if available) or no selection.
// If "All" option exists (confId: ''), index should be 0.
// If no "All" option, but optionList exist, default to first.
index = 0;
}
// Default to first option if empty and not "All" (which is also empty string but valid)
// Actually, if we have "All" option, confId is '', so formData is empty string, which is fine.
// If we don't have "All" option, and currentValue is empty, we might want to default to first option.
if (!currentValue && optionList.length > 0) {
// If it's effectively "All" or unselected, we sync it.
const firstOption = optionList[0];
const updateData = {};
this.updateForm(filter, firstOption.confId, firstOption.confLabel, updateData);
this.setData(updateData);
}
console.log(index, 'index---')
this.setData({
tempSelectIndex: index,
currentSelectOptions: optionList // Store current optionList to use in wxml or change handler
});
},
onSelectPickerChange(e) {
const { index } = e.detail;
// We need to use the same optionList list as displayed
const filter = this.properties.filters[this.data.activeFilterIndex];
const optionList = this.data.currentSelectOptions || this.getSelectOptions(filter);
const value = optionList[index];
const updateData = {
tempSelectIndex: index
};
// Update showKey if present
this.updateForm(filter, value ? value.confId : '', value ? value.confLabel : '', updateData);
this.setData(updateData);
},
/**
* Updates form data with value and optional display label
* @param {Object} filter - Filter configuration object
* @param {String|Number} keyVal - The value to set for the filter key
* @param {String} showKeyVal - The label to set for the showKey (if configured)
* @param {Object} targetObj - The object to mutate with updates
*/
updateForm(filter, keyVal, showKeyVal, targetObj) {
Object.assign(targetObj, {
[`formData.${filter.key}`]: keyVal,
...(filter.showKey && { [`formData.${filter.showKey}`]: showKeyVal }),
});
},
// Reset
resetFilters() {
// Reset search
this.setData({ searchValue: "" });
// Reset all filters
const updates = {};
this.properties.filters.forEach((f) => {
this.updateForm(f, "", "", updates);
});
this.setData(updates);
this.triggerEvent("filterChange", this.getFilterData());
},
// Helper
getFilterData() {
const result = {
[this.properties.searchKey]: this.data.searchValue,
};
this.properties.filters.forEach((f) => {
let keyVal = this.data.formData[f.key] || "";
let showKeyVal = f.showKey ? (this.data.formData[f.showKey] || "") : "";
// 如果当前选中的confId为空并且confLabel为全部,则传‘’
// If the value is empty and the label is "全部", treat label as empty string for output
if (keyVal == "" && showKeyVal == "全部") {
showKeyVal = "";
}
result[f.key] = keyVal;
if (f.showKey) {
result[f.showKey] = showKeyVal;
}
});
return result;
},
},
});