Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda