Calendar
Examples
Single date
By default, clicking on a date selects a single date.
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
Selected: Monday, September 9, 2024
Supports v-model
with value type being the native Date
.
<template>
<article>
<veui-calendar v-model="date"/>
<p>Selected: {{ readableDate }}</p>
</article>
</template>
<script>
import { Calendar } from 'veui'
export default {
components: {
'veui-calendar': Calendar
},
data () {
return {
date: new Date()
}
},
computed: {
readableDate () {
return this.date.toLocaleDateString(this.$i18n.locale, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
})
}
}
}
</script>
Multiple dates and ranges
When the multiple
prop is set, multiple dates can be selected. When the range
prop is set, a date range can be selected.
Multiple dates
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
Date ranges
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
Supports v-model
, with value type being Array<Date>
when selecting multiple dates, being [Date, Date]
when selecting a date range.
<template>
<article>
<section class="col">
<h4>Multiple dates</h4>
<veui-calendar
v-model="dates"
multiple
/>
<section>Selected: {{ readableDates }}</section>
</section>
<section class="col">
<h4>Date ranges</h4>
<veui-calendar
v-model="range"
range
/>
<section>Selected: {{ readableRange }}</section>
</section>
</article>
</template>
<script>
import { Calendar } from 'veui'
export default {
components: {
'veui-calendar': Calendar
},
data () {
return {
dates: null,
range: null
}
},
computed: {
readableDates () {
if (!this.dates || !this.dates.length) {
return 'Nothing.'
}
return this.toReadable(this.dates).join(', ')
},
readableRange () {
if (!this.range) {
return 'Nothing.'
}
return this.toReadable(this.range).join(' to ')
}
},
methods: {
toReadable (dates) {
return dates.map(date => date.toLocaleDateString(this.$i18n.locale))
}
}
}
</script>
<style lang="less" scoped>
article {
overflow: hidden;
}
.col {
float: left;
width: 340px;
margin-right: 20px;
}
h4 {
margin-top: 0;
}
</style>
Multiple date ranges
When the multiple
and range
properties are set simultaneously, multiple date ranges can be selected. When the panel
prop is set, the number of calendar panels can be specified. The strategy for merging the two selected time periods is to select the time period if no date has been selected, otherwise the time period is deselected.
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
Supports v-model
, with value type being Array<[Date, Date]>
when selecting multiple date ranges.
<template>
<article>
<veui-calendar
v-model="ranges"
range
multiple
:panel="2"
/>
<section>Selected: {{ readableRanges }}</section>
</article>
</template>
<script>
import { Calendar } from 'veui'
export default {
components: {
'veui-calendar': Calendar
},
data () {
return {
ranges: null
}
},
computed: {
readableRanges () {
if (!this.ranges || this.ranges.length === 0) {
return 'Nothing.'
}
return this.ranges
.map(range => this.toReadable(range).join(' to '))
.join(', ')
}
},
methods: {
toReadable (dates) {
return dates.map(date => date.toLocaleDateString(this.$i18n.locale))
}
}
}
</script>
Calendar types
Set the type
prop to specify the type of calendar: year, month, or day.
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
<template>
<article>
<veui-radio-group
v-model="type"
class="group"
:items="types"
/>
<veui-calendar :type="type"/>
</article>
</template>
<script>
import { Calendar, RadioGroup } from 'veui'
export default {
components: {
'veui-calendar': Calendar,
'veui-radio-group': RadioGroup
},
data () {
return {
date: new Date(),
type: 'date',
types: [
{ label: 'date', value: 'date' },
{ label: 'month', value: 'month' },
{ label: 'year', value: 'year' }
]
}
}
}
</script>
<style lang="less" scoped>
.group {
margin-bottom: 20px;
}
</style>
Show Dates from Other Months
Set the fill-month
prop to control whether to display dates from other months.
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
<template>
<article>
<section>
<veui-checkbox v-model="isFill">
Fill month
</veui-checkbox>
</section>
<veui-calendar :fill-month="isFill"/>
</article>
</template>
<script>
import { Calendar, Checkbox } from 'veui'
export default {
components: {
'veui-calendar': Calendar,
'veui-checkbox': Checkbox
},
data () {
return {
date: new Date(),
isFill: true
}
}
}
</script>
<style lang="less" scoped>
section {
margin-bottom: 20px;
}
</style>
Disabled dates
Set the disabled-date
prop to customize whether a specific date is disabled.
只允许选未来的日期
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
<template>
<article>
<section>
<h4>只允许选未来的日期</h4>
<veui-calendar :disabled-date="disabledDate"/>
</section>
</article>
</template>
<script>
import { Calendar } from 'veui'
export default {
components: {
'veui-calendar': Calendar
},
created () {
this.now = new Date()
this.now.setHours(0, 0, 0, 0)
},
methods: {
disabledDate (date) {
return this.now.getTime() > date.getTime()
}
}
}
</script>
Custom date style
Set date-class
to customize the class
of specified dates.
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
<template>
<article>
<section>
<veui-calendar
class="demo-date-class"
:date-class="dayClass"
/>
</section>
</article>
</template>
<script>
import { Calendar } from 'veui'
export default {
components: {
'veui-calendar': Calendar
},
methods: {
dayClass (date) {
return [0, 6].includes(date.getDay()) ? 'weekend' : undefined
}
}
}
</script>
<style lang="less" scoped>
.demo-date-class {
::v-deep .weekend button {
color: rebeccapurple;
}
}
</style>
Custom date content
Use the before
slot to customize the content before the calendar.
Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|---|---|---|---|---|---|
<template>
<article>
<veui-calendar
v-model="date"
class="default"
>
<template #before>
<veui-stack
class="before"
gap="xxs"
justify="center"
>
<veui-button
ui="s ghost"
@click="pickDay(-1)"
>
Yesterday
</veui-button>
<veui-button
ui="s ghost"
@click="pickDay(0)"
>
Today
</veui-button>
<veui-button
ui="s ghost"
@click="pickDay(1)"
>
Tomorrow
</veui-button>
</veui-stack>
</template>
<template #date="d">
<u v-if="d.date === 1">{{ d.date }}</u>
<template v-else>
{{ d.date }}
</template>
</template>
</veui-calendar>
</article>
</template>
<script>
import { Calendar, Button, Stack } from 'veui'
export default {
components: {
'veui-calendar': Calendar,
'veui-button': Button,
'veui-stack': Stack
},
data () {
return {
date: new Date()
}
},
methods: {
pickDay (d) {
let date = new Date()
date.setDate(date.getDate() + d)
this.date = date
}
}
}
</script>
<style scoped>
.before {
padding: 8px;
border-bottom: 1px solid #e2e6f0;
}
</style>
API
Props
Name | Type | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
type | string | 'date' | The type of the calendar, available values are 'date' , 'month' , and 'year' , corresponding to the date/month/year view. | |||||||||||||||
multiple | boolean | false | Whether multiple dates (ranges) can be selected. | |||||||||||||||
range | boolean | false | Whether to select a date range. | |||||||||||||||
selected | Date | Array | - |
The value of the selected date (range), depending on the values of the
| |||||||||||||||
panel | number | 1 | The number of calendar panels. | |||||||||||||||
today | Date | new Date() | The date of "today". | |||||||||||||||
week-start | number | calendar.weekStart | The start of the week. Can be configured globally here. | |||||||||||||||
fill-month | boolean | true | Whether to display dates outside of the current month in the current month panel when there is only one panel. | |||||||||||||||
date-class | string | Array | Object | function | {} | Custom HTML class for specific dates. When passed a non-function value, the data format is all Vue-supported class expressions; when passed a function, the signature is function(Date): string | Array<string>|Object<string, boolean> , and the return value format is also all Vue-supported class expressions. | |||||||||||||||
disabled-date | function(Date, Date=): boolean | () => false | Used to customize whether specified dates are disabled. The first parameter is the date that needs to be checked for disablement. If in the process of selecting a range and a date has already been selected, it will be passed in as the second parameter. | |||||||||||||||
disabled | boolean | false | Whether the calendar is disabled. | |||||||||||||||
readonly | boolean | false | Whether the calendar is read-only. |
Slots
Name | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
before | An area above the panel that can be customized. | ||||||||||||
after | An area below the panel that can be customized. | ||||||||||||
date | An area within a single day cell that can be used to customize the content for each day. Default content: the
|
Events
Name | Description | ||||||
---|---|---|---|---|---|---|---|
select |
Triggered after selection changes, called back with | ||||||
selectstart | Triggered when selecting a date range, called back with (picking: Date) , representing the selected start date. | ||||||
selectprogress | Triggered when selecting a date range, and the end date marked by mouse or keyboard interaction changes after selecting the start date. Called back with
| ||||||
viewchange | Triggered when the displayed month of the panel changes. Called back with (month: Object<{year: number, month: number, index: number}>) , representing the current year and month (month is 0 for January). |
Configs
Key | Type | Default | Description |
---|---|---|---|
calendar.weekStart | number | 1 | The day of the week when the week starts. Monday to Sunday corresponds to 1 to 7 . |
Icons
Name | Description |
---|---|
backward | Previous year. |
prev | Previous page. |
next | Next page. |
forward | Next year. |
expand | Expand the drop-down menu. |