KeyboardDatePicker grayout days - reactjs

So I have this KeyboardDatePicker and I want to gray out past days and some feature days that. those grayed should not be clickable.
here how my looks now
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="dd/MM/yyyy"
margin="normal"
id="date-picker-inline"
label="Select Date"
value={this.state.selectedDate}
onChange={(e) => {this.getTimeSchedule(e); this.handleChangeDate(e)}}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
autoOk={true}
/>
</MuiPickersUtilsProvider>
I read few explanation on the internet on how it can be done here enter link description here
it should look like this
renderDay={(day, selectedDate, dayInCurrentMonth, dayComponent) => {
if (isHoliday) {
return 'Special component';
} else {
return dayComponent;
}
}}
however I am confused on how to make "Special component"

You don't have to use a custom renderer, you can use the following KeyboardDatePicker props:
disablePast will disable the past dates
shouldDisableDate will allow you to use your own rule to disable dates
In the following example I disable all the past dates (with disablePast) and all the sundays (with shouldDisableDate):
function disableSundays(date) {
if (date.getDay() == 0) return true;
}
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="dd/MM/yyyy"
margin="normal"
id="date-picker-inline"
label="Select Date"
value={this.state.selectedDate}
onChange={(e) => {this.getTimeSchedule(e); this.handleChangeDate(e)}}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
autoOk={true}
disablePast={true}
shouldDisableDate={disableSundays}
/>
</MuiPickersUtilsProvider>
);

Related

Can we remove the calendar icon from the TextField (MUI)?

Here is my basic code to accomplish the task, but couldn't come up with any outcome
<TextField
sx={{
'&::-webkit-calendar-picker-indicator': {
display: 'none',
'-webkit-appearance': 'none',
},
}}
id="outlined-basic"
variant="outlined"
type="date"
helperText="Please select the date"
/>
Also, I did a bit research on InputProps (endAdornment) within TextField to remove the icon, but that didn't work.
You can define in the components property the icon to be null for both cases.
<TimePicker
label="Time"
value={value}
onChange={handleChange}
renderInput={(params) => <TextField {...params} />}
disableOpenPicker
/>
<DateTimePicker
label="Date&Time picker"
value={value}
onChange={handleChange}
renderInput={(params) => <TextField {...params} />}
disableOpenPicker
/>
Here is a working sandbox
I guess u r using MUIv5
(and I guess u talking about DatePicker)
In that case (as u mentioned), the icon is rendered cuz the InputProps with endAdornment is passed to the text field, if u omit that prop, there will be no icon.
<DatePicker
label="Basic example"
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
renderInput={({ InputProps, ...props }) => (
<TextField {...props} />
)}
/>

React MUI datepicker gives me a datetime not a date

Even though I have initialized the format as a prop in DesktopDatePicker, when I change the value it comes like this: 2020-01-01T00:00:00.000Z
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DesktopDatePicker
inputVariant="outlined"
id="date-picker-dialog"
label="Birthday"
fullWidth
name="dob"
onChange={(val) => {
formik.setFieldValue("dob", val);
}}
renderInput={(params) => <TextField {...params} />}
onBlur={formik.handleBlur}
value={formik.values.dob}
format="yyyy-MM-dd"
error={formik.errors.dob && formik.touched.dob}
/>
</LocalizationProvider>

How to validate KeyboardDatePicker with react-hook-form when date is invalid

I am validating a material UI KeyboardDatePicker with react-hook-form:
<Controller
name="endDate"
control={control}
defaultValue={new Date()}
rules={{
required: "End date is required"
}}
render={({ field, fieldState }) => {
return (
<KeyboardDatePicker
autoOk
variant="inline"
inputVariant="standard"
label="End Date"
format="MM/dd/yyyy"
value={field.value}
InputAdornmentProps={{ position: "start" }}
onChange={() => {
field.onChange();
}}
error={(!!fieldState.error || field.value === 'Invalid Date')}
helperText={
fieldState.error ? fieldState.error.message : null
}
/>
);
}}
/>
It works fine and throws the message "End date is required" if KeyboardDatePicker is empty. However I also need to throw an error message "Invalid date format" if user edits the date and removes a couple of characters like '06/01/2021' to '06/01/20__'. This functionality works built-in and works fine in isolation i.e. if I don't combine date picker with react-hook-form.
However I am not getting how to achieve this in combination with react-hook-form. I tried to modify helperText but to no avail.
This is how I do it :
<Controller
name="endDate"
control={control}
defaultValue={new Date()}
rules={{ required: true }}
render={({ field: { ref, ...rest } }) => (
<KeyboardDatePicker
margin="normal"
id="date-picker-dialog"
format="dd/MM/yyyy"
disablePast={true}
initialFocusedDate={Date.now()}
KeyboardButtonProps={{
"aria-label": "change end date",
}}
invalidDateMessage={"End date is required"}
{...rest}
/>
)}
/>

