
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
@ryuuji3/react-mask-hook
Advanced tools
An input masking lib designed to be testable and with simplicity in mind.
npm install @ryuuji3/react-mask-hook
import useMask from '@ryuuji3/react-mask-hook'
function PostalCodeDemo() {
const [ value, setValue ] = useState('')
function handleChange(value) {
setValue(value.toUpperCase()) // render as uppercase
}
const maskProps = useMask({
value,
onChange: handleChange,
// array of regexp and formatting characters
mask: [ /[a-z]/i, /[\d]/, /[a-z]/i, " ", /[\d]/, /[a-z]/i, /[\d]/ ],
placeholder: '_', // renders all regexp with _ (ie. "___ ___")
})
return (
<label>
Sample input with postal code mask:
<input
name="test"
type="text"
{...maskProps}
/>
</label>
);
}
function TelephoneDemo() {
const [ value, setValue ] = useState('')
const maskProps = useMask({
value,
onChange: setValue,
mask: '(###)-###-####', // deprecated syntax but still supported
placeholder: '_',
})
return (
<label>
Sample input with phone mask:
<input
name="test"
type="tel"
{...maskProps}
/>
</label>
);
}
function DateDemo() {
const [ value, setValue ] = useState('')
const maskProps = useMask({
value,
onChange: setValue,
mask: '## - ## - ####',
placeholder: 'DD - MM - YYYY', // will render mask exactly like this
})
return (
<label>
Sample input with date mask:
<input
name="test"
type="text"
{...maskProps}
/>
</label>
);
}
const { value, placeholder, onChange, onKeyUp, onKeyDown, onFocus } = useMask({ value, onChange, mask, placeholder })
Arguments:
argument | description |
---|---|
value | is the string value without formatting. this will hold only the raw values without formatting characters or spaces |
onChange | is the callback used to update value. you must supply local state or deferred state. |
mask | is an array of regular expressions and formatting characters. |
placeholder | The third argument is a single character to use as the displayed placeholder instead of the regular expression, or it can be an entire string that matches the mask but with different characters are the placeholder. Your displayed mask must have the same length as the mask. |
You may opt-out of placeholder, but the other properties returned by useMask
are required and should be bound to the input in order for the masking to function as expected.
FAQs
A simple masking library as a react hook
The npm package @ryuuji3/react-mask-hook receives a total of 799 weekly downloads. As such, @ryuuji3/react-mask-hook popularity was classified as not popular.
We found that @ryuuji3/react-mask-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.