使用 React material-ui 更改 OutlinedInput 的轮廓

Jou*_*uke 7 css reactjs material-ui

快速说明:这不是如何更改 Material UI React 输入组件的轮廓颜色的副本

使用 material-ui (React) 我无法删除悬停或聚焦时的轮廓。我使用这个输入的原因是在出现警告时请求添加一个小的红色边框。我可以更改聚焦和悬停样式。这在下图中进行了测试: 在此处输入图片说明

输入焦点时应用此 CSS 的位置:

outlinedInputFocused: {
     borderStyle: 'none',
     borderColor: 'red',
     outlineWidth: 0,
     outline: 'none',
     backgroundColor: 'green'
  },
Run Code Online (Sandbox Code Playgroud)

成分

 <OutlinedInput
            disableUnderline={true}
            notched={true}
            id="adornment-weight"
            classes={{root: classes.outlinedInput, focused: classes.outlinedInputFocused}}
            value={this.state.budgetValue}
            onChange={evt => this.updateBudgetValue(evt)}
            onKeyPress={evt => this.handleKeyPress(evt)}
            endAdornment={<InputAdornment sposition="end">BTC</InputAdornment>}
          />
Run Code Online (Sandbox Code Playgroud)

如您所见,图像的颜色为绿色,但仍有轮廓。即使outlineWidth 为0 并且在CSS 中将outline 设置为none。如何更改/禁用此大纲?

Rya*_*ell 15

理解如何覆盖这些样式的关键是查看它们在 Material-UI 源代码中是如何定义的。您引用的问题还显示了一些所需的语法。

下面是OutlinedInput.js样式的缩写版本(我省略了与轮廓无关的样式):

export const styles = theme => {
  const borderColor =
    theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.23)' : 'rgba(255, 255, 255, 0.23)';

  return {
    /* Styles applied to the root element. */
    root: {
      position: 'relative',
      '& $notchedOutline': {
        borderColor,
      },
      '&:hover:not($disabled):not($focused):not($error) $notchedOutline': {
        borderColor: theme.palette.text.primary,
        // Reset on touch devices, it doesn't add specificity
        '@media (hover: none)': {
          borderColor,
        },
      },
      '&$focused $notchedOutline': {
        borderColor: theme.palette.primary.main,
        borderWidth: 2,
      },
      '&$error $notchedOutline': {
        borderColor: theme.palette.error.main,
      },
      '&$disabled $notchedOutline': {
        borderColor: theme.palette.action.disabled,
      },
    },
    /* Styles applied to the root element if the component is focused. */
    focused: {},
    /* Styles applied to the root element if `disabled={true}`. */
    disabled: {},
    /* Styles applied to the root element if `error={true}`. */
    error: {},
    /* Styles applied to the `NotchedOutline` element. */
    notchedOutline: {}

  };
};
Run Code Online (Sandbox Code Playgroud)

的“轮廓”OutlinedInput是通过嵌套在其中borderNotchedOutline组件控制的。为了影响该嵌套元素,您需要定义一个“notchedOutline”类(即使是空的),然后您可以使用它来针对父元素的不同状态(例如聚焦、悬停)定位该元素。

这是一个完全删除边框的示例:

import React from "react";
import ReactDOM from "react-dom";
import OutlinedInput from "@material-ui/core/OutlinedInput";
import InputAdornment from "@material-ui/core/InputAdornment";
import { withStyles } from "@material-ui/core/styles";

const styles = theme => ({
  root: {
    "& $notchedOutline": {
      borderWidth: 0
    },
    "&:hover $notchedOutline": {
      borderWidth: 0
    },
    "&$focused $notchedOutline": {
      borderWidth: 0
    }
  },
  focused: {},
  notchedOutline: {}
});
function App(props) {
  const { classes } = props;
  return (
    <div className="App">
      <OutlinedInput
        disableUnderline={true}
        notched={true}
        id="adornment-weight"
        classes={classes}
        value={1}
        endAdornment={<InputAdornment sposition="end">BTC</InputAdornment>}
      />
    </div>
  );
}
const StyledApp = withStyles(styles)(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<StyledApp />, rootElement);
Run Code Online (Sandbox Code Playgroud)

编辑 94k34o11np


小智 0

OutlinedInput的设计方式是您无法关闭其轮廓,您必须使用TextFieldvariant轮廓”作为默认值,并且在焦点上使用“无”。您可以在此处Outlined Input Adornments查看使用示例TextField