Search code examples
reactjsmaterial-uidate-fns

How to set the placeholder text of the MUI DatePicker?


How can I set the placeholder text of the MUI DatePicker. The text that is displayed when deleting the text in the input field. I want to set the text to "tt.mm.jjjj" and I always the following error message:

Format string contains an unescaped latin alphabet character `j`

Sandbox

<DatePicker
  inputFormat="tt.mm.jjjj"
    label="Basic example"
    value={value}
    onChange={(newValue) => {
      setValue(newValue);
    }}
    renderInput={(params) => <TextField placeholder="tt.mm.jjjj" {...params} />}
  />

Solution

  • This is how you reset the placeholder of the TextField inside the DatePicker. The reason it doesn't work is because it is overridden by the params.inputProps provided by the DatePicker itself based on the inputFormat:

    <DatePicker
      {...}
      inputFormat="tt.mm.yyyy"
      renderInput={(params) => {
        console.log(params);
        return (
          <TextField
            {...params}
            inputProps={{
              ...params.inputProps,
              placeholder: "tt.mm.jjjj"
            }}
          />
        );
      }}
    />
    

    Codesandbox Demo