Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material ui Textfield type date, min not working

I am trying to add a min date from where the pick can start but the min date is not working.

<TextField
    id="date"
    type="date"
    defaultValue="2017-05-24"
    minDate="24/01/2019"
    InputLabelProps={{
      shrink: true
    }}
/>

Does anyone have any idea what the issue is?

like image 609
Mihail Avatar asked Jul 16 '19 13:07

Mihail


Video Answer


2 Answers

This should do the trick

<TextField
  InputProps={{inputProps: { min: "2020-05-01", max: "2020-05-04"} }}
  type="date"
  defaultValue="2019-05-24"
/>
like image 68
Bello Damilola Avatar answered Oct 17 '22 02:10

Bello Damilola


If you are just trying to leverage the HTML date attributes, you can specify the min and max using inputProps:

import React from "react";
import ReactDOM from "react-dom";

import TextField from "@material-ui/core/TextField";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  input: {
    "&:valid": {
      backgroundColor: "yellow"
    },
    "&:invalid": {
      backgroundColor: "red"
    }
  }
});

function App() {
  const classes = useStyles();
  return (
    <div>
      <TextField
        InputProps={{ classes: classes }}
        type="date"
        defaultValue="2019-05-24"
        inputProps={{ min: "2019-01-24", max: "2020-05-31" }}
      />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit TextField type date

You can find more about the constraint validation API here.

If you are looking for a more full-featured date picker, that functionality is not provided by TextField. Instead, you should check out Material UI Pickers.

like image 43
Ryan Cogswell Avatar answered Oct 17 '22 04:10

Ryan Cogswell