How to custom title of weekday of Material-ui DateRangePicker - reactjs

I spend almost half day but can't not found any solution to customize the title of week day of Material-ui DateRangePicker. Instead of "S", "M",..., I want to show the title as I expected.
Already tried to custom the dateAdapter but this not helps.
I see that Material-ui has component called MuiPickersUtilsProvider which I can customize the day title as I want but this component not support date-range picker.
I would highly appreciate any advices. Here is my code so far:
import * as React from 'react';
import TextField from '#mui/material/TextField';
import AdapterDateFns from '#mui/lab/AdapterDateFns';
import LocalizationProvider from '#mui/lab/LocalizationProvider';
import Box from '#mui/material/Box';
import MobileDateRangePicker from '#mui/lab/MobileDateRangePicker';
export class CustomDateFns extends AdapterDateFns {
getWeekdays() {
return ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG'];
}
}
export default function ResponsiveDateRangePicker() {
const [value, setValue] = React.useState([null, null]);
return (
<LocalizationProvider dateAdapter={CustomDateFns}>
<MobileDateRangePicker
inputFormat="dd/MM/yyyy"
startText="Start date"
endText="End date"
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
clearable={!!value[0] && !!value[1] ? true : false}
clearText="Reset"
cancelText=""
okText="Confirm"
toolbarTitle=""
renderInput={(startProps, endProps) => (
<React.Fragment>
<TextField {...startProps} />
<Box sx={{ mx: 2 }}> to </Box>
<TextField {...endProps} />
</React.Fragment>
)}
/>
</LocalizationProvider>
);
}
Here is what current it shows:
What I expect is:

The only way I found until now is manipulating the DOM, then set innerHTML. But I don't think this is a good way because we can't control DOM as this is generated by library, not by me.

Related

DatePicker MUI how to display "Clear" text on the left side of the calendar

I have DesktopDatePicker from material-ui(version 5.0.0) and like to display "Clear" text on the left side of the calendar.
import * as React from "react";
import dayjs from "dayjs";
import TextField from "#mui/material/TextField";
import { AdapterDayjs } from "#mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "#mui/x-date-pickers/LocalizationProvider";
import { DesktopDatePicker } from "#mui/x-date-pickers/DesktopDatePicker";
export default function ResponsiveDatePickers() {
const [value, setValue] = React.useState(dayjs("2022-04-07"));
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DesktopDatePicker
label="For desktop"
value={value}
minDate={dayjs("2017-01-01")}
onChange={(newValue) => {
setValue(newValue);
}}
componentsProps={{
actionBar: { actions: ["clear"], position: "left" }
}}
renderInput={(params) => <TextField {...params} />}
/>
</LocalizationProvider>
);
}
I added position:"left" in actionBar but this didn't work.
Since it is justified by a flexbox, you will need to add justifyContent: "flex-start" to your action bar style properties.
eg:
componentsProps={{
actionBar: { actions: ["clear"], style: {justifyContent: "flex-start"} }
}}
To further answer the question in the comment, since MUI components all give you access to styling I tried it and it worked - this is the MUIv4 way to access styling and it appears that it still works. The MUIv5 way to do it would likely be to access styling through the sx property that the components now have.
componentsProps={{
actionBar: { actions: ["clear"], sx: {justifyContent: "flex-start"} }
}}
All MUI components give access to styling with the sx property.

DatePicker component from mui display different model based on the screen size

I have DatePicker component from MUI and this is what it looks like when I open it on the PC screen.
This is what it looks like when it's a mobile screen.
It seems like MUI automatically switched the view of DatePicker for mobile.
Is there a way to keep the PC view of DatePicker for the mobile screen?
This is code sandbox demo
Here is MUI DatePicker component document
import * as React from "react";
import TextField from "#mui/material/TextField";
import { AdapterDayjs } from "#mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "#mui/x-date-pickers/LocalizationProvider";
import { DatePicker } from "#mui/x-date-pickers/DatePicker";
export default function BasicDatePicker() {
const [value, setValue] = React.useState(null);
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
label="Basic example"
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
renderInput={(params) => <TextField {...params} />}
/>
</LocalizationProvider>
);
}
Attempts
Currently, I was able to display the PC view of DatePicker on the mobile screen, but that's only I open the google developer tool and toggle the device toolbar.
When I close it, the DatePicker will be displayed as the Mobile view on both the PC and mobile screen.
import * as React from "react";
import TextField from "#mui/material/TextField";
import { AdapterDayjs } from "#mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "#mui/x-date-pickers/LocalizationProvider";
import { DatePicker } from "#mui/x-date-pickers/DatePicker";
export default function BasicDatePicker() {
const [value, setValue] = React.useState(null);
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
label="Basic example"
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
renderInput={(params) => <TextField {...params} />}
desktopModeMediaQuery="#media (pointer: coarse)"
/>
</LocalizationProvider>
);
}
Having DesktopDatePicker works both on the PC and mobile.
mui.com/x/react-date-pickers/date-picker/#responsiveness

How to custom ToolbarComponent of Material-UI KeyboardDatePicker in React.js?

