- Material UI 简介
 - Material UI 组件库详解
 - 使用 Material UI 的 Autocomplete 组件详解
 - 使用 Material UI 的 Button 组件详解
 - 使用 Material UI 创建按钮组(Button Group)的详细指南
 - 使用 Material UI 创建复选框(Checkbox)的详细指南
 - Material UI 中 Floating Action Button 的使用详解
 - Material UI 中 Radio Group 的使用详解
 - Material UI 中 Rating 组件的使用详解
 - Material UI 中 Select 组件的使用详解
 - Material UI 中 Slider 组件的使用详解
 - Material UI Switch 组件使用详解
 - Material UI Text Field 组件使用详解
 - Material UI Transfer List 使用详解
 - Material UI Toggle Button 使用详解
 - Material UI Avatar 组件使用详解
 - Material UI Badge 组件使用详解
 - Material UI Chip 组件使用详解
 - Material UI Divider 组件使用详解
 - Material UI Icons 组件使用详解
 - Material UI 中 Material Icons 的使用详解
 - Material UI 框架中 Lists 的使用详解
 - Material UI 框架中 Table 的使用详解
 - Material UI 框架中 Tooltip 的使用详解
 - Material UI 框架中 Typography 的使用详解
 - Material UI 框架中 Alert 组件的使用详解
 - Material UI 框架中 Backdrop 组件的详细使用教程
 - Material UI 框架中 Dialog 组件的详细使用教程
 - Material UI Progress 组件详解及示例
 - Material UI 框架中的 Skeleton 组件详解
 - Material UI 框架中的 Snackbar 组件详解
 - Material UI 框架中的 Accordion 组件详解
 - Material UI 框架中的 App Bar 组件详解
 - Material UI 框架中的 Card 组件详解
 - Material UI 框架中的 Paper 组件详解
 - 使用 Material UI 框架中的 Bottom Navigation 组件
 - 使用 Material UI 框架中的 Breadcrumbs 组件
 - 使用 Material UI 框架中的 Drawer 组件
 - 使用 Material UI 框架中的 Links 组件
 - 使用 Material UI 框架中的 Menu 组件
 - 使用 Material UI 框架中的 Pagination 组件
 - 使用 Material UI 框架中的 Speed Dial 组件
 - 使用 Material UI 框架中的 Stepper 组件
 - 使用 Material UI 框架中的 Tabs 组件
 - 使用 Material UI 框架中的 Layout Box 组件
 - 使用 Material UI 框架中的 Layout Container
 - 使用 Material UI 框架中的 Layout Grid
 - 使用 Material UI 框架中的 Layout Grid Version 2
 - 使用 Material UI 框架中的 Layout Stack
 - 使用 Material UI 框架中的 Layout Image List
 - 使用 Material UI 框架中的 Layout Hidden
 - 使用 Material UI 框架中的 Click-Away Listener
 - 使用 Material UI 框架中的 CSS Baseline
 - 使用 Material UI 框架中的 Utils Modal
 - 使用 Material UI 框架中的 Utils No SSR
 - 使用 Material UI 框架中的 Utils Popover
 - 使用 Material UI 框架中的 Utils Popper
 - 使用 Material UI 框架中的 Utils Portal
 - 使用 Material UI 框架中的 Utils Textarea Autosize
 - 使用 Material UI 框架中的 Utils Transitions
 - 使用 Material UI 框架中的 `useMediaQuery`
 - 使用 Material UI 框架中的 MUI X Data Grid
 - 使用 Material UI 框架中的 Data Grid - Layout
 - 使用 Material UI 框架中的 Data Grid:深入探索列定义与管理
 - 深入探索 Material UI 框架中的 Data Grid:行定义与管理
 - 深入探索 Material UI 框架中的 Data Grid 编辑功能
 - 深入探索 Material UI 框架中的 Data Grid 排序功能
 - 深入探索 Material UI 框架中的 Data Grid 过滤功能
 - 深入探索 Material UI 框架中的 Data Grid 分页功能
 - 深入探索 Material UI 框架中的 Data Grid 行选择和单元格选择功能
 - 深入探索 Material UI 框架中的 Data Grid 导出功能
 - 深入探索 Material UI 框架中的 Data Grid - Copy and Paste 功能
 - 深入探索 Material UI 框架中的 Data Grid - Overlays 功能
 - 深入探索 Material UI 框架中的 Data Grid - Custom Slots and Subcomponents
 - 深入探索 Material UI 框架中的 Data Grid - Styling 和 Styling Recipes
 - 深入探索 Material UI 框架中的 Data Grid - Translated Components
 - 使用 Material UI 的 Data Grid - Scrolling 功能
 - 使用 Material UI 的 Data Grid - Virtualization 功能
 - 使用 Material UI 的 Data Grid - 可访问性(Accessibility)功能
 - 使用 Material UI 的 Data Grid - 性能(Performance)优化
 - 使用 Material UI 的 Data Grid - 树形数据(Tree Data)
 - 使用 Material UI 的 Data Grid - 行分组(Row Grouping)
 - 使用 Material UI 的 Data Grid - 数据聚合(Aggregation)
 - 使用 Material UI 的 Data Grid - 数据透视(Pivoting)
 - 使用 Material UI 的 Data Grid - 服务器端数据(Server-side Data)
 - 使用 Material UI 的 Data Grid - 服务器端树形数据(Server-side Tree Data)
 - 使用 Material UI 的 Data Grid - 服务器端懒加载(Server-side Lazy Loading)
 - 使用 Material UI 的 Data Grid - 服务器端行分组(Server-side Row Grouping)
 - 使用 Material UI 的 Data Grid - 服务器端聚合(Server-side Aggregation)
 - 使用 Material UI 的 Data Grid - API Object
 - 使用 Material UI 的 Data Grid - 事件处理(Events)
 - 使用 Material UI 的 Data Grid - State 管理
 - Material UI Data Grid - 编辑功能详解与使用示例
 - Material UI Data Grid - Row Grouping 使用指南与示例
 - Material UI DataGrid API 使用指南
 - Material UI DataGridPremium API 使用指南
 - Material UI 框架 DataGridPro API 使用指南
 - Material UI 框架 GridFilterForm API 使用指南
 - Material UI 框架 GridFilterPanel API 使用指南
 - Material UI 框架 GridApi API 使用指南
 - Material UI 框架 GridCellParams API 使用指南
 - Material UI 框架 GridColDef API 使用指南
 - Material UI 框架 GridSingleSelectColDef API 使用指南
 - Material UI 框架 GridActionsColDef API 使用指南
 - Material UI 框架 GridExportStateParams API 使用指南
 - Material UI 框架 GridFilterItem API 使用指南
 - Material UI 框架 GridFilterModel API 使用指南
 - Material UI 框架:GridToolbarQuickFilter API 使用详解
 - Material UI 框架:GridApi 接口详解及其使用
 - Material UI 框架:Interface `GridCellParams` API 使用详解
 - Material UI 框架:Interface `GridColDef` API 使用详解
 - Material UI 框架:Interface `GridSingleSelectColDef` API 使用详解
 - Material UI 框架:Interface `GridActionsColDef` API 使用详解
 - Material UI 框架:Interface `GridExportStateParams` API 使用详解
 - Material UI 框架:`GridFilterItem` API 使用详解
 - Material UI 框架:`GridFilterModel` API 使用详解
 - Material UI 框架:`Interface GridFilterOperator` API 使用详解
 - Material UI 框架:`GridRowClassNameParams` 接口详解及使用指南
 - Material UI 框架:`GridRowParams` 接口详解及使用指南
 - Material UI 框架:`GridRowSpacingParams` 接口详解及使用指南
 - Material UI 框架:`GridAggregationFunction` 接口详解及使用指南
 - Material UI 框架:`GridCsvExportOptions` 接口详解及使用指南
 - Material UI 框架:`GridPrintExportOptions` 接口详解及使用指南
 - Material UI 框架:`GridExcelExportOptions` 接口详解及使用指南
 - Material UI 框架:MUI X 日期和时间选择器使用指南
 - Material UI 框架:日期和时间选择器 - 入门指南
 - Material UI 框架:日期和时间选择器 - 基本概念
 - Material UI 框架:日期和时间选择器 - 可访问性
 - Material UI 框架之 Date Picker 组件详解
 - Material UI 框架之 Date Field 组件详解
 - Material UI 框架之 Date Calendar 组件详解
 - Material UI 框架之 Time Picker 组件详解
 - Material UI 框架之 Time Field 组件详解
 - Material UI 框架之 Time Clock 组件详解
 - Material UI 框架之 Digital Clock 组件详解
 - Material UI 框架之 DateTime Picker 组件详解
 - Material UI 框架之 DateTime Field 组件详解
 - Material UI 组件 Date Range Picker 使用详解
 - Material UI 组件 Date Range Field 使用详解
 - Material UI 组件 Date Range Calendar 使用详解
 - Material UI 组件 Time Range Picker 使用详解
 - Material UI 组件 Time Range Field 使用详解
 - Material UI 组件 Date Time Range Picker 使用详解
 - Material UI 组件 Date Time Range Field 使用详解
 - 使用 Material-UI 的 Fields 组件详解
 - 使用 Material-UI 的 DateCalendar 组件详解
 - 使用 Material-UI 的 DateField 组件详解
 - 使用 Material-UI 的 DatePicker 组件详解
 - 使用 Material-UI 的 DatePickerToolbar API 详解
 - 使用 Material-UI 的 DateRangeCalendar API 详解
 - 使用 Material-UI 的 DateRangePicker API 详解
 - 使用 Material-UI 的 DateRangePickerDay API 详解
 - 使用 Material UI DateRangePickerToolbar API 详解
 - 深入了解 Material UI DateTimeField API 使用
 - 深入了解 Material UI DateTimePicker API 使用
 - 深入了解 Material UI `DateTimePickerTabs` API 的使用
 - 深入了解 Material UI 框架中的 `DateTimePickerToolbar` API
 - 深入了解 Material UI 框架中的 `DateTimeRangePicker` API
 - Material UI 框架中的 DateTimeRangePickerTabs API 使用详解
 - 深入了解 Material UI 框架中的 `DateTimeRangePickerToolbar API`
 - 深入了解 Material UI 框架中的 `DayCalendarSkeleton API`
 - 深入解析 Material UI 的 DesktopDatePicker API 使用指南
 - 深入解析 Material UI 的 `DesktopDateRangePicker API` 使用指南
 - 深入解析 Material UI 的 `DesktopDateTimePicker API` 使用指南
 - 深入解析 Material UI 的 `DesktopDateTimeRangePicker API` 使用指南
 - 深入解析 Material UI 的 `DesktopTimePicker API` 使用指南
 - 深入解析 Material UI 的 `DigitalClock API` 使用指南
 - 深入解析 Material UI 的 `LocalizationProvider API` 使用指南
 - Material UI 框架中 MobileDatePicker API 的全面使用指南
 - 深入了解 Material UI 的 MobileDateRangePicker API 使用
 
