Skip to content

Commit cdc4163

Browse files
fix(DateTimePicker): 修复在 showWeek 模式下日列表未正确排除 start、end、且 steps 步长不生效的 Bug (#3861)
* fix(DateTimePicker): 修复在 showWeek 模式下日列表未正确排除 start 和 end 的 Bug * refactor(date-time-picker): 优化日期周显示逻辑,使用统一边界和步长控制 重构 getDaysOfWeekInMonth 方法,使用 getOptionEdge 获取边界值并支持步长配置,简化循环逻辑,移除不必要的日期范围检查
1 parent c7a62dc commit cdc4163

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/date-time-picker/date-time-picker.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,20 @@ export default class DateTimePicker extends SuperComponent {
101101
});
102102
},
103103

104-
getDaysOfWeekInMonth(date: Dayjs): Array<{ value: string; label: string }> {
105-
const { locale, dayjsLocale } = this.data;
104+
getDaysOfWeekInMonth(date: Dayjs, type: string): Array<{ value: string; label: string }> {
105+
const { locale, steps, dayjsLocale } = this.data;
106106
const startOfMonth = date.startOf('month');
107-
const endOfMonth = date.endOf('month');
107+
const minEdge = this.getOptionEdge('min', type);
108+
const maxEdge = this.getOptionEdge('max', type);
109+
const step = steps?.[type] ?? 1;
108110
const daysOfWeek = [];
109111

110-
for (let i = 0; i <= endOfMonth.diff(startOfMonth, 'days'); i += 1) {
111-
const currentDate = startOfMonth.add(i, 'days').locale(dayjsLocale);
112+
for (let i = minEdge; i <= maxEdge; i += step) {
113+
const currentDate = startOfMonth.date(i).locale(dayjsLocale);
112114
const dayName = currentDate.format('ddd');
113-
114115
daysOfWeek.push({
115-
value: `${i + 1}`,
116-
label: `${i + 1}${locale.date || ''} ${dayName}`,
116+
value: `${i}`,
117+
label: `${i}${locale.date || ''} ${dayName}`,
117118
});
118119
}
119120

@@ -217,7 +218,7 @@ export default class DateTimePicker extends SuperComponent {
217218
const dayjsMonthsShort = dayjs().locale(this.data.dayjsLocale).localeData().monthsShort();
218219

219220
if (type === 'date' && showWeek) {
220-
return this.getDaysOfWeekInMonth(this.date);
221+
return this.getDaysOfWeekInMonth(this.date, type);
221222
}
222223

223224
for (let i = minEdge; i <= maxEdge; i += step) {

0 commit comments

Comments
 (0)