[英]How to override border-color in outlined styled TextField component in Material-UI
The task is to use Material UI outlined input field ( TextField
component from "@material-ui/core": "^4.12.2",
) and style it with custom blue colour.任务是使用 Material UI 概述的输入字段(来自"@material-ui/core": "^4.12.2",
TextField
组件)并使用自定义蓝色对其进行样式设置。
Here's what I managed to achieve:这是我设法实现的目标:
Current theme override for InputLabel: InputLabel 的当前主题覆盖:
import { createTheme } from "@material-ui/core/styles";
export const muiTheme = createTheme({
overrides: {
MuiInputLabel: {
outlined: {
"&$focused": {
color: "#3f3fa0",
},
},
},
},
},
The above general theme override helps change the label ("Email address") colour to blue.上述通用主题覆盖有助于将 label(“电子邮件地址”)颜色更改为蓝色。 The blue border-color
is achieved by wrapping TextField
in a styled component with the below CSS:蓝色border-color
是通过将TextField
包装在具有以下 CSS 的样式组件中来实现的:
import styled from "styled-components";
import { TextField } from "@material-ui/core";
export const CustomTextField = styled(TextField)`
.MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline {
border-color: #3f3fa0;
}
`;
I render the above component like this (inside of ThemeProvider
):我像这样渲染上面的组件(在ThemeProvider
内部):
<CustomTextField
variant="outlined"
label={label}
/>
This hacky solution works, as seen on the image I supplied above, however, sometimes Mui code decides to append random numbers to their class names and this causes my custom (hacky) CSS solution to fail and it ends up looking like this:这个 hacky 解决方案有效,如我上面提供的图像所示,但是,有时 Mui 代码决定将 append 随机数添加到其 class 名称,这导致我的自定义(hacky)Z2C56C360580420D293172F42D8 最终看起来像这样
I tried to add almost any combination I could think of to the Mui overrides object to override the border-color
property but could not figure it out.我尝试将我能想到的几乎任何组合添加到 Mui 覆盖 object 以覆盖border-color
属性,但无法弄清楚。 Can you help?你能帮我吗?
Here's how to override border-color on OutlinedInput (material-ui v4).这是在 OutlinedInput (material-ui v4) 上覆盖边框颜色的方法。
I made acodesandbox here我在这里做了一个codesandbox
const defaultColor = "#ff0000";
const hoverColor = "#0000ff";
const focusColor = "#00ff00";
const theme = createTheme({
overrides: {
MuiOutlinedInput: {
root: {
// Hover state
"&:hover $notchedOutline": {
borderColor: hoverColor
},
// Focused state
"&$focused $notchedOutline": {
borderColor: focusColor
}
},
// Default State
notchedOutline: {
borderColor: defaultColor
}
}
}
});
Well, I am not that experienced with material-ui
but this seems to be something you are looking for.好吧,我对material-ui
不是很有经验,但这似乎是您正在寻找的东西。 https://mui.com/styles/api/#examples https://mui.com/styles/api/#examples
Using that, I think it is possible to customize classname
.使用它,我认为可以自定义classname
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.