Skip to content

Commit 8e4d2cd

Browse files
author
Nick Gard
committed
Add revealing mask feature
Revealing mask only shows static and placeholder characters contained within the current value Example: Given an input with a mask of '111-1111 x 111', a value of '47', and isRevealingMask set to true, then the input's value is '47' Given the same input but with a value of '476', then the input's value is formatted as '476-' Given the same input but with a value of '47 3191', then the input's value is formatted as '47_-3191 x '
1 parent 4e2ebf9 commit 8e4d2cd

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var DEFAULT_FORMAT_CHARACTERS = {
6868
* @param {string} source
6969
* @patam {?Object} formatCharacters
7070
*/
71-
function Pattern(source, formatCharacters, placeholderChar) {
71+
function Pattern(source, formatCharacters, placeholderChar, isRevealingMask) {
7272
if (!(this instanceof Pattern)) {
7373
return new Pattern(source, formatCharacters, placeholderChar)
7474
}
@@ -87,9 +87,10 @@ function Pattern(source, formatCharacters, placeholderChar) {
8787
this.firstEditableIndex = null
8888
/** Index of the last editable character. */
8989
this.lastEditableIndex = null
90-
9190
/** Lookup for indices of editable characters in the pattern. */
9291
this._editableIndices = {}
92+
/** If true, only the pattern before the last valid value character shows. */
93+
this.isRevealingMask = isRevealingMask || false
9394

9495
this._parse()
9596
}
@@ -139,6 +140,11 @@ Pattern.prototype.formatValue = function format(value) {
139140

140141
for (var i = 0, l = this.length; i < l ; i++) {
141142
if (this.isEditableIndex(i)) {
143+
if (this.isRevealingMask &&
144+
value.length <= valueIndex &&
145+
!this.isValidAtIndex(value[valueIndex], i)) {
146+
break
147+
}
142148
valueBuffer[i] = (value.length > valueIndex && this.isValidAtIndex(value[valueIndex], i)
143149
? this.transform(value[valueIndex], i)
144150
: this.placeholderChar)
@@ -181,10 +187,10 @@ Pattern.prototype.transform = function transform(char, index) {
181187

182188
function InputMask(options) {
183189
if (!(this instanceof InputMask)) { return new InputMask(options) }
184-
185190
options = extend({
186191
formatCharacters: null,
187192
pattern: null,
193+
isRevealingMask: false,
188194
placeholderChar: DEFAULT_PLACEHOLDER_CHAR,
189195
selection: {start: 0, end: 0},
190196
value: ''
@@ -202,7 +208,8 @@ function InputMask(options) {
202208
this.formatCharacters = mergeFormatCharacters(options.formatCharacters)
203209
this.setPattern(options.pattern, {
204210
value: options.value,
205-
selection: options.selection
211+
selection: options.selection,
212+
isRevealingMask: options.isRevealingMask
206213
})
207214
}
208215

@@ -447,7 +454,7 @@ InputMask.prototype.setPattern = function setPattern(pattern, options) {
447454
selection: {start: 0, end: 0},
448455
value: ''
449456
}, options)
450-
this.pattern = new Pattern(pattern, this.formatCharacters, this.placeholderChar)
457+
this.pattern = new Pattern(pattern, this.formatCharacters, this.placeholderChar, options.isRevealingMask)
451458
this.setValue(options.value)
452459
this.emptyValue = this.pattern.formatValue([]).join('')
453460
this.selection = options.selection

test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test('formatValueToPattern', function(t) {
4545
})
4646

4747
test('Constructor options', function(t) {
48-
t.plan(21)
48+
t.plan(24)
4949

5050
t.throws(function() { new InputMask() },
5151
/InputMask: you must provide a pattern./,
@@ -113,6 +113,14 @@ test('Constructor options', function(t) {
113113
t.equal(mask.getValue(), '___ ___', 'null value treated as blank')
114114
mask = new InputMask({pattern: '111 111', value: undefined})
115115
t.equal(mask.getValue(), '___ ___', 'undefined value treated as blank')
116+
117+
// Mask is progressive
118+
mask = new InputMask({pattern: '111-1111 x 111', value: '47', isRevealingMask: true})
119+
t.equal(mask.getValue(), '47', 'no mask characters or placeholders are revealed')
120+
mask = new InputMask({pattern: '111-1111 x 111', value: '476', isRevealingMask: true})
121+
t.equal(mask.getValue(), '476-', 'mask is revealed up to the next editable character')
122+
mask = new InputMask({pattern: '111-1111 x 111', value: '47 3191', isRevealingMask: true})
123+
t.equal(mask.getValue(), '47_-3191 x ', 'mask is revealed up to the last value')
116124
})
117125

118126
test('Formatting characters', function(t) {

0 commit comments

Comments
 (0)