datetime-minute-picker.js
5.66 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
/**
* 动态表单分钟 datetime 一体选择(日/时/分三列滚轮)
* @file 参考 tabRepairOrder/timeSelect,输出 YYYY-MM-DD HH:mm,无页面耦合
*/
const {
endOfToday,
formatMinutePickerValue,
parseMinutePickerDefaultTime,
buildMinutePickerDayList,
resolveMinutePickerDayStart,
resolveMinutePickerDayEnd
} = require('../../form-engine/datetime-picker-mode.js')
function pad2(n) {
return `${n}`.padStart(2, '0')
}
Component({
properties: {
show: {
type: Boolean,
observer(visible) {
if (visible) {
this.initPicker()
}
}
},
title: {
type: String,
value: ''
},
defaultTime: {
type: String,
value: ''
},
/** now | today | 空 — 对齐 resolveMaxDateKind */
maxDateKind: {
type: String,
value: ''
}
},
data: {
days: [],
hours: [],
minutes: [],
dateValues: [0],
hourValues: [0],
minuteValues: [0],
loading: true
},
methods: {
shouldCapTimeToNow() {
return this.properties.maxDateKind === 'now'
},
shouldCapDateToToday() {
const kind = this.properties.maxDateKind
return kind === 'now' || kind === 'today'
},
parseDefaultTime() {
return parseMinutePickerDefaultTime(this.properties.defaultTime)
},
buildHourList(dateStr) {
const todayStr = endOfToday()
const isToday = dateStr === todayStr
if (this.shouldCapTimeToNow() && isToday) {
const now = new Date()
const limit = now.getHours() + 1
return Array.from({ length: limit }, (_, hour) => pad2(hour))
}
return Array.from({ length: 24 }, (_, hour) => pad2(hour))
},
buildMinuteList(dateStr, hourStr) {
const todayStr = endOfToday()
const isToday = dateStr === todayStr
if (this.shouldCapTimeToNow() && isToday) {
const now = new Date()
const currentHour = pad2(now.getHours())
if (hourStr !== currentHour) {
return Array.from({ length: 60 }, (_, minute) => pad2(minute))
}
const limit = now.getMinutes() + 1
return Array.from({ length: limit }, (_, minute) => pad2(minute))
}
return Array.from({ length: 60 }, (_, minute) => pad2(minute))
},
/**
* 同步滚轮选中项;二次 setData 规避 picker-view 首帧空白(对齐 timeSelect)
*/
syncCurrentSelection(days, hours, minutes, defaultTime) {
const dateIndex = days.findIndex(day => day === defaultTime.dateStr)
const hourStr = pad2(defaultTime.hour)
const minuteStr = pad2(defaultTime.minute)
let hourIndex = hours.findIndex(hour => hour === hourStr)
if (hourIndex < 0) {
hourIndex = Math.max(0, hours.length - 1)
}
let minuteIndex = minutes.findIndex(minute => minute === minuteStr)
if (minuteIndex < 0) {
minuteIndex = Math.max(0, minutes.length - 1)
}
const resolvedDateIndex = dateIndex >= 0 ? dateIndex : Math.max(0, days.length - 1)
const resolvedHourIndex = hourIndex >= 0 ? hourIndex : Math.max(0, hours.length - 1)
const resolvedMinuteIndex = minuteIndex >= 0 ? minuteIndex : Math.max(0, minutes.length - 1)
this.setData(
{
days,
hours,
minutes,
dateValues: [resolvedDateIndex],
hourValues: [resolvedHourIndex],
minuteValues: [resolvedMinuteIndex],
loading: true
},
() => {
setTimeout(() => {
this.currentDate = days[resolvedDateIndex]
this.currentHour = hours[resolvedHourIndex]
this.currentMinute = minutes[resolvedMinuteIndex]
this.setData({
dateValues: [resolvedDateIndex],
hourValues: [resolvedHourIndex],
minuteValues: [resolvedMinuteIndex],
loading: false
})
}, 0)
}
)
},
initPicker() {
this.setData({ loading: true })
const defaultTime = this.parseDefaultTime()
const todayStr = endOfToday()
const capToToday = this.shouldCapDateToToday()
const startDay = resolveMinutePickerDayStart(defaultTime.dateStr)
const endDay = resolveMinutePickerDayEnd({
defaultDateStr: defaultTime.dateStr,
todayStr,
capToToday
})
const days = buildMinutePickerDayList(startDay, endDay)
const hours = this.buildHourList(defaultTime.dateStr)
const minutes = this.buildMinuteList(defaultTime.dateStr, pad2(defaultTime.hour))
this.syncCurrentSelection(days, hours, minutes, defaultTime)
},
refreshHourMinuteColumns() {
const hours = this.buildHourList(this.currentDate)
const hourIndex = Math.max(
0,
hours.findIndex(hour => hour === this.currentHour)
)
this.currentHour = hours[hourIndex] || hours[hours.length - 1] || '00'
const minutes = this.buildMinuteList(this.currentDate, this.currentHour)
const minuteIndex = Math.max(
0,
minutes.findIndex(minute => minute === this.currentMinute)
)
this.currentMinute = minutes[minuteIndex] || minutes[minutes.length - 1] || '00'
this.setData({
hours,
minutes,
hourValues: [hourIndex],
minuteValues: [minuteIndex]
})
},
changeDate(e) {
const index = e.detail.value[0]
this.currentDate = this.data.days[index]
this.refreshHourMinuteColumns()
},
changeHour(e) {
const index = e.detail.value[0]
this.currentHour = this.data.hours[index]
const minutes = this.buildMinuteList(this.currentDate, this.currentHour)
const minuteIndex = Math.max(
0,
minutes.findIndex(minute => minute === this.currentMinute)
)
this.currentMinute = minutes[minuteIndex] || minutes[minutes.length - 1] || '00'
this.setData({
minutes,
minuteValues: [minuteIndex]
})
},
changeMinutes(e) {
const index = e.detail.value[0]
this.currentMinute = this.data.minutes[index]
},
confirm() {
const value = formatMinutePickerValue(
this.currentDate,
this.currentHour,
this.currentMinute
)
this.triggerEvent('confirm', { value })
},
cancel() {
this.triggerEvent('cancel')
}
}
})