mui-date-time-range-picker
v0.8.0
Published
Free single-popover date range picker for MUI v5 and MUI X Date Pickers v6 (dayjs).
Maintainers
Readme
mui-date-time-range-picker
Free date range picker for MUI v5 and MUI X Date Pickers v6 (dayjs): one popover with one or two months, presets, keyboard-editable start/end inputs and optional time. Built only from free MIT components (DateCalendar, PickersDay, DateField, TimeField).
It has no dependencies of its own. It uses your app's React, MUI, MUI X and dayjs, so it takes its look from your MUI theme.
Install
npm i mui-date-time-range-pickerPeer dependencies: react 17–18, @mui/material ^5.15, @mui/x-date-pickers ^6.19, dayjs ^1.11.
Usage
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { DateTimeRangePicker, DateRangeValue } from 'mui-date-time-range-picker'
const [range, setRange] = useState<DateRangeValue>([null, null])
<LocalizationProvider adapterLocale="uk" dateAdapter={AdapterDayjs}>
<DateTimeRangePicker value={range} onChange={setRange} presets={['this-week', 'last-7-days', 'this-month']} />
</LocalizationProvider>The picker must be inside MUI's LocalizationProvider with AdapterDayjs. Import the dayjs locales you use (e.g. import 'dayjs/locale/uk').
Props
| Prop | Type | Default | |
| --- | --- | --- | --- |
| value | [Dayjs \| null, Dayjs \| null] | — | Required. |
| onChange | (value) => void | — | Called on Apply, on a click outside with a complete range, and on Clear. |
| minDate, maxDate | Dayjs | — | Days outside are disabled; presets are cut to these bounds. |
| maxRange | { amount, unit: 'day' | 'week' | 'month' | 'year' } | — | Longest range. After the first click, later days further than this are disabled; the end input rejects them; longer presets keep their end and move their start. |
| shouldDisableDate | (day, { rangeStart }) => boolean | — | Extra disabled days. rangeStart is the first picked day while the end is being chosen, otherwise null. |
| selectionUnit | 'day' \| 'week' \| 'month' \| 'year' | 'day' | What one click picks. 'week': whole weeks (the hovered week row is highlighted; the start is the first day of its week, the end the last day of its week). 'month': a grid of months per year instead of days; inputs edit month and year (e.g. MM.YYYY). 'year': a grid of 12 years per page (e.g. 2016–2027) with arrows to switch pages; inputs edit the year only (YYYY). The month and year formats are derived from format. Typed dates and presets are widened to whole units. A week, month or year is disabled only when all its days are disabled by minDate/maxDate/maxRange/shouldDisableDate. onChange gets [first day, last day] of the units at the start of the day. |
| withTime | boolean | false | Adds start/end time fields. Only with selectionUnit 'day'. |
| ampm | boolean | false | 12-hour time. |
| presets | (DateRangePresetKey \| DateRangePreset)[] | [] | Built-in keys and/or custom { key, label, getValue }, in display order. |
| calendars | 1 \| 2 | 2 | Always 1 on small screens. |
| showDaysOutsideCurrentMonth | boolean | true | |
| placeholder | string | locale text | Button text when empty. |
| clearable | boolean | true | |
| buttonProps | ButtonProps | — | Passed to the trigger button. |
| sx | SxProps<Theme> | — | Styles of the trigger button (margins, width, etc.). Applied after buttonProps.sx. |
| locale | string | adapterLocale | dayjs locale for month names, week start and texts. |
| format | string | locale L format | Date format of the button and inputs. |
| localeText | partial texts | — | Overrides built-in texts, including fieldPlaceholders of the empty date/time inputs. |
| icons | { calendar, expand, previous, next, separator } | Material icons | Any of them can be replaced with a ReactNode. |
Built-in preset keys: today, yesterday, this-week, last-week, last-7-days, last-30-days, this-month, last-month, this-year, last-year.
Texts
English and Ukrainian are built in and picked by locale; other locales fall back to English. Pass localeText for your own texts, for example from i18next:
<DateTimeRangePicker
localeText={{ placeholder: t('select-period'), apply: t('ok'), presets: { today: t('today') } }}
{...props}
/>Empty date and time inputs show section placeholders from localeText.fieldPlaceholders: DD.MM.YYYY hh:mm in English, ДД.ММ.РРРР гг:хх in Ukrainian. year is cut to the digits of the format, so YY shows two letters:
<DateTimeRangePicker
localeText={{ fieldPlaceholders: { day: 'TT', month: 'MM', year: 'JJJJ' } }}
{...props}
/>Week start
The picker (including selectionUnit="week") follows the locale's weekStart. To make weeks start on Monday in English too, set it in your app:
import updateLocale from 'dayjs/plugin/updateLocale'
dayjs.extend(updateLocale)
dayjs.updateLocale('en', { weekStart: 1 })Development
npm i
npm run dev # playground on http://localhost:5180
npm run typecheck
npm run build # dist/ (ESM + CJS + types)