Back to Mantine

Date Picker

apps/mantine.dev/src/pages/dates/date-picker.mdx

9.4.07.8 KB
Original Source

import { DatePickerDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.DatePicker);

Usage

<Demo data={DatePickerDemos.usage} />

Allow deselect

Set allowDeselect to allow users to deselect the currently selected date by clicking on it. allowDeselect is disregarded when the type prop is range or multiple. When a date is deselected, onChange is called with null.

<Demo data={DatePickerDemos.deselect} />

Multiple dates

Set type="multiple" to allow users to pick multiple dates:

<Demo data={DatePickerDemos.multiple} />

Dates range

Set type="range" to allow users to pick a date range:

<Demo data={DatePickerDemos.range} />

Single date in range

By default, it is not allowed to select a single date as a range – when the user clicks the same date a second time, it is deselected. To change this behavior, set the allowSingleDateInRange prop. allowSingleDateInRange is ignored when the type prop is not range.

<Demo data={DatePickerDemos.singleRange} />

Presets

Use the presets prop to add custom date presets. Presets are displayed next to the calendar:

<Demo data={DatePickerDemos.presets} />

To use presets with type="range", define the value as a tuple of two dates:

<Demo data={DatePickerDemos.presetsRange} />

Default date

Use the defaultDate prop to set the date value that will be used to determine which year should be displayed initially. For example, to display the 2015 February month, set defaultDate={new Date(2015, 1)}. If the value is not specified, then defaultDate will use new Date(). Day, minutes and seconds are ignored in the provided date object, only year and month data is used – you can specify any date value.

Note that if you set the date prop, then the defaultDate value will be ignored.

<Demo data={DatePickerDemos.defaultDate} />

Controlled date

Set the date and onDateChange props to make the currently displayed month, year and decade controlled. By doing so, you can customize the date picking experience. For example, when the user selects the first date in a range, you can add one month to the current date value:

<Demo data={DatePickerDemos.controlledDate} />

Default level

Set the defaultLevel prop to configure the initial level that will be displayed:

<Demo data={DatePickerDemos.defaultLevel} />

Hide outside dates

Set the hideOutsideDates prop to remove all dates that do not belong to the current month:

<Demo data={DatePickerDemos.hideOutsideDates} />

Display week numbers

Set the withWeekNumbers prop to display week numbers:

<Demo data={DatePickerDemos.withWeekNumbers} />

First day of week

Set the firstDayOfWeek prop to configure the first day of the week. The prop accepts a number from 0 to 6, where 0 is Sunday and 6 is Saturday. The default value is 1 – Monday. You can also configure this option for all components with DatesProvider.

<Demo data={DatePickerDemos.firstDayOfWeek} />

Hide weekdays

Set the hideWeekdays prop to hide weekday names:

<Demo data={DatePickerDemos.hideWeekdays} />

Weekend days

Use the weekendDays prop to configure weekend days. The prop accepts an array of numbers from 0 to 6, where 0 is Sunday and 6 is Saturday. The default value is [0, 6] – Saturday and Sunday. You can also configure this option for all components with DatesProvider.

<Demo data={DatePickerDemos.weekendDays} />

Render day function

You can customize day rendering with the renderDay prop. For example, it can be used to add Indicator to certain days.

<Demo data={DatePickerDemos.renderDay} />

Min and max date

Set the minDate and maxDate props to define minimum and maximum dates. If the previous/next page is not available, then the corresponding control will be disabled.

<Demo data={DatePickerDemos.minMax} />

Change header controls order

Use the headerControlsOrder prop to change the order of header controls. The prop accepts an array of 'next' | 'previous' | 'level'. Note that each control can be used only once in the array.

<Demo data={DatePickerDemos.headerControlsOrder} />

Add props to day, year and month control

You can add props to year, month and day controls with the getYearControlProps, getMonthControlProps and getDayProps functions. All functions accept a date as a single argument, and props returned from the function will be added to the year/month/day control. For example, it can be used to disable a specific control or add styles:

<Demo data={DatePickerDemos.controlProps} />

Exclude dates

To disable specific dates, use the excludeDate prop. It accepts a function that takes a date as an argument and returns a boolean value – if true is returned, the date will be disabled. Example of disabling all dates that are not Fridays:

<Demo data={DatePickerDemos.excludeDate} />

Number of columns

Set the numberOfColumns prop to define the number of pickers that will be rendered side by side:

<Demo data={DatePickerDemos.numberOfColumns} />

Max level

<Demo data={DatePickerDemos.maxLevel} />

Full width

Set the fullWidth prop to make the date picker stretch to fill 100% of its parent container width:

<Demo data={DatePickerDemos.fullWidth} />

Size

<Demo data={DatePickerDemos.sizeConfigurator} />

Change year and months controls format

Use the yearsListFormat and monthsListFormat props to change the dayjs format of year/month controls:

<Demo data={DatePickerDemos.listFormat} />

Change label format

Use the decadeLabelFormat, yearLabelFormat and monthLabelFormat props to change the dayjs format of the decade/year label:

<Demo data={DatePickerDemos.labelFormat} />

Localization

Usually it is better to specify the @mantine/dates package locale in DatesProvider, but you can also override the locale per component:

<Demo data={DatePickerDemos.locale} />

Accessibility

Aria labels

Set the ariaLabels prop to specify aria-label attributes for next/previous controls:

tsx
import { DatePicker } from '@mantine/dates';

function Demo() {
  return (
    <DatePicker
      ariaLabels={{
        nextDecade: 'Next decade',
        previousDecade: 'Previous decade',
        nextYear: 'Next year',
        previousYear: 'Previous year',
        nextMonth: 'Next month',
        previousMonth: 'Previous month',
        yearLevelControl: 'Change to decade view',
        monthLevelControl: 'Change to year view',
      }}
    />
  );
}

Year/month control aria-label

Use getYearControlProps/getMonthControlProps/getDayProps to customize the aria-label attribute:

tsx
import { DatePicker } from '@mantine/dates';

function Demo() {
  return (
    <DatePicker
      getDayProps={(date) => ({
        'aria-label': `Select date ${
          date.getMonth() + 1
        }/${date.getDate()}/${date.getFullYear()}`,
      })}
      getYearControlProps={(date) => ({
        'aria-label': `Select year ${date.getFullYear()}`,
      })}
      getMonthControlProps={(date) => ({
        'aria-label': `Select month ${date.getFullYear()}/${date.getMonth()}`,
      })}
    />
  );
}

Keyboard interactions

Note that the following events will only trigger if focus is on a date control.

<KeyboardEventsTable data={[ { key: 'ArrowRight', description: 'Focuses next non-disabled date', }, { key: 'ArrowLeft', description: 'Focuses previous non-disabled date', }, { key: 'ArrowDown', description: 'Focuses next non-disabled date in the same column', }, { key: 'ArrowUp', description: 'Focuses previous non-disabled date in the same column', }, ]} />