Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask Textfield component in Material-UI

I'm trying to apply a mask in a TextField component, but im without success.

I already tried this solution but not worked. I tried every way but seems to not work anymore.

I tried to follow the instructions given in docs but they use the Input component and this component is breaking my design.

Anyone knows a way to mask the TextField component? I'm using material-ui 1.0.0-beta.24

like image 351
Eduardo Lima Avatar asked Feb 10 '18 17:02

Eduardo Lima


People also ask

How do I invalidate a TextField in material UI?

To invalidate a TextField in React Material UI, we set the error and helperText props of the TextField to display the error message we want. We set the error prop to make the TextField 's border red when we enter invalid input. And we set the helperText prop to display the error text at the bottom.

How do I hide the label in TextField material UI?

Use a TextField. Set variant='outlined' Pass no label or pass label={null}

What is SX in material UI?

The sx prop is a shortcut for defining custom styles that has access to the theme. The sx prop lets you work with a superset of CSS that packages all of the style functions exposed in @mui/system . You can specify any valid CSS using this prop, as well as many theme-aware properties that are unique to MUI System.


1 Answers

Use InputProps to set the mask directly on a TextField component. For example, suppose your desired mask is represented by the TextMaskCustom component. Then, instead of applying it to an Input directly, you can apply it to your TextField using the InputProps like so:

<TextField
  id="maskExample"
  label="Mask Example"
  className={classes.textField}
  margin="normal"
  InputProps={{
    inputComponent: TextMaskCustom,
    value: this.state.textmask,
    onChange: this.handleChange('textmask'),
  }}
/>

This works because a TextField is actually a wrapper around an Input component (with some other things mixed in). The InputProps prop of the TextField gives you access to the internal Input, so you can set the mask that way while preserving the styling of the TextField component.

Here's a full working example adapted from the demos in the docs:

import React from 'react';
import MaskedInput from 'react-text-mask';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  input: {
    margin: theme.spacing.unit,
  },
});

const TextMaskCustom = (props) => {
  const { inputRef, ...other } = props;

  return (
    <MaskedInput
      {...other}
      ref={inputRef}
      mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
      placeholderChar={'\u2000'}
      showMask
    />
  );
}

TextMaskCustom.propTypes = {
  inputRef: PropTypes.func.isRequired,
};

class FormattedInputs extends React.Component {
  state = {
    textmask: '(1  )    -    ',
  };

  handleChange = name => event => {
    this.setState({
      [name]: event.target.value,
    });
  };

  render() {
    const { classes } = this.props;
    return (
      <div className={classes.container}>
        <TextField
          id="maskExample"
          label="Mask Example"
          className={classes.textField}
          margin="normal"
          InputProps={{
            inputComponent: TextMaskCustom,
            value:this.state.textmask,
            onChange: this.handleChange('textmask'),
          }}
        />
      </div>
    );
  }
}

FormattedInputs.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(FormattedInputs);
like image 68
Jules Dupont Avatar answered Oct 08 '22 03:10

Jules Dupont