How can I change the icon in material-ui date picker for React

How can I change the material UI Date picker icon?
I can't see it anywhere in the code or the API sections of the docs.
Here's a link to their docs: https://material-ui.com/components/pickers/
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
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
label="Date picker inline"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
/>
</Grid>
</MuiPickersUtilsProvider>
);
}
Everything else is working properly, I just need to edit the image to a different icon.
First include the Material icon font into your project, via Google Web Fonts:
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
then you need to import the Icon component like so
import Icon from "#material-ui/core/Icon";
Then include the 'keyboardIcon' property in your KeyboardDatePicker component by wrapping the icon name (font ligature) with the Icon component like so:
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
label="Date picker inline"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
keyboardIcon={<Icon>add_circle</Icon>}
/>
If you're using Font Awesome instead then you have to supply the class name using the Icon component's className property:
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
label="Date picker inline"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
keyboardIcon={keyboardIcon={<Icon className="fa fa-plus-circle" />}}
/>
My customize shared ui import {Datepicker} from '#dwp/ui'
<Datepicker
fullWidth
classes={classes}
format="dd/MM/yyyy"
minDateMessage=""
maxDateMessage=""
value={selectedDate}
coloricon={$white}
InputProps={{
classes: {
underline: classes.underline,
disabled: classes.disabled,
},
}}
onChangeDate={(value) => handleDateChange(value)}
disabled
/>
source of shared datepicker from `#dwp/ui
export function Datepicker(props) {
const {
minDate = new Date(),
onChangeDate,
value,
classes,
coloricon,
InputProps,
disabled,
} = props
const onChange = (payload) => onChangeDate(payload)
return (
<MuiPickersUtilsProvider utils={DateFnsUtils} locale={localeMap['au']}>
<KeyboardDatePicker
variant="inline"
minDate={minDate}
format="dd/MM/yyyy"
margin="normal"
placeholder="10/10/2018"
onChange={(payload) => {
onChange(payload)
}}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
value={value || minDate}
keyboardIcon={
<A.QueryBuilderIcon
coloricon={disabled ? `rgba(255, 255, 255, 0.36)` : coloricon}
/>
}
className={classes.underline}
InputProps={InputProps}
disabled={disabled}
/>
</MuiPickersUtilsProvider>
)
}
A.QueryBuilderIcon = styled(QueryBuilderIcon)`
color: ${(props) => props.coloricon};
`
You can use keyboardIcon props in <KeyboardDatePicker component
<KeyboardDatePicker
margin="normal"
id="date-picker-dialog"
format="MM/dd/yyyy"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
keyboardIcon={<img src="https://.../calendar.png" alt="calendar" width="33px" height="33px"/>}
/>
You can also use anouther tag inside this keyboardIcon property (not only <img />):
<svg>
<Icon> (from material ui)
<div>
etc...
components = {{OpenPickerIcon : AccessAlarmIcon}}
//I have set AccessAlarmIcon in material ui datepicker, you can change and set another Icon

Change format date on Datepicker (material-ui)

I want to change the format of the date on DatePicker (material-ui-pickers), but when I use the formatDate feature, the format of the date does not change and still show only month and date, not the year selected.
This is my code:
<DatePicker
margin="normal"
disableFuture
openTo="year"
views={["year", "month", "day"]}
value={selectedDate}
onChange={this.handleDateChange}
maxDate={maxdate}
formatDate={(date) => moment(date).format('DD-MM-YYYY')}
/>
2022 method is:
<DatePicker inputFormat="MM/dd/yyyy" />
The prop for changing the date format is called format and takes a string instead of a function.
<DatePicker
margin="normal"
disableFuture
openTo="year"
views={["year", "month", "day"]}
value={selectedDate}
onChange={this.handleDateChange}
maxDate={maxdate}
format="DD-MM-YYYY"
/>
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DateTimePicker
renderInput={(props) => <TextField variant="standard" className="w-100" required {...props} />}
label="voyage Start Date"
value={formik.values?.voyageStartDate}
onChange={(newValue) => {
formik.setFieldValue("voyageStartDate",moment(newValue).format("YYYY-MM-DD HH:mm:ss"))
}}
onKeyPress={() => formik.setFieldTouched("voyageStartDate")}
/>
</LocalizationProvider>
For 2022 the solution is ampm={false} parameter
<DateTimePicker
ampm={false}
renderInput={...}
/>
Check mui-x doc
Try this:
<DatePicker inputFormat="DD/MM/YYYY" views={["day", "month", "year"]}
value={value} onChange {(newValue) => {setValue(newValue);}} />

Resources