This module contains the core functions that power Text Mask. Currently, Text Mask has a wrapper for React, which can be used directly.
However, Text Mask Core functions could be useful on their own. That's why they are published and documented here as a separate module.
To download the script, use npm.
npm i text-mask-core --save
After installing with npm, you could possibly do something like this from your index.html
:
<script src="./node_modules/text-mask-core/dist/textMaskCore.js"></script>
Including this file in your source code will expose the global object textMaskCore
.
Or if you're using Node.js or a bundler such as webpack or Browserify, you can require
textMaskCore
as such:
var textMaskCore = require('text-mask-core')
textMaskCore
exposes three functions:
- conformToMask
- adjustCaretPosition
- convertMaskToPlaceholder
The general idea is to take user input, conform it to your desired mask using conformToMask
,
and then apply the output of conformToMask
to the value of the HTML input element.
Once you do that however, the caret position will be lost. You can then use adjustCaretPosition
to restore the caret to its proper position.
This function takes a mask (string), i.e. 11/11/1111
, and returns a placeholder (string).
const placeholder = convertMaskToPlaceholder('11/11/1111')
placeholder // __/__/____
You can use this function to initialize an input
element to a placeholder value.
This function takes three arguments:
- rawValue (string): the string value that you want to conform to the mask
- mask (string): the mask to which you want the string to conform. You can find mask documentation here.
- config (object): config object. See below for details
This function returns the conformed value as a string.
The config
object takes the following values
-
guide
(boolean) (defaults totrue
): this tellsconformToMask
whether you want the conformed string to contain a guide or no. Theguide
is basically the placeholder character and the mask hard characters. For example, with mask(111) 111-1111
, input123
withguide
set totrue
would return(123) ___-____
. Withguide
set tofalse
, it would return(123)
. -
previousConformedValue
(string) (required): this is the previousoutput
ofconformToMask
. If you're callingconformToMask
for the first time, you don't have to pass this value. -
placeholderChar
(string) (optional): for documentation on this key, see this section of the component documentation page.
const results = conformToMask('5554833902', '(111) 111-1111')
results // '(555) 483-3902'
Whenever the value of the input
element changes, you can pass that value to conformToMask
and it'll make sure that the string looks like the given mask. You can then set that conformed
string as the new value of the input
element.
When you set the value of the input
element, you lose the position of the caret. This function
helps you restore the position.
adjustCaretPosition
takes the following object of arguments:
previousConformedValue
(string): the string value of theinput
before the last time you set its value. If you're calling this function for the first time, you can pass an empty string.conformedToMaskResults
(object): the return value of the last call toconformToMask
currentCaretPosition
(integer): the position of the caret right before you called this function
adjustCaretPosition
will return an integer representing the index of where the caret should be
moved to next.