3
Method 1:
const StyledTextField = styled(({ ...rest }) => (
  <OutlinedInput {...rest} classes={{ root: 'outlinedRoot' }} />
))`
  .outlinedRoot {
    .$notchedOutline {
      border-color: red;
    }
  }
  background: #fff;
  border-radius: 4px;
  color: #808080;
  height: 53px;
`;

Method 2:
const StyledTextField = styled(OutlinedInput).attrs({
  classes: { root: 'outlinedRoot', notchedOutline: 'notchedOutline' }
})`
  .outlinedRoot {
    .notchedOutline {
      border-color: red;
    }
  }
  background: #fff;
  border-radius: 4px;
  color: #808080;
  height: 53px;
`;

I have seen also what classes needs to be added and read articles about that, but I am not able to overwrite

I tried above two ways to change the outline color but i am not able to do that

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
Rahul Shrivastava
  • 1,391
  • 3
  • 14
  • 38

1 Answers1

3

Below is an example of one way to do this. To do this with TextField rather than OutlinedInput, just add a space before .MuiOutlinedInput-root so that you match that as a descendant of TextField rather than matching that as one of the other classes on the root element. .MuiOutlinedInput-root is actually unnecessary for the styling except for helping with CSS specificity.

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

import OutlinedInput from "@material-ui/core/OutlinedInput";
import styled from "styled-components";

const StyledOutlinedInput = styled(OutlinedInput)`
  &.MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline {
    border-color: green;
  }
  &:hover.MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline {
    border-color: red;
  }
  &.MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline {
    border-color: purple;
  }
`;

function App() {
  return (
    <div className="App">
      <StyledOutlinedInput defaultValue="My Default Value" />
    </div>
  );
}

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

Edit custom outline styled-components

Related answers:

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