Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
MUI DatePicker's shouldDisableDate prop leading to error
<Controller
   name="toDate"
   control={control}
   defaultValue={null}
   render={({ field }) => (
    <DatePicker 
        format="DD/MM/yyyy"
        value={field.value}
        onChange={(e) => { setToDate(e); field.onChange(e); }}
        minDate={fromDate}
        shouldDisableDate={fromDate}
        />
    )}
    {...register("toDate",{ required: true })}
/>

I used the shouldDisableProp to disable the date which is selected inside another datepicker whose value is stored in a state variable 'fromDate'. But when I press the datepicker it is leading to this error.

The minDate is working perfectly fine using the fromDate state variable.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The shouldDisableDate method receives a date param and expect a boolean.

import * as React from "react";
import TextField from "@mui/material/TextField";
import AdapterDateFns from "@mui/lab/AdapterDateFns";
import LocalizationProvider from "@mui/lab/LocalizationProvider";
import DatePicker from "@mui/lab/DatePicker";

export default function BasicDatePicker() {
  const [value, setValue] = React.useState<Date | null>(null);
  const [fromDate, setFromDate] = React.useState<Date | null>(null);

  return (
    <>
      <LocalizationProvider dateAdapter={AdapterDateFns}>
        <DatePicker
          label="From Date"
          value={fromDate}
          onChange={(newValue) => {
            setFromDate(newValue);
          }}
          renderInput={(params) => <TextField {...params} />}
        />
      </LocalizationProvider>
      <br />
      <LocalizationProvider dateAdapter={AdapterDateFns}>
        <DatePicker
          label="To Date"
          value={value}
          onChange={(newValue) => {
            setValue(newValue);
          }}
          minDate={fromDate}
          shouldDisableDate={(dateParam) => {
            //your_condition here
            //return true to disabled and false to enable
            const fromFormatted = fromDate.toISOString().split("T")[0];
            const currentDate = dateParam.toISOString().split("T")[0];

            return fromFormatted === currentDate ? true : false;
          }}
          renderInput={(params) => <TextField {...params} />}
        />
      </LocalizationProvider>
    </>
  );
}


UpdatedCode

So do something like that and you will achieve your goal. In your case, compare dateParam with your fromDate var, to return true or false and disable or not the date.

Edit: Now is adapted to your real case. Just check it out.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!