Hi currently I am using React, Material-UI, KeyboardDatePicker.
You can see the code in:
https://codesandbox.io/s/material-demo-forked-rt1f1?file=/index.js
import "date-fns";
import React from "react";
import Grid from "#material-ui/core/Grid";
import DateFnsUtils from "#date-io/date-fns";
import {
MuiPickersUtilsProvider,
KeyboardTimePicker,
KeyboardDatePicker
} from "#material-ui/pickers";
export default function MaterialUIPickers() {
// The first commit of Material-UI
const [selectedDate, setSelectedDate] = React.useState(
new Date("2014-08-18T21:11:54")
);
const handleDateChange = (date) => {
setSelectedDate(date);
};
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<Grid container justify="space-around">
<KeyboardDatePicker
margin="normal"
id="date-picker-dialog"
label="Date picker dialog"
format="MM/dd/yyyy"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
"aria-label": "change date"
}}
views={["year", "month", "date"]}
/>
</Grid>
</MuiPickersUtilsProvider>
);
}
What I want to do is when I click the calender Icon, if I click the date tool bar(which is current date in the UI), I want the interface changes to month selection interface.
I am planning to custom the datPicker using ToolbarComponent.
from the Material-UI official documnet (https://material-ui-pickers.dev/api/KeyboardDatePicker), found out I got that I need to enter some code like this:
ToolbarComponent={ComponentClass<ToolbarComponentProps<unknown, DateTimePickerView>, any> | FunctionComponent<ToolbarComponentProps<unknown, DateTimePickerView>> }
But hard to understand ToolbarComponent I should enter.
How can I customize the prop of the calendar? please let me know.
Thanks
Simply pass a function to the ToolbarComponent API and return a JSX.
ToolbarComponent={() => <Box>HELLO, CUSTOM TOOL BAR</Box>}
The above jsx will be rendered above the calendar.

How can I change material ui datepicker language and default timezone?

How can I change the language of a Material-UI date picker? It doesn't seem to fully work for the whole date picker.
What my current date picker looks like:
#MUI5 solution
import * as React from 'react';
import { AdapterDateFns } from '#mui/x-date-pickers/AdapterDateFns';
import { LocalizationProvider } from '#mui/x-date-pickers/LocalizationProvider';
import { DatePicker } from '#mui/x-date-pickers/DatePicker';
import {es} from 'date-fns/locale'
import {TextField} from "#mui/material";
export const MyDatePicker = () => {
const [value, setValue] = React.useState(new Date())
const handleChange = (newValue) => {
setValue(newValue)
}
return (
<LocalizationProvider locale={es} dateAdapter={AdapterDateFns}>
<DatePicker
label="Date desktop"
inputFormat="dd-MMMM-yyyyy"
value={value}
onChange={handleChange}
renderInput={(params) => <TextField {...params} />}
/>
</LocalizationProvider>
)
}
Another solution is to use date-fns in addition to the #Shikyo solution: :
import DateFnsUtils from "#date-io/date-fns";
import { fr } from "date-fns/locale";
<MuiPickersUtilsProvider locale={fr} utils={DateFnsUtils}>
...
</MuiPickersUtilsProvider>
It's on the bottom of the documentation page https://material-ui-pickers.dev/api/DatePicker
<DatePicker
okLabel="Text"
clearLabel="Text"
cancelLabel="Text"
...
/>
If you are using day.js adapter
import "dayjs/locale/es";
import "dayjs/locale/en";
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale="es">
or
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale="en">
with i18
<LocalizationProvider dateAdapter={AdapterDayjs} adapterLocale=
{i18n?.language}>
with moment.js
import 'moment/locale/es'
import 'moment/locale/en'
The following page of the docs looks like it should be what you want: https://material-ui-pickers.firebaseapp.com/localization/date-fns
This page has an example of changing the locale with a menu item. Specifically, if you want to just provide one locale you can pass the prop locale into the component MuiPickersUtilsProvider.
You should import the locales depending on the adapter, here is a possible solution with dayjs in the Material UI documentation:
https://mui.com/x/react-date-pickers/localization/

React Js - clear Material UI Datepicker

I'm having some troubles with Material UI's Datepicker, I'm searching a way to reset its field but I didn't find a way to do it.
I've also consulted this issue.
Can someone help me please?
You can set the value property as null and it will clear it.
I found the same issue, researched, and got a solution for that,
We can use InputAdornments to have a close button in that.
Remember, we have to use event.stopPropagation() to stop the DatePicker pop up on click of Close button.
You may have a look at here, https://codesandbox.io/s/material-ui-pickers-usage-demo-forked-tptz3?file=/src/App.jsx or https://tptz3.csb.app/
import React, { useState } from "react";
import DateFnsUtils from "#date-io/date-fns";
import { MuiPickersUtilsProvider, DatePicker } from "#material-ui/pickers";
import ClearIcon from "#material-ui/icons/Clear";
import { IconButton } from "#material-ui/core";
function App() {
const [selectedDate, handleDateChange] = useState(new Date());
function handleClr(e) {
e.stopPropagation();
handleDateChange(null);
}
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<DatePicker
autoOk
variant="inline"
format="dd/MM/yyyy"
value={selectedDate}
onChange={handleDateChange}
InputProps={{
endAdornment: (
<IconButton onClick={(e) => handleClr(e)}>
<ClearIcon />
</IconButton>
)
}}
/>
</MuiPickersUtilsProvider>
);
}
export default App;
null clears datepicker, as an example take a look at https://codesandbox.io/s/material-ui-pickers-clear-hhlvg
In material ui version upper 4.0.0 you have to set empty string to reset the date pickers.
value={person.birthday ?? ''}

Resources