feat(calendar): synchronize the vant calendar component, and add the attribute of custom week starting from the day of the week (#4211) (#4223)

* feat(calendar): synchronize the vant calendar component, and add the attribute of custom week starting from the day of the week

* feat(calendar): supplement to the description document of calendar components

* feat(calendar): remove the validator of firstDayOfWeek attribute
This commit is contained in:
表弟 2021-05-19 18:12:49 +08:00 committed by GitHub
parent 0d08ec1489
commit 7979dee465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 58 additions and 11 deletions

View File

@ -13,7 +13,7 @@ Page({
customConfirm: [], customConfirm: [],
customRange: null, customRange: null,
customDayText: [], customDayText: [],
customPosition: null customPosition: null,
}, },
type: 'single', type: 'single',
round: true, round: true,
@ -32,7 +32,8 @@ Page({
tiledMinDate: new Date(2012, 0, 10).getTime(), tiledMinDate: new Date(2012, 0, 10).getTime(),
tiledMaxDate: new Date(2012, 2, 20).getTime(), tiledMaxDate: new Date(2012, 2, 20).getTime(),
confirmText: undefined, confirmText: undefined,
confirmDisabledText: undefined confirmDisabledText: undefined,
firstDayOfWeek: 0,
}, },
onConfirm(event) { onConfirm(event) {
@ -40,7 +41,9 @@ Page({
this.setData({ showCalendar: false }); this.setData({ showCalendar: false });
this.setData({ this.setData({
[`date.${this.data.id}`]: Array.isArray(event.detail) ? event.detail.map(date => date.valueOf()) : event.detail.valueOf() [`date.${this.data.id}`]: Array.isArray(event.detail)
? event.detail.map((date) => date.valueOf())
: event.detail.valueOf(),
}); });
}, },
@ -83,7 +86,7 @@ Page({
formatter: null, formatter: null,
showConfirm: true, showConfirm: true,
confirmText: '确定', confirmText: '确定',
confirmDisabledText: null confirmDisabledText: null,
}); });
}, },
@ -93,7 +96,7 @@ Page({
const data = { const data = {
id, id,
type, type,
showCalendar: true showCalendar: true,
}; };
switch (id) { switch (id) {
@ -150,5 +153,5 @@ Page({
} }
return day; return day;
} },
}); });

View File

@ -112,6 +112,7 @@
show-confirm="{{ false }}" show-confirm="{{ false }}"
min-date="{{ tiledMinDate }}" min-date="{{ tiledMinDate }}"
max-date="{{ tiledMaxDate }}" max-date="{{ tiledMaxDate }}"
first-day-of-week="{{ firstDayOfWeek }}"
class="tiled-calendar" class="tiled-calendar"
/> />
</demo-block> </demo-block>
@ -129,6 +130,7 @@
show-confirm="{{ showConfirm }}" show-confirm="{{ showConfirm }}"
confirm-text="{{ confirmText }}" confirm-text="{{ confirmText }}"
confirm-disabled-text="{{ confirmDisabledText }}" confirm-disabled-text="{{ confirmDisabledText }}"
first-day-of-week="{{ firstDayOfWeek }}"
bind:confirm="onConfirm" bind:confirm="onConfirm"
bind:select="onSelect" bind:select="onSelect"
bind:unselect="onUnselect" bind:unselect="onUnselect"

View File

