`
+ }
+ })
+
+ // Fetch data when date or view changes
+ const handleDataUpdate = (date, view) => {
+ const dateRange = getDateRangeForView(date, view)
+ if (dateRange) {
+ fetchPricingData(dateRange.startDate, dateRange.endDate, view).then(() => {
+ calendar.refresh()
+ })
+ }
+ }
+
+ // Listen for calendar date changes
+ myCalendarCustomizeCells.addEventListener('calendarDateChange.coreui.calendar', event => {
+ handleDataUpdate(event.date, event.view || 'days')
+ })
+
+ // Listen for calendar view changes
+ myCalendarCustomizeCells.addEventListener('calendarViewChange.coreui.calendar', event => {
+ if (event.source !== 'cellClick') {
+ handleDataUpdate(calendar._calendarDate || initialDate, event.view)
+ }
+ })
+ }
+
+ initCalendar()
+ }
+ // js-docs-end calendar-customize-cells
+
// -------------------------------
// Date Pickers
// -------------------------------
diff --git a/docs/content/components/calendar.md b/docs/content/components/calendar.md
index 26c4a4f7e..845075b36 100644
--- a/docs/content/components/calendar.md
+++ b/docs/content/components/calendar.md
@@ -61,6 +61,22 @@ Set the `data-coreui-selection-type` to `month` to enable selection of entire mo
{{< /example >}}
+### Quarters
+
+Set the `data-coreui-selection-type` property to `quarter` to enable quarters range selection.
+
+{{< example stackblitz_pro="true" >}}
+
+
+
+{{< /example >}}
+
### Years
Set the `data-coreui-selection-type` to `year` to enable years range selection.
@@ -231,6 +247,66 @@ Example of the Bootstrap Calendar component with Persian locale settings.
{{< /example >}}
+## Custom cell rendering
+
+The Calendar component provides powerful customization options through render functions that allow you to completely control how calendar cells are displayed. These render functions are particularly useful when you need to add custom content, styling, or data to calendar cells.
+
+### Render functions
+
+The component supports four render functions, one for each view type:
+
+- `renderDayCell(date, meta)` - Customize day cells in the days view
+- `renderMonthCell(date, meta)` - Customize month cells in the months view
+- `renderQuarterCell(date, meta)` - Customize quarter cells in the quarters view
+- `renderYearCell(date, meta)` - Customize year cells in the years view
+
+Each render function receives two parameters:
+
+1. `date` - A JavaScript Date object representing the cell's date
+2. `meta` - An object containing cell state information:
+ - `isDisabled` - Whether the cell is disabled
+ - `isInRange` - Whether the cell is within a selected range (range selection only)
+ - `isSelected` - Whether the cell is selected
+ - For `renderDayCell` only:
+ - `isInCurrentMonth` - Whether the day belongs to the current month
+ - `isToday` - Whether the day is today
+
+The render functions should return an HTML string that will be displayed inside the cell. The returned HTML is automatically sanitized to prevent XSS attacks.
+
+### Format options
+
+In addition to custom rendering, you can control the display format of calendar elements using format options:
+
+- `dayFormat` - Controls how day numbers are displayed (`'numeric'` or `'2-digit'`)
+- `monthFormat` - Controls how month names are displayed (`'long'`, `'narrow'`, `'short'`, `'numeric'`, or `'2-digit'`)
+- `yearFormat` - Controls how year numbers are displayed (`'numeric'` or `'2-digit'`)
+- `weekdayFormat` - Controls how weekday names are displayed (number for character length, or `'long'`, `'narrow'`, `'short'`)
+
+These format options use the JavaScript `Intl.DateTimeFormat` API and respect the `locale` setting.
+
+### Security considerations
+
+For security reasons, all HTML returned by render functions is automatically sanitized using the built-in sanitizer. You can:
+
+- Disable sanitization by setting `sanitize: false` (not recommended)
+- Customize allowed HTML tags and attributes using the `allowList` option
+- Provide your own sanitization function using the `sanitizeFn` option
+
+Note that `sanitize`, `sanitizeFn`, and `allowList` options cannot be supplied via data attributes for security reasons.
+
+### Pricing calendar with custom cells
+
+This example demonstrates advanced usage of custom cell rendering to display pricing data across different calendar views. It uses `renderDayCell` to show daily prices, `renderMonthCell` to display monthly price ranges, and `renderYearCell` to show annual price ranges. The data is fetched from an external API and cached for performance.
+
+{{< example stackblitz_pro="true" stackblitz_add_js="true">}}
+
+
+
+{{< /example >}}
+
+{{< js-docs name="calendar-customize-cells" file="docs/assets/js/partials/snippets.js" >}}
+
+
## Usage
{{< bootstrap-compatibility >}}
@@ -264,29 +340,43 @@ const calendarList = calendarElementList.map(calendarEl => {
{{< partial "js-data-attributes.md" >}}
{{< /markdown >}}
+{{< callout warning >}}
+Note that for security reasons the `sanitize`, `sanitizeFn`, and `allowList` options cannot be supplied using data attributes.
+{{< /callout >}}
+
{{< bs-table >}}
| Name | Type | Default | Description |
| --- | --- | --- | --- |
+| `allowList` | object | [Default value]({{< docsref "/getting-started/javascript#sanitizer" >}}) | Object which contains allowed attributes and tags. |
| `ariaNavNextMonthLabel` | string | `'Next month'` | A string that provides an accessible label for the button that navigates to the next month in the calendar. This label is read by screen readers to describe the action associated with the button. |
| `ariaNavNextYearLabel` | string | `'Next year'` | A string that provides an accessible label for the button that navigates to the next year in the calendar. This label is intended for screen readers to help users understand the button's functionality. |
| `ariaNavPrevMonthLabel` | string | `'Previous month'` | A string that provides an accessible label for the button that navigates to the previous month in the calendar. Screen readers will use this label to explain the purpose of the button. |
| `ariaNavPrevYearLabel` | string | `'Previous year'` | A string that provides an accessible label for the button that navigates to the previous year in the calendar. This label helps screen reader users understand the button's function. |
| `calendarDate` | date, number, string, null | `null` | Default date of the component. |
| `calendars` | number | `2` | The number of calendars that render on desktop devices. |
+| `dayFormat` | `'numeric'`, `'2-digit'` | `'numeric'` | Sets the format for days. Accepts a built-in format (`'numeric'` or `'2-digit'`) |
| `disabledDates` | array, function, null | `null` | Specify the list of dates that cannot be selected. |
| `endDate` | date, number, string, null | `null` | Initial selected to date (range). |
| `firstDayOfWeek` | number | `1` |
Sets the day of start week.
`0` - Sunday
`1` - Monday
`2` - Tuesday
`3` - Wednesday
`4` - Thursday
`5` - Friday
`6` - Saturday
|
| `locale` | string | `'default'` | Sets the default locale for components. If not set, it is inherited from the navigator.language. |
| `maxDate` | date, number, string, null | `null` | Max selectable date. |
| `minDate` | date, number, string, null | `null` | Min selectable date. |
+| `monthFormat` | `'long'`, `'narrow'`, `'short'`, `'numeric'`, `'2-digit'` | `'short'` | Sets the format for month names. Accepts built-in formats (`'long'`, `'narrow'`, `'short'`, `'numeric'`, `'2-digit'`). |
| `range` | boolean | `false` | Allow range selection |
+| `renderDayCell` | function, null | `null` | Custom function to render day cells. Receives `date` and `meta` object (with `isDisabled`, `isInCurrentMonth`, `isInRange`, `isSelected`, `isToday`) as parameters and should return HTML string. |
+| `renderMonthCell` | function, null | `null` | Custom function to render month cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `renderQuarterCell` | function, null | `null` | Custom function to render quarter cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `renderYearCell` | function, null | `null` | Custom function to render year cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `sanitize` | boolean | `true` | Enable or disable the sanitization. If activated `renderDayCell`, `renderMonthCell`, `renderQuarterCell`, and `renderYearCell` options will be sanitized. |
+| `sanitizeFn` | null, function | `null` | Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization. |
| `selectAdjacementDays` | boolean | `false` | Set whether days in adjacent months shown before or after the current month are selectable. This only applies if the `showAdjacementDays` option is set to true. |
-| `selectionType` | `'day'`, `'week'`, `'month'`, `'year'` | `day` | Specify the type of date selection as day, week, month, or year. |
+| `selectionType` | `'day'`, `'week'`, `'month'`, `'quarter'`, `'year'` | `day` | Specify the type of date selection as day, week, month, quarter, or year. |
| `showAdjacementDays` | boolean | `true` | Set whether to display dates in adjacent months (non-selectable) at the start and end of the current month. |
| `showWeekNumber` | boolean | `false` | Set whether to display week numbers in the calendar. |
| `startDate` | date, number, string, null | `null` | Initial selected date. |
-| `weekdayFormat` | number, 'long', 'narrow', 'short' | `2` | Set length or format of day name. |
+| `weekdayFormat` | number, `'long'`, `'narrow'`, `'short'` | `2` | Set length or format of day name. |
| `weekNumbersLabel` | string | `null` | Label displayed over week numbers in the calendar. |
+| `yearFormat` | `'numeric'`, `'2-digit'` | `'numeric'` | Sets the format for years. Accepts built-in formats (`'numeric'` or `'2-digit'`) |
{{< /bs-table >}}
### Methods
@@ -303,14 +393,15 @@ const calendarList = calendarElementList.map(calendarEl => {
### Events
{{< bs-table >}}
-| Method | Description |
-| --- | --- |
-| `calendarDateChange.coreui.calendar` | Callback fired when the calendar date changed. |
-| `calendarMouseleave.coreui.calendar` | Callback fired when the cursor leave the calendar. |
-| `cellHover.coreui.calendar` | Callback fired when the user hovers over the calendar cell. |
-| `endDateChange.coreui.calendar` | Callback fired when the end date changed. |
-| `selectEndChange.coreui.calendar` | Callback fired when the selection type changed. |
-| `startDateChange.coreui.calendar` | Callback fired when the start date changed. |
+| Method | Description | Event detail |
+| --- | --- | --- |
+| `calendarDateChange.coreui.calendar` | Fired when the calendar date changes. | `{ date: Date, view: 'days' \| 'months' \| 'quarters' \| 'years' }` |
+| `calendarViewChange.coreui.calendar` | Fired when the calendar view changes. | `{ view: 'days' \| 'months' \| 'quarters' \| 'years', source: 'cellClick' \| 'navigation' }` |
+| `calendarMouseleave.coreui.calendar` | Fired when the cursor leaves the calendar. | — |
+| `cellHover.coreui.calendar` | Fired when the user hovers over a calendar cell. | `{ date: Date \| null }` |
+| `endDateChange.coreui.calendar` | Fired when the end date changes. | `{ date: Date \| null }` |
+| `selectEndChange.coreui.calendar` | Fired when the selection mode changes. | `{ value: boolean }` |
+| `startDateChange.coreui.calendar` | Fired when the start date changes. | `{ date: Date \| null }` |
{{< /bs-table >}}
```js
diff --git a/docs/content/forms/date-picker.md b/docs/content/forms/date-picker.md
index a5d69ca13..314b65c45 100644
--- a/docs/content/forms/date-picker.md
+++ b/docs/content/forms/date-picker.md
@@ -109,6 +109,30 @@ Selecting whole month by adding the `data-coreui-selection-type="month"` attribu
{{< /example >}}
+### Quarters
+
+Selecting quarter by adding the `data-coreui-selection-type="quarter"` attribute.
+
+{{< example stackblitz_pro="true" >}}
+
+
+
+
+
+
+
+
+
+
+{{< /example >}}
+
### Years
Add the `data-coreui-selection-type="year"` attribute to allow pick years.
@@ -371,9 +395,14 @@ const datePickerList = datePickerElementList.map(datePickerEl => {
{{< partial "js-data-attributes.md" >}}
{{< /markdown >}}
+{{< callout warning >}}
+Note that for security reasons the `sanitize`, `sanitizeFn`, and `allowList` options cannot be supplied using data attributes.
+{{< /callout >}}
+
{{< bs-table >}}
| Name | Type | Default | Description |
| --- | --- | --- | --- |
+| `allowList` | object | [Default value]({{< docsref "/getting-started/javascript#sanitizer" >}}) | Object which contains allowed attributes and tags. |
| `ariaNavNextMonthLabel` | string | `'Next month'` | A string that provides an accessible label for the button that navigates to the next month in the calendar. This label is read by screen readers to describe the action associated with the button. |
| `ariaNavNextYearLabel` | string | `'Next year'` | A string that provides an accessible label for the button that navigates to the next year in the calendar. This label is intended for screen readers to help users understand the button's functionality. |
| `ariaNavPrevMonthLabel` | string | `'Previous month'` | A string that provides an accessible label for the button that navigates to the previous month in the calendar. Screen readers will use this label to explain the purpose of the button. |
@@ -386,6 +415,7 @@ const datePickerList = datePickerElementList.map(datePickerEl => {
| `confirmButtonClasses` | array, string | `['btn', 'btn-sm', 'btn-primary']` | CSS class names that will be added to the confirm button |
| `container` | string, element, false | `false` | Appends the dropdown to a specific element. Example: `container: 'body'`. |
| `date` | date, number, string, null | `null` | Default value of the component |
+| `dayFormat` | `'numeric'`, `'2-digit'` | `'numeric'` | Sets the format for days. Accepts a built-in format (`'numeric'` or `'2-digit'`) |
| `disabled` | boolean | `false` | Toggle the disabled state for the component. |
| `disabledDates` | array, function, null | `null` | Specify the list of dates that cannot be selected. |
| `firstDayOfWeek` | number | `1` |
Sets the day of start week.
`0` - Sunday
`1` - Monday
`2` - Tuesday
`3` - Wednesday
`4` - Thursday
`5` - Friday
`6` - Saturday
|
@@ -399,11 +429,18 @@ const datePickerList = datePickerElementList.map(datePickerEl => {
| `locale` | string | `'default'` | Sets the default locale for components. If not set, it is inherited from the navigator.language. |
| `maxDate` | date, number, string, null | `null` | Max selectable date. |
| `minDate` | date, number, string, null | `null` | Min selectable date. |
+| `monthFormat` | `'long'`, `'narrow'`, `'short'`, `'numeric'`, `'2-digit'` | `'short'` | Sets the format for month names. Accepts built-in formats (`'long'`, `'narrow'`, `'short'`, `'numeric'`, `'2-digit'`). |
| `name` | string, null | `null` | Set the name attribute for the input element. |
| `placeholder` | string | `'Select time'` | Specifies a short hint that is visible in the input. |
| `previewDateOnHover` | boolean | `true` | Enable live preview of dates in input field when hovering over calendar cells. |
+| `renderDayCell` | function, null | `null` | Custom function to render day cells. Receives `date` and `meta` object (with `isDisabled`, `isInCurrentMonth`, `isInRange`, `isSelected`, `isToday`) as parameters and should return HTML string. |
+| `renderMonthCell` | function, null | `null` | Custom function to render month cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `renderQuarterCell` | function, null | `null` | Custom function to render quarter cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `renderYearCell` | function, null | `null` | Custom function to render year cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `sanitize` | boolean | `true` | Enable or disable the sanitization. If activated `renderDayCell`, `renderMonthCell`, `renderQuarterCell`, and `renderYearCell` options will be sanitized. |
+| `sanitizeFn` | null, function | `null` | Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization. |
| `selectAdjacementDays` | boolean | `false` | Set whether days in adjacent months shown before or after the current month are selectable. This only applies if the `showAdjacementDays` option is set to true. |
-| `selectionType` | `'day'`, `'week'`, `'month'`, `'year'` | `day` | Specify the type of date selection as day, week, month, or year. |
+| `selectionType` | `'day'`, `'week'`, `'month'`, `'quarter'`, `'year'` | `day` | Specify the type of date selection as day, week, month, quarter, or year. |
| `showAdjacementDays` | boolean | `true` | Set whether to display dates in adjacent months (non-selectable) at the start and end of the current month. |
| `showWeekNumber` | boolean | `false` | Set whether to display week numbers in the calendar. |
| `size` | `'sm'`, `'lg'` | `null` | Size the component small or large. |
@@ -411,8 +448,9 @@ const datePickerList = datePickerElementList.map(datePickerEl => {
| `todayButton` | string | `'Today'` | Today button inner HTML |
| `todayButtonClasses` | array, string | `['btn', 'btn-sm', 'me-2']` | CSS class names that will be added to the today button |
| `valid` | boolean | `false` | Toggle the valid state for the component. |
-| `weekdayFormat` | number, 'long', 'narrow', 'short' | `2` | Set length or format of day name. |
+| `weekdayFormat` | number, `'long'`, `'narrow'`, `'short'` | `2` | Set length or format of day name. |
| `weekNumbersLabel` | string | `null` | Label displayed over week numbers in the calendar. |
+| `yearFormat` | `'numeric'`, `'2-digit'` | `'numeric'` | Sets the format for years. Accepts built-in formats (`'numeric'` or `'2-digit'`) |
{{< /bs-table >}}
### Methods
diff --git a/docs/content/forms/date-range-picker.md b/docs/content/forms/date-range-picker.md
index 3f67c1356..b02ea1e2c 100644
--- a/docs/content/forms/date-range-picker.md
+++ b/docs/content/forms/date-range-picker.md
@@ -137,6 +137,31 @@ Select range of months by adding the `data-coreui-selection-type="month"` attrib
{{< /example >}}
+### Quarters
+
+Select range of quartes by adding the `data-coreui-selection-type="quarter"` attribute.
+
+{{< example stackblitz_pro="true" >}}
+
+
+
+
+
+
+
+
+
+
+{{< /example >}}
+
### Years
Add the `data-coreui-selection-type="year"` attribute to allow a pick range of years.
@@ -422,9 +447,14 @@ const dateRangePickerList = dateRangePickerElementList.map(dateRangePickerEl =>
{{< partial "js-data-attributes.md" >}}
{{< /markdown >}}
+{{< callout warning >}}
+Note that for security reasons the `sanitize`, `sanitizeFn`, and `allowList` options cannot be supplied using data attributes.
+{{< /callout >}}
+
{{< bs-table >}}
| Name | Type | Default | Description |
| --- | --- | --- | --- |
+| `allowList` | object | [Default value]({{< docsref "/getting-started/javascript#sanitizer" >}}) | Object which contains allowed attributes and tags. |
| `ariaNavNextMonthLabel` | string | `'Next month'` | A string that provides an accessible label for the button that navigates to the next month in the calendar. This label is read by screen readers to describe the action associated with the button. |
| `ariaNavNextYearLabel` | string | `'Next year'` | A string that provides an accessible label for the button that navigates to the next year in the calendar. This label is intended for screen readers to help users understand the button's functionality. |
| `ariaNavPrevMonthLabel` | string | `'Previous month'` | A string that provides an accessible label for the button that navigates to the previous month in the calendar. Screen readers will use this label to explain the purpose of the button. |
@@ -437,6 +467,7 @@ const dateRangePickerList = dateRangePickerElementList.map(dateRangePickerEl =>
| `confirmButton` | boolean, string | `'OK'` | Confirm button inner HTML |
| `confirmButtonClasses` | array, string | `['btn', 'btn-sm', 'btn-primary']` | CSS class names that will be added to the confirm button |
| `container` | string, element, false | `false` | Appends the dropdown to a specific element. Example: `container: 'body'`. |
+| `dayFormat` | `'numeric'`, `'2-digit'` | `'numeric'` | Sets the format for days. Accepts a built-in format (`'numeric'` or `'2-digit'`) |
| `disabled` | boolean | `false` | Toggle the disabled state for the component. |
| `disabledDates` | array, function, null | `null` | Specify the list of dates that cannot be selected. |
| `endDate` | date, number, string, null | `null` | Initial selected to date (range). |
@@ -452,12 +483,19 @@ const dateRangePickerList = dateRangePickerElementList.map(dateRangePickerEl =>
| `locale` | string | `'default'` | Sets the default locale for components. If not set, it is inherited from the navigator.language. |
| `maxDate` | date, number, string, null | `null` | Max selectable date. |
| `minDate` | date, number, string, null | `null` | Min selectable date. |
+| `monthFormat` | `'long'`, `'narrow'`, `'short'`, `'numeric'`, `'2-digit'` | `'short'` | Sets the format for month names. Accepts built-in formats (`'long'`, `'narrow'`, `'short'`, `'numeric'`, `'2-digit'`). |
| `placeholder` | string | `['Start date', 'End date']` | Specifies a short hint that is visible in the input. |
| `previewDateOnHover` | boolean | `true` | Enable live preview of dates in input fields when hovering over calendar cells. |
| `ranges` | object | `{}` | Predefined date ranges the user can select from. |
| `rangesButtonsClasses` | array, string | `['btn', 'btn-ghost-secondary']` | CSS class names that will be added to ranges buttons |
+| `renderDayCell` | function, null | `null` | Custom function to render day cells. Receives `date` and `meta` object (with `isDisabled`, `isInCurrentMonth`, `isInRange`, `isSelected`, `isToday`) as parameters and should return HTML string. |
+| `renderMonthCell` | function, null | `null` | Custom function to render month cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `renderQuarterCell` | function, null | `null` | Custom function to render quarter cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `renderYearCell` | function, null | `null` | Custom function to render year cells. Receives `date` and `meta` object (with `isDisabled`, `isInRange`, `isSelected`) as parameters and should return HTML string. |
+| `sanitize` | boolean | `true` | Enable or disable the sanitization. If activated `renderDayCell`, `renderMonthCell`, `renderQuarterCell`, and `renderYearCell` options will be sanitized. |
+| `sanitizeFn` | null, function | `null` | Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization. |
| `selectAdjacementDays` | boolean | `false` | Set whether days in adjacent months shown before or after the current month are selectable. This only applies if the `showAdjacementDays` option is set to true. |
-| `selectionType` | `'day'`, `'week'`, `'month'`, `'year'` | `day` | Specify the type of date selection as day, week, month, or year. |
+| `selectionType` | `'day'`, `'week'`, `'month'`, `'quarter'`, `'year'` | `day` | Specify the type of date selection as day, week, month, quarter, or year. |
| `separator` | boolean | `true` | Toggle visibility or set the content of the inputs separator. |
| `showAdjacementDays` | boolean | `true` | Set whether to display dates in adjacent months (non-selectable) at the start and end of the current month. |
| `showWeekNumber` | boolean | `false` | Set whether to display week numbers in the calendar. |
@@ -468,8 +506,9 @@ const dateRangePickerList = dateRangePickerElementList.map(dateRangePickerEl =>
| `todayButton` | string | `'Today'` | Today button inner HTML |
| `todayButtonClasses` | array, string | `['btn', 'btn-sm', 'me-2']` | CSS class names that will be added to the today button |
| `valid` | boolean | `false` | Toggle the valid state for the component. |
-| `weekdayFormat` | number, 'long', 'narrow', 'short' | `2` | Set length or format of day name. |
+| `weekdayFormat` | number, `'long'`, `'narrow'`, `'short'` | `2` | Set length or format of day name. |
| `weekNumbersLabel` | string | `null` | Label displayed over week numbers in the calendar. |
+| `yearFormat` | `'numeric'`, `'2-digit'` | `'numeric'` | Sets the format for years. Accepts built-in formats (`'numeric'` or `'2-digit'`) |
{{< /bs-table >}}
### Methods
diff --git a/js/src/calendar.js b/js/src/calendar.js
index 3f34b5a9d..e5e989636 100644
--- a/js/src/calendar.js
+++ b/js/src/calendar.js
@@ -10,6 +10,7 @@ import BaseComponent from './base-component.js'
import EventHandler from './dom/event-handler.js'
import Manipulator from './dom/manipulator.js'
import SelectorEngine from './dom/selector-engine.js'
+import { DefaultAllowlist, sanitizeHtml } from './util/sanitizer.js'
import { defineJQueryPlugin } from './util/index.js'
import {
convertToDateObject,
@@ -26,6 +27,9 @@ import {
isMonthDisabled,
isMonthInRange,
isMonthSelected,
+ isQuarterDisabled,
+ isQuarterInRange,
+ isQuarterSelected,
isToday,
isYearDisabled,
isYearInRange,
@@ -41,6 +45,7 @@ const NAME = 'calendar'
const DATA_KEY = 'coreui.calendar'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
+const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn'])
const ARROW_UP_KEY = 'ArrowUp'
const ARROW_RIGHT_KEY = 'ArrowRight'
@@ -52,6 +57,7 @@ const SPACE_KEY = 'Space'
const EVENT_BLUR = `blur${EVENT_KEY}`
const EVENT_CALENDAR_DATE_CHANGE = `calendarDateChange${EVENT_KEY}`
const EVENT_CALENDAR_MOUSE_LEAVE = `calendarMouseleave${EVENT_KEY}`
+const EVENT_CALENDAR_VIEW_CHANGE = `calendarViewChange${EVENT_KEY}`
const EVENT_CELL_HOVER = `cellHover${EVENT_KEY}`
const EVENT_END_DATE_CHANGE = `endDateChange${EVENT_KEY}`
const EVENT_FOCUS = `focus${EVENT_KEY}`
@@ -83,19 +89,28 @@ const SELECTOR_CALENDAR_ROW_CLICKABLE = `${SELECTOR_CALENDAR_ROW}[tabindex="0"]`
const SELECTOR_DATA_TOGGLE = '[data-coreui-toggle="calendar"]'
const Default = {
+ allowList: DefaultAllowlist,
ariaNavNextMonthLabel: 'Next month',
ariaNavNextYearLabel: 'Next year',
ariaNavPrevMonthLabel: 'Previous month',
ariaNavPrevYearLabel: 'Previous year',
calendarDate: null,
calendars: 1,
+ dayFormat: 'numeric',
disabledDates: null,
endDate: null,
firstDayOfWeek: 1,
locale: 'default',
maxDate: null,
minDate: null,
+ monthFormat: 'short',
range: false,
+ renderDayCell: null,
+ renderMonthCell: null,
+ renderQuarterCell: null,
+ renderYearCell: null,
+ sanitize: true,
+ sanitizeFn: null,
selectAdjacementDays: false,
selectEndDate: false,
selectionType: 'day',
@@ -103,23 +118,33 @@ const Default = {
showWeekNumber: false,
startDate: null,
weekdayFormat: 2,
- weekNumbersLabel: null
+ weekNumbersLabel: null,
+ yearFormat: 'numeric'
}
const DefaultType = {
+ allowList: 'object',
ariaNavNextMonthLabel: 'string',
ariaNavNextYearLabel: 'string',
ariaNavPrevMonthLabel: 'string',
ariaNavPrevYearLabel: 'string',
calendarDate: '(date|number|string|null)',
calendars: 'number',
+ dayFormat: 'string',
disabledDates: '(array|date|function|null)',
endDate: '(date|number|string|null)',
firstDayOfWeek: 'number',
locale: 'string',
maxDate: '(date|number|string|null)',
minDate: '(date|number|string|null)',
+ monthFormat: 'string',
range: 'boolean',
+ renderDayCell: '(function|null)',
+ renderMonthCell: '(function|null)',
+ renderQuarterCell: '(function|null)',
+ renderYearCell: '(function|null)',
+ sanitize: 'boolean',
+ sanitizeFn: '(null|function)',
selectAdjacementDays: 'boolean',
selectEndDate: 'boolean',
selectionType: 'string',
@@ -127,7 +152,8 @@ const DefaultType = {
showWeekNumber: 'boolean',
startDate: '(date|number|string|null)',
weekdayFormat: '(number|string)',
- weekNumbersLabel: '(string|null)'
+ weekNumbersLabel: '(string|null)',
+ yearFormat: 'string'
}
/**
@@ -169,6 +195,12 @@ class Calendar extends BaseComponent {
this._createCalendar()
}
+ refresh() {
+ // Clear the current calendar content
+ this._element.innerHTML = ''
+ this._createCalendar()
+ }
+
// Private
_focusOnFirstAvailableCell() {
const cell = SelectorEngine.findOne(SELECTOR_CALENDAR_CELL_CLICKABLE, this._element)
@@ -188,7 +220,7 @@ class Calendar extends BaseComponent {
}
_handleCalendarClick(event) {
- const target = event.target.classList.contains(CLASS_NAME_CALENDAR_CELL_INNER) ? event.target.parentElement : event.target
+ const target = event.target.closest(SELECTOR_CALENDAR_CELL)
const date = this._getDate(target)
const cloneDate = new Date(date)
const index = Manipulator.getDataAttribute(target.closest(SELECTOR_CALENDAR), 'calendar-index')
@@ -198,15 +230,15 @@ class Calendar extends BaseComponent {
}
if (this._view === 'months' && this._config.selectionType !== 'month') {
- this._setCalendarDate(index ? new Date(cloneDate.setMonth(cloneDate.getMonth() - index)) : date)
- this._view = 'days'
+ this._setCalendarDate(index ? new Date(cloneDate.setMonth(cloneDate.getMonth() - index)) : date, 'days')
+ this._setCalendarView('days', 'cellClick')
this._updateCalendar(this._focusOnFirstAvailableCell.bind(this))
return
}
if (this._view === 'years' && this._config.selectionType !== 'year') {
- this._setCalendarDate(index ? new Date(cloneDate.setFullYear(cloneDate.getFullYear() - index)) : date)
- this._view = 'months'
+ this._setCalendarDate(index ? new Date(cloneDate.setFullYear(cloneDate.getFullYear() - index)) : date, 'months')
+ this._setCalendarView(this._config.selectionType === 'quarter' ? 'quarters' : 'months', 'cellClick')
this._updateCalendar(this._focusOnFirstAvailableCell.bind(this))
return
}
@@ -310,7 +342,7 @@ class Calendar extends BaseComponent {
this._modifyCalendarDate(0, event.key === ARROW_RIGHT_KEY || event.key === ARROW_DOWN_KEY ? 1 : -1, callback.bind(this, event.key))
}
- if (this._view === 'months') {
+ if (this._view === 'months' || this._view === 'quarters') {
this._modifyCalendarDate(event.key === ARROW_RIGHT_KEY || event.key === ARROW_DOWN_KEY ? 1 : -1, 0, callback.bind(this, event.key))
}
@@ -336,7 +368,7 @@ class Calendar extends BaseComponent {
}
_handleCalendarMouseEnter(event) {
- const target = event.target.classList.contains(CLASS_NAME_CALENDAR_CELL_INNER) ? event.target.parentElement : event.target
+ const target = event.target.closest(SELECTOR_CALENDAR_CELL)
const date = this._getDate(target)
if (isDateDisabled(date, this._minDate, this._maxDate, this._config.disabledDates)) {
@@ -426,11 +458,11 @@ class Calendar extends BaseComponent {
[SELECTOR_BTN_NEXT]: () => this._modifyCalendarDate(0, 1),
[SELECTOR_BTN_DOUBLE_NEXT]: () => this._modifyCalendarDate(this._view === 'years' ? 10 : 1),
[SELECTOR_BTN_MONTH]: () => {
- this._view = 'months'
+ this._setCalendarView('months', 'navigation')
this._updateCalendar()
},
[SELECTOR_BTN_YEAR]: () => {
- this._view = 'years'
+ this._setCalendarView('years', 'navigation')
this._updateCalendar()
}
}
@@ -451,11 +483,21 @@ class Calendar extends BaseComponent {
}
}
- _setCalendarDate(date) {
+ _setCalendarDate(date, view = this._view) {
this._calendarDate = date
EventHandler.trigger(this._element, EVENT_CALENDAR_DATE_CHANGE, {
- date
+ date,
+ view
+ })
+ }
+
+ _setCalendarView(view, source) {
+ this._view = view
+
+ EventHandler.trigger(this._element, EVENT_CALENDAR_VIEW_CHANGE, {
+ view,
+ source
})
}
@@ -474,11 +516,10 @@ class Calendar extends BaseComponent {
this._calendarDate = d
- if (this._view === 'days') {
- EventHandler.trigger(this._element, EVENT_CALENDAR_DATE_CHANGE, {
- date: d
- })
- }
+ EventHandler.trigger(this._element, EVENT_CALENDAR_DATE_CHANGE, {
+ date: d,
+ view: this._view
+ })
this._updateCalendar(callback)
}
@@ -590,7 +631,7 @@ class Calendar extends BaseComponent {
`
const monthDetails = getMonthDetails(year, month, this._config.firstDayOfWeek)
- const listOfMonths = createGroupsInArray(getMonthsNames(this._config.locale), 4)
+ const listOfMonths = createGroupsInArray(getMonthsNames(this._config.locale, this._config.monthFormat), 4)
const listOfYears = createGroupsInArray(getYears(calendarDate.getFullYear()), 4)
const weekDays = monthDetails[0].days
@@ -641,8 +682,8 @@ class Calendar extends BaseComponent {
${cellAttributes.ariaSelected ? 'aria-selected="true"' : ''}
data-coreui-date="${date}"
>
-