深入了解 Material UI 框架中的 `DateTimeRangePicker` API
class DateTimeRangePickerDateTimeRangePicker 是 Material UI 的日期时间范围选择器组件,允许用户选择一个时间段的开始和结束日期时间。在需要选择时间范围的场景中,比如预订、数据筛选、活动安排等,DateTimeRangePicker 是一个强大的工具。
本文将详细介绍 DateTimeRangePicker 的使用,包括其主要属性、方法及其与其他组件的结合方式。通过丰富的代码示例,帮助你全面掌握这一组件。
1. 组件简介
DateTimeRangePicker 提供了一种直观的用户界面,用于选择两个日期时间值(开始日期和结束日期)。它在 Material UI 的 @mui/x-date-pickers 扩展包中提供。
2. 安装和配置
要使用 DateTimeRangePicker,首先确保安装了必要的依赖项:
npm install @mui/material @mui/x-date-pickers @emotion/react @emotion/styled
接着配置适配器,比如 AdapterDateFns:
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
3. DateTimeRangePicker 的主要 API
DateTimeRangePicker 支持丰富的属性和事件,用于自定义其行为。以下是其关键属性的说明:
| 属性名 | 类型 | 描述 | 
|---|---|---|
value | 
[Date, Date] | 
当前选择的开始和结束日期时间值。 | 
onChange | 
(value: [Date, Date]) => void | 
当选择日期时间范围更改时触发的回调函数。 | 
minDateTime | 
Date | 
允许选择的最小日期时间值。 | 
maxDateTime | 
Date | 
允许选择的最大日期时间值。 | 
disablePast | 
boolean | 
如果为true,禁止选择过去的日期时间。 | 
disableFuture | 
boolean | 
如果为true,禁止选择未来的日期时间。 | 
renderInput | 
function | 
自定义输入框的渲染函数。 | 
startText | 
string | 
开始日期输入框的标签文本。 | 
endText | 
string | 
结束日期输入框的标签文本。 | 
components | 
object | 
自定义工具栏或部分组件的替代内容。 | 
componentsProps | 
object | 
传递给自定义组件的附加属性。 | 
localeText | 
object | 
自定义本地化文本,例如“选择日期时间”等提示。 | 
4. 使用示例
示例 1:基本使用
以下是一个基本的 DateTimeRangePicker 示例:
import React, { useState } from 'react';
import { DateTimeRangePicker } from '@mui/x-date-pickers-pro';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import TextField from '@mui/material/TextField';
const BasicDateTimeRangePicker = () => {
  const [value, setValue] = useState([null, null]);
  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
      <DateTimeRangePicker
        value={value}
        onChange={(newValue) => setValue(newValue)}
        renderInput={(startProps, endProps) => (
          <>
            <TextField {...startProps} label="开始日期时间" />
            <TextField {...endProps} label="结束日期时间" style={{ marginLeft: 8 }} />
          </>
        )}
      />
    </LocalizationProvider>
  );
};
export default BasicDateTimeRangePicker;
此示例中,用户可以选择日期时间范围,并显示在两个输入框中。
示例 2:限制日期时间范围
通过 minDateTime 和 maxDateTime 限制可选范围:
<DateTimeRangePicker
  value={value}
  onChange={(newValue) => setValue(newValue)}
  minDateTime={new Date(2023, 0, 1)}
  maxDateTime={new Date(2023, 11, 31)}
  renderInput={(startProps, endProps) => (
    <>
      <TextField {...startProps} label="开始日期时间" />
      <TextField {...endProps} label="结束日期时间" style={{ marginLeft: 8 }} />
    </>
  )}
