-
Notifications
You must be signed in to change notification settings - Fork 10
/
MaskInput.svelte
182 lines (156 loc) · 4.43 KB
/
MaskInput.svelte
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
<script>
import { createEventDispatcher, tick, onMount, onDestroy } from 'svelte';
import { createInput, defaults } from 'input-core';
export let value = '';
export let defaultValue = undefined;
export let reformat = undefined;
export let maskString = undefined;
export let maskChar = defaults.maskChar;
export let mask = defaults.mask;
export let maskFormat = defaults.maskFormat;
export let alwaysShowMask = false;
export let showMask = false;
const KEYBOARD = {
BACKSPACE: 8,
DELETE: 46,
};
const dispatch = createEventDispatcher();
const input = createInput({
value: value || defaultValue || '',
reformat,
maskString,
maskChar,
mask,
maskFormat,
});
let shouldShowMask = alwaysShowMask || showMask;
$: shouldShowMask = alwaysShowMask || showMask;
$: input.setReformat(reformat);
$: input.setMaskFormat(maskFormat);
$: input.setMask(mask);
$: input.setMaskString(maskString);
$: input.setMaskChar(maskChar);
$: value !== undefined && input.setValue(value);
onMount(() => {
input.subscribe(applyValue);
});
onDestroy(() => {
input.unsubscribe(applyValue);
});
let canSetSelection = false;
let inputValue = setupInputValue(input.getState());
let inputEl;
function setupInputValue({ maskedValue, visibleValue }) {
if (shouldShowMask && (canSetSelection || alwaysShowMask)) {
return maskedValue;
}
return visibleValue;
}
function applyValue({ maskedValue, visibleValue, selection, value }) {
inputValue = setupInputValue({ maskedValue, visibleValue });
setSelection(selection);
dispatchChangeEvent({
unmasked: reformat
? value
: value
.filter(item => item.type === 1)
.map(item => item.char)
.join(''),
maskedValue,
visibleValue,
});
}
async function setSelection({ start, end }) {
if (!canSetSelection) {
return;
}
await tick();
inputEl.setSelectionRange(start, end);
const raf =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
(fn => setTimeout(fn, 0));
// For android
raf(() => inputEl.setSelectionRange(start, end));
}
function setupSelection() {
input.setSelection({
start: inputEl.selectionStart,
end: inputEl.selectionEnd,
});
}
function getValue() {
if (showMask && (canSetSelection || alwaysShowMask)) {
return input.getState().maskedValue;
} else {
return input.getState().visibleValue;
}
}
function handleInput(e) {
const prevValue = getValue();
// fix conflict by update value in mask model
if (e.target.value !== prevValue) {
input.input(e.data);
setSelection(input.getSelection());
// Timeout needed for IE
setTimeout(() => setSelection(input.getSelection()), 0);
}
}
function handlePaste(e) {
e.preventDefault();
setupSelection();
// getData value needed for IE also works in FF & Chrome
input.paste(e.clipboardData.getData('Text'));
setSelection(input.getSelection());
// Timeout needed for IE
setTimeout(() => setSelection(input.getSelection()), 0);
}
function handleKeyPress(e) {
if (e.metaKey || e.altKey || e.ctrlKey || e.key === 'Enter') {
return;
}
e.preventDefault();
setupSelection();
input.input(e.key || e.data || String.fromCharCode(e.which));
setSelection(input.getSelection());
}
function handleKeyDown(e) {
if (e.which === KEYBOARD.BACKSPACE) {
e.preventDefault();
setupSelection();
input.removePreviosOrSelected();
setSelection(input.getSelection());
}
if (e.which === KEYBOARD.DELETE) {
e.preventDefault();
setupSelection();
input.removeNextOrSelected();
setSelection(input.getSelection());
}
}
function handleFocus(e) {
canSetSelection = true;
dispatch('focus', e);
}
function handleBlur(e) {
canSetSelection = false;
dispatch('blur', e);
}
function dispatchChangeEvent({ unmasked, maskedValue, visibleValue }) {
dispatch('change', {
element: inputEl,
inputState: { unmaskedValue: unmasked, maskedValue, visibleValue },
});
}
</script>
<input
{...$$restProps}
value={inputValue}
on:input={handleInput}
on:keydown={handleKeyDown}
on:keypress={handleKeyPress}
on:paste={handlePaste}
on:focus={handleFocus}
on:blur={handleBlur}
bind:this={inputEl} />