@ -234,6 +234,14 @@ Page({
<van-calendar type="range" max-range="{{ 3 }}" /> <van-calendar type="range" max-range="{{ 3 }}" />
``` ```
### 自定义周起始日
通过 `first-day-of-week` 属性设置一周从哪天开始。
```html
<van-calendar first-day-of-week="{{ 1 }}" />
```
### 平铺展示 ### 平铺展示
`poppable`设置为`false`,日历会直接展示在页面内,而不是以弹层的形式出现。 `poppable`设置为`false`,日历会直接展示在页面内,而不是以弹层的形式出现。
@ -274,6 +282,7 @@ Page({
| show-confirm | 是否展示确认按钮 | _boolean_ | `true` | | show-confirm | 是否展示确认按钮 | _boolean_ | `true` |
| confirm-text | 确认按钮的文字 | _string_ | `确定` | | confirm-text | 确认按钮的文字 | _string_ | `确定` |
| confirm-disabled-text | 确认按钮处于禁用状态时的文字 | _string_ | `确定` | | confirm-disabled-text | 确认按钮处于禁用状态时的文字 | _string_ | `确定` |
| first-day-of-week | 设置周起始日 | _0~6_ | `0` |
### Poppable Props ### Poppable Props

View File

@ -4,6 +4,7 @@
showTitle="{{ showTitle }}" showTitle="{{ showTitle }}"
subtitle="{{ subtitle }}" subtitle="{{ subtitle }}"
showSubtitle="{{ showSubtitle }}" showSubtitle="{{ showSubtitle }}"
firstDayOfWeek="{{ firstDayOfWeek }}"
> >
<slot name="title" slot="title"></slot> <slot name="title" slot="title"></slot>
</header> </header>
@ -31,6 +32,7 @@
showSubtitle="{{ showSubtitle }}" showSubtitle="{{ showSubtitle }}"
allowSameDay="{{ allowSameDay }}" allowSameDay="{{ allowSameDay }}"
showMonthTitle="{{ index !== 0 || !showSubtitle }}" showMonthTitle="{{ index !== 0 || !showSubtitle }}"
firstDayOfWeek="{{ firstDayOfWeek }}"
bind:click="onClickDay" bind:click="onClickDay"
/> />
</scroll-view> </scroll-view>

View File

@ -9,11 +9,31 @@ VantComponent({
subtitle: String, subtitle: String,
showTitle: Boolean, showTitle: Boolean,
showSubtitle: Boolean, showSubtitle: Boolean,
firstDayOfWeek: {
type: Number,
observer: 'initWeekDay',
},
}, },
data: { data: {
weekdays: ['日', '一', '二', '三', '四', '五', '六'], weekdays: [] as Array<string>,
}, },
methods: {}, created() {
this.initWeekDay();
},
methods: {
initWeekDay() {
const defaultWeeks = ['日', '一', '二', '三', '四', '五', '六'];
const firstDayOfWeek = this.data.firstDayOfWeek || 0;
this.setData({
weekdays: [
...defaultWeeks.slice(firstDayOfWeek, 7),
...defaultWeeks.slice(0, firstDayOfWeek),
],
});
},
},
}); });

View File

@ -42,6 +42,10 @@ VantComponent({
type: null, type: null,
observer: 'setDays', observer: 'setDays',
}, },
firstDayOfWeek: {
type: Number,
observer: 'setDays',
},
allowSameDay: Boolean, allowSameDay: Boolean,
showSubtitle: Boolean, showSubtitle: Boolean,
showMonthTitle: Boolean, showMonthTitle: Boolean,

View File

@ -14,7 +14,7 @@
<view <view
wx:for="{{ days }}" wx:for="{{ days }}"
wx:key="index" wx:key="index"
style="{{ computed.getDayStyle(item.type, index, date, rowHeight, color) }}" style="{{ computed.getDayStyle(item.type, index, date, rowHeight, color, firstDayOfWeek) }}"
class="{{ utils.bem('calendar__day', [item.type]) }} {{ item.className }}" class="{{ utils.bem('calendar__day', [item.type]) }} {{ item.className }}"
data-index="{{ index }}" data-index="{{ index }}"
bindtap="onClick" bindtap="onClick"

View File

@ -7,9 +7,12 @@ function getMark(date) {
var ROW_HEIGHT = 64; var ROW_HEIGHT = 64;
function getDayStyle(type, index, date, rowHeight, color) { function getDayStyle(type, index, date, rowHeight, color, firstDayOfWeek) {
var style = []; var style = [];
var offset = getDate(date).getDay(); var current = getDate(date).getDay() || 7;
var offset = current < firstDayOfWeek ? (7 - firstDayOfWeek + current) :
current === 7 && firstDayOfWeek === 0 ? 0 :
(current - firstDayOfWeek);
if (index === 0) { if (index === 0) {
style.push(['margin-left', (100 * offset) / 7 + '%']); style.push(['margin-left', (100 * offset) / 7 + '%']);

View File

@ -110,6 +110,10 @@ VantComponent({
type: null, type: null,
value: null, value: null,
}, },
firstDayOfWeek: {
type: Number,
value: 0,
},
}, },
data: { data: {