/>
示例 3:禁用过去和未来的日期时间
使用 disablePast 和 disableFuture 禁用无效时间范围:
<DateTimeRangePicker
  value={value}
  onChange={(newValue) => setValue(newValue)}
  disablePast
  disableFuture
  renderInput={(startProps, endProps) => (
    <>
      <TextField {...startProps} label="开始日期时间" />
      <TextField {...endProps} label="结束日期时间" style={{ marginLeft: 8 }} />
    </>
  )}
/>
示例 4:自定义工具栏
通过 components 属性,可以自定义工具栏外观和行为:
import React from 'react';
import { DateTimeRangePicker } from '@mui/x-date-pickers-pro';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import TextField from '@mui/material/TextField';
import Typography from '@mui/material/Typography';
const CustomToolbar = (props) => {
  return (
    <div style={{ padding: 16, backgroundColor: '#1976d2', color: 'white' }}>
      <Typography variant="h6">选择日期时间范围</Typography>
    </div>
  );
};
const CustomToolbarDateTimeRangePicker = () => {
  const [value, setValue] = React.useState([null, null]);
  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
      <DateTimeRangePicker
        value={value}
        onChange={(newValue) => setValue(newValue)}
        components={{
          Toolbar: CustomToolbar,
        }}
        renderInput={(startProps, endProps) => (
          <>
            <TextField {...startProps} label="开始日期时间" />
            <TextField {...endProps} label="结束日期时间" style={{ marginLeft: 8 }} />
          </>
        )}
      />
    </LocalizationProvider>
  );
};
export default CustomToolbarDateTimeRangePicker;
此示例展示了如何替换默认工具栏,并应用自定义样式。
示例 5:结合表单和按钮
以下示例展示了 DateTimeRangePicker 如何与表单提交结合:
import React, { useState } from 'react';
import { DateTimeRangePicker } from '@mui/x-date-pickers-pro';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import TextField from '@mui/material/TextField';
import Button from '@mui/material/Button';
const FormWithDateTimeRangePicker = () => {
  const [value, setValue] = useState([null, null]);
  const handleSubmit = () => {
    alert(`Selected range: ${value[0]} - ${value[1]}`);
  };
  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
      <DateTimeRangePicker
        value={value}
        onChange={(newValue) => setValue(newValue)}
        renderInput={(startProps, endProps) => (
          <>
            <TextField {...startProps} label="开始日期时间" />
            <TextField {...endProps} label="结束日期时间" style={{ marginLeft: 8 }} />
          </>
        )}
      />
      <Button
        variant="contained"
        color="primary"
        onClick={handleSubmit}
        style={{ marginTop: 16 }}
      >
        提交
      </Button>
    </LocalizationProvider>
  );
};
export default FormWithDateTimeRangePicker;
5. 注意事项
- 验证日期时间范围:确保用户选择的开始时间小于结束时间。
 - 支持本地化:通过 
localeText自定义日期时间选择器的提示文本和标签。 - 性能优化:在大数据场景中,限制日期时间范围以提高性能。
 
6. 总结
DateTimeRangePicker 是 Material UI 提供的强大日期时间范围选择组件,支持高度的可定制性和广泛的应用场景。通过本文介绍的详细 API、属性说明和示例代码,你可以轻松将其集成到项目中,为用户提供优秀的交互体验。
希望本文能够帮助你全面掌握 DateTimeRangePicker 的使用!