代码之家  ›  专栏  ›  技术社区  ›  user944513

如何更改必填*字段中的星号颜色

  •  0
  • user944513  · 技术社区  · 7 年前

    我的表单中有两个必填字段。我想要星号的颜色应该是 red .目前显示为黑色。我正在使用材料库? 这是我的密码 https://codesandbox.io/s/r7lq1jnjl4 文件 https://material-ui.com/demos/text-fields/

    <FormControl>
                      <TextField
                        required
                        InputLabelProps={{
                          shrink: true
                        }}
                        id="standard-name"
                        label="Name"
                        margin="normal"
                        helperText="Some important text"
                      />
                    </FormControl>
    
    2 回复  |  直到 7 年前
        1
  •  15
  •   Alvin S. Lee    7 年前

    基于此 documentation on how to customize components through theme overrides 暂时 FormLabel (which will also include InputLabel) ,你应该使用 createMuiTheme 并添加以下覆盖:

    const formLabelsTheme = createMuiTheme({
      overrides: {
        MuiFormLabel: {
          asterisk: {
            color: '#db3131',
            '&$error': {
              color: '#db3131'
            },
          }
        }
      }
    })
    

    然后,你把衣服包起来 <form> 在一分钟之内 <MuiThemeProvider> 就像这样:

    <MuiThemeProvider theme={formLabelsTheme}>
      <form noValidate autoComplete="off">
    
    ...
    ...
    ...
    
      </form>
    </MuiThemeProvider>
    

    这里有一个 forked code sandbox 这将在实际操作中演示此代码。

    由于您已经创建了一个主题,您可以将覆盖放在该主题中,但您需要移动 <表格> <MuiThemeProvider> 代码中已经包含的。

    生成的表单标签如下所示: resulting form, with theme overridden

        2
  •  3
  •   sakshya73    4 年前

    根据最新版本的材料界面。比如。 "@mui/material": "^5.0.1"

    我们可以这样做:

     <FormLabel required>Name:</FormLabel>
    

    在主题中:

    import { createTheme } from "@mui/material";
    
    export const theme = createTheme({
      components: {
        MuiFormLabel: {
          styleOverrides: {
            asterisk: {
              color: "#db3131",
              "&$error": {
                color: "#db3131",
              },
            },
          },
        },
      },
    });
    
        3
  •  2
  •   Ryan Cogswell    7 年前

    阿尔文的回答展示了如何在你的主题中做到这一点。您也可以使用 FormLabel asterisk class 通过 InputLabel props .

    下面是我更改的代码中的相关部分。还请注意,如果输入处于“错误”状态,星号的默认行为是红色。例如,如果您添加 error 财产归 TextField 星号将为红色,但这也会对星号之外的样式产生额外影响。

    const styles = {
      labelAsterisk: {
        color: "red"
      }
    };
    
    <InputLabel
                        FormLabelClasses={{
                          asterisk: this.props.classes.labelAsterisk
                        }}
                        required
                        shrink
                        htmlFor="age-native-simple"
                      >
                        Age
                      </InputLabel>
    
                      <TextField
                        required
                        InputLabelProps={{
                          shrink: true,
                          FormLabelClasses: {
                            asterisk: this.props.classes.labelAsterisk
                          }
                        }}
                        id="standard-name"
                        label="Name"
                        margin="normal"
                        helperText="Some important text"
                      />
    
    const StyledApp = withStyles(styles)(App);
    

    Edit Asterisk

        4
  •  2
  •   Samira    4 年前

    enter image description here

    在里面 梅v5 :

    const theme = createTheme({
    
      components: {
        MuiFormLabel: {
          styleOverrides: {
            asterisk: {color:"red"},
          },
        },
      },
    
    })
    
        5
  •  0
  •   Vijay Mewada Rajput    4 年前

    //在顶部导入createTheme和ThemeProvider

    从“@mui/material/styles”导入{createTheme,ThemeProvider};

    常数abc=()=>{

    //add the theme at the top of your arrow function
    
    const theme = createTheme({
    
        components: {
            MuiFormLabel: {
                styleOverrides: {
                    asterisk: { color: "red" },
                },
            },
        },
    
    })
    return ( // wrap your jsx with <ThemeProvider>
    
        <ThemeProvider theme={theme}>
    
            <TextField required
                id="outlined-required"
                label="Full Name"
                type="text"
                size='small'
            />
    
        </ThemeProvider>
    
    )
    

    }

        6
  •  0
  •   Antolin Bernas    4 年前

    对于那些正在寻找带有文本字段的MUI v5的答案的人

    const theme = createTheme({
       components:{
         MuiInputLabel:{
              styleOverrides:{
                  asterisk:{
                     color:"#d32f2f"
                  }
              }
           }
        }
    });
    
        7
  •  -2
  •   Laxman    6 年前

    试试这个简单易行的方法

    render(){
        const name = <p>Name<span style={{ color: "red" } >*</span></p>
        const email = <p>Email<span style={{ color: "red" } >*</span></p>
        .
        .
        .
        return (
            <div>
    
                <TextField type="text" label={name} />//or Input tag
                <TextField type="email" label={email} />//or Input tag
                .
                .
                .
            </div>
        )
    }
    
    推荐文章