How do I custom style the underline of Material-UI without using theme?

前端 未结 3 1615
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 16:24

I have success with outline custom styling when variant=\"outlined\" and I use notchedOutline in InputProps.

Otherwise -

相关标签:
3条回答
  • 2020-11-28 16:49

    In order to determine how to do this, it is helpful to look at how the default styling is done within Input.

    :before is used for the default and hover styling and :after is used for the focused styling.

    Here is a working example of how to style it:

    import React from "react";
    import ReactDOM from "react-dom";
    import TextField from "@material-ui/core/TextField";
    import { withStyles } from "@material-ui/core/styles";
    const styles = {
      underline: {
        "&:before": {
          borderBottom: "2px solid green"
        },
        "&:hover:not($disabled):not($focused):not($error):before": {
          borderBottom: "2px solid blue"
        },
        "&:after": {
          borderBottom: "3px solid purple"
        }
      },
      disabled: {},
      focused: {},
      error: {}
    };
    function App({ classes }) {
      return (
        <div className="App">
          <TextField InputProps={{ classes }} />
        </div>
      );
    }
    const StyledApp = withStyles(styles)(App);
    const rootElement = document.getElementById("root");
    ReactDOM.render(<StyledApp />, rootElement);
    

    0 讨论(0)
  • 2020-11-28 16:52

    You can just use InputProps={{ disableUnderline: true }}.It will disable the underLine of textField in all cases. Tested on react-material-ui version 3 and above.

    0 讨论(0)
  • 2020-11-28 16:52

    Not sure which version of material-ui you are using, but you can override classes as needed, see the following API documentation:

    https://material-ui.com/api/outlined-input/#demos

    https://material-ui.com/api/outlined-input/

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