UNPKG

6.77 kBMarkdownView Raw
1# inputmask-core [![Build Status](https://secure.travis-ci.org/insin/inputmask-core.png)](http://travis-ci.org/insin/inputmask-core)
2
3A standalone input mask implementation, which is independent of any GUI.
4
5`InputMask` encapsulates editing operations on a string which must conform to a
6fixed-width pattern defining editable positions and the types of data they may
7contain, plus optional static characters which may not be edited.
8
9## Install
10
11```
12npm install inputmask-core
13```
14
15## Usage
16
17Importing and creating an instance:
18
19```javascript
20var InputMask = require('inputmask-core')
21
22var mask = new InputMask({pattern: '11/11/1111'})
23```
24
25Examples of editing a mask:
26
27```javascript
28/* Invalid input is rejected */
29mask.input('a')
30// → false
31
32/* Valid input is accepted */
33mask.input('1')
34// → true
35mask.getValue()
36// → '1_/__/____'
37
38/* Editing operations update the cursor position */
39mask.selection
40// → {start: 1, end: 1}
41
42/* Pasting is supported */
43mask.paste('2345678')
44// → true
45mask.getValue()
46// → '12/34/5678'
47
48/* Backspacing is supported */
49mask.backspace()
50// → true
51mask.getValue()
52// → '12/34/567_'
53
54/* Editing operations also know how to deal with selected ranges */
55mask.selection = {start: 0, end: 9}
56mask.backspace()
57// → true
58mask.getValue()
59// → '__/__/____'
60```
61
62## API
63
64See the [Types](#types) section below for type definitions referenced in this
65section.
66
67## `InputMask(options: `[`InputMaskOptions`](#inputmaskoptions)`)`
68
69Constructs a new `InputMask` - use of `new` is optional, so these examples are
70equivalent:
71
72```javascript
73var mask = new InputMask({pattern: '1111-1111', value: '12345678'})
74```
75```javascript
76var mask = InputMask({pattern: '1111-1111', value: '12345678'})
77```
78
79## `InputMask` public properties, getters & setters
80
81### `selection` : `Selection`
82
83The current selection within the input.
84
85If `start` and `end` are the same, this indicates the current cursor position in
86the string, otherwise it indicates a range of selected characters within the
87string.
88
89`selection` will be updated as necessary by editing methods, e.g. if you
90`input()` a valid character, `selection` will be updated to place the cursor
91after the newly-inserted character.
92
93If you're using `InputMask` as the backend for an input mask in a GUI, make
94sure `selection` is accurate before calling any editing methods!
95
96### `setSelection(selection: Selection)` : `boolean`
97
98Sets the selection and performs an editable cursor range check if the selection
99change sets the cursor position (i.e. `start` and `end` are the same).
100
101If the mask's pattern begins or ends with static characters, this method will
102prevent the cursor being placed prior to a leading static character or beyond a
103tailing static character. Only use this method to set `selection` if this is
104the behaviour you want.
105
106Returns `true` if the selection needed to be adjusted as described above,
107`false` otherwise.
108
109### `getValue()` : `string`
110
111Gets the current value in the mask, which will always conform to the mask's
112pattern.
113
114### `setValue(value: string)`
115
116Overwrites the current value in the mask.
117
118The given value will be applied to the mask's pattern, with invalid - or
119missing - editable characters replaced with placeholders.
120
121The value may optionally contain static parts of the mask's pattern.
122
123### `setPattern(pattern: string, value: ?string)`
124
125Overwrites the mask's pattern.
126
127A new value can also be provided - if not provided, the value will default to
128blank, clearing the mask.
129
130## `InputMask` editing methods
131
132Editing methods will not allow the string being edited to contain invalid
133values according to the mask's pattern.
134
135Any time an editing method results in either the value or the selection
136changing, it will return `true`.
137
138Otherwise, if an invalid (e.g. trying to input a letter where the pattern
139specifies a number) or meaningless (e.g. backspacing when the cursor is at the
140start of the string) editing operation is attempted, it will return `false`.
141
142### `input(character: string)` : `boolean`
143
144Applies a single character of input based on the current selection.
145
146* If a text selection has been made, editable characters within the selection
147 will be blanked out, the cursor will be moved to the start of the selection
148 and input will proceed as below.
149
150* If the cursor is positioned before an editable character and the input is
151 valid, the input will be added. The cursor will then be advanced to the next
152 editable character in the mask.
153
154* If the cursor is positioned before a static part of the mask, the cursor will
155 be advanced to the next editable character.
156
157After input has been added, the cursor will be advanced to the next editable
158character position.
159
160### `backspace()` : `boolean`
161
162Performs a backspace operation based on the current selection.
163
164* If a text selection has been made, editable characters within the selection
165 will be blanked out and the cursor will be placed at the start of the
166 .selection
167
168* If the cursor is positioned after an editable character, that character will
169 be blanked out and the cursor will be placed before it.
170
171* If the cursor is positioned after a static part of the mask, the cursor will
172 be placed before it.
173
174### `paste(input: string)`: `boolean`
175
176Applies a string of input based on the current selection.
177
178This behaves the same as - and is effectively like - calling `input()` for each
179character in the given string *with one key difference* - if any character
180within the input is determined to be invalid, the entire paste operation fails
181and the mask's value and selection are unaffected.
182
183Pasted input may optionally contain static parts of the mask's pattern.
184
185## Types
186
187These type definitions are purely for reference, they're not part of the
188API exported by this module.
189
190### `Selection` : `{start: number; end: number}`
191
192An object with `start` and `end` properties, where `end >= start`.
193
194### `InputMaskOptions`
195
196#### `{pattern: string, value: ?string, selection: ?Selection}`
197
198Options for the `InputMask` constructor.
199
200#### `pattern`
201
202A masking pattern. The following characters signify editable parts of the mask:
203
204* `1` - number
205* `A` - letter
206* `*` - alphanumeric
207
208All other characters are treated as static.
209
210A pattern must be provided and must contain at least one editable character, or
211an `Error` will be thrown.
212
213##### Example patterns
214
215* Credit card number: `1111 1111 1111 1111`
216* Date: `11/11/1111`
217* ISO date: `1111-11-11`
218* Time: `11:11`
219* Canadian postal code: `A1A 1A1`
220* Norn Iron license plate: `AAA 1111`
221
222#### `value`
223
224An optional initial value for the mask - see [`setValue()`](#setvaluevalue-string)
225above for more details.
226
227#### `selection`
228
229An optional default selection - see [`selection`](#selection-selection)
230above for more details.
231
232## MIT Licensed
\No newline at end of file