hy-app 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,375 +1,377 @@
1
- <template>
2
- <hy-popup
3
- :show="show"
4
- mode="bottom"
5
- closeable
6
- @close="close"
7
- :round="round"
8
- :closeOnClickOverlay="closeOnClickOverlay"
9
- >
10
- <view class="hy-calendar">
11
- <HyHeader
12
- :title="title"
13
- :subtitle="subtitle"
14
- :showSubtitle="showSubtitle"
15
- :showTitle="showTitle"
16
- :weekText="weekText"
17
- ></HyHeader>
18
- <scroll-view
19
- :style="{
20
- height: addUnit(listHeight)
21
- }"
22
- scroll-y
23
- @scroll="onScroll"
24
- :scroll-top="scrollTop"
25
- :scrollIntoView="scrollIntoView"
26
- >
27
- <HyMount
28
- :color="color"
29
- :rowHeight="rowHeight"
30
- :showMark="showMark"
31
- :months="months"
32
- :mode="mode"
33
- :maxCount="maxCount"
34
- :startText="startText"
35
- :endText="endText"
36
- :defaultDate="defaultDate"
37
- :minDate="innerMinDate"
38
- :maxDate="innerMaxDate"
39
- :maxMonth="monthNum"
40
- :readonly="readonly"
41
- :maxRange="maxRange"
42
- :rangePrompt="rangePrompt"
43
- :showRangePrompt="showRangePrompt"
44
- :allowSameDay="allowSameDay"
45
- :forbidDays="forbidDays"
46
- :forbidDaysToast="forbidDaysToast"
47
- ref="month"
48
- @monthSelected="monthSelected"
49
- @updateMonthTop="updateMonthTop"
50
- ></HyMount>
51
- </scroll-view>
52
- <slot name="footer" v-if="showConfirm">
53
- <view class="hy-calendar__confirm">
54
- <HyButton
55
- shape="circle"
56
- :text="buttonDisabled ? confirmDisabledText : confirmText"
57
- :color="color"
58
- @click="confirm"
59
- :disabled="buttonDisabled"
60
- ></HyButton>
61
- </view>
62
- </slot>
63
- </view>
64
- </hy-popup>
65
- </template>
66
-
67
- <script lang="ts">
68
- export default {
69
- name: 'hy-calendar',
70
- options: {
71
- virtualHost: true,
72
- styleIsolation: 'shared'
73
- }
74
- }
75
- </script>
76
-
77
- <script setup lang="ts">
78
- import type { ICalendarEmits } from './typing'
79
- import { computed, nextTick, onMounted, ref, watch } from 'vue'
80
- import {
81
- addUnit,
82
- error,
83
- formatTime,
84
- isArray,
85
- isNumericString,
86
- padZero,
87
- range,
88
- useTranslate
89
- } from '../../libs'
90
- import Calendar from '../../libs/utils/calendar.js'
91
- import calendarProps from './props'
92
- import dayjs from 'dayjs/esm'
93
- // 组件
94
- import HyMount from './month.vue'
95
- import HyHeader from './header.vue'
96
- import HyPopup from '../hy-popup/hy-popup.vue'
97
- import HyButton from '../hy-button/hy-button.vue'
98
-
99
- /**
100
- * 用于单个选择日期,范围选择日期等,日历被包裹在底部弹起的容器中。
101
- * @displayName hy-calendar
102
- */
103
- defineOptions({})
104
-
105
- const props = defineProps(calendarProps)
106
- const emit = defineEmits<ICalendarEmits>()
107
-
108
- const { t } = useTranslate('calendar')
109
- // 需要显示的月份的数组
110
- const months = ref<any[]>([])
111
- // 在月份滚动区域中,当前视图中月份的index索引
112
- const monthIndex = ref(0)
113
- // 月份滚动区域的高度
114
- const listHeight = ref(0)
115
- // month组件中选择的日期数组
116
- const selected = ref<string[]>([])
117
- const scrollIntoView = ref('')
118
- const scrollIntoViewScroll = ref('')
119
- const scrollTop = ref(0)
120
- // 过滤处理方法
121
- let innerFormatter = (value: string) => value
122
-
123
- // 由于maxDate和minDate可以为字符串(2021-10-10),或者数值(时间戳),但是dayjs如果接受字符串形式的时间戳会有问题,这里进行处理
124
- const innerMaxDate = computed(() => {
125
- return isNumericString(props.maxDate) ? Number(props.maxDate) : props.maxDate
126
- })
127
- const innerMinDate = computed(() => {
128
- return isNumericString(props.minDate) ? Number(props.minDate) : props.minDate
129
- })
130
-
131
- /**
132
- * 多个条件的变化,会引起选中日期的变化,这里统一管理监听
133
- * */
134
- const selectedChange = computed(() => {
135
- return [innerMinDate.value, innerMaxDate.value, props.defaultDate]
136
- })
137
-
138
- /**
139
- * 获得两个日期之间的月份数
140
- * @param minDate 最小日期
141
- * @param maxDate 最大日期
142
- * */
143
- const getMonths = (minDate: number | string, maxDate: number | string) => {
144
- const minYear = dayjs(minDate).year()
145
- const minMonth = dayjs(minDate).month() + 1
146
- const maxYear = dayjs(maxDate).year()
147
- const maxMonth = dayjs(maxDate).month() + 1
148
- return (maxYear - minYear) * 12 + (maxMonth - minMonth) + 1
149
- }
150
-
151
- /**
152
- * 设置月份数据
153
- * */
154
- const setMonth = () => {
155
- // 最小日期的毫秒数
156
- const minDate = innerMinDate.value || dayjs().valueOf()
157
- // 如果没有指定最大日期,则往后推3个月
158
- const maxDate =
159
- innerMaxDate.value ||
160
- dayjs(minDate)
161
- .add(props.monthNum - 1, 'month')
162
- .valueOf()
163
- // 最大最小月份之间的共有多少个月份,
164
- const monthList = range(1, props.monthNum, getMonths(minDate, maxDate))
165
- // 先清空数组
166
- months.value = []
167
- for (let i = 0; i < monthList; i++) {
168
- months.value.push({
169
- date: new Array(dayjs(minDate).add(i, 'month').daysInMonth())
170
- .fill(1)
171
- .map((item, index) => {
172
- // 日期,取值1-31
173
- let day = index + 1
174
- // 星期,0-6,0为周日
175
- const week = dayjs(minDate).add(i, 'month').date(day).day()
176
- const date = dayjs(minDate).add(i, 'month').date(day).format('YYYY-MM-DD')
177
- let bottomInfo = ''
178
- if (props.showLunar) {
179
- // 将日期转为农历格式
180
- const lunar = Calendar.solar2lunar(
181
- dayjs(date).year(),
182
- dayjs(date).month() + 1,
183
- dayjs(date).date()
184
- )
185
- bottomInfo = lunar.IDayCn
186
- }
187
- let config = {
188
- day,
189
- week,
190
- // 小于最小允许的日期,或者大于最大的日期,则设置为disabled状态
191
- disabled:
192
- dayjs(date).isBefore(dayjs(minDate).format('YYYY-MM-DD')) ||
193
- dayjs(date).isAfter(dayjs(maxDate).format('YYYY-MM-DD')),
194
- // 返回一个日期对象,供外部的formatter获取当前日期的年月日等信息,进行加工处理
195
- date: new Date(date),
196
- bottomInfo,
197
- dot: false,
198
- month: dayjs(minDate).add(i, 'month').month() + 1
199
- }
200
- const format = props.formatter || innerFormatter
201
- return format(config as any)
202
- }),
203
- // 当前所属的月份
204
- month: dayjs(minDate).add(i, 'month').month() + 1,
205
- // 当前年份
206
- year: dayjs(minDate).add(i, 'month').year()
207
- })
208
- }
209
- }
210
-
211
- watch(
212
- () => selectedChange.value,
213
- () => setMonth(),
214
- { immediate: true }
215
- )
216
-
217
- watch(
218
- () => props.show,
219
- (newVal: boolean) => {
220
- if (newVal) {
221
- setMonth()
222
- } else {
223
- // 关闭时重置scrollIntoView,否则会出现二次打开日历,当前月份数据显示不正确。
224
- // scrollIntoView需要有一个值变动过程,才会产生作用。
225
- scrollIntoView.value = ''
226
- }
227
- },
228
- { immediate: true }
229
- )
230
-
231
- const subtitle = computed(() => {
232
- // 初始化时,this.months为空数组,所以需要特别判断处理
233
- if (months.value.length) {
234
- return formatTime(
235
- `${months.value[monthIndex.value].year}-${months.value[monthIndex.value].month}`,
236
- t('monthFormat')
237
- )
238
- } else {
239
- return ''
240
- }
241
- })
242
-
243
- const buttonDisabled = computed(() => {
244
- // 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态
245
- if (props.mode === 'range') {
246
- return selected.value.length <= 1
247
- } else {
248
- return false
249
- }
250
- })
251
-
252
- onMounted(() => {
253
- // start.value = Date.now();
254
- init()
255
- })
256
-
257
- // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
258
- const setFormatter = (e: (value: string) => string) => {
259
- innerFormatter = e
260
- }
261
-
262
- /**
263
- * month组件内部选择日期后,通过事件通知给父组件
264
- */
265
- const monthSelected = (e: string[], scene = 'init') => {
266
- selected.value = e
267
- if (!props.showConfirm) {
268
- // 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还
269
- if (
270
- props.mode === 'multiple' ||
271
- props.mode === 'single' ||
272
- (props.mode === 'range' && selected.value.length >= 2)
273
- ) {
274
- if (scene === 'init') {
275
- return
276
- }
277
- if (scene === 'tap') {
278
- emit('confirm', selected.value)
279
- }
280
- }
281
- }
282
- }
283
-
284
- const init = () => {
285
- // 校验maxDate,不能小于minDate。
286
- if (
287
- innerMaxDate.value &&
288
- innerMinDate.value &&
289
- new Date(innerMaxDate.value).getTime() < new Date(innerMinDate.value).getTime()
290
- ) {
291
- return error('maxDate不能小于minDate时间')
292
- }
293
- // 滚动区域的高度
294
- listHeight.value = props.rowHeight * 5 + 30
295
- setMonth()
296
- }
297
-
298
- const close = () => {
299
- emit('close')
300
- }
301
-
302
- /**
303
- * 点击确定按钮
304
- * */
305
- const confirm = () => {
306
- if (!buttonDisabled.value) {
307
- emit('confirm', selected.value)
308
- }
309
- }
310
-
311
- /**
312
- * 滚动到默认设置的月份
313
- * @param selected 日期
314
- * */
315
- const scrollIntoDefaultMonth = (selected: string) => {
316
- // 查询默认日期在可选列表的下标
317
- const _index = months.value.findIndex(({ year, month }) => {
318
- month = padZero(month)
319
- return `${year}-${month}` === selected
320
- })
321
- if (_index !== -1) {
322
- // #ifndef MP-WEIXIN
323
- nextTick(() => {
324
- scrollIntoView.value = `month-${_index}`
325
- scrollIntoViewScroll.value = scrollIntoView.value
326
- })
327
- // #endif
328
- // #ifdef MP-WEIXIN
329
- scrollTop.value = months.value[_index].top || 0
330
- // #endif
331
- }
332
- }
333
- // scroll-view滚动监听
334
- const onScroll = (event: any) => {
335
- // 不允许小于0的滚动值,如果scroll-view到顶了,继续下拉,会出现负数值
336
- const scrollTop = Math.max(0, event.detail.scrollTop)
337
- // 将当前滚动条数值,除以滚动区域的高度,可以得出当前滚动到了哪一个月份的索引
338
- for (let i = 0; i < months.value.length; i++) {
339
- if (scrollTop >= (months.value[i].top || listHeight.value)) {
340
- monthIndex.value = i
341
- scrollIntoViewScroll.value = `month-${i}`
342
- }
343
- }
344
- }
345
- // 更新月份的top值
346
- const updateMonthTop = (topArr = []) => {
347
- // 设置对应月份的top值,用于onScroll方法更新月份
348
- topArr.map((item, index) => {
349
- months.value[index].top = item
350
- })
351
- // 如果没有设置默认日期,则将当天日期设置为默认选中的日期
352
- let selected = dayjs().format('YYYY-MM')
353
-
354
- // 获取默认日期的下标
355
- if (!props.defaultDate) {
356
- scrollIntoDefaultMonth(selected)
357
- return
358
- }
359
- // 单选模式,可以是字符串或数组,Date对象等
360
- if (!isArray(props.defaultDate)) {
361
- selected = dayjs(props.defaultDate).format('YYYY-MM')
362
- } else {
363
- selected = dayjs(props.defaultDate[0]).format('YYYY-MM')
364
- }
365
- scrollIntoDefaultMonth(selected)
366
- }
367
-
368
- defineExpose({
369
- setFormatter
370
- })
371
- </script>
372
-
373
- <style scoped lang="scss">
374
- @import './index.scss';
375
- </style>
1
+ <template>
2
+ <hy-popup
3
+ :show="show"
4
+ mode="bottom"
5
+ closeable
6
+ @close="close"
7
+ :round="round"
8
+ :closeOnClickOverlay="closeOnClickOverlay"
9
+ >
10
+ <view class="hy-calendar">
11
+ <hy-header
12
+ :title="title"
13
+ :subtitle="subtitle"
14
+ :showSubtitle="showSubtitle"
15
+ :showTitle="showTitle"
16
+ :weekText="weekText"
17
+ ></hy-header>
18
+ <scroll-view
19
+ :style="{
20
+ height: addUnit(listHeight)
21
+ }"
22
+ scroll-y
23
+ @scroll="onScroll"
24
+ :scroll-top="scrollTop"
25
+ :scrollIntoView="scrollIntoView"
26
+ >
27
+ <hy-mount
28
+ :color="color"
29
+ :rowHeight="rowHeight"
30
+ :showMark="showMark"
31
+ :months="months"
32
+ :mode="mode"
33
+ :maxCount="maxCount"
34
+ :startText="startText"
35
+ :endText="endText"
36
+ :defaultDate="defaultDate"
37
+ :minDate="innerMinDate"
38
+ :maxDate="innerMaxDate"
39
+ :maxMonth="monthNum"
40
+ :readonly="readonly"
41
+ :maxRange="maxRange"
42
+ :rangePrompt="rangePrompt"
43
+ :showRangePrompt="showRangePrompt"
44
+ :allowSameDay="allowSameDay"
45
+ :forbidDays="forbidDays"
46
+ :forbidDaysToast="forbidDaysToast"
47
+ ref="month"
48
+ @monthSelected="monthSelected"
49
+ @updateMonthTop="updateMonthTop"
50
+ ></hy-mount>
51
+ </scroll-view>
52
+ <slot name="footer" v-if="showConfirm">
53
+ <view class="hy-calendar__confirm">
54
+ <hy-button
55
+ shape="circle"
56
+ :text="buttonDisabled ? confirmDisabledText : confirmText"
57
+ :color="color"
58
+ @click="confirm"
59
+ :disabled="buttonDisabled"
60
+ ></hy-button>
61
+ </view>
62
+ </slot>
63
+ </view>
64
+ </hy-popup>
65
+ </template>
66
+
67
+ <script lang="ts">
68
+ export default {
69
+ name: 'hy-calendar',
70
+ options: {
71
+ virtualHost: true,
72
+ styleIsolation: 'shared'
73
+ }
74
+ }
75
+ </script>
76
+
77
+ <script setup lang="ts">
78
+ import type { ICalendarEmits } from './typing'
79
+ import { computed, nextTick, onMounted, ref, watch } from 'vue'
80
+ import {
81
+ addUnit,
82
+ error,
83
+ formatTime,
84
+ isArray,
85
+ isNumericString,
86
+ padZero,
87
+ range,
88
+ useTranslate
89
+ } from '../../libs'
90
+ import Calendar from '../../libs/utils/calendar.js'
91
+ import calendarProps from './props'
92
+ import dayjs from 'dayjs/esm'
93
+ // 组件
94
+ import HyMount from './month.vue'
95
+ import HyHeader from './header.vue'
96
+ import HyPopup from '../hy-popup/hy-popup.vue'
97
+ import HyButton from '../hy-button/hy-button.vue'
98
+
99
+ /**
100
+ * 用于单个选择日期,范围选择日期等,日历被包裹在底部弹起的容器中。
101
+ * @displayName hy-calendar
102
+ */
103
+ defineOptions({})
104
+
105
+ const props = defineProps(calendarProps)
106
+ const emit = defineEmits<ICalendarEmits>()
107
+
108
+ const { t } = useTranslate('calendar')
109
+ // 需要显示的月份的数组
110
+ const months = ref<any[]>([])
111
+ // 在月份滚动区域中,当前视图中月份的index索引
112
+ const monthIndex = ref(0)
113
+ // 月份滚动区域的高度
114
+ const listHeight = ref(0)
115
+ // month组件中选择的日期数组
116
+ const selected = ref<string[]>([])
117
+ const scrollIntoView = ref('')
118
+ const scrollIntoViewScroll = ref('')
119
+ const scrollTop = ref(0)
120
+ // 过滤处理方法
121
+ let innerFormatter = (value: string) => value
122
+
123
+ // 由于maxDate和minDate可以为字符串(2021-10-10),或者数值(时间戳),但是dayjs如果接受字符串形式的时间戳会有问题,这里进行处理
124
+ const innerMaxDate = computed(() => {
125
+ return isNumericString(props.maxDate) ? Number(props.maxDate) : props.maxDate
126
+ })
127
+ const innerMinDate = computed(() => {
128
+ return isNumericString(props.minDate) ? Number(props.minDate) : props.minDate
129
+ })
130
+
131
+ /**
132
+ * 多个条件的变化,会引起选中日期的变化,这里统一管理监听
133
+ * */
134
+ const selectedChange = computed(() => {
135
+ return [innerMinDate.value, innerMaxDate.value, props.defaultDate]
136
+ })
137
+
138
+ /**
139
+ * 获得两个日期之间的月份数
140
+ * @param minDate 最小日期
141
+ * @param maxDate 最大日期
142
+ * */
143
+ const getMonths = (minDate: number | string, maxDate: number | string) => {
144
+ const minYear = dayjs(minDate).year()
145
+ const minMonth = dayjs(minDate).month() + 1
146
+ const maxYear = dayjs(maxDate).year()
147
+ const maxMonth = dayjs(maxDate).month() + 1
148
+ return (maxYear - minYear) * 12 + (maxMonth - minMonth) + 1
149
+ }
150
+
151
+ /**
152
+ * 设置月份数据
153
+ * */
154
+ const setMonth = () => {
155
+ // 最小日期的毫秒数
156
+ const minDate = innerMinDate.value || dayjs().valueOf()
157
+ // 如果没有指定最大日期,则往后推3个月
158
+ const maxDate =
159
+ innerMaxDate.value ||
160
+ dayjs(minDate)
161
+ .add(props.monthNum - 1, 'month')
162
+ .valueOf()
163
+ // 最大最小月份之间的共有多少个月份,
164
+ const monthList = range(1, props.monthNum, getMonths(minDate, maxDate))
165
+ // 先清空数组
166
+ months.value = []
167
+ for (let i = 0; i < monthList; i++) {
168
+ months.value.push({
169
+ date: new Array(dayjs(minDate).add(i, 'month').daysInMonth())
170
+ .fill(1)
171
+ .map((item, index) => {
172
+ // 日期,取值1-31
173
+ let day = index + 1
174
+ // 星期,0-6,0为周日
175
+ const week = dayjs(minDate).add(i, 'month').date(day).day()
176
+ const date = dayjs(minDate).add(i, 'month').date(day).format('YYYY-MM-DD')
177
+ let bottomInfo = ''
178
+ if (props.showLunar) {
179
+ // 将日期转为农历格式
180
+ const lunar = Calendar.solar2lunar(
181
+ dayjs(date).year(),
182
+ dayjs(date).month() + 1,
183
+ dayjs(date).date()
184
+ )
185
+ bottomInfo = lunar.IDayCn
186
+ }
187
+ let config = {
188
+ day,
189
+ week,
190
+ // 小于最小允许的日期,或者大于最大的日期,则设置为disabled状态
191
+ disabled:
192
+ dayjs(date).isBefore(dayjs(minDate).format('YYYY-MM-DD')) ||
193
+ dayjs(date).isAfter(dayjs(maxDate).format('YYYY-MM-DD')),
194
+ // 返回一个日期对象,供外部的formatter获取当前日期的年月日等信息,进行加工处理
195
+ date: new Date(date),
196
+ bottomInfo,
197
+ dot: false,
198
+ month: dayjs(minDate).add(i, 'month').month() + 1
199
+ }
200
+ const format = props.formatter || innerFormatter
201
+ return format(config as any)
202
+ }),
203
+ // 当前所属的月份
204
+ month: dayjs(minDate).add(i, 'month').month() + 1,
205
+ // 当前年份
206
+ year: dayjs(minDate).add(i, 'month').year()
207
+ })
208
+ }
209
+ }
210
+
211
+ watch(
212
+ () => selectedChange.value,
213
+ () => setMonth(),
214
+ { immediate: true }
215
+ )
216
+
217
+ watch(
218
+ () => props.show,
219
+ (newVal: boolean) => {
220
+ if (newVal) {
221
+ setMonth()
222
+ } else {
223
+ // 关闭时重置scrollIntoView,否则会出现二次打开日历,当前月份数据显示不正确。
224
+ // scrollIntoView需要有一个值变动过程,才会产生作用。
225
+ scrollIntoView.value = ''
226
+ }
227
+ },
228
+ { immediate: true }
229
+ )
230
+
231
+ const subtitle = computed(() => {
232
+ // 初始化时,this.months为空数组,所以需要特别判断处理
233
+ if (months.value.length) {
234
+ return formatTime(
235
+ `${months.value[monthIndex.value].year}-${months.value[monthIndex.value].month}`,
236
+ t('monthFormat')
237
+ )
238
+ } else {
239
+ return ''
240
+ }
241
+ })
242
+
243
+ const buttonDisabled = computed(() => {
244
+ // 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态
245
+ if (props.mode === 'range') {
246
+ return selected.value.length <= 1
247
+ } else {
248
+ return false
249
+ }
250
+ })
251
+
252
+ onMounted(() => {
253
+ // start.value = Date.now();
254
+ init()
255
+ })
256
+
257
+ // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
258
+ const setFormatter = (e: (value: string) => string) => {
259
+ innerFormatter = e
260
+ }
261
+
262
+ /**
263
+ * month组件内部选择日期后,通过事件通知给父组件
264
+ */
265
+ const monthSelected = (e: string[], scene = 'init') => {
266
+ selected.value = e
267
+ if (!props.showConfirm) {
268
+ // 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还
269
+ if (
270
+ props.mode === 'multiple' ||
271
+ props.mode === 'single' ||
272
+ (props.mode === 'range' && selected.value.length >= 2)
273
+ ) {
274
+ if (scene === 'init') {
275
+ return
276
+ }
277
+ if (scene === 'tap') {
278
+ emit('confirm', selected.value)
279
+ }
280
+ }
281
+ }
282
+ }
283
+
284
+ const init = () => {
285
+ // 校验maxDate,不能小于minDate。
286
+ if (
287
+ innerMaxDate.value &&
288
+ innerMinDate.value &&
289
+ new Date(innerMaxDate.value).getTime() < new Date(innerMinDate.value).getTime()
290
+ ) {
291
+ return error('maxDate不能小于minDate时间')
292
+ }
293
+ // 滚动区域的高度
294
+ listHeight.value = props.rowHeight * 5 + 30
295
+ setMonth()
296
+ }
297
+
298
+ const close = () => {
299
+ emit('close')
300
+ emit('update:show', false)
301
+ }
302
+
303
+ /**
304
+ * 点击确定按钮
305
+ * */
306
+ const confirm = () => {
307
+ if (!buttonDisabled.value) {
308
+ emit('confirm', selected.value)
309
+ emit('update:show', false)
310
+ }
311
+ }
312
+
313
+ /**
314
+ * 滚动到默认设置的月份
315
+ * @param selected 日期
316
+ * */
317
+ const scrollIntoDefaultMonth = (selected: string) => {
318
+ // 查询默认日期在可选列表的下标
319
+ const _index = months.value.findIndex(({ year, month }) => {
320
+ month = padZero(month)
321
+ return `${year}-${month}` === selected
322
+ })
323
+ if (_index !== -1) {
324
+ // #ifndef MP-WEIXIN
325
+ nextTick(() => {
326
+ scrollIntoView.value = `month-${_index}`
327
+ scrollIntoViewScroll.value = scrollIntoView.value
328
+ })
329
+ // #endif
330
+ // #ifdef MP-WEIXIN
331
+ scrollTop.value = months.value[_index].top || 0
332
+ // #endif
333
+ }
334
+ }
335
+ // scroll-view滚动监听
336
+ const onScroll = (event: any) => {
337
+ // 不允许小于0的滚动值,如果scroll-view到顶了,继续下拉,会出现负数值
338
+ const scrollTop = Math.max(0, event.detail.scrollTop)
339
+ // 将当前滚动条数值,除以滚动区域的高度,可以得出当前滚动到了哪一个月份的索引
340
+ for (let i = 0; i < months.value.length; i++) {
341
+ if (scrollTop >= (months.value[i].top || listHeight.value)) {
342
+ monthIndex.value = i
343
+ scrollIntoViewScroll.value = `month-${i}`
344
+ }
345
+ }
346
+ }
347
+ // 更新月份的top
348
+ const updateMonthTop = (topArr = []) => {
349
+ // 设置对应月份的top值,用于onScroll方法更新月份
350
+ topArr.map((item, index) => {
351
+ months.value[index].top = item
352
+ })
353
+ // 如果没有设置默认日期,则将当天日期设置为默认选中的日期
354
+ let selected = dayjs().format('YYYY-MM')
355
+
356
+ // 获取默认日期的下标
357
+ if (!props.defaultDate) {
358
+ scrollIntoDefaultMonth(selected)
359
+ return
360
+ }
361
+ // 单选模式,可以是字符串或数组,Date对象等
362
+ if (!isArray(props.defaultDate)) {
363
+ selected = dayjs(props.defaultDate).format('YYYY-MM')
364
+ } else {
365
+ selected = dayjs(props.defaultDate[0]).format('YYYY-MM')
366
+ }
367
+ scrollIntoDefaultMonth(selected)
368
+ }
369
+
370
+ defineExpose({
371
+ setFormatter
372
+ })
373
+ </script>
374
+
375
+ <style scoped lang="scss">
376
+ @import './index.scss';
377
+ </style>