日歷組件用于選擇日期或日期區(qū)間,2.4 版本開始支持此組件。
import Vue from 'vue';
import { Calendar } from 'vant';
Vue.use(Calendar);
下面演示了結(jié)合單元格來使用日歷組件的用法,日期選擇完成后會觸發(fā) confirm 事件。
<van-cell title="選擇單個日期" :value="date" @click="show = true" />
<van-calendar v-model="show" @confirm="onConfirm" />
export default {
data() {
return {
date: '',
show: false,
};
},
methods: {
formatDate(date) {
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(date) {
this.show = false;
this.date = this.formatDate(date);
},
},
};
設(shè)置 type 為 multiple 后可以選擇多個日期,此時 confirm 事件返回的 date 為數(shù)組結(jié)構(gòu),數(shù)組包含若干個選中的日期。
<van-cell title="選擇多個日期" :value="text" @click="show = true" />
<van-calendar v-model="show" type="multiple" @confirm="onConfirm" />
export default {
data() {
return {
text: '',
show: false,
};
},
methods: {
onConfirm(date) {
this.show = false;
this.text = `選擇了 ${date.length} 個日期`;
},
},
};
設(shè)置 type 為 range 后可以選擇日期區(qū)間,此時 confirm 事件返回的 date 為數(shù)組結(jié)構(gòu),數(shù)組第一項為開始時間,第二項為結(jié)束時間。
<van-cell title="選擇日期區(qū)間" :value="date" @click="show = true" />
<van-calendar v-model="show" type="range" @confirm="onConfirm" />
export default {
data() {
return {
date: '',
show: false,
};
},
methods: {
formatDate(date) {
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(date) {
const [start, end] = date;
this.show = false;
this.date = `${this.formatDate(start)} - ${this.formatDate(end)}`;
},
},
};
Tips: 默認(rèn)情況下,日期區(qū)間的起止時間不能為同一天,可以通過設(shè)置 allow-same-day 屬性來允許選擇同一天。
將 show-confirm 設(shè)置為 false 可以隱藏確認(rèn)按鈕,這種情況下選擇完成后會立即觸發(fā) confirm 事件。
<van-calendar v-model="show" :show-confirm="false" />
通過 color 屬性可以自定義日歷的顏色,對選中日期和底部按鈕生效。
<van-calendar v-model="show" color="#1989fa" />
通過 min-date 和 max-date 定義日歷的范圍。
<van-calendar v-model="show" :min-date="minDate" :max-date="maxDate" />
export default {
data() {
return {
show: false,
minDate: new Date(2010, 0, 1),
maxDate: new Date(2010, 0, 31),
};
},
};
通過 confirm-text 設(shè)置按鈕文字,通過 confirm-disabled-text 設(shè)置按鈕禁用時的文字。
<van-calendar
v-model="show"
type="range"
confirm-text="完成"
confirm-disabled-text="請選擇結(jié)束時間"
/>
通過傳入 formatter 函數(shù)來對日歷上每一格的內(nèi)容進(jìn)行格式化。
<van-calendar v-model="show" type="range" :formatter="formatter" />
export default {
methods: {
formatter(day) {
const month = day.date.getMonth() + 1;
const date = day.date.getDate();
if (month === 5) {
if (date === 1) {
day.topInfo = '勞動節(jié)';
} else if (date === 4) {
day.topInfo = '青年節(jié)';
} else if (date === 11) {
day.text = '今天';
}
}
if (day.type === 'start') {
day.bottomInfo = '入住';
} else if (day.type === 'end') {
day.bottomInfo = '離店';
}
return day;
},
},
};
通過 position 屬性自定義彈出層的彈出位置,可選值為 top、left、right。
<van-calendar v-model="show" :round="false" position="right" />
選擇日期區(qū)間時,可以通過 max-range 屬性來指定最多可選天數(shù),選擇的范圍超過最多可選天數(shù)時,會彈出相應(yīng)的提示文案。
<van-calendar type="range" :max-range="3" :style="{ height: '500px' }" />
通過 first-day-of-week 屬性設(shè)置一周從哪天開始。
<van-calendar first-day-of-week="1" />
將 poppable 設(shè)置為 false,日歷會直接展示在頁面內(nèi),而不是以彈層的形式出現(xiàn)。
<van-calendar
title="日歷"
:poppable="false"
:show-confirm="false"
:style="{ height: '500px' }"
/>
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
type v2.5.4
|
選擇類型:
single 表示選擇單個日期,
multiple 表示選擇多個日期,
range 表示選擇日期區(qū)間 |
string | single
|
title | 日歷標(biāo)題 | string | 日期選擇
|
color | 主題色,對底部按鈕和選中日期生效 | string | #ee0a24
|
min-date | 可選擇的最小日期 | Date | 當(dāng)前日期 |
max-date | 可選擇的最大日期 | Date | 當(dāng)前日期的六個月后 |
default-date | 默認(rèn)選中的日期,type 為 multiple 或 range 時為數(shù)組,傳入 null 表示默認(rèn)不選擇 |
Date | Date[] | null | 今天 |
row-height | 日期行高 | number | string | 64
|
formatter | 日期格式化函數(shù) | (day: Day) => Day | - |
poppable | 是否以彈層的形式展示日歷 | boolean | true
|
lazy-render v2.8.1
|
是否只渲染可視區(qū)域的內(nèi)容 | boolean | true
|
show-mark | 是否顯示月份背景水印 | boolean | true
|
show-title v2.5.5
|
是否展示日歷標(biāo)題 | boolean | true
|
show-subtitle v2.5.5
|
是否展示日歷副標(biāo)題(年月) | boolean | true
|
show-confirm | 是否展示確認(rèn)按鈕 | boolean | true
|
readonly v2.10.5
|
是否為只讀狀態(tài),只讀狀態(tài)下不能選擇日期 | boolean | false
|
confirm-text | 確認(rèn)按鈕的文字 | string | 確定
|
confirm-disabled-text | 確認(rèn)按鈕處于禁用狀態(tài)時的文字 | string | 確定
|
first-day-of-week v2.9.2
|
設(shè)置周起始日 | 0-6 | 0
|
當(dāng) Calendar 的 poppable 為 true 時,支持以下 props:
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
v-model | 是否顯示日歷彈窗 | boolean | false
|
position | 彈出位置,可選值為 top right left
|
string | bottom
|
round | 是否顯示圓角彈窗 | boolean | true
|
close-on-popstate | 是否在頁面回退時自動關(guān)閉 | boolean | true
|
close-on-click-overlay | 是否在點擊遮罩層后關(guān)閉 | boolean | true
|
safe-area-inset-bottom | 是否開啟底部安全區(qū)適配 | boolean | true
|
get-container | 指定掛載的節(jié)點,用法示例 | string | () => Element | - |
當(dāng) Calendar 的 type 為 range 時,支持以下 props:
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
max-range | 日期區(qū)間最多可選天數(shù) | number | string | 無限制 |
range-prompt | 范圍選擇超過最多可選天數(shù)時的提示文案 | string | 選擇天數(shù)不能超過 xx 天
|
allow-same-day v2.5.6
|
是否允許日期范圍的起止時間為同一天 | boolean | false
|
當(dāng) Calendar 的 type 為 multiple 時,支持以下 props:
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
max-range v2.7.2
|
日期最多可選天數(shù) | number | string | 無限制 |
range-prompt | 選擇超過最多可選天數(shù)時的提示文案 | string | 選擇天數(shù)不能超過 xx 天
|
日歷中的每個日期都對應(yīng)一個 Day 對象,通過formatter屬性可以自定義 Day 對象的內(nèi)容
鍵名 | 說明 | 類型 |
---|---|---|
date | 日期對應(yīng)的 Date 對象 | Date |
type | 日期類型,可選值為selected 、start 、middle 、end 、disabled
|
string |
text | 中間顯示的文字 | string |
topInfo | 上方的提示信息 | string |
bottomInfo | 下方的提示信息 | string |
className | 額外類名 | string |
事件名 | 說明 | 回調(diào)參數(shù) |
---|---|---|
select | 點擊并選中任意日期時觸發(fā) | value: Date | Date[] |
confirm | 日期選擇完成后觸發(fā),若show-confirm 為true ,則點擊確認(rèn)按鈕后觸發(fā) |
value: Date | Date[] |
open v2.5.2
|
打開彈出層時觸發(fā) | - |
close v2.5.2
|
關(guān)閉彈出層時觸發(fā) | - |
opened v2.5.2
|
打開彈出層且動畫結(jié)束后觸發(fā) | - |
closed v2.5.2
|
關(guān)閉彈出層且動畫結(jié)束后觸發(fā) | - |
unselect v2.7.2
|
當(dāng)日歷組件的 type 為 multiple 時,取消選中日期時觸發(fā) |
value: Date |
month-show v2.8.2
|
當(dāng)某個月份進(jìn)入可視區(qū)域時觸發(fā) | { date: Date, title: string } |
名稱 | 說明 | 參數(shù) |
---|---|---|
title | 自定義標(biāo)題 | - |
footer | 自定義底部區(qū)域內(nèi)容 | - |
top-info v2.12.22
|
自定義日期上方的提示信息 | day: Day |
bottom-info v2.12.22
|
自定義日期下方的提示信息 | day: Day |
通過 ref 可以獲取到 Calendar 實例并調(diào)用實例方法,詳見組件實例方法。
方法名 | 說明 | 參數(shù) | 返回值 |
---|---|---|---|
reset | 將選中的日期重置到指定日期,未傳參時會重置到默認(rèn)日期 | date?: Date | Date[] | - |
scrollToDate v2.12.2
|
滾動到某個日期 | date: Date | - |
組件提供了下列 Less 變量,可用于自定義樣式,使用方法請參考主題定制。
名稱 | 默認(rèn)值 | 描述 |
---|---|---|
@calendar-background-color | @white
|
- |
@calendar-popup-height | 80%
|
- |
@calendar-header-box-shadow | 0 2px 10px rgba(125, 126, 128, 0.16)
|
- |
@calendar-header-title-height | 44px
|
- |
@calendar-header-title-font-size | @font-size-lg
|
- |
@calendar-header-subtitle-font-size | @font-size-md
|
- |
@calendar-weekdays-height | 30px
|
- |
@calendar-weekdays-font-size | @font-size-sm
|
- |
@calendar-month-title-font-size | @font-size-md
|
- |
@calendar-month-mark-color | fade(@gray-2, 80%)
|
- |
@calendar-month-mark-font-size | 160px
|
- |
@calendar-day-height | 64px
|
- |
@calendar-day-font-size | @font-size-lg
|
- |
@calendar-range-edge-color | @white
|
- |
@calendar-range-edge-background-color | @red
|
- |
@calendar-range-middle-color | @red
|
- |
@calendar-range-middle-background-opacity | 0.1
|
- |
@calendar-selected-day-size | 54px
|
- |
@calendar-selected-day-color | @white
|
- |
@calendar-info-font-size | @font-size-xs
|
- |
@calendar-info-line-height | @line-height-xs
|
- |
@calendar-selected-day-background-color | @red
|
- |
@calendar-day-disabled-color | @gray-5
|
- |
@calendar-confirm-button-height | 36px
|
- |
@calendar-confirm-button-margin | 7px 0
|
- |
如果你遇到了在 iOS 上無法渲染組件的問題,請確認(rèn)在創(chuàng)建 Date 對象時沒有使用new Date('2020-01-01')這樣的寫法,iOS 不支持以中劃線分隔的日期格式,正確寫法是new Date('2020/01/01')。
對此問題的詳細(xì)解釋:stackoverflow。
更多建議: