Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Stephen Charles Weiss
Stephen Charles Weiss

Posted on • Originally published atstephencharlesweiss.com on

     

Masking Inputs And More Ref Fun

In learning aboutAuto-Complete In HTML Forms, I discovered that thetype of an<input> makes can make a significant difference in how they are presented to users. The differences, however, are browser specific.

Taketype="date" for example. Chromium browsers supply quite a bit of styling for free. Safari adds nothing.

The standard inputs in the screenshots below are simply:

constapp=()=>{return({/* … */}<p>Standarddateinput</p><inputtype="date"placeholder={"dob mm/dd/yyyy"}/>{/* … */})}
Enter fullscreen modeExit fullscreen mode

comparing chromium to safari browser rendering

To not be at the whim of a browser, I reverted back to a standard input and sought other approaches.

In this particular case, I really wanted to provide guidance to users on how to put the information in since formatting is important for the application to work properly.

To think about how this would work, I thought about passwords, but in reverse. Instead of taking a password and turning it into a string of ● (e.g.,pa$$w0rd becomes●●●●●●●●), I wanted a string that could be overwritten. In this case__/__ / ____.

CSS Tricks outlined several approaches depending on the stack - vanilla JS, jQuery, etc. and also noted some accessibility considerations to bear in mind (like that certain approaches will use thevalue of the input, not theplaceholder to display the mask).1

I ended up selecting the libraryreact-text-mask because of it’s support for React andstyled-components. The latter, however, turned out to be more fun than I was expecting when I started, largely thanks torefs.

The library provides arender method for custom<input> components which is noted helpfullyin the docs. Interestingly for me, the way this method works is by providing its own ref which can be passed along to the styled component.

From the docs:

For example, to use with styled-components,which requires an innerRef:

<MaskedInputmask={[(,/[1-9]/,/\d/,/\d/,),,/\d/,/\d/,/\d/,-,/\d/,/\d/,/\d/,/\d/]}placeholder="Enter a phone number"id="my-input-id"render={(ref,props)=>(<MyStyledInputinnerRef={ref}{props}/>)}/>constMyStyledInput=styled.input`background: papayawhip;`;

Notice that the render method has an argumentref that’s passed along to the<MyStyledInput>? That’s provided by the component! There’s no need to create a ref in a containing component, no need foruseRef orforwardRef! It’s actually quite simple.

Part of my confusion lay in the demonstration of usinginnerRef. As of v4,styled-components do not require aninnerRef, however, despite havingread thestyled-component documentation, it still took a conversation with several others to understand that theinnerRef was part of thestyled-component api and not thereact-text-mask. In retrospect should have been more obvious.

After all of that, I arrived at a working solution: Masking the input, guiding my users, and providing a consistent user experience across browsers and devices.

If you’re interested, you can see my prototype in aCode Sandbox.

Footnotes

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Engineer | Lover of dogs, books, and learning | Dos XX can be most interesting man in the world, I'm happy being the luckiest. | I write about what I learn @ code-comments.com
  • Location
    Chicago, IL
  • Work
    Software Engineer at Olo
  • Joined

More fromStephen Charles Weiss

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp