UNPKG

materialuiupgraded

Version:

Material-UI's workspace package

113 lines (98 loc) 2.82 kB
/* eslint-disable react/prefer-stateless-function */ import React from 'react'; import MaskedInput from 'react-text-mask'; import NumberFormat from 'react-number-format'; import PropTypes from 'prop-types'; import { withStyles } from '@material-ui/core/styles'; import Input from '@material-ui/core/Input'; import InputLabel from '@material-ui/core/InputLabel'; import TextField from '@material-ui/core/TextField'; import FormControl from '@material-ui/core/FormControl'; const styles = theme => ({ container: { display: 'flex', flexWrap: 'wrap', }, formControl: { margin: theme.spacing.unit, }, }); function 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, }; function NumberFormatCustom(props) { const { inputRef, onChange, ...other } = props; return ( <NumberFormat {...other} getInputRef={inputRef} onValueChange={values => { onChange({ target: { value: values.value, }, }); }} thousandSeparator prefix="$" /> ); } NumberFormatCustom.propTypes = { inputRef: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired, }; class FormattedInputs extends React.Component { state = { textmask: '(1  )    -    ', numberformat: '1320', }; handleChange = name => event => { this.setState({ [name]: event.target.value, }); }; render() { const { classes } = this.props; const { textmask, numberformat } = this.state; return ( <div className={classes.container}> <FormControl className={classes.formControl}> <InputLabel htmlFor="formatted-text-mask-input">react-text-mask</InputLabel> <Input value={textmask} onChange={this.handleChange('textmask')} id="formatted-text-mask-input" inputComponent={TextMaskCustom} /> </FormControl> <TextField className={classes.formControl} label="react-number-format" value={numberformat} onChange={this.handleChange('numberformat')} id="formatted-numberformat-input" InputProps={{ inputComponent: NumberFormatCustom, }} /> </div> ); } } FormattedInputs.propTypes = { classes: PropTypes.object.isRequired, }; export default withStyles(styles)(FormattedInputs);