3

I'm building a new component from TextField and I need to apply the same style for text fields with the readOnly than disabled property.

So, I was trying to use the property className but it does not work.

// some logic here ..

<TextField
    {...props}
    className={readOnly ? 'Mui-disabled' : undefined}
 />

So, I also tried to use the classses prop, but I don't know how to get the current config from the disabled class.

fingerprints
  • 2,751
  • 1
  • 25
  • 45
  • Try the prop `classes` instead of `className`. https://material-ui.com/api/text-field/ – sallf Oct 02 '19 at 17:38
  • @sallf as I mentioned in my question, I tried to use `classes` prop, but I don't know how to get the style from `Mui-disabled`. Do you have a suggestion for it? – fingerprints Oct 02 '19 at 17:51

3 Answers3

6

When you use the disabled property, Material-UI places the Mui-disabled class on many different elements. To get the equivalent look, you need to add it to all the same elements.

Below is an example of how to do this. In addition to adding the Mui-disabled class, it is also necessary to override the "focused" styling of the underline (handled via the :after pseudo-class).

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

import TextField from "@material-ui/core/TextField";
import { makeStyles } from "@material-ui/core/styles";
const disabledClassNameProps = { className: "Mui-disabled" };
const useStyles = makeStyles(theme => {
  const light = theme.palette.type === "light";
  const bottomLineColor = light
    ? "rgba(0, 0, 0, 0.42)"
    : "rgba(255, 255, 255, 0.7)";
  return {
    underline: {
      "&.Mui-disabled:after": {
        borderBottom: `1px dotted ${bottomLineColor}`,
        transform: "scaleX(0)"
      }
    }
  };
});
function App() {
  const classes = useStyles();
  return (
    <div className="App">
      <TextField
        {...disabledClassNameProps}
        inputProps={{ readOnly: true }}
        InputProps={{ ...disabledClassNameProps, classes }}
        InputLabelProps={disabledClassNameProps}
        FormHelperTextProps={disabledClassNameProps}
        label="Test Disabled Look"
        defaultValue="Some Text"
        helperText="Helper Text"
      />
    </div>
  );
}

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

Edit TextField readOnly looking disabled

Related answers:

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
0

After some digging, I found that the InputProps also have the className property. The following worked for me:

<TextField
    {...props}
    inputProps={{
        readOnly,
    }}
    InputProps={{
        className: (readOnly) ? 'Mui-disabled' : undefined,
    }}
/>

To see all the properties you can look here.

fingerprints
  • 2,751
  • 1
  • 25
  • 45
  • 1
    If you use helper text or labels this will not be sufficient to get the disabled look. Also if you click on the input the underline will still change color (unless you're using the filled or outlined variant). – Ryan Cogswell Oct 02 '19 at 18:41
-2

You need to use classes on TextField

<TextField
 {...props}
 classes={readOnly ? { root: 'Mui-disabled' } : {}}
/>

Hope it helps

ibtsam
  • 1,620
  • 1
  • 10
  • 10