Skip to content

Commit 7a457c3

Browse files
committed
feat(DateTimePicker): support showWeek props
1 parent d9f064b commit 7a457c3

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/date-time-picker/_example/year-month-date/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,41 @@ Page({
3434
},
3535

3636
formatter(item, index) {
37-
let label = item.label.slice(0, -1);
3837
if (index === 1) {
39-
label = calendarMonth[Number(label) - 1];
38+
const label = item.label.slice(0, -1);
39+
return {
40+
value: item.value,
41+
label: calendarMonth[Number(label) - 1],
42+
};
4043
}
4144
if (index === 2) {
42-
const suffixes = {
45+
const [dateValue, weekValue] = item.label.split(' ');
46+
const dateSuffixes = {
4347
1: 'st',
4448
2: 'nd',
4549
3: 'rd',
4650
};
47-
label = `${label}${suffixes[label] || 'th'}`;
51+
const weekMapCNtoEN = {
52+
"周一": "Mon.",
53+
"周二": "Tues.",
54+
"周三": "Wed.",
55+
"周四": "Thurs.",
56+
"周五": "Fri.",
57+
"周六": "Sat.",
58+
"周日": "Sun."
59+
};
60+
const label = dateValue.slice(0, -1);
61+
62+
return {
63+
value: item.value,
64+
label: `${label}${dateSuffixes[label] || 'th'} ${weekMapCNtoEN[weekValue]}`
65+
}
4866
}
67+
4968
return {
5069
value: item.value,
51-
label,
52-
};
70+
label: item.label.slice(0, -1)
71+
}
5372
},
5473
},
5574
showPicker(e) {

src/date-time-picker/_example/year-month-date/index.wxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<t-date-time-picker
1414
auto-close
1515
title="选择日期"
16+
showWeek
1617
visible="{{dateVisible}}"
1718
mode="date"
1819
default-value="{{date}}"

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import dayjsLocaleMap from './locale/dayjs';
1212
* https://dayjs.fenxianglu.cn/category/plugin.html#localedata
1313
*/
1414
dayjs.extend(localeData);
15-
// 设置 demo 的默认语言为 zh
1615
dayjs.locale('zh-cn');
1716

1817
// const defaultLocale = dayjsLocaleMap.default.key;
@@ -104,6 +103,25 @@ export default class DateTimePicker extends SuperComponent {
104103
});
105104
},
106105

106+
getDaysOfWeekInMonth(date: Dayjs) {
107+
const { locale, dayjsLocale } = this.data;
108+
const startOfMonth = date.startOf('month');
109+
const endOfMonth = date.endOf('month');
110+
const daysOfWeek = [];
111+
112+
for (let i = 1; i <= endOfMonth.diff(startOfMonth, 'days') + 1; i += 1) {
113+
const currentDate = startOfMonth.add(i, 'days').locale(dayjsLocale); // 显式设置每个日期对象的 locale
114+
const dayName = currentDate.format('ddd');
115+
116+
daysOfWeek.push({
117+
value: `${i}`,
118+
label: `${i + locale.date} ${dayName}`,
119+
});
120+
}
121+
122+
return daysOfWeek;
123+
},
124+
107125
getParseDate(): Dayjs {
108126
const { value, defaultValue } = this.properties;
109127
const minDate = this.getMinDate();
@@ -178,27 +196,30 @@ export default class DateTimePicker extends SuperComponent {
178196
const { fullModes, filter } = this.data;
179197

180198
const columnOptions = [];
181-
fullModes?.forEach((mode) => {
199+
fullModes?.forEach((mode: string) => {
182200
const columnOption = this.getOptionByType(mode);
183201
if (typeof filter === 'function') {
184202
columnOptions.push(filter(mode, columnOption));
185203
} else {
186204
columnOptions.push(columnOption);
187205
}
188206
});
189-
190207
return columnOptions;
191208
},
192209

193-
getOptionByType(type) {
194-
const { locale, steps } = this.data;
210+
getOptionByType(type: string) {
211+
const { locale, steps, showWeek } = this.data;
195212
const options: ColumnItemValue[] = [];
196213

197214
const minEdge = this.getOptionEdge('min', type);
198215
const maxEdge = this.getOptionEdge('max', type);
199216
const step = steps?.[type] ?? 1;
200217
const dayjsMonthsShort = dayjs().locale(this.data.dayjsLocale).localeData().monthsShort();
201218

219+
if (type === 'date' && showWeek) {
220+
return this.getDaysOfWeekInMonth(this.date);
221+
}
222+
202223
for (let i = minEdge; i <= maxEdge; i += step) {
203224
options.push({
204225
value: `${i}`,

0 commit comments

Comments
 (0)