跳转到内容

DatePicker API

想要学习更多组件属性和 CSS API,可以查阅 React DatePicker 组件的 API 文档。

导入

import DatePicker from '@mui/lab/DatePicker';
// 或
import { DatePicker } from '@mui/lab';
您可以通过 阅读本指南以最小化捆包大小 来了解这种差异。

属性

原生(Native) 组件的属性也是可用的。

名称类型默认值描述
onChange*func
Callback fired when the value (the selected date) changes @DateIOType.
renderInput*func
The renderInput prop allows you to customize the rendered input. The props argument of this render prop contains props of TextField that you need to forward. Pay specific attention to the ref and inputProps keys.
acceptRegexRegExp/\dap/gi
正则表达式检测并接受” “符号
allowSameDateSelectionboolfalse
If true, onChange is fired on click even if the same date is selected.
cancelTextnode'Cancel'
取消信息
classNamestring
className applied to the root component.
clearableboolfalse
If true, it shows the clear action in the picker dialog.
clearTextnode'Clear'
清除文本信息
components{ LeftArrowButton?: elementType, LeftArrowIcon?: elementType, OpenPickerIcon?: elementType, RightArrowButton?: elementType, RightArrowIcon?: elementType, SwitchViewButton?: elementType, SwitchViewIcon?: elementType }{}
The components used for each slot. Either a string to use a HTML element or a component.
componentsPropsobject{}
The props used for each slot inside.
defaultCalendarMonthany
Default calendar month displayed when value={null}.
desktopModeMediaQuerystring'@media (pointer: fine)'
CSS media query when Mobile mode will be changed to Desktop.
DialogPropsobject
Props applied to the Dialog element.
disableCloseOnSelectbool`true` for Desktop, `false` for Mobile (based on the chosen wrapper and `desktopModeMediaQuery` prop).
If true the popup or dialog will immediately close after submitting full date.
disabledboolfalse
If true, the picker and text field are disabled.
disableHighlightTodayboolfalse
If true, todays date is rendering without highlighting with circle.
disableMaskedInputboolfalse
Disable mask on the keyboard, this should be used rarely. Consider passing proper mask for your format.
disableOpenPickerboolfalse
Do not render open picker button (renders only text field with validation).
getOpenDialogAriaTextfunc(value, utils) => `Choose date, selected date is ${utils.format(utils.date(value), 'fullDate')}`
Get aria-label text for control that opens picker dialog. Aria-label text must include selected date. @DateIOType
getViewSwitchingButtonTextfunc
Get aria-label text for switching between views button.
InputAdornmentPropsobject
Props to pass to keyboard input adornment.
inputFormatstring
格式化字符串
inputReffunc
| { current?: object }
Pass a ref to the input element.
leftArrowButtonTextstring
Left arrow icon aria-label text.
loadingboolfalse
If true renders LoadingComponent in calendar instead of calendar view. Can be used to preload information and show it in calendar.
maskstring
Custom mask. Can be used to override generate from format. (e.g. __/__/____ __:__ or __/__/____ __:__ _M).
maxDateany
Max selectable date. @DateIOType
minDateany
Min selectable date. @DateIOType
okTextnode'OK'
Ok button text.
onAcceptfunc
Callback fired when date is accepted @DateIOType.
onClosefunc
Callback fired when the popup requests to be closed. Use in controlled mode (see open).
onErrorfunc
Callback that fired when input value or new value prop validation returns new validation error (or value is valid after error). In case of validation error detected reason prop return non-null value and TextField must be displayed in error state. This can be used to render appropriate form error.
Read the guide about form integration and error displaying.
onMonthChangefunc
Callback firing on month change. @DateIOType
onOpenfunc
Callback fired when the popup requests to be opened. Use in controlled mode (see open).
onViewChangefunc
Callback fired on view change.
onYearChangefunc
Callback firing on year change @DateIOType.
openboolfalse
Control the popup or dialog open state.
OpenPickerButtonPropsobject
Props to pass to keyboard adornment button.
openTo'day'
| 'month'
| 'year'
First view to show.
orientation'landscape'
| 'portrait'
Force rendering in particular orientation.
PaperPropsobject
Paper props passed down to Paper component.
PopperPropsobject
Popper props passed down to Popper component.
readOnlyboolfalse
Make picker read only.
reduceAnimationsbooltypeof navigator !== 'undefined' && /(android)/i.test(navigator.userAgent)
Disable heavy animations.
renderDayfunc
Custom renderer for day. Check the PickersDay component.
renderLoadingfunc() => <span data-mui-test="loading-progress">...</span>
Component displaying when passed loading true.
rifmFormatterfunc
Custom formatter to be passed into Rifm component.
rightArrowButtonTextstring
Right arrow icon aria-label text.
shouldDisableDatefunc
Disable specific date. @DateIOType
shouldDisableYearfunc
Disable specific years dynamically. Works like shouldDisableDate but for year selection view @DateIOType.
showDaysOutsideCurrentMonthboolfalse
If true, days that have outsideCurrentMonth={true} are displayed.
showTodayButtonboolfalse
If true, the today button is displayed. Note that showClearButton has a higher priority.
showToolbarboolfalse
If true, show the toolbar even in desktop mode.
todayTextnode'Today'
Today text message.
ToolbarComponentelementTypeDatePickerToolbar
Component that will replace default toolbar renderer.
toolbarFormatstring
Date format, that is displaying in toolbar.
toolbarPlaceholdernode'–'
Mobile picker date value placeholder, displaying if value === null.
toolbarTitlenode'Select date'
Mobile picker title, displaying in the toolbar.
TransitionComponentelementType
Custom component for popper Transition.
valueany
| Date
| number
| string
The value of the picker.
viewsArray<'day'
| 'month'
| 'year'>
显示视图数组

组件无法持有 ref。

演示项目