1.0.5 • Published 2 years ago

mask-field v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React mask-field

React input mask with simply interface and just customisation

Features

  • colorizes mask
  • works with different mask
  • takes different validators
  • can works with copy/paste events
  • supports dynamic changed masks
  • supports all major browsers
  • no external dependencies

demo

Setup

Install the package npm i mask-field --save

Use

React Mask exports default MaskInput component

import MaskField from "mask-field"
import "mask-field/lib/index.css"

importing styles are require for correct work

MaskInput has interface look like simple native input with two addition props (mask, separators)

mask is showing how transform value separators are array of symbols who must be a skipped

We just should need paste MaskInput in yours component and hand over needs props

<MaskField
  value={value}
  mask={mask}
  separators={separators}
  onChange={onChange}
  placeholder="placeholder"
  modifiers={modifiers}
/>

Props

PropRequiredTypeDescriptionExample
masktruestringpattern"xxx (xx) xxx xx xx"
separatorstruestring[]to skipping symbols" ", "(", ")"
valuetruestringinput value-
onChangetruefuncon change cb-
validatorsfalsefunc[]list of validators who will call by chain. (default isNumber) (value: string): stringundefinedisNumber
placeholderfalsestringshowing with empty input-
modifiersfalsestringsetting custom styles-
modifiersErrorsfalsestringsetting custom styles for errors container-
typefalsestringinput type (default tel), don't have to use numberstring
withErrorsfalsebooltrigger for visible errors block (default false)true
errorsfalsestring[]array of errors"Value is not a number"
disabledfalseboolflag for disabling inputtrue
readOnlyfalseboolflag for set readOnly in inputtrue

Advance

If you want using custom react input component, mask exports additional wrapper.

import { MaskWrapper } from "mask-field"
import "mask-field/lib/index.css"

You should wrap your custom input and hand over props

example:

<MaskWrapper
  value={value}
  mask={mask}
  errors={errors}
  separators={[" "]}
  onChange={onChange}
  modifiers={modifiers}
>
  {({
    maskInputStyle,
    maskInputWrapperStyle,
    inputRef,
    value,
    handleMaskChange,
  }) => (
      <Input
        type="tel"
        wrapperModifiers={maskInputWrapperStyle}
        modifiers={joinClasses("input", maskInputStyle)}
        value={value}
        required={required}
        onChange={handleMaskChange}
        name={name}
        readOnly={readOnly}
        disabled={isDisabled}
        tabIndex={tabIndex}
        onBlur={onBlur}
        inputRef={inputRef}
      />
    )
  }
</MaskWrapper>

In children MaskWrapper returns props who needed hand over to custom input

Mask utils

If you want using simply function to transform value to custom mask or cut all separators You can import prepareValueFromMask, prepareValueToMask

import { prepareValueFromMask, prepareValueToMask } from "mask-field""

example:

const value = "375292222222"
const mask = "xxx (xx) xxx xx xx"
const separators = [" ", "(", ")"]

prepareValueToMask(value, mask, [" "])
  .toBe("375 (29) 222 22 22")
prepareValueFromMask("12/01/2000", ["/"]))
  .toBe("12012000")
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago