Calendar also known as DatePicker, is a form component to work with dates.
Wk | Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|---|
52 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
1 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
2 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
3 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
4 | 29 | 30 | 31 | 1 | 2 | 3 | 4 |
import { Calendar } from 'primereact/calendar';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/calendar/calendar.min.js"></script>
Calendar is used a controlled input component with value and onChange properties.
<Calendar value={date} onChange={(e) => setDate(e.value)}></Calendar>
Calendar is displayed in a popup by default whereas inline property needs to be enabled for inline mode.
<Calendar inline value={date} onChange={(e) => setDate(e.value)}></Calendar>
viewDate defines the date whose month and year are used to display the calendar. By default calendar uses value to render the view and falls back to today's date when value is not defined. In case you'd like to display a different month/year use viewDate. The usage of this property can either be controlled or uncontrolled. In controlled mode, onViewDateChange is required to manage the viewDate whereas in uncontrolled mode, viewDate is used only once in initial rendering and ignored in updates. If you'd like to change the displayed month/year programmatically, use the onViewDateChange in controlled mode.
<Calendar value={dates} onChange={(e) => setDate(e.value)} viewDate={viewDate} onViewDateChange={(e) => setViewDate(e.value)}></Calendar>
Calendar offers "single" (default), "multiple" and "range" selection types controlled via the selectionMode property. In single, mode the bound value should be an array whereas in multiple case an array is required. Third alternative is the range mode that allows selecting a range based on an array of two values where first value is the start date and second value is the end date. Note: Time picker is supported in range mode but not in multiple mode.
<Calendar selectionMode="multiple" value={dates} onChange={(e) => setDates(e.value)}></Calendar>
Default date format is "mm/dd/yy" which can be customized using the dateFormat property.
<Calendar dateFormat="dd/mm/yy" value={date} onChange={(e) => setDate(e.value)}></Calendar>
Following options can be a part of the format.
Translations for the calendar are defined with the Locale API.
TimePicker is enabled with showTime property and hourFormat is used to select the 24 (default) or 12 hour mode. Optionally enabling timeOnlydisplays a calendare with time controls only.
<Calendar showTime hourFormat="12" value={date1} onChange={(e) => setDate1(e.value)}></Calendar>
<Calendar showTime hourFormat="24" value={date2} onChange={(e) => setDate2(e.value)}></Calendar>
<Calendar timeOnly showTime hourFormat="24" value={date3} onChange={(e) => setDate3(e.value)}></Calendar>
To disable entering dates manually, set readOnlyInput to true and to restrict selectable date ranges use minDate and maxDate options.
<Calendar minDate={minDate} maxDate={maxDate} readOnlyInput value={date} onChange={(e) => setDate(e.value)}></Calendar>
Specific dates or days can be disabled as well, in this case set readOnlyInput to true and to restrict selectable dates use disabledDates and/or disabledDays options. disabledDatesproperty should be an array of dates and disabledDays should be an array of disabled weekdays.
<Calendar disabledDates={invalidDates} disabledDays={[0,6]} readOnlyInput value={date} onChange={(e) => setDate(e.value)}></Calendar>
Button bar displays today and clear buttons and activated using the showButtonBar property.
<Calendar value={date} onChange={(e) => setDate(e.value)} showButtonBar></Calendar>
Displaying multiple months is enabled by setting numberOfMonths property to a value greater than 1.
<Calendar value={date} onChange={(e) => setDate(e.value)} numberOfMonths={3}></Calendar>
Date cell contents can be templated using the dateTemplate property that returns the content of a cell. This is a handy feature to highlight specific dates. Note that the variable passed to the template is not a date instance but a metadata object to represent a Date with "day", "month", "year", "otherMonth", "today" and "selectable" properties to represent the date. Example below changes the styling of dates between 10 and 15.
<Calendar value={date} onChange={(e) => setDate(e.value)} dateTemplate={dateTemplate} />
dateTemplate(date) {
if (date.day > 10 && date.day < 15) {
return (
<div style={{backgroundColor: '#1dcbb3', color: '#ffffff', fontWeight: 'bold', borderRadius: '50%', width: '2em', height: '2em', lineHeight: '2em', padding: 0}}>{date.day}</div>
);
}
else {
return date.day;
}
}
The headerTemplate and footerTemplate properties are available to place custom content at these sections.
<Calendar value={date} onChange={(e) => setDate(e.value)} headerTemplate={() => <Button label="Custom Button" />} footerTemplate={() => <div>Footer Content</div>} />
Navigators are used to quickly change the displayed month and year using dropdowns. Enabling monthNavigator displays a dropdown with months whereas yearNavigator along with yearRange displays available years. Format of the yearRange is "startYear:endYear".
<Calendar value={date} onChange={(e) => setDate(e.value)} monthNavigator yearNavigator yearRange="2010:2030" />
The monthNavigatorTemplate and yearNavigatorTemplate properties are available to place custom content at the navigator sections.
<Calendar value={date} onChange={(e) => setDate(e.value)} monthNavigator yearNavigator yearRange="2010:2030" monthNavigatorTemplate={this.monthNavigatorTemplate} />
const monthNavigatorTemplate = (options) => {
// options.onChange: Change event for the default element.
// options.className: Style class of the default element.
// options.value: Selected value.
// options.names: Names of options.
// options.options: Options as SelectItem API.
// options.element: Default element created by the component.
// options.props: component props.
}
Month picker is used to select month and year only without the date, set view mode as "month" to activate month picker.
<Calendar value={date} onChange={(e) => setDate(e.value)} view="month" dateFormat="mm/yy" yearNavigator yearRange="2010:2030"/>
Touch UI mode displays the calendar overlay at the center of the screen as optimized for touch devices.
<Calendar value={date} onChange={(e) => setDate(e.value)} touchUI />
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
name | string | null | Name of the input element. |
value | any | null | Value of the component. |
visible | boolean | false | Specifies the visibility of the overlay. |
viewDate | date | null | Date instance whose month and year are used to display the calendar. |
style | string | null | Inline style of the element. |
className | string | null | Style class of the element. |
inline | boolean | false | When enabled, displays the calendar as inline instead of an overlay. |
inputId | string | null | Identifier of the input element. |
inputStyle | object | null | Inline style of the input element. |
inputClassName | string | null | Style class of the input element. |
inputMode | string | null | HTML inputmode attribute of input. |
required | boolean | false | When present, it specifies that an input field must be filled out before submitting the form. |
readOnlyInput | boolean | null | When specified, prevents entering the date manually with keyboard. |
keepInvalid | boolean | false | Keep invalid value when input blur. |
mask | string | null | Mask pattern for input element. |
disabled | boolean | false | When specified, disables the component. |
tabIndex | number | null | Index of the element in tabbing order. |
placeholder | string | null | Placeholder text for the input. |
showIcon | boolean | false | When enabled, displays a button with icon next to input. |
icon | string | pi pi-calendar | Icon of the calendar button. |
iconPos | string | right | Icon position of the calendar button. Valid values is 'left' and 'right'. |
showOnFocus | boolean | true | When disabled, datepicker will not be visible with input focus. |
numberOfMonths | number | 1 | Number of months to display. |
view | string | date | Type of view to display, valid values are "date" for datepicker and "month" for month picker. |
touchUI | boolean | false | When enabled, calendar overlay is displayed as optimized for touch devices. |
showTime | boolean | false | Whether to display timepicker. |
timeOnly | boolean | false | Whether to display timepicker only. |
showSeconds | boolean | false | Whether to show the seconds in time picker. |
showMillisec | boolean | false | Whether to show the milliseconds in time picker. |
hourFormat | string | 24 | Specifies 12 or 24 hour format. |
locale | string | en | Used to display the values of the locale object defined in the Locale API |
stepHour | number | 1 | Hours to change per step. |
stepMinute | number | 1 | Minutes to change per step. |
stepSecond | number | 1 | Seconds to change per step. |
stepMillisec | number | 1 | Milliseconds to change per step. |
shortYearCutoff | string | +10 | The cutoff year for determining the century for a date. |
hideOnDateTimeSelect | boolean | false | Whether to hide the overlay on date selection when showTime is enabled. |
showWeek | boolean | false | When enabled, calendar will show week numbers. |
dateFormat | string | mm/dd/yy | Format of the date. |
panelStyle | object | null | Inline style of the datetimepicker panel. |
panelClassName | string | null | Style class of the datetimepicker panel. |
monthNavigator | boolean | false | Whether the month should be rendered as a dropdown instead of text. Deprecated: Navigator is always on |
yearNavigator | boolean | false | Whether the year should be rendered as a dropdown instead of text. Deprecated: Navigator is always on. |
disabledDates | array; | null | Array with dates to disable. |
disabledDays | array | null | Array with disabled weekday numbers. |
minDate | Date | null | The minimum selectable date. |
maxDate | Date | null | The maximum selectable date. |
maxDateCount | number | null | Maximum number of selectable dates in multiple mode. |
showMinMaxRange | boolean | false | Whether to allow navigation past min/max dates. |
showOtherMonths | boolean | true | Whether to display dates in other months (non-selectable) at the start or end of the current month. To make these days selectable use the selectOtherMonths option. |
selectOtherMonths | boolean | false | Whether days in other months shown before or after the current month are selectable. This only applies if the showOtherMonths option is set to true. |
showButtonBar | boolean | false | Whether to display today and clear buttons at the footer |
todayButtonClassName | string | p-secondary-button | Style class of the today button. |
clearButtonClassName | string | p-secondary-button | Style class of the clear button. |
baseZIndex | number | 0 | Base zIndex value to use in layering. |
autoZIndex | boolean | true | Whether to automatically manage layering. |
appendTo | DOM element | string | document.body | DOM element instance where the overlay panel should be mounted. Valid values are any DOM Element and 'self'. The self value is used to render a component where it is located. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
ariaLabelledBy | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
dateTemplate | function | null | Function that gets a date information and returns the cell content in datepicker. |
decadeTempate | function | null | Function that gets a navigator information and returns the decade selections in the panel. |
monthNavigatorTemplate | function | null | Function that gets a navigator information and returns the navigator element in header. |
yearNavigatorTemplate | function | null | Function that gets a navigator information and returns the novigator in header. |
formatDateTime | function | null | Function for overriding default behavior that formats a Date to the string representation. |
parseDateTime | function | null | Function for overriding default behavior that parses text into the Date. |
transitionOptions | object | null | The properties of CSSTransition can be customized, except for "nodeRef" and "in" properties. |
Name | Parameters | Description |
---|---|---|
onFocus | event: Browser event | Callback to invoke on focus event of input field. |
onBlur | event: Browser event | Callback to invoke on blur event of input field. |
onInput | event: Browser event | Callback to invoke on input event of input field. |
onSelect | originalEvent: Browser event value: Selected date | Callback to invoke when a date is selected. |
onChange | originalEvent: Browser event value: New date | Callback to invoke when value changes. |
onTodayButtonClick | event: Browser event | Callback to invoke when today button is clicked. |
onClearButtonClick | event: Browser event | Callback to invoke when clear button is clicked. |
onViewDateChange | originalEvent: Browser event value: New date | Callback to invoke when the displayed month/year is changed. |
onShow | - | Callback to invoke when overlay panel or modal becomes visible. |
onHide | - | Callback to invoke when overlay panel or modal becomes hidden. |
onVisibleChange | event.visible: Whether the overlay is visible event.type: Action type when the overlay is visible/hidden event.callback: It is used to refocus the input field in some cases when the overlay is hidden. | Callback to invoke when visible is changed. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-calendar | Main container element |
p-calendar-w-btn | Main container element when button is enabled. |
p-calendar-timeonly | Main container element in time picker only mode. |
p-inputtext | Input element |
p-datepicker | Datepicker element |
p-datepicker-inline | Datepicker element in inline mode |
p-monthpicker | Datepicker element in month view. |
p-monthpicker-month | Month cell in month view mode. |
p-datepicker-touch-ui | Datepicker element in touch ui mode. |
p-datepicker-calendar | Table containing dates of a month. |
p-datepicker-current-day | Cell of selected date. |
p-datepicker-today | Cell of today's date. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.