From aefec84e14ee9fe3e9b74c4422dfc6568b077a3a Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 5 Sep 2020 15:22:40 +0800 Subject: [PATCH] feat(Calendar): allow default-date to be null (#7116) --- src/calendar/README.md | 2 +- src/calendar/README.zh-CN.md | 2 +- src/calendar/components/Month.js | 4 ++++ src/calendar/index.js | 33 +++++++++++++++++++++++++------- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/calendar/README.md b/src/calendar/README.md index 409de79b3..259b3b768 100644 --- a/src/calendar/README.md +++ b/src/calendar/README.md @@ -232,7 +232,7 @@ Set `poppable` to `false`, the calendar will be displayed directly on the page i | color | Color for the bottom button and selected date | _string_ | `#ee0a24` | | min-date | Min date | _Date_ | Today | | max-date | Max date | _Date_ | Six months after the today | -| default-date | Default selected date | _Date \| Date[]_ | Today | +| default-date | Default selected date | _Date \| Date[] \| null_ | Today | | row-height | Row height | _number \| string_ | `64` | | formatter | Day formatter | _(day: Day) => Day_ | - | | poppable | Whether to show the calendar inside a popup | _boolean_ | `true` | diff --git a/src/calendar/README.zh-CN.md b/src/calendar/README.zh-CN.md index 56b08b00b..20d8c0b17 100644 --- a/src/calendar/README.zh-CN.md +++ b/src/calendar/README.zh-CN.md @@ -234,7 +234,7 @@ export default { | color | 主题色,对底部按钮和选中日期生效 | _string_ | `#ee0a24` | | min-date | 可选择的最小日期 | _Date_ | 当前日期 | | max-date | 可选择的最大日期 | _Date_ | 当前日期的六个月后 | -| default-date | 默认选中的日期,`type`为`multiple`或`range`时为数组 | _Date \| Date[]_ | 今天 | +| default-date | 默认选中的日期,`type` 为 `multiple` 或 `range` 时为数组,传入 `null` 表示默认不选择 | _Date \| Date[] \| null_ | 今天 | | row-height | 日期行高 | _number \| string_ | `64` | | formatter | 日期格式化函数 | _(day: Day) => Day_ | - | | poppable | 是否以弹层的形式展示日历 | _boolean_ | `true` | diff --git a/src/calendar/components/Month.js b/src/calendar/components/Month.js index 8161ca41b..63a0b9abc 100644 --- a/src/calendar/components/Month.js +++ b/src/calendar/components/Month.js @@ -189,6 +189,10 @@ export default createComponent({ return 'disabled'; } + if (currentDate === null) { + return; + } + if (type === 'single') { return compareDay(day, currentDate) === 0 ? 'selected' : ''; } diff --git a/src/calendar/index.js b/src/calendar/index.js index e28d51cd6..360bbae7d 100644 --- a/src/calendar/index.js +++ b/src/calendar/index.js @@ -136,12 +136,13 @@ export default createComponent({ buttonDisabled() { const { type, currentDate } = this; - if (type === 'range') { - return !currentDate[0] || !currentDate[1]; - } - - if (type === 'multiple') { - return !currentDate.length; + if (currentDate) { + if (type === 'range') { + return !currentDate[0] || !currentDate[1]; + } + if (type === 'multiple') { + return !currentDate.length; + } } return !currentDate; @@ -198,6 +199,11 @@ export default createComponent({ scrollIntoView() { this.$nextTick(() => { const { currentDate } = this; + + if (!currentDate) { + return; + } + const targetDate = this.type === 'single' ? currentDate : currentDate[0]; const displayed = this.value || !this.poppable; @@ -222,6 +228,10 @@ export default createComponent({ getInitialDate() { const { type, minDate, maxDate, defaultDate } = this; + if (defaultDate === null) { + return defaultDate; + } + let defaultVal = new Date(); if (compareDay(defaultVal, minDate) === -1) { @@ -295,6 +305,11 @@ export default createComponent({ const { type, currentDate } = this; if (type === 'range') { + if (!currentDate) { + this.select([date, null]); + return; + } + const [startDay, endDay] = currentDate; if (startDay && !endDay) { @@ -311,8 +326,12 @@ export default createComponent({ this.select([date, null]); } } else if (type === 'multiple') { - let selectedIndex; + if (!currentDate) { + this.select([date]); + return; + } + let selectedIndex; const selected = this.currentDate.some((dateItem, index) => { const equal = compareDay(dateItem, date) === 0; if (equal) {