简体   繁体   English

React Material UI TextField FormHelperTextProps 样式不起作用

[英]React Material UI TextField FormHelperTextProps Styling Not Working

I'm trying to style the helper text that comes with the TextField Component given by Material UI ( found here ).我正在尝试设置由 Material UI(在此处找到)提供的 TextField 组件附带的帮助文本的样式。 I'm using FormHelperTextProps (found here ).我正在使用 FormHelperTextProps(在此处找到)。 However, it seems that, for some reason, the styling I'm writing is not being applied to the component itself.但是,由于某种原因,我正在编写的样式似乎并未应用于组件本身。 I would appreciate any help I could get on this.我将不胜感激我能得到的任何帮助。 Here is my code:这是我的代码:

    const useHelperTextStyles = makeStyles(()=> ({
    root: { 
        "& .MuiFormHelperText-root" : { color: TextFieldColours.error["status-text"] }, 
        // "& .MuiFormHelperText-contained"
    }
    })
     );

    const EmptyTextField = (props: CustomEmptyFieldProps) => {
    const {
         usernameOrPass,
    } = props; 
    const inputLabel = "VolunteerHub " + usernameOrPass; 
    const errorMessage = "Please enter your VolunteerHub " + usernameOrPass; 

    const textFieldStyles = useTextFieldStyles(false);
    const helperTextStyles = useHelperTextStyles(); 
    return (
        <div>
            <TextField
                className={textFieldStyles.root}
                placeholder={inputLabel}
                id="outlined-error-helper-text"
                defaultValue=""
                helperText={errorMessage}
                variant="outlined"
                FormHelperTextProps={{
                    classes={helperTextStyles.helperText,}
                }}
            />
        </div >
    );
}

first of all a class needs to targetted in classes prop, like root , focused etc. so in classes props select pass the style to the root class. And another issue is there is no helperText class in useHelperTextStyles hook.首先,class 需要在 classes 道具中定位,例如rootfocused等。因此在 classes 道具 select 中将样式传递给root class。另一个问题是useHelperTextStyles钩子中没有helperText class。
So for targeting the root style the code will like this:因此,为了定位根样式,代码将如下所示:

const useHelperTextStyles = makeStyles(() => ({
    root: {
        color: TextFieldColours.error["status-text"]
    }
}));

const EmptyTextField = (props) => {
    const { usernameOrPass } = props;
    const inputLabel = "VolunteerHub " + usernameOrPass;
    const errorMessage = "Please enter your VolunteerHub " + usernameOrPass;

    const textFieldStyles = useTextFieldStyles(false);
    const helperTextStyles = useHelperTextStyles();
    return (
        <div>
            <TextField
                className={textFieldStyles.root}
                placeholder={inputLabel}
                id="outlined-error-helper-text"
                defaultValue=""
                helperText={errorMessage}
                variant="outlined"
                FormHelperTextProps={{
                        classes:{
                            root:helperTextStyles.root
                        }
                }}
            />
        </div>
    );
};

Here is a working demo:这是一个工作演示:

编辑 quizzical-bash-ggynj

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

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