-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmixin.js
246 lines (214 loc) · 5.81 KB
/
mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// react-mask-mixin
// http://github.com/borbit/react-mask-mixin
// Copyright (c) 2016 Serge Borbit
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
// Version: 0.0.12
(function(root) {
var ReactDOM;
if (typeof module !== 'undefined' && module.exports) {
ReactDOM = require('react-dom')
} else if (typeof(window) !== 'undefined') {
ReactDOM = window.ReactDOM
}
var MASK_REGEX = {
'9': /\d/,
'A': /[A-Za-z\u0410-\u044f\u0401\u0451\xc0-\xff\xb5]/,
'*': /[\dA-Za-z\u0410-\u044f\u0401\u0451\xc0-\xff\xb5]/
}
var MASK_CHARS = Object.keys(MASK_REGEX)
var PTRN_REGEX = new RegExp('[' + MASK_CHARS.join(',') + ']', 'g')
var ReactMaskMixin = {
componentWillMount: function() {
this.mask = {
props: {
value: this.props.value,
onClick: this._onClick,
onChange: this._onChange,
onKeyDown: this._onKeyDown,
onFocus: this._onFocus,
onBlur: this._onBlur
},
focused: false,
focusing: false,
empty: true,
cursorPrev: 0,
cursor: 0
}
if (this.props.value && this.props.mask) {
this.processValue(this.props.value)
}
},
componentDidUpdate: function(prevProps) {
var input = ReactDOM.findDOMNode(this);
if (input === document.activeElement) {
input.setSelectionRange(
this.mask.cursor,
this.mask.cursor
)
}
if (prevProps.mask != this.props.mask) {
this.processValue(this.mask.props.value)
this.forceUpdate()
}
},
processValue: function(value) {
var mask = this.props.mask
var pattern = mask.replace(PTRN_REGEX, '_')
var rexps = {}
mask.split('').forEach(function(c, i) {
if (~MASK_CHARS.indexOf(c)) {
rexps[i+1] = MASK_REGEX[c]
}
})
var cursorMax = 0
var cursorMin = 0
var newValue = ''
var newValueMasked;
var nextChar
for (i = 0; i < mask.length; i++) {
if (~MASK_CHARS.indexOf(mask[i])) {
cursorMin = i
break
}
}
for (var i = 0, j = 0; i < mask.length;) {
if (!~MASK_CHARS.indexOf(mask[i])) {
newValue += mask[i]
if (mask[i] == value[j]) {
j++
}
i++
} else {
if (nextChar = value.substr(j++, 1)) {
if (rexps[newValue.length+1].test(nextChar)) {
newValue += nextChar
cursorMax = newValue.length
i++
}
} else {
newValue = newValue.substr(0, cursorMax)
if (this.mask.focusing ||
this.mask.focused) {
newValueMasked = newValue + pattern.slice(cursorMax)
}
break
}
}
}
var cursorCurr = 0
var cursorPrev = this.mask.cursor
cursorMax = Math.max(cursorMax, cursorMin)
if (this.isMounted()) {
if (this.mask.focused) {
cursorCurr = ReactDOM.findDOMNode(this).selectionStart
} else {
cursorCurr = cursorMax
}
}
if (cursorCurr <= cursorMin) {
cursorCurr = cursorMin
} else if (cursorCurr >= cursorMax) {
cursorCurr = cursorMax
} else if (this.mask.cursor > cursorCurr) { //removing
for (var i = cursorCurr; i >= 0; i--) {
cursorCurr = i
if (rexps[i] && !rexps[i+1]) break
if (rexps[i] && rexps[i+1] && rexps[i+1].test(newValue[i])) {
break
}
}
} else {
for (var i = cursorCurr; i <= cursorMax; i++) {
cursorCurr = i
if (!rexps[i+1] && rexps[i]) break
if (rexps[i+1] && rexps[i+1].test(newValue[i])) {
if (!rexps[i]) {
cursorCurr++
}
break
}
}
}
this.mask.value = newValue
this.mask.props.value = newValueMasked || newValue
this.mask.empty = cursorMax == cursorMin
this.mask.cursor = cursorCurr
},
_onBlur: function(e) {
if (this.props.mask) {
var cursor = this.mask.cursor
var value = this.mask.value
if (!this.mask.empty) {
this.mask.props.value = value
} else {
this.mask.props.value = ''
}
e.target.value = value
if (this.props.onChange) {
this.props.onChange(e)
}
if (this.props.valueLink) {
this.props.valueLink.requestChange(value)
}
this.mask.focused = false
this.forceUpdate()
}
if (this.props.onBlur) {
this.props.onBlur(e)
}
},
_onChange: function(e) {
if (this.props.mask) {
this.processValue(e.target.value)
e.target.value = this.mask.props.value
this.forceUpdate()
}
if (this.props.onChange) {
this.props.onChange(e)
}
if (this.props.valueLink) {
this.props.valueLink.requestChange(e.target.value)
}
},
_onKeyDown: function(e) {
if (this.props.mask) {
this.mask.cursor = ReactDOM.findDOMNode(this).selectionStart
}
if (this.props.onKeyDown) {
this.props.onKeyDown(e)
}
},
_onFocus: function(e) {
this.mask.focusing = true
this._onChange(e)
this.mask.focusing = false
this.mask.focused = true
if (this.props.onFocus) {
this.props.onFocus(e)
}
},
_onClick: function(e) {
this._onChange(e)
if (this.props.onClick) {
this.props.onClick(e)
}
}
}
// Export ReactMaskMixin for CommonJS. If being loaded as an
// AMD module, define it as such. Otherwise, just add
// `ReactMaskMixin` to the global object
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = ReactMaskMixin;
}
exports.ReactMaskMixin = ReactMaskMixin;
} else if (typeof define === 'function' && define.amd) {
// Return the ReactMaskMixin as an AMD module:
define([], function() {
return ReactMaskMixin;
});
} else {
// Declare `ReactMaskMixin` on the root (global/window) object:
root['ReactMaskMixin'] = ReactMaskMixin;
}
})(this)