Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextField] Mask #2130

Closed
cezarsmpio opened this issue Nov 11, 2015 · 20 comments
Closed

[TextField] Mask #2130

cezarsmpio opened this issue Nov 11, 2015 · 20 comments
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation

Comments

@cezarsmpio
Copy link

cezarsmpio commented Nov 11, 2015

  1. There is an idea for apply masks in textfields?
  2. Why I need pass props like fullWidth={true}, why not simply pass fullWidth? Because single props without values returns a bool.

Example:

<TextField mask="9999-9999-9999-9999" />
<TextField mask="99-999-aaa" fullWidth primary raised />

Thanks!

@celsofabri
Copy link

Nice idea +1

@matheus-vieira
Copy link

That would make coding a little bit simpler +1

@oliviertassinari
Copy link
Member

I don't get it. Why is 2) not working?

In JSX when using a boolean attribute you can set the attribute value to true or omit the value.

@cezarsmpio
Copy link
Author

Hi @oliviertassinari, 2) it's working perfectly! Sorry. I saw only the documentation and thought it would only be that way.

Why 1) isn't a good idea?

@oliviertassinari
Copy link
Member

  1. mask is a good feature. But is this specific to material-ui?
  2. Do you have an example of a failing usage of the omited value?

@cezarsmpio
Copy link
Author

  1. Yes, include on material-ui specification. Native.
  2. Sorry, it was my fault. Forget it ;)

@alitaheri alitaheri added the new feature New feature or request label Dec 9, 2015
@dobryanskyy
Copy link
Contributor

Indeed, please add this. I've tried using third-party mask library, and it's a nightmare to integrate something with a TextField. Finally i gave up on this.

@oliviertassinari oliviertassinari changed the title [Enhancement] Mask for TextField and simplify properties? [TextField] Mask for TextField and simplify properties? Sep 18, 2016
@oliviertassinari oliviertassinari changed the title [TextField] Mask for TextField and simplify properties? [TextField] Mask Sep 18, 2016
@lucasbento lucasbento added the component: text field This is the name of the generic UI component, not the React module! label Oct 15, 2016
@oliviertassinari oliviertassinari added component: text field This is the name of the generic UI component, not the React module! and removed component: text field This is the name of the generic UI component, not the React module! labels Oct 19, 2016
@kybarg
Copy link
Contributor

kybarg commented Apr 10, 2017

Sould I implement it in #6566 with help of https://github.com/text-mask/text-mask/ or more simple https://github.com/insin/react-maskedinput ?

@mbrookes
Copy link
Member

mbrookes commented Apr 10, 2017

@kybarg react-maskedinput does look simpler, but it also looks less well maintained. How about if TextField took a prop to substitute the <input> element with an arbitrary React component and passed the mask prop (&&|| a more generic inputProps) to it?

@kylem038
Copy link

kylem038 commented Sep 18, 2017

Not sure if this is too late @dobryanskyy or if this is of use to anyone else, but this is how I added the text-mask onto a MUI v1 input.

Parent component

          <Input
            classes={{
              underline: classes.underline,
            }}
            inputComponent={OurMaskedInput}
          />

OurMaskedInput

import React from 'react';
import MaskedInput from 'react-text-mask';
import { withStyles } from 'material-ui/styles';
import detailStyleSheet from './LocationDetailStyles';

const OurMaskedInput = (props) => {
  const classes = props.classes;

  return (
    <div>
      <MaskedInput
        mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
        placeholderChar={'\u2000'}
        showMask
        className={classes.maskedInput}
      />
    </div>
  );
};

export default withStyles(detailStyleSheet)(OurMaskedInput);

Because we're only replacing the html input element we needed to replicate the styles MUI places via the input and inputSingleline classes.

maskedInput: {
    height: '1em',
    font: 'inherit',
    color: 'currentColor',
    width: '100%',
    border: 0,
    margin: 0,
    padding: '7px 0',
    display: 'block',
    boxSizing: 'content-box',
    background: 'none',
    verticalAlign: 'middle',
  },

Did the trick for my use case.

@oliviertassinari
Copy link
Member

@kylem038 This sounds like an excellent idea! Do you want to add a demo into the documentation? This would be a great addition to have https://github.com/text-mask/text-mask.

Also, https://github.com/s-yadav/react-number-format seems to be a good alternative. cc @rosskevin

@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation and removed new feature New feature or request labels Sep 18, 2017
@rosskevin
Copy link
Member

@kylem038 I used react-number-format in our lib like:

<TextField
        InputProps={{ inputComponent: NumberFormat }}
</TextField>

It worked well, and should work for your input too (assuming you want all the extras TextField brings. It would be great if you would be willing to PR the demos.

@kylem038
Copy link

@oliviertassinari @rosskevin Would love to contribute and PR this. I have to admit, this would be my first contribution to MUI. Is there a place where I can see the preferred format for PR'ing into MUI?

@oliviertassinari
Copy link
Member

@kylem038 There is no preferred format. At least, I don't see what you are referring to. Don't miss this CONTRIBUTING.md guide.

@dobryanskyy
Copy link
Contributor

@kylem038 great solution, thanks.

@felipenmoura
Copy link

Hi.

I'm trying to implement this (using #2130 (comment) 's example) on material-ui-next and I'm getting the error:

React does not recognize the inputRef prop on a DOM element.

This inputRef is being used inside the NumberFormat class, I don't use it at all.

// ...
elAttrs.InputProps.inputComponent = NumberFormat
// ...
return <TextField { ...elAttrs } />

I would love to use something like what was initially suggested:

// SO MUCH cleaner and simpler!
<TextField mask="99-999-aaa" />

@oliviertassinari
Copy link
Member

@felipenmoura We have an example in the documentation. Don't miss it.
inputRef needs to be apply on the native input.

@luscas
Copy link

luscas commented Sep 21, 2022

be happy

import MaskedInput from 'react-text-mask'
import TextField from '@mui/material/TextField'

// ...
const Component = () => (
  <MaskedInput
    mask={/* YOUR MASK */}
    render={(innerRef, props) => (
      <TextField
        {...props}
        inputRef={innerRef}
      />
    )}
  />
);

@ShareinSK
Copy link

ShareinSK commented Jun 8, 2023

Is there any option to dynamically update the mask pattern based on user input?

@jalalbmnf
Copy link

Any best solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation
Projects
None yet
Development

No branches or pull requests