If the calendar needs a field, take the ready CdsDateInput — it assembles the very same thing out of a field, a menu and this calendar, adding manual input and time. For picking the time alone there is CdsTimePicker.
Anatomy
The calendar consists of an optional header with the summary of the choice, the month and year navigation, the grid of days and the actions zone. The header is turned on by header, the "Today" button — by today-btn, and the actions are added into the default slot.
Variations
Value and format
The chosen date arrives in v-model as a string in the mask format — DD.MM.YYYY by default. A repeated click on the selected day clears the selection; no-unset forbids such a reset, and hide-sibling-month hides the days of the neighboring months.
Header
header turns on the header with the summary of the choice: the title shows the date or the number of the chosen days, the description — the year. The texts are replaced by the title and description props or by the slots of the same name. today-btn adds a button that brings you back to the current day.
A date range
range switches the calendar to picking a period: the first click fixes the start (the range-start event), the second one — the end (range-end), and the value arrives as a { from, to } object. Together with multiple several periods can be picked.
Several dates
multiple allows marking individual dates — the model becomes an array of strings.
Events
events marks dates with dots: it accepts an array of dates in the YYYY/MM/DD format or a predicate function. The color of a dot is set by events-color — as a string or as a function of the date, when there are several types of events.
Available dates
allowed-dates limits the choice: an array of dates or a function that receives a YYYY/MM/DD date and returns whether it can be picked. The rest of the days are turned off.
Navigation bounds
min-year-month and max-year-month (the YYYY/MM format) do not let you go beyond the range — the arrows at the bound are turned off. Watch the moves between months through the navigation event.
Starting view
view opens the calendar right at the list of months (Months) or years (Years) — handy for dates far from the current one. default-year-month sets the starting month when the model is empty.
Localization
locale accepts a locale name (ru | en) or your own object of translations; without the prop the locale of the application is used. first-day-of-week sets the first day of the week — for en it is usually Sunday.
States
disabled turns the calendar off and dims it, readonly keeps the usual look but blocks both the selection and the navigation.
Guidelines
Scenarios
A field with a date
Most often the calendar lives not on the page but in a menu next to a text field. CdsDateInput exists for that — it is already assembled out of a field, a menu and the calendar, it parses the manual input and accepts all the calendar props directly. Build the control yourself only if you need a non-standard scheme: out of CdsTextInput, a CdsMenu with activator="parent" and the calendar — that way all the props and slots of the field and the menu stay available to you. In that case add the input mask yourself: we recommend the lightweight (2.5 KB) Maska library.
A reporting period
The default slot adds actions under the calendar. Here the chosen range is a draft: it gets into the report only after the "Apply" button, while "Reset" returns the calendar to its initial state.
A delivery date
The restrictions work together: allowed-dates leaves only the weekdays, and min-year-month and max-year-month do not let you go beyond the nearest two months — the unavailable options do not even have to be explained.
Date and time
The calendar does not pick the time — put a CdsTimePicker next to it with a shared model and the same DD.MM.YYYY HH:mm mask: every component changes its own part of the string. In a field the very same thing is turned on by a single prop — with-time on CdsDateInput.
API
Props
| Name | Type | Default | Description |
|---|---|---|---|
allowedDates | TCalendarAllowedDates | undefined | — | Array of dates available in the calendar. When a function is used, it receives the date in the YYYY/MM/DD format and must return true or false. |
defaultYearMonth | string | undefined | — | Displayed year/month in the YYYY/MM format. The current year/month by default when v-model is not set. |
description | string | undefined | — | Description of the calendar. Shown in the calendar header. |
disabled | boolean | null | undefined | null | Disabled state |
emitImmediately | boolean | undefined | — | Emit the update:modelValue event right when the month or the year changes. Not available with multiple and range. |
events | TCalendarEvents | undefined | — | List of events shown in the calendar. Format: YYYY/MM/DD. |
eventsColor | TCalendarEventsColor | undefined | 'default' | Color of the events in the calendar. |
firstDayOfWeek | string | number | undefined | 1 | First day of the week (Monday by default). |
header | boolean | undefined | — | Show the calendar header |
hideSiblingMonth | boolean | undefined | — | Hide the days of the neighboring months in the calendar. |
locale | string | TDateLocale | undefined | — | Locale for the dates. A locale name ('ru' | 'en') or a TDateLocale object. The current locale from useLocale is taken by default, otherwise ru. |
mask | string | undefined | 'DD.MM.YYYY' | Date/time format |
maxYearMonth | string | undefined | — | Forbid navigating the calendar past the given maximum date in the YYYY/MM format. |
minYearMonth | string | undefined | — | Forbid navigating the calendar before the given minimum date in the YYYY/MM format. |
modelValue | string | TDatetimeValueRange | (string | TDatetimeValueRange)[] | null | undefined | null | Value of the model |
multiple | boolean | undefined | — | Allows selecting several dates. The v-model must be an array. |
noUnset | boolean | undefined | — | Forbid resetting the date in the calendar. |
range | boolean | undefined | — | Allows selecting a range of dates. The v-model must be an array. |
readonly | boolean | null | undefined | null | Read-only state |
theme | string | undefined | — | Component theme |
title | string | undefined | — | Title of the calendar. Shown in the calendar header. |
todayBtn | boolean | undefined | — | Show the "Today" button in the calendar header |
view | "Calendar" | "Years" | "Months" | undefined | 'Calendar' | Default calendar view. Possible values: 'Calendar', 'Years', 'Months'. |
Events
| Name | Type | Description |
|---|---|---|
navigation | [payload: { year: number | undefined; month: number | undefined; }] | Navigation to another month or year (also fires when the model is changed from outside). |
rangeEnd | [payload: { from: Partial<IRawDate>; to: Partial<IRawDate>; }] | End of a date range selection. |
rangeStart | [value: Partial<IRawDate>] | Start of a date range selection. |
update:modelValue | [value: string | TDatetimeValueRange | (string | TDatetimeValueRange)[] | null, reason: string, details: Partial<IRawDate>] | The value changed (v-model). Passes the value, the reason of the change and the details of the date. |
Slots
| Name | Type | Description |
|---|---|---|
default | any | Additional buttons under the calendar. |
description | { year: string | number | undefined; } | Description of the title. |
title | { date: string; } | Title of the calendar. |
Exposed
| Name | Type | Description |
|---|---|---|
offsetCalendar | (type: "year" | "month", descending: number) => void | Moves the calendar one step forward or backward by months or by years. |
setCalendarTo | (year: number, month: number) => void | Scrolls the calendar to the given year and month without changing the selected value. |
setEditingRange | (from: IRawDate, to: IRawDate) => void | Sets the highlighted date range in range mode without confirming the selection. |
setToday | () => void | Selects today's date and scrolls the calendar to the current month. |
setView | (viewMode: "Calendar" | "Years" | "Months") => void | Switches the calendar view: days, months or years. |