CxJS

Calendar

import { Calendar } from 'cx/widgets'; Copied

The Calendar widget displays a standalone calendar for date selection. It’s also used internally by DateField.

export default <Calendar value={bind(m.date, "2024-06-15")} showTodayButton />
June
2024
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Custom Day Styling

Use dayData to customize individual days with styles, classes, or disable specific dates. Keys are created using date.toDateString().

<Calendar
  value={m.date}
  minValue="2024-06-10"
  maxValue="2024-06-20"
  refDate="2024-06-08"
  dayData={{
    [new Date("2024-06-11").toDateString()]: {
      style: "color: red; font-weight: bold",
    },
    [new Date("2024-06-12").toDateString()]: {
      disabled: true,
    },
    [new Date("2024-06-13").toDateString()]: {
      style: "outline: 2px solid #3b82f6",
    },
  }}
/>
June
2024
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Keyboard Navigation

The Calendar is fully keyboard accessible. Click on the calendar to focus it, then use the following keys:

KeyAction
← →Move cursor by one day
↑ ↓Move cursor by one week
Page UpPrevious month
Page DownNext month
HomeFirst day of current month
EndLast day of current month
EnterSelect the focused date
Mouse wheelScroll through months

Configuration

Core Properties

PropertyTypeDefaultDescription
valuestringnullSelected date. Accepts Date objects or ISO strings
refDatestringReference date for initial view when no value is selected
disabledbooleanfalseDisables the calendar
highlightTodaybooleantrueHighlights today’s date
showTodayButtonbooleanfalseShows a “Today” button for quick selection
todayButtonTextstring"Today"Text for the today button
startWithMondaybooleanfalseStart weeks with Monday instead of Sunday

Date Constraints

PropertyTypeDefaultDescription
minValuestringMinimum selectable date
minExclusivebooleanfalseIf true, the minimum date itself is not selectable
maxValuestringMaximum selectable date
maxExclusivebooleanfalseIf true, the maximum date itself is not selectable
disabledDaysOfWeeknumber[]Days of week that cannot be selected. Sunday is 0, Saturday is 6
dayDataobjectMap of dates to custom day info (style, className, disabled)

Validation

PropertyTypeDefaultDescription
minValueErrorTextstring"Select a date not before {0:d}."Error message when date is before minimum
minExclusiveErrorTextstring"Select a date after {0:d}."Error message when date equals exclusive minimum
maxValueErrorTextstring"Select a date not after {0:d}."Error message when date is after maximum
maxExclusiveErrorTextstring"Select a date before {0:d}."Error message when date equals exclusive maximum

Behavior

PropertyTypeDefaultDescription
autoFocusbooleanfalseAutomatically focuses the calendar on mount
focusablebooleantrueWhether the calendar can receive focus
partialbooleanfalsePreserves time component when updating date
encodingfunctionCustom function to encode Date objects before storing. Default: toISOString()

Callbacks

PropertyTypeDescription
onSelectfunction(e, instance, date) => void - Called when a date is selected
onBeforeSelectfunction(e, instance, date) => boolean - Return false to prevent selection
onBlurfunction(e, instance) => void - Called when calendar loses focus
onFocusOutfunction(instance) => void - Called when focus leaves the calendar