简体   繁体   English

如何在 Material-UI 中的轮廓样式 TextField 组件中覆盖边框颜色

[英]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 最终看起来像这样

带有随机 CSS 类的 TextField

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.

相关问题 反应如何有条件地覆盖 Material-UI 中的 TextField 错误颜色? - React How to conditionally override TextField error color in Material-UI? 如何为禁用的 Material UI TextField 覆盖全局边框颜色 styles? - How to override global border color styles for disabled Material UI TextField? 有没有办法增加material-ui中轮廓按钮的边框厚度? - Is there a way to increase the border thickness of outlined buttons in material-ui? 如何使用 Styled-Component 和 Material-UI 主题化组件 - How to theme components with Styled-Component and Material-UI 如何将props传递给Material-ui样式的组件? - How to pass props to a material-ui styled component? Material-UI 中的 Mask Textfield 组件 - Mask Textfield component in Material-UI 如何在 Material-ui 的 TableSortText 组件中自定义颜色文本和图标? - How to custom color text and icon in TableSortText component of Material-ui? 如何在打开下拉菜单时更改 Material UI Select 组件(在轮廓模式下)的轮廓颜色(当前为蓝色) - How to change outline color of Material UI Select component (in outlined mode) when dropdown is opened ( currently it is blue ) 如何设置 material-ui 文本字段的样式 - How to style material-ui textfield 如何在 Material-UI React 中更改 TextField 的边框半径? - How can I change the border radius of a TextField in Material-UI React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM