简体   繁体   English

如何仅在第一次单击 MUI React 后调用无效的红色边框样式?

[英]How to invoke invalid red border styling only after first click in MUI React?

I have been using MUI Textfields, and would like the field to have a red outline only after the first click, and if the input is blank.我一直在使用 MUI 文本字段,并且希望该字段仅在第一次单击后具有红色轮廓,并且如果输入为空白。 So the field is normal, but once the person clicks on the field, if he deletes or leaves the field blank, the outline should become red.所以这个字段是正常的,但是一旦这个人点击了这个字段,如果他删除了这个字段或者把这个字段留空,轮廓应该变成红色。 The styling below immediately creates a red border on render.下面的样式会立即在渲染上创建一个红色边框。 What should I change?我应该改变什么?

在此处输入图像描述

A few more questions regarding this:关于这个还有几个问题:

  1. Currently I have the field being called required so as to check for errors, is there a way to remove the asterisk from using the required keyword?目前我有被称为 required 的字段以检查错误,有没有办法从使用 required 关键字中删除星号?

  2. By adding a backgroundColor variable, it seems that the z-index is higher than the text in the field, leading to the whole field being coloured and the text being hidden behind.通过添加 backgroundColor 变量,似乎 z-index 高于字段中的文本,导致整个字段被着色,文本被隐藏在后面。 What can I do to fix it?我能做些什么来修复它?

When setting backgroundColor:设置背景颜色时:

在此处输入图像描述

Textfield Styling:文本域样式:

const ValidationTextField = styled(TextFieldStyled)({
  '& input:valid + fieldset': {
    borderColor: 'green',
    borderWidth: 2,
    // backgroundColor: 'green'; // This covers the whole input so that the text input is now blocked.
  },
  '& input:invalid + fieldset': {
    borderColor: 'red',
    borderWidth: 2,
  }
});

This is the class that uses the textfield:这是使用文本字段的 class:

class CompanyField extends React.Component {

  render() {
    return (
      <ValidationTextField
        id = "outlined-basic" 
        label = "Company" 
        variant = "outlined"
        fullWidth
        required

        value={this.props.value}
        onChange={this.props.handleChange}
      />
    );
  }
}

Thank you!谢谢!

  1. Access the InputLabelProps of textfield and set required to false.访问 textfield 的 InputLabelProps 并将 required 设置为 false。 The input will still be required but the label is treated as not required.输入仍然是必需的,但 label 被视为不需要。 (shown in my example below) (在我下面的示例中显示)

  2. Don't target the input background, just set the backgroundColor of the textfield (shown in my example below)不要以输入背景为目标,只需设置文本字段的背景颜色(如下面的示例所示)

To achieve your red border styling, create a state variable to track whether the component has been clicked yet, then using the textfields onFocus prop, use a function that changes the state to 'has been clicked'.要实现红色边框样式,请创建一个 state 变量来跟踪组件是否已被单击,然后使用文本字段 onFocus 属性,使用 function 将 state 更改为“已被单击”。 Use that variable to apply styles to the textfield:使用该变量将 styles 应用于文本字段:

https://codesandbox.io/s/cool-varahamihira-mio1qw?file=/src/App.js https://codesandbox.io/s/cool-varahamihira-mio1qw?file=/src/App.js

import { TextField, Box, Button } from "@mui/material";
import { useState } from "react";
import "./styles.css";

const ValidationTextField = ({ ...args }) => {
  const [firstClick, setFirstClick] = useState(true);

  return (
    <TextField
      {...args}
      onFocus={() => setFirstClick(false)}
      InputLabelProps={{
        required: false
      }}
      sx={{
        backgroundColor: "green",
        "& input:valid + fieldset": {
          borderColor: "green",
          borderWidth: 2
        },
        "& input:invalid + fieldset": {
          borderColor: firstClick ? "green" : "red",
          borderWidth: 2
        }
      }}
    />
  );
};

export default function App() {
  return (
    <div className="App">
      <Box component="form">
        <ValidationTextField
          id="outlined-basic"
          label="Company"
          variant="outlined"
          fullWidth
          required
        />
        <Button type="submit">Submit</Button>
      </Box>
    </div>
  );
}

Sorry for posting an edit here, but Stackoverflow does not allow me to edit my previous post.很抱歉在这里发布编辑,但 Stackoverflow 不允许我编辑我以前的帖子。 coot3's method does work for my problem, however, I am finding it difficult to use this on MUI's autocomplete: coot3 的方法确实适用于我的问题,但是,我发现很难在 MUI 的自动完成中使用它:

在此处输入图像描述

Suggested changes for styling:样式的建议更改:

const ValidationDropDown = ({ ...args }) => {
    const [firstClick, setFirstClick] = useState(true);
  
    return (
      <DropDownStyled //Autocomplete Item
        {...args}
        onFocus={() => setFirstClick(false)}
        InputLabelProps={{
          required: false
        }}
        sx={{
          backgroundColor: "green",
          "& input:valid + fieldset": {
            borderColor: "green",
            borderWidth: 2
          },
          "& input:invalid + fieldset": {
            borderColor: firstClick ? "" : "red",
            borderWidth: 2
          }
        }}
      />
    );
  };

The class: class:

class JobTypeField extends React.Component {

    render() {
        return (
            <ValidationDropDown
                id="combo-box-demo"
                options={jobType}
                required

                onChange={this.props.handleChange}
                renderInput={(params) => <TextField {...params} required label="Job Type" />}
            />
        );
    }
}

The autocomplete background did change colour, but the state does not appear to be applied, hence a lack of colour change in the border.自动完成背景确实改变了颜色,但 state 似乎没有应用,因此边框没有颜色变化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM