14

In my React (v16.3) app I am rendering date-picker controls using material-ui-pickers library's DatePicker component. This component renders a Material-UI TextField component. I like to change this so it renders only a Material-UI Input without the chrome rendered by TextField.

As I understood one can to this with the DatePickers TextFieldComponent field (here at the bottom) but I couldn't figure out how to use this field.

 <DatePicker 
     id={name}
     TextFieldComponent={...<HOW_TO> ...}
     value={value} 
     onChange={this.handleChange}
     disabled={isReadOnly} />

Any ideas how to do this?

Update: Got one step further by finding the right syntax to use and not its rendering without the chrome (FormControl, InputLabel, etc.). But also no DatePicker is opened anymore. Do I have to open it programmatically?

<DatePicker 
    id={name}
    TextFieldComponent={(props) => this.renderInput(props)}
    value={value} 
    onChange={this.handleChange}
    disabled={isReadOnly} />

And here is renderInput():

renderInput(props: TextFieldProps): any {
   return ( <Input
     id={props.id}
     value={props.value}
     onChange={this.handleChange}
     type={'text'}
     disabled={props.disabled}
   /> );
 }
Marc
  • 4,715
  • 3
  • 27
  • 34
  • Are you using latest version? material-ui@next? If latest, try: inputComponent union: string | func The component used for the native input. Either a string to use a DOM element or a component. – Adolfo Onrubia Apr 18 '18 at 12:34
  • I use the latest vnext material-ui and material-ui-pickers. I can't see anything called 'inputComponent'. Only 'TextFieldComponent'. – Marc Apr 18 '18 at 12:49
  • Got it what you mean. That's exactly what I try to do but without success. Got it running to the point it only renders the input but now no data-picker is shown anymore. I guess I need to open it grammatically. See "update". – Marc Apr 18 '18 at 13:01
  • 1
    Try adding `onClick={props.onClick}` to your `Input` component. – Luke Peavey Apr 18 '18 at 15:11

1 Answers1

20

Updated October 2020

@material-ui/pickers v3.x (latest)

To render an Input instead of a TextField component, you can pass a custom component to the TextFieldComponent prop on DatePicker.

You will need to pass along the following props to the Input: value, onChange, and onClick.

import React, { useState } from "react";
import { Input, TextFieldProps } from "@material-ui/core";
import { DatePicker } from "@material-ui/pickers";
import { MaterialUiPickersDate } from "@material-ui/pickers/typings/date";

export default function Demo() {
  const [selectedDate, setSelectedDate] = useState<MaterialUiPickersDate>(
    new Date()
  );

  const renderInput = (props: TextFieldProps): any => (
    <Input
      type="text"
      onClick={props.onClick}
      value={props.value}
      onChange={props.onChange}
    />
  );

  return (
    <DatePicker
      label="Basic example"
      value={selectedDate}
      onChange={setSelectedDate}
      TextFieldComponent={renderInput}
    />
  );
}

Edit Material UI Pickers Example

@material-ui/pickers v4.0.0-alpha12 (next)

NOTE: v4 is currently in alpha, so the API might change in the future.

In V4, you use the renderInput prop on DatePicker to customize the input (see docs). The following example renders a material ui Input instead of a TextField.

import React, { useState } from "react";
import { Input, TextFieldProps } from "@material-ui/core";
import { DatePicker } from "@material-ui/pickers";

export default function Demo() {
  const [selectedDate, setSelectedDate] = useState<Date | null>(new Date());

  const renderInput = (props: TextFieldProps): any => (
    <Input
      type="text"
      inputRef={props.inputRef}
      inputProps={props.inputProps}
      value={props.value}
      onClick={props.onClick}
      onChange={props.onChange}
      endAdornment={props.InputProps?.endAdornment}
    />
  );

  return (
    <DatePicker
      label="Basic example"
      value={selectedDate}
      onChange={setSelectedDate}
      renderInput={renderInput}
    />
  );
}

Edit Material UI Pickers v4 Example

Luke Peavey
  • 3,777
  • 22
  • 24
  • The Code Sandbox is returning an error at the moment – Matt Stobbs Apr 30 '20 at 13:55
  • 1
    Thanks for the heads up! This answer was written in 2018 so it refers to older versions of Material UI and Material UI Pickers. The CodeSandbox demo needs to be rebuilt using current versions of those libraries. I temporarily removed the link while I work on updating it. – Luke Peavey May 03 '20 at 07:55
  • @LukePeavey you ever get around to figuring out how this is done with current API? It's giving me a lot of trouble with `"@material-ui/pickers": "^4.0.0-alpha.9"` – Murcielago Oct 04 '20 at 00:07
  • @Murcielago I'll take a look at it this weekend and see if I can update it for current versions of material-ui. – Luke Peavey Oct 04 '20 at 07:22
  • @Murcielago I updated my answer and added a new sandbox demo that uses the latest version of both libraries. Let me know if this solution works for you. – Luke Peavey Oct 04 '20 at 08:39
  • @Murcielago Sorry just realized you were asking about v4 alpha. I will take a look at the v4 API tomorrow and add a second example – Luke Peavey Oct 04 '20 at 08:54
  • @Murcielago I added an example using @material-ui/pickers 4.0.0-alpha – Luke Peavey Oct 04 '20 at 21:54
  • Hi @Luke Peavey, sorry to bother you. I'm trying to achieve this. Everything is working fine except that my picker is getting displayed out of place. I have this error : "Material-UI: The `anchorEl` prop provided to the component is invalid". Do you have any idea? Thanks for your help – Nicolas Ould Bouamama Nov 06 '20 at 07:52
  • @NicolasOuldBouamama Not sure what would be causing that. It's hard to say without seeing your code. I would suggest creating a new question for this. Id be happy to take a look at it! – Luke Peavey Nov 06 '20 at 13:59
  • @LukePeavey thanks for your answer. I just found that using TextFieldComponent with "inline" variant is causing the issue with v3.x so I changed the variant to "dialog" and now everything is working fine. Thanks anyways – Nicolas Ould Bouamama Nov 06 '20 at 20:38
  • @NicolasOuldBouamama glad you were able to figure it out – Luke Peavey Nov 07 '20 at 10:43