简体   繁体   English

在 Material UI 中,如何仅在用户输入文本后更改 TextField 的边框颜色?

[英]In Material UI, how can I change the border color of the TextField only after the user enters text?

Given a TextField , is there a way I can detect that it has text input, and then style the border only on that condition?给定一个TextField ,有没有一种方法可以检测到它有文本输入,然后仅在该条件下设置边框样式?

I'm using the outlined TextField .我正在使用概述的TextField It has placeholder text when empty.为空时它有占位符文本。

<TextField variant="outlined" />

Currently I've only succeeded in changing the text input color when it's filled by detecting if there's placeholder text currently visible.目前,我只通过检测当前是否有占位符文本来成功更改文本输入颜色。

In my overrides:在我的覆盖中:

MuiOutlinedInput: {
  root: {
    '& $notchedOutline': {
      borderColor: 'green', // default colour, want to change this when field has input
    },
  },
  input: {
    '&:not(:placeholder-shown)': {
      color: 'red', // input field text becomes red when input is provided
    },
  },
},

Code Sandbox 代码沙盒

I want the border to also become red here when the user enters text.当用户输入文本时,我希望边框在这里也变成红色。 I can't get the placeholder trick to work on changing the border color because the border by default is on the <fieldset> not the <input> .我无法使用占位符技巧来更改边框颜色,因为默认情况下边框位于<fieldset>而不是<input>上。 Does anyone have any ideas?有人有什么想法吗?

Because CSS has no pseudo selector to detect whether input is empty or not ( this question ), so you must programmatically detect input and override the TextField border color因为 CSS 没有伪选择器来检测输入是否为空( 这个问题),所以你必须以编程方式检测输入并覆盖 TextField 边框颜色

<TextField
  id="fullName"
  placeholder="Full name"
  variant="outlined"
  value={this.state.inputValue}
  onChange={(e) => this.setState({ inputValue: e.target.value })}
  InputProps={{
    classes: {
      notchedOutline: this.state.inputValue && classes.greenBorder
    }
  }}
/>

Codesandbox for demo用于演示的 Codesandbox

编辑 material-ui 文本字段(分叉)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Material Ui TextField 如何改变边框的颜色 - Material Ui TextField how to change color of border 在材质UI TextField上悬停时更改边框颜色 - change border color on hover for Material UI TextField Material UI:如何更改 Select 组件的边框颜色? - Material UI: How can I change the border color of the Select component? 如何更改 Material-ui 中的 TextField 悬停边框效果? - how can i change TextField hover border effect in material-ui? 材质 ui,如何更改自动完成文本字段的背景颜色、字体颜色、边框颜色 - material ui, how to change background color, font color, border color of autocomplete textfield 如果反应/材质 ui 中需要道具,则更改 TextField 边框颜色 - Change TextField border color if props is required in a react/material ui 如何更改 Material UI 对话框边框的粗细和颜色? - How do I change the weight and color of the border of the Material UI Dialog? 如何更改材质 ui TextField 的 label 标高? - How can I change the label elevation of a material ui TextField? 如何更改材质 ui TextField 的 label 大小? - How can I change the label size of a material ui TextField? 有没有办法设置边框颜色和文本颜色的样式<TextField/>在 Material-UI 中不使用 makeStyles - Is there a way to style the border color and text color of <TextField/> in Material-UI without using makeStyles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM