apps/mantine.dev/src/pages/dates/year-picker.mdx
import { YearPickerDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.YearPicker);
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.
Set type="multiple" to allow users to pick multiple dates:
Set type="range" to allow users to pick a date 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.
Use the presets prop to add custom year presets. Presets are displayed next to the calendar:
To use presets with type="range", define the value as a tuple of two dates:
Use the defaultDate prop to set the date value that will be used to determine which decade should be displayed initially.
For example, to display the 2040 – 2049 decade, set defaultDate={new Date(2040, 1)}. If the value is not specified,
then defaultDate will use new Date(). Month, day, minutes and seconds are ignored in the provided date object, only the year is used –
you can specify any date value.
Note that if you set the date prop, then the defaultDate value will be ignored.
Set the date and onDateChange props to make the currently displayed 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 20 years to the current date value:
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.
You can add props to year controls with the getYearControlProps function. It accepts a year date as a single argument,
and props returned from the function will be added to the year control. For example, it can be used to disable a specific
control or add styles:
Set the numberOfColumns prop to define the number of pickers that will be rendered side by side:
Set the fullWidth prop to make the year picker stretch to fill 100% of its parent container width:
Use yearsListFormat to change the dayjs format of the year control:
Use decadeLabelFormat to change the dayjs format of the decade label:
Set the ariaLabels prop to specify aria-label attributes for next/previous controls:
import { YearPicker } from '@mantine/dates';
function Demo() {
return (
<YearPicker
ariaLabels={{
nextDecade: 'Next decade',
previousDecade: 'Previous decade',
}}
/>
);
}
Use getYearControlProps to customize the aria-label attribute:
import { YearPicker } from '@mantine/dates';
function Demo() {
return (
<YearPicker
getYearControlProps={(date) => ({
'aria-label': `Select year ${date.getFullYear()}`,
})}
/>
);
}
Note that the following events will only trigger if focus is on a year control.
<KeyboardEventsTable data={[ { key: 'ArrowRight', description: 'Focuses next non-disabled year', }, { key: 'ArrowLeft', description: 'Focuses previous non-disabled year', }, { key: 'ArrowDown', description: 'Focuses next non-disabled year in the same column', }, { key: 'ArrowUp', description: 'Focuses previous non-disabled year in the same column', }, ]} />