Material-ui: Asteriks class for TextField

Created on 11 Feb 2019  路  7Comments  路  Source: mui-org/material-ui

FormControlClassKey does not include the ability to add a class to the asterisk set by the required prop of a TextField.

I would like to be able to do:

<TextField classes={{asterisk: 'asterisk'}} />

...

'.asterisk': { color: 'blue' }

Current situation has no ability to do this and in my styling I need to do this:

'> label > span': { color: 'blue' }
TextField enhancement good first issue

All 7 comments

@e11en Here you go:

import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";

const styles = theme => ({
  asterisk: {
    color: "red"
  }
});

function TextFields(props) {
  const { classes } = props;

  return (
    <form noValidate autoComplete="off">
      <TextField
        required
        InputLabelProps={{
          FormLabelClasses: {
            asterisk: classes.asterisk
          }
        }}
        id="standard-required"
        label="Required"
        defaultValue="Hello World"
        margin="normal"
      />
    </form>
  );
}

TextFields.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(TextFields);

https://codesandbox.io/s/kxvv9w085r

capture d ecran 2019-02-11 a 11 38 01

But yeah, we could add an asterisk style rule to the InputLabel component so you can do:

      <TextField
        required
        InputLabelProps={{
          classes: {
            asterisk: classes.asterisk
          }
        }}
        id="standard-required"
        label="Required"
        defaultValue="Hello World"
        margin="normal"
      />

instead. Do you want to work on it? :)

Ah yeah of course! I didn't think of that way of adding the class name. Thanks! I'll have a look at adding that style rule

FormLabel docs are also missing a comment about the asterisk class. It is listed as a class key but not explanation about the purpose: https://material-ui.com/api/form-label/#css

@e11en @oliviertassinari If anyone is not working on this, would love to make PR. Thanks

@umairfarooq44 We would be honored to review your pull request :)

@oliviertassinari thanks for assigning issue. The solution should be in InputLabel component remove FormLabelClasses and use only classes from props. As FormLabelClasses and classes are two props for same purpose. Also asterisk should be included in documentation of InputLabel css API.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

darkowic picture darkowic  路  62Comments

HZooly picture HZooly  路  63Comments

tleunen picture tleunen  路  59Comments

NonameSLdev picture NonameSLdev  路  56Comments

amcasey picture amcasey  路  70Comments