Skip to content

tracksuitdev/use-imask

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
September 9, 2021 23:08
src
September 9, 2021 23:08
September 9, 2021 23:08
May 27, 2021 02:26
May 27, 2021 02:26
May 27, 2021 02:26
September 9, 2021 23:25
September 9, 2021 23:27
May 27, 2021 02:26
September 9, 2021 23:18

use-imask npm npm bundle size NPM

React hook that exposes IMask object

Installation

npm install use-imask

or if you use yarn

yarn add use-imask

Usage

const maskOptions = { mask: Number }; // make sure options don't change on each render (use constant or useMemo or ref)

function Example() {
  const [value, setValue] = useState("");

  const [ ref ] = useIMask(maskOptions, {
  // use onAccept callback instead of onChange to change value
    onAccept: e => { 
      setValue(e.target.value);
    },
  });

  return (
    <input ref={ref} value={value} />
  );
}

Props

name type description
options <M extends AnyMaskedOptions> mask options, when options change updateOptions function is called by the IMask object
onAccept (e: ChangeEvent<E>, mask: InputMask<M> | undefined) => void function called on IMask accept event
onComplete (e: ChangeEvent<E>, mask: InputMask<M> | undefined) => void function called on IMask complete event

Return value

name type description
ref RefObject<HTMLInputElement|null> apply this to your input element
maskRef RefObject<InputMask<M> | undefined> ref with IMask object