Open In App

React MUI MobileDateRangePicker API

React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based on top of Material Design by Google.

The MobileDateRangePicker component in React Material-UI is a specialized picker that allows a user to select a range of dates. This component provides a mobile-friendly interface for date range selection and works well on touch devices. Material UI provides a customizable <MobileDateRangePicker/> component that we can use for several purposes with the help of its props



Import Statement:

import { MobileDateRangePicker } from 
    '@mui/x-date-pickers-pro/MobileDateRangePicker';
// or
import { MobileDateRangePicker } from 
    '@mui/x-date-pickers-pro';

 



Props List: 

Slots List: Here is a list of the available slots in the MobileDateRangePicker component:

Syntax:

<MobileDateRangePicker
    value={value}
    onChange={(newValue) => {
        setValue(newValue);
    }}
    renderInput={(startProps, endProps) => (
        <React.Fragment>
            <TextField {...startProps} />
            <Box sx={{ mx: 2 }}> to </Box>
            <TextField {...endProps} />
        </React.Fragment>
    )}
/>

Installing React App:

Step 1: Create a React app using the following command.

npx create-react-app mobile-date-range-picker example

Step 2: Now get into the project directory.

cd mobile-date-range-picker-example

Installing Material-UI: Installing Material-UI’s source files via npm/yarn, and they take care of injecting CSS needed.

npm install @material-ui/core
OR
yarn add @material-ui/core

Installing the required package:

npm install @mui/material @emotion/react @emotion/styled
npm install @mui/x-date-pickers dayjs @mui/x-date-pickers-pro

Project Structure:

Project Structure

Step to run the application:

npm start

Example 1: 




import * as React from "react";
  
import TextField from "@mui/material/TextField";
import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack";
import { LocalizationProvider } from 
    "@mui/x-date-pickers-pro";
import { AdapterDayjs } from 
    "@mui/x-date-pickers-pro/AdapterDayjs";
import { MobileDateRangePicker } from 
    "@mui/x-date-pickers-pro/MobileDateRangePicker";
  
export default function ResponsiveDateRangePicker() {
    const [value, setValue] = React.useState([null, null]);
  
    return (
        <Stack spacing={3}>
            <LocalizationProvider
                dateAdapter={AdapterDayjs}
                localeText={{
                    start: "Start Date",
                    end: "End date"
                }}
            >
                <h1 style={{ color: "green" }}>
                    GeeksForGeeks
                </h1>
                <h3>React MUI MobileDateRangePicker API</h3>
                <MobileDateRangePicker
                    value={value}
                    onChange={(newValue) => {
                        setValue(newValue);
                    }}
                    renderInput={(startProps, endProps) => (
                        <React.Fragment>
                            <TextField {...startProps} />
                            <Box sx={{ mx: 2 }}> to </Box>
                            <TextField {...endProps} />
                        </React.Fragment>
                    )}
                />
            </LocalizationProvider>
        </Stack>
    );
}

Output: 

Selecting date range

Example 2: These displayed days are customizable with the renderDay function prop. You can take advantage of the internal DateRangePickerDay component.

App.js 




import * as React from 'react';
import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';
import { LocalizationProvider } from 
    '@mui/x-date-pickers-pro';
import { AdapterDayjs } from 
    '@mui/x-date-pickers-pro/AdapterDayjs';
import { StaticDateRangePicker } from 
    '@mui/x-date-pickers-pro/StaticDateRangePicker';
import { DateRangePickerDay as MuiDateRangePickerDay } 
    from '@mui/x-date-pickers-pro/DateRangePickerDay';
  
const DateRangePickerDay = styled(MuiDateRangePickerDay)(
    ({
        theme,
        isHighlighting,
        isStartOfHighlighting,
        isEndOfHighlighting,
        outsideCurrentMonth,
    }) => ({
        ...(!outsideCurrentMonth &&
            isHighlighting && {
            borderRadius: 0,
            backgroundColor: theme.palette.primary.main,
            color: theme.palette.common.white,
            '&:hover, &:focus': {
                backgroundColor: theme.palette.primary.dark,
            },
        }),
        ...(isStartOfHighlighting && {
            borderTopLeftRadius: '50%',
            borderBottomLeftRadius: '50%',
        }),
        ...(isEndOfHighlighting && {
            borderTopRightRadius: '50%',
            borderBottomRightRadius: '50%',
        }),
    }),
);
  
export default function CustomDateRangePickerDay() {
    const [value, setValue] = React.useState([null, null]);
  
    const renderWeekPickerDay = (date, dateRangePickerDayProps) => {
        return <DateRangePickerDay {...dateRangePickerDayProps} />;
    };
  
    return (
        <LocalizationProvider dateAdapter={AdapterDayjs}>
            <StaticDateRangePicker
                displayStaticWrapperAs="desktop"
                label="date range"
                value={value}
                onChange={(newValue) => setValue(newValue)}
                renderDay={renderWeekPickerDay}
                renderInput={(startProps, endProps) => (
                    <React.Fragment>
                        <TextField {...startProps} />
                        <Box sx={{ mx: 2 }}> to </Box>
                        <TextField {...endProps} />
                    </React.Fragment>
                )}
            />
        </LocalizationProvider>
    );
}

Output:

 

Reference: https://mui.com/x/api/date-pickers/mobile-date-range-picker/


Article Tags :