Change border color on Material-UI TextField

后端 未结 2 1160
[愿得一人]
[愿得一人] 2020-11-30 16:08

This should be simple but for some reason I can\'t figure out how to change the border color on a TextField



        
相关标签:
2条回答
  • 2020-11-30 16:10

    Below is an example of how to override the color of the outline, label, and input text on the outlined variant of TextField. This shows using three different colors: one for the default (green), one for hover (red), and a different one when the input has focus (purple).

    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({
      root: {
        "& .MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline": {
          borderColor: "green"
        },
        "&:hover .MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline": {
          borderColor: "red"
        },
        "& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": {
          borderColor: "purple"
        },
        "& .MuiOutlinedInput-input": {
          color: "green"
        },
        "&:hover .MuiOutlinedInput-input": {
          color: "red"
        },
        "& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-input": {
          color: "purple"
        },
        "& .MuiInputLabel-outlined": {
          color: "green"
        },
        "&:hover .MuiInputLabel-outlined": {
          color: "red"
        },
        "& .MuiInputLabel-outlined.Mui-focused": {
          color: "purple"
        }
      }
    });
    
    function App() {
      const classes = useStyles();
      return (
        <div className="App">
          <TextField
            className={classes.root}
            defaultValue="My Default Value"
            variant="outlined"
            label="My Label"
          />
        </div>
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    Related answers:

    • Change outline for OutlinedInput with React material-ui
    • How do I override hover notchedOutline for OutlinedInput
    • Global outlined override
    0 讨论(0)
  • 2020-11-30 16:24

    TextField does not accept the style propriety:

    https://codesandbox.io/s/45if2

    what you can do is adding className and style it

    0 讨论(0)
提交回复
热议问题