@octanejs/mantine-hooks 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/package.json +45 -0
- package/src/index.ts +259 -0
- package/src/internal.ts +6 -0
- package/src/use-click-outside/use-click-outside.ts +57 -0
- package/src/use-clipboard/use-clipboard.ts +69 -0
- package/src/use-collapse/use-collapse.ts +162 -0
- package/src/use-collapse/use-horizontal-collapse.ts +164 -0
- package/src/use-color-scheme/use-color-scheme.ts +13 -0
- package/src/use-counter/use-counter.ts +59 -0
- package/src/use-debounced-callback/use-debounced-callback.ts +159 -0
- package/src/use-debounced-state/use-debounced-state.ts +44 -0
- package/src/use-debounced-value/use-debounced-value.ts +71 -0
- package/src/use-did-update/use-did-update.ts +26 -0
- package/src/use-disclosure/use-disclosure.ts +57 -0
- package/src/use-document-title/use-document-title.ts +9 -0
- package/src/use-document-visibility/use-document-visibility.ts +14 -0
- package/src/use-drag/use-drag.ts +463 -0
- package/src/use-event-listener/use-event-listener.ts +47 -0
- package/src/use-eye-dropper/use-eye-dropper.ts +41 -0
- package/src/use-favicon/use-favicon.ts +41 -0
- package/src/use-fetch/use-fetch.ts +85 -0
- package/src/use-file-dialog/use-file-dialog.ts +140 -0
- package/src/use-floating-window/use-floating-window.ts +431 -0
- package/src/use-focus-return/use-focus-return.ts +65 -0
- package/src/use-focus-trap/scope-tab.ts +36 -0
- package/src/use-focus-trap/tabbable.ts +65 -0
- package/src/use-focus-trap/use-focus-trap.ts +84 -0
- package/src/use-focus-within/use-focus-within.ts +90 -0
- package/src/use-force-update/use-force-update.ts +8 -0
- package/src/use-fullscreen/use-fullscreen.ts +153 -0
- package/src/use-hash/use-hash.ts +42 -0
- package/src/use-headroom/use-headroom.ts +160 -0
- package/src/use-hotkeys/parse-hotkey.ts +130 -0
- package/src/use-hotkeys/use-hotkeys.ts +63 -0
- package/src/use-hover/use-hover.ts +57 -0
- package/src/use-id/use-id.ts +23 -0
- package/src/use-idle/use-idle.ts +54 -0
- package/src/use-in-viewport/use-in-viewport.ts +34 -0
- package/src/use-input-state/use-input-state.ts +41 -0
- package/src/use-intersection/use-intersection.ts +41 -0
- package/src/use-interval/use-interval.ts +84 -0
- package/src/use-is-first-render/use-is-first-render.ts +12 -0
- package/src/use-isomorphic-effect/use-isomorphic-effect.ts +5 -0
- package/src/use-list-state/use-list-state.ts +157 -0
- package/src/use-local-storage/create-storage.ts +200 -0
- package/src/use-local-storage/use-local-storage.ts +19 -0
- package/src/use-logger/use-logger.ts +16 -0
- package/src/use-long-press/use-long-press.ts +188 -0
- package/src/use-map/use-map.ts +28 -0
- package/src/use-mask/use-mask.ts +1049 -0
- package/src/use-media-query/use-media-query.ts +57 -0
- package/src/use-merged-ref/use-merged-ref.ts +53 -0
- package/src/use-mounted/use-mounted.ts +7 -0
- package/src/use-mouse/use-mouse.ts +91 -0
- package/src/use-move/use-move.ts +165 -0
- package/src/use-mutation-observer/use-mutation-observer.ts +61 -0
- package/src/use-network/use-network.ts +70 -0
- package/src/use-orientation/use-orientation.ts +87 -0
- package/src/use-os/use-os.ts +93 -0
- package/src/use-page-leave/use-page-leave.ts +10 -0
- package/src/use-pagination/use-pagination.ts +149 -0
- package/src/use-previous/use-previous.ts +11 -0
- package/src/use-queue/use-queue.ts +71 -0
- package/src/use-radial-move/use-radial-move.ts +161 -0
- package/src/use-reduced-motion/use-reduced-motion.ts +6 -0
- package/src/use-resize-observer/use-resize-observer.ts +95 -0
- package/src/use-roving-index/use-roving-index.ts +386 -0
- package/src/use-scroll-direction/use-scroll-direction.ts +41 -0
- package/src/use-scroll-into-view/use-scroll-into-view.ts +294 -0
- package/src/use-scroll-spy/use-scroll-spy.ts +155 -0
- package/src/use-scroller/use-scroller.ts +225 -0
- package/src/use-selection/use-selection.ts +120 -0
- package/src/use-session-storage/use-session-storage.ts +19 -0
- package/src/use-set/use-set.ts +88 -0
- package/src/use-set-state/use-set-state.ts +29 -0
- package/src/use-shallow-effect/use-shallow-effect.ts +40 -0
- package/src/use-splitter/use-splitter.ts +1242 -0
- package/src/use-state-history/use-state-history.ts +70 -0
- package/src/use-text-selection/use-text-selection.ts +20 -0
- package/src/use-throttled-callback/use-throttled-callback.ts +60 -0
- package/src/use-throttled-state/use-throttled-state.ts +12 -0
- package/src/use-throttled-value/use-throttled-value.ts +23 -0
- package/src/use-timeout/use-timeout.ts +56 -0
- package/src/use-toggle/use-toggle.ts +23 -0
- package/src/use-uncontrolled/use-uncontrolled.ts +53 -0
- package/src/use-validated-state/use-validated-state.ts +50 -0
- package/src/use-viewport-size/use-viewport-size.ts +23 -0
- package/src/use-window-event/use-window-event.ts +16 -0
- package/src/use-window-scroll/use-window-scroll.ts +49 -0
- package/src/utils/clamp/clamp.ts +15 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/lower-first/lower-first.ts +3 -0
- package/src/utils/random-id/random-id.ts +3 -0
- package/src/utils/range/range.ts +10 -0
- package/src/utils/shallow-equal/shallow-equal.ts +34 -0
- package/src/utils/upper-first/upper-first.ts +3 -0
- package/src/utils/use-callback-ref/use-callback-ref.ts +11 -0
|
@@ -0,0 +1,1049 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_TOKENS: Record<string, RegExp> = {
|
|
4
|
+
'9': /[0-9]/,
|
|
5
|
+
a: /[A-Za-z]/,
|
|
6
|
+
A: /[A-Z]/,
|
|
7
|
+
'*': /[A-Za-z0-9]/,
|
|
8
|
+
'#': /[-+0-9]/,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface UseMaskOptions {
|
|
12
|
+
/** Mask pattern string or array of string literals and RegExp objects */
|
|
13
|
+
mask: string | Array<string | RegExp>;
|
|
14
|
+
|
|
15
|
+
/** Override or extend the default token map */
|
|
16
|
+
tokens?: Record<string, RegExp>;
|
|
17
|
+
|
|
18
|
+
/** Called before masking on each keystroke, can return overrides for mask options */
|
|
19
|
+
modify?: (
|
|
20
|
+
value: string,
|
|
21
|
+
) => Partial<Pick<UseMaskOptions, 'mask' | 'tokens' | 'slotChar' | 'separate'>> | undefined;
|
|
22
|
+
|
|
23
|
+
/** When true, raw and display values are decoupled */
|
|
24
|
+
separate?: boolean;
|
|
25
|
+
|
|
26
|
+
/** Character displayed in unfilled slots, `"_"` by default */
|
|
27
|
+
slotChar?: string | null;
|
|
28
|
+
|
|
29
|
+
/** Show mask pattern even when field is empty and unfocused */
|
|
30
|
+
alwaysShowMask?: boolean;
|
|
31
|
+
|
|
32
|
+
/** Show mask placeholder on focus, `true` by default */
|
|
33
|
+
showMaskOnFocus?: boolean;
|
|
34
|
+
|
|
35
|
+
/** Transform each character before validation and insertion */
|
|
36
|
+
transform?: (char: string) => string;
|
|
37
|
+
|
|
38
|
+
/** Clear value on blur when mask is incomplete, `false` by default */
|
|
39
|
+
autoClear?: boolean;
|
|
40
|
+
|
|
41
|
+
/** Sets aria-invalid on the input */
|
|
42
|
+
invalid?: boolean;
|
|
43
|
+
|
|
44
|
+
/** Called on every change with raw and masked values */
|
|
45
|
+
onChangeRaw?: (rawValue: string, maskedValue: string) => void;
|
|
46
|
+
|
|
47
|
+
/** Called when all required mask slots are filled */
|
|
48
|
+
onComplete?: (maskedValue: string, rawValue: string) => void;
|
|
49
|
+
|
|
50
|
+
/** Escape hatch for advanced cursor/value manipulation */
|
|
51
|
+
beforeMaskedStateChange?: (states: {
|
|
52
|
+
previousState: MaskState;
|
|
53
|
+
currentState: MaskState;
|
|
54
|
+
nextState: MaskState;
|
|
55
|
+
}) => MaskState;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface MaskState {
|
|
59
|
+
value: string;
|
|
60
|
+
selection: { start: number; end: number } | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface UseMaskReturnValue {
|
|
64
|
+
/** Ref to attach to the input element */
|
|
65
|
+
ref: React.RefCallback<HTMLInputElement>;
|
|
66
|
+
|
|
67
|
+
/** Current masked display value */
|
|
68
|
+
value: string;
|
|
69
|
+
|
|
70
|
+
/** Current raw unmasked value */
|
|
71
|
+
rawValue: string;
|
|
72
|
+
|
|
73
|
+
/** Whether all required mask slots are filled */
|
|
74
|
+
isComplete: boolean;
|
|
75
|
+
|
|
76
|
+
/** Clear the input value and reset state */
|
|
77
|
+
reset: () => void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface MaskSlot {
|
|
81
|
+
type: 'token' | 'literal';
|
|
82
|
+
char: string;
|
|
83
|
+
pattern?: RegExp;
|
|
84
|
+
optional?: boolean;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
interface UndoState {
|
|
88
|
+
rawValue: string;
|
|
89
|
+
selectionStart: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const MAX_UNDO_HISTORY = 100;
|
|
93
|
+
|
|
94
|
+
function parseMask(
|
|
95
|
+
mask: string | Array<string | RegExp>,
|
|
96
|
+
tokens: Record<string, RegExp>,
|
|
97
|
+
): MaskSlot[] {
|
|
98
|
+
if (Array.isArray(mask)) {
|
|
99
|
+
return mask.map((item) => {
|
|
100
|
+
if (item instanceof RegExp) {
|
|
101
|
+
return { type: 'token', char: '_', pattern: item };
|
|
102
|
+
}
|
|
103
|
+
return { type: 'literal', char: item };
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const slots: MaskSlot[] = [];
|
|
108
|
+
let optional = false;
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < mask.length; i++) {
|
|
111
|
+
const char = mask[i];
|
|
112
|
+
|
|
113
|
+
if (char === '\\' && i + 1 < mask.length) {
|
|
114
|
+
i++;
|
|
115
|
+
slots.push({ type: 'literal', char: mask[i] });
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (char === '?') {
|
|
120
|
+
optional = true;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (tokens[char]) {
|
|
125
|
+
slots.push({ type: 'token', char, pattern: tokens[char], optional });
|
|
126
|
+
} else {
|
|
127
|
+
slots.push({ type: 'literal', char, optional });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return slots;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getSlotChar(slotCharOption: string | null | undefined, index: number): string {
|
|
135
|
+
if (slotCharOption === null || slotCharOption === '' || slotCharOption === undefined) {
|
|
136
|
+
return '';
|
|
137
|
+
}
|
|
138
|
+
if (slotCharOption.length > 1) {
|
|
139
|
+
return slotCharOption[index] || '_';
|
|
140
|
+
}
|
|
141
|
+
return slotCharOption;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function applyMaskToRaw(
|
|
145
|
+
raw: string,
|
|
146
|
+
slots: MaskSlot[],
|
|
147
|
+
_slotCharOption: string | null | undefined,
|
|
148
|
+
transform?: (char: string) => string,
|
|
149
|
+
): string {
|
|
150
|
+
let result = '';
|
|
151
|
+
let rawIndex = 0;
|
|
152
|
+
let slotIndex = 0;
|
|
153
|
+
|
|
154
|
+
for (slotIndex = 0; slotIndex < slots.length; slotIndex++) {
|
|
155
|
+
const slot = slots[slotIndex];
|
|
156
|
+
if (slot.type === 'literal') {
|
|
157
|
+
result += slot.char;
|
|
158
|
+
} else if (rawIndex < raw.length) {
|
|
159
|
+
const ch = transform ? transform(raw[rawIndex]) : raw[rawIndex];
|
|
160
|
+
if (slot.pattern && slot.pattern.test(ch)) {
|
|
161
|
+
result += ch;
|
|
162
|
+
rawIndex++;
|
|
163
|
+
} else {
|
|
164
|
+
rawIndex++;
|
|
165
|
+
slotIndex--;
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function buildDisplayValue(
|
|
176
|
+
value: string,
|
|
177
|
+
slots: MaskSlot[],
|
|
178
|
+
slotCharOption: string | null | undefined,
|
|
179
|
+
showSlots: boolean,
|
|
180
|
+
): string {
|
|
181
|
+
if (!showSlots) {
|
|
182
|
+
return value;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let display = value;
|
|
186
|
+
|
|
187
|
+
for (let i = value.length; i < slots.length; i++) {
|
|
188
|
+
const slot = slots[i];
|
|
189
|
+
if (slot.type === 'literal') {
|
|
190
|
+
display += slot.char;
|
|
191
|
+
} else {
|
|
192
|
+
const sc = getSlotChar(slotCharOption, i);
|
|
193
|
+
if (!sc) {
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
display += sc;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return display;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function extractRaw(masked: string, slots: MaskSlot[]): string {
|
|
204
|
+
let raw = '';
|
|
205
|
+
for (let i = 0; i < masked.length && i < slots.length; i++) {
|
|
206
|
+
if (slots[i].type === 'token') {
|
|
207
|
+
raw += masked[i];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return raw;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function checkComplete(masked: string, slots: MaskSlot[]): boolean {
|
|
214
|
+
for (let i = 0; i < slots.length; i++) {
|
|
215
|
+
if (slots[i].type === 'token' && !slots[i].optional) {
|
|
216
|
+
if (i >= masked.length) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if (!slots[i].pattern!.test(masked[i])) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function findNextTokenIndex(slots: MaskSlot[], from: number): number {
|
|
228
|
+
for (let i = from; i < slots.length; i++) {
|
|
229
|
+
if (slots[i].type === 'token') {
|
|
230
|
+
return i;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return slots.length;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function findPrevTokenIndex(slots: MaskSlot[], from: number): number {
|
|
237
|
+
for (let i = from; i >= 0; i--) {
|
|
238
|
+
if (slots[i].type === 'token') {
|
|
239
|
+
return i;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return -1;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function processInput(
|
|
246
|
+
inputValue: string,
|
|
247
|
+
slots: MaskSlot[],
|
|
248
|
+
_slotCharOption: string | null | undefined,
|
|
249
|
+
): string {
|
|
250
|
+
let result = '';
|
|
251
|
+
let inputIndex = 0;
|
|
252
|
+
|
|
253
|
+
for (
|
|
254
|
+
let slotIndex = 0;
|
|
255
|
+
slotIndex < slots.length && inputIndex <= inputValue.length;
|
|
256
|
+
slotIndex++
|
|
257
|
+
) {
|
|
258
|
+
const slot = slots[slotIndex];
|
|
259
|
+
|
|
260
|
+
if (slot.type === 'literal') {
|
|
261
|
+
result += slot.char;
|
|
262
|
+
if (inputIndex < inputValue.length && inputValue[inputIndex] === slot.char) {
|
|
263
|
+
inputIndex++;
|
|
264
|
+
}
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (inputIndex >= inputValue.length) {
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
while (inputIndex < inputValue.length) {
|
|
273
|
+
const ch = inputValue[inputIndex];
|
|
274
|
+
inputIndex++;
|
|
275
|
+
|
|
276
|
+
if (slot.pattern!.test(ch)) {
|
|
277
|
+
result += ch;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (result.length <= slotIndex) {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function getResolvedOptions(options: UseMaskOptions, rawValue: string) {
|
|
291
|
+
const tokens = { ...DEFAULT_TOKENS, ...options.tokens };
|
|
292
|
+
let mask = options.mask;
|
|
293
|
+
let slotChar: string | null | undefined = options.slotChar === undefined ? '_' : options.slotChar;
|
|
294
|
+
let separate = options.separate ?? false;
|
|
295
|
+
|
|
296
|
+
if (options.modify) {
|
|
297
|
+
const overrides = options.modify(rawValue);
|
|
298
|
+
if (overrides) {
|
|
299
|
+
if (overrides.mask !== undefined) {
|
|
300
|
+
mask = overrides.mask;
|
|
301
|
+
}
|
|
302
|
+
if (overrides.tokens !== undefined) {
|
|
303
|
+
Object.assign(tokens, overrides.tokens);
|
|
304
|
+
}
|
|
305
|
+
if (overrides.slotChar !== undefined) {
|
|
306
|
+
slotChar = overrides.slotChar;
|
|
307
|
+
}
|
|
308
|
+
if (overrides.separate !== undefined) {
|
|
309
|
+
separate = overrides.separate;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const slots = parseMask(mask, tokens);
|
|
315
|
+
return { slots, slotChar, separate, tokens, transform: options.transform };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export function formatMask(raw: string, options: UseMaskOptions): string {
|
|
319
|
+
const { slots, slotChar, transform } = getResolvedOptions(options, raw);
|
|
320
|
+
return applyMaskToRaw(raw, slots, slotChar, transform);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export function unformatMask(masked: string, options: UseMaskOptions): string {
|
|
324
|
+
const { slots } = getResolvedOptions(options, '');
|
|
325
|
+
return extractRaw(masked, slots);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function isMaskComplete(masked: string, options: UseMaskOptions): boolean {
|
|
329
|
+
const { slots } = getResolvedOptions(options, '');
|
|
330
|
+
return checkComplete(masked, slots);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function generatePattern(mode: 'full' | 'full-inexact', options: UseMaskOptions): string {
|
|
334
|
+
const { slots } = getResolvedOptions(options, '');
|
|
335
|
+
let pattern = '';
|
|
336
|
+
|
|
337
|
+
for (const slot of slots) {
|
|
338
|
+
if (slot.type === 'literal') {
|
|
339
|
+
pattern += slot.char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
340
|
+
} else {
|
|
341
|
+
const src = slot.pattern!.source;
|
|
342
|
+
if (mode === 'full-inexact') {
|
|
343
|
+
pattern += slot.optional ? `${src}?` : src;
|
|
344
|
+
} else {
|
|
345
|
+
pattern += slot.optional ? `(${src})?` : `(${src})`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return pattern;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function useMask(options: UseMaskOptions): UseMaskReturnValue {
|
|
354
|
+
const optionsRef = useRef(options);
|
|
355
|
+
optionsRef.current = options;
|
|
356
|
+
|
|
357
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
358
|
+
const [maskedValue, setMaskedValue] = useState('');
|
|
359
|
+
const [rawValue, setRawValue] = useState('');
|
|
360
|
+
const processedRef = useRef('');
|
|
361
|
+
const displayValueRef = useRef('');
|
|
362
|
+
const rawValueRef = useRef('');
|
|
363
|
+
const wasCompleteRef = useRef(false);
|
|
364
|
+
const isFocusedRef = useRef(false);
|
|
365
|
+
const undoStackRef = useRef<UndoState[]>([]);
|
|
366
|
+
const redoStackRef = useRef<UndoState[]>([]);
|
|
367
|
+
|
|
368
|
+
const getOptions = useCallback(() => {
|
|
369
|
+
const opts = optionsRef.current;
|
|
370
|
+
return getResolvedOptions(opts, rawValue);
|
|
371
|
+
}, [rawValue]);
|
|
372
|
+
|
|
373
|
+
const applyValue = useCallback(
|
|
374
|
+
({
|
|
375
|
+
reprocessed,
|
|
376
|
+
newRaw,
|
|
377
|
+
displayValue,
|
|
378
|
+
resolvedSlots,
|
|
379
|
+
cursorPos,
|
|
380
|
+
notifyChange,
|
|
381
|
+
}: {
|
|
382
|
+
reprocessed: string;
|
|
383
|
+
newRaw: string;
|
|
384
|
+
displayValue: string;
|
|
385
|
+
resolvedSlots: MaskSlot[];
|
|
386
|
+
cursorPos?: number;
|
|
387
|
+
notifyChange: boolean;
|
|
388
|
+
}) => {
|
|
389
|
+
const opts = optionsRef.current;
|
|
390
|
+
|
|
391
|
+
processedRef.current = reprocessed;
|
|
392
|
+
displayValueRef.current = displayValue;
|
|
393
|
+
rawValueRef.current = newRaw;
|
|
394
|
+
setMaskedValue(displayValue);
|
|
395
|
+
setRawValue(newRaw);
|
|
396
|
+
|
|
397
|
+
if (inputRef.current) {
|
|
398
|
+
inputRef.current.value = displayValue;
|
|
399
|
+
if (cursorPos !== undefined && document.activeElement === inputRef.current) {
|
|
400
|
+
const pos = Math.min(cursorPos, reprocessed.length);
|
|
401
|
+
inputRef.current.setSelectionRange(pos, pos);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (notifyChange && opts.onChangeRaw) {
|
|
406
|
+
opts.onChangeRaw(newRaw, displayValue);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const complete = checkComplete(reprocessed, resolvedSlots);
|
|
410
|
+
if (notifyChange && complete && !wasCompleteRef.current && opts.onComplete) {
|
|
411
|
+
opts.onComplete(displayValue, newRaw);
|
|
412
|
+
}
|
|
413
|
+
wasCompleteRef.current = complete;
|
|
414
|
+
},
|
|
415
|
+
[],
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
const updateValue = useCallback(
|
|
419
|
+
(newMasked: string, cursorPos?: number) => {
|
|
420
|
+
const opts = optionsRef.current;
|
|
421
|
+
const { slots } = getResolvedOptions(
|
|
422
|
+
opts,
|
|
423
|
+
extractRaw(newMasked, getResolvedOptions(opts, '').slots),
|
|
424
|
+
);
|
|
425
|
+
const raw = extractRaw(newMasked, slots);
|
|
426
|
+
|
|
427
|
+
const { slots: resolvedSlots, slotChar } = getResolvedOptions(opts, raw);
|
|
428
|
+
|
|
429
|
+
const reprocessed = processInput(newMasked, resolvedSlots, slotChar);
|
|
430
|
+
const newRaw = extractRaw(reprocessed, resolvedSlots);
|
|
431
|
+
|
|
432
|
+
const showSlots = opts.alwaysShowMask || isFocusedRef.current;
|
|
433
|
+
const showOnFocus = opts.showMaskOnFocus !== false;
|
|
434
|
+
const shouldShowSlots = showSlots && (showOnFocus || reprocessed.length > 0);
|
|
435
|
+
|
|
436
|
+
const displayValue = buildDisplayValue(reprocessed, resolvedSlots, slotChar, shouldShowSlots);
|
|
437
|
+
|
|
438
|
+
applyValue({
|
|
439
|
+
reprocessed,
|
|
440
|
+
newRaw,
|
|
441
|
+
displayValue,
|
|
442
|
+
resolvedSlots,
|
|
443
|
+
cursorPos,
|
|
444
|
+
notifyChange: true,
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
return { displayValue, newRaw, reprocessed, resolvedSlots };
|
|
448
|
+
},
|
|
449
|
+
[applyValue, getOptions],
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
const initializeInputValue = useCallback(
|
|
453
|
+
(node: HTMLInputElement) => {
|
|
454
|
+
const opts = optionsRef.current;
|
|
455
|
+
|
|
456
|
+
if (!node.value) {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const { slots: initialSlots, slotChar: initialSlotChar } = getResolvedOptions(opts, '');
|
|
461
|
+
const initialProcessed = processInput(node.value, initialSlots, initialSlotChar);
|
|
462
|
+
const initialRaw = extractRaw(initialProcessed, initialSlots);
|
|
463
|
+
const { slots: resolvedSlots, slotChar } = getResolvedOptions(opts, initialRaw);
|
|
464
|
+
const reprocessed = processInput(node.value, resolvedSlots, slotChar);
|
|
465
|
+
const newRaw = extractRaw(reprocessed, resolvedSlots);
|
|
466
|
+
const showSlots = opts.alwaysShowMask || isFocusedRef.current;
|
|
467
|
+
const showOnFocus = opts.showMaskOnFocus !== false;
|
|
468
|
+
const shouldShowSlots = showSlots && (showOnFocus || reprocessed.length > 0);
|
|
469
|
+
const displayValue = buildDisplayValue(reprocessed, resolvedSlots, slotChar, shouldShowSlots);
|
|
470
|
+
|
|
471
|
+
applyValue({
|
|
472
|
+
reprocessed,
|
|
473
|
+
newRaw,
|
|
474
|
+
displayValue,
|
|
475
|
+
resolvedSlots,
|
|
476
|
+
notifyChange: false,
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
return true;
|
|
480
|
+
},
|
|
481
|
+
[applyValue],
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
const pushUndoState = useCallback(() => {
|
|
485
|
+
const input = inputRef.current;
|
|
486
|
+
const selectionStart = input?.selectionStart ?? rawValueRef.current.length;
|
|
487
|
+
const state: UndoState = {
|
|
488
|
+
rawValue: rawValueRef.current,
|
|
489
|
+
selectionStart,
|
|
490
|
+
};
|
|
491
|
+
const stack = undoStackRef.current;
|
|
492
|
+
const top = stack[stack.length - 1];
|
|
493
|
+
if (top && top.rawValue === state.rawValue && top.selectionStart === state.selectionStart) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
stack.push(state);
|
|
497
|
+
if (stack.length > MAX_UNDO_HISTORY) {
|
|
498
|
+
stack.shift();
|
|
499
|
+
}
|
|
500
|
+
redoStackRef.current = [];
|
|
501
|
+
}, []);
|
|
502
|
+
|
|
503
|
+
const applyHistoryState = useCallback(
|
|
504
|
+
(target: UndoState) => {
|
|
505
|
+
const opts = optionsRef.current;
|
|
506
|
+
const { slots, slotChar, transform } = getResolvedOptions(opts, target.rawValue);
|
|
507
|
+
const newMasked = applyMaskToRaw(target.rawValue, slots, slotChar, transform);
|
|
508
|
+
updateValue(newMasked, target.selectionStart);
|
|
509
|
+
},
|
|
510
|
+
[updateValue],
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
const handleInput = useCallback(
|
|
514
|
+
(e: Event) => {
|
|
515
|
+
const input = e.target as HTMLInputElement;
|
|
516
|
+
const opts = optionsRef.current;
|
|
517
|
+
|
|
518
|
+
const { slots: resolvedSlots, slotChar, transform } = getResolvedOptions(opts, '');
|
|
519
|
+
const prev = displayValueRef.current;
|
|
520
|
+
const curr = input.value;
|
|
521
|
+
|
|
522
|
+
let prefixLen = 0;
|
|
523
|
+
const maxPrefix = Math.min(prev.length, curr.length);
|
|
524
|
+
while (prefixLen < maxPrefix && prev[prefixLen] === curr[prefixLen]) {
|
|
525
|
+
prefixLen++;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
let suffixLen = 0;
|
|
529
|
+
const maxSuffix = Math.min(prev.length - prefixLen, curr.length - prefixLen);
|
|
530
|
+
while (
|
|
531
|
+
suffixLen < maxSuffix &&
|
|
532
|
+
prev[prev.length - 1 - suffixLen] === curr[curr.length - 1 - suffixLen]
|
|
533
|
+
) {
|
|
534
|
+
suffixLen++;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const insertedText = curr.slice(prefixLen, curr.length - suffixLen);
|
|
538
|
+
const removedEnd = prev.length - suffixLen;
|
|
539
|
+
|
|
540
|
+
const beforeRaw = extractRaw(prev.slice(0, prefixLen), resolvedSlots.slice(0, prefixLen));
|
|
541
|
+
const afterRaw = extractRaw(prev.slice(removedEnd), resolvedSlots.slice(removedEnd));
|
|
542
|
+
const reformatted = applyMaskToRaw(
|
|
543
|
+
beforeRaw + insertedText + afterRaw,
|
|
544
|
+
resolvedSlots,
|
|
545
|
+
slotChar,
|
|
546
|
+
transform,
|
|
547
|
+
);
|
|
548
|
+
const maskedPrefix = applyMaskToRaw(
|
|
549
|
+
beforeRaw + insertedText,
|
|
550
|
+
resolvedSlots,
|
|
551
|
+
slotChar,
|
|
552
|
+
transform,
|
|
553
|
+
);
|
|
554
|
+
|
|
555
|
+
if (reformatted !== prev) {
|
|
556
|
+
pushUndoState();
|
|
557
|
+
}
|
|
558
|
+
updateValue(reformatted, maskedPrefix.length);
|
|
559
|
+
},
|
|
560
|
+
[pushUndoState, updateValue],
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
const clampCursorToProcessed = useCallback((input: HTMLInputElement) => {
|
|
564
|
+
const start = input.selectionStart ?? 0;
|
|
565
|
+
const end = input.selectionEnd ?? 0;
|
|
566
|
+
if (start !== end) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const opts = optionsRef.current;
|
|
571
|
+
const { slots } = getResolvedOptions(opts, '');
|
|
572
|
+
const processed = processedRef.current;
|
|
573
|
+
const endPos =
|
|
574
|
+
processed.length > 0
|
|
575
|
+
? findNextEditablePosition(processed.length, slots, processed)
|
|
576
|
+
: findNextTokenIndex(slots, 0);
|
|
577
|
+
const startPos = findNextTokenIndex(slots, 0);
|
|
578
|
+
|
|
579
|
+
if (start > endPos || start < startPos) {
|
|
580
|
+
input.setSelectionRange(endPos, endPos);
|
|
581
|
+
}
|
|
582
|
+
}, []);
|
|
583
|
+
|
|
584
|
+
const handleFocus = useCallback(() => {
|
|
585
|
+
isFocusedRef.current = true;
|
|
586
|
+
const opts = optionsRef.current;
|
|
587
|
+
const input = inputRef.current;
|
|
588
|
+
|
|
589
|
+
if (!input) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const { slots, slotChar } = getResolvedOptions(opts, '');
|
|
594
|
+
const showOnFocus = opts.showMaskOnFocus !== false;
|
|
595
|
+
const processed = processedRef.current;
|
|
596
|
+
|
|
597
|
+
if (showOnFocus || opts.alwaysShowMask) {
|
|
598
|
+
const display = buildDisplayValue(processed, slots, slotChar, true);
|
|
599
|
+
input.value = display;
|
|
600
|
+
displayValueRef.current = display;
|
|
601
|
+
setMaskedValue(display);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
requestAnimationFrame(() => {
|
|
605
|
+
if (input === document.activeElement) {
|
|
606
|
+
clampCursorToProcessed(input);
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}, [clampCursorToProcessed]);
|
|
610
|
+
|
|
611
|
+
const handleMouseUp = useCallback(() => {
|
|
612
|
+
const input = inputRef.current;
|
|
613
|
+
if (!input || input !== document.activeElement) {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
clampCursorToProcessed(input);
|
|
618
|
+
}, [clampCursorToProcessed]);
|
|
619
|
+
|
|
620
|
+
const handleMouseDown = useCallback(() => {
|
|
621
|
+
const input = inputRef.current;
|
|
622
|
+
if (!input) {
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
requestAnimationFrame(() => {
|
|
627
|
+
if (input !== document.activeElement) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const start = input.selectionStart ?? 0;
|
|
632
|
+
const end = input.selectionEnd ?? 0;
|
|
633
|
+
if (start !== end) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const opts = optionsRef.current;
|
|
638
|
+
const { slots } = getResolvedOptions(opts, '');
|
|
639
|
+
const processed = processedRef.current;
|
|
640
|
+
const endPos =
|
|
641
|
+
processed.length > 0
|
|
642
|
+
? findNextEditablePosition(processed.length, slots, processed)
|
|
643
|
+
: findNextTokenIndex(slots, 0);
|
|
644
|
+
|
|
645
|
+
if (start > endPos) {
|
|
646
|
+
input.setSelectionRange(endPos, endPos);
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
}, []);
|
|
650
|
+
|
|
651
|
+
const handleBlur = useCallback(() => {
|
|
652
|
+
isFocusedRef.current = false;
|
|
653
|
+
const opts = optionsRef.current;
|
|
654
|
+
const input = inputRef.current;
|
|
655
|
+
|
|
656
|
+
if (!input) {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const { slots, slotChar } = getResolvedOptions(opts, rawValue);
|
|
661
|
+
const expectedFocusDisplay = buildDisplayValue(processedRef.current, slots, slotChar, true);
|
|
662
|
+
const processed =
|
|
663
|
+
input.value === expectedFocusDisplay
|
|
664
|
+
? processedRef.current
|
|
665
|
+
: processInput(input.value, slots, slotChar);
|
|
666
|
+
const complete = checkComplete(processed, slots);
|
|
667
|
+
|
|
668
|
+
if (opts.autoClear && !complete && processed.length > 0) {
|
|
669
|
+
input.value = '';
|
|
670
|
+
processedRef.current = '';
|
|
671
|
+
displayValueRef.current = '';
|
|
672
|
+
rawValueRef.current = '';
|
|
673
|
+
setMaskedValue('');
|
|
674
|
+
setRawValue('');
|
|
675
|
+
wasCompleteRef.current = false;
|
|
676
|
+
|
|
677
|
+
if (opts.onChangeRaw) {
|
|
678
|
+
opts.onChangeRaw('', '');
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
if (opts.alwaysShowMask) {
|
|
682
|
+
const emptyDisplay = buildDisplayValue('', slots, slotChar, true);
|
|
683
|
+
input.value = emptyDisplay;
|
|
684
|
+
displayValueRef.current = emptyDisplay;
|
|
685
|
+
setMaskedValue(emptyDisplay);
|
|
686
|
+
}
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
if (!opts.alwaysShowMask && !complete) {
|
|
691
|
+
if (extractRaw(processed, slots).length === 0) {
|
|
692
|
+
input.value = '';
|
|
693
|
+
processedRef.current = '';
|
|
694
|
+
displayValueRef.current = '';
|
|
695
|
+
rawValueRef.current = '';
|
|
696
|
+
setMaskedValue('');
|
|
697
|
+
setRawValue('');
|
|
698
|
+
wasCompleteRef.current = false;
|
|
699
|
+
|
|
700
|
+
if (opts.onChangeRaw) {
|
|
701
|
+
opts.onChangeRaw('', '');
|
|
702
|
+
}
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
const display = buildDisplayValue(processed, slots, slotChar, false);
|
|
707
|
+
input.value = display;
|
|
708
|
+
displayValueRef.current = display;
|
|
709
|
+
setMaskedValue(display);
|
|
710
|
+
}
|
|
711
|
+
}, [rawValue]);
|
|
712
|
+
|
|
713
|
+
const handleKeyDown = useCallback(
|
|
714
|
+
(e: KeyboardEvent) => {
|
|
715
|
+
const input = e.target as HTMLInputElement;
|
|
716
|
+
const opts = optionsRef.current;
|
|
717
|
+
|
|
718
|
+
const { slots, slotChar, transform } = getResolvedOptions(opts, rawValue);
|
|
719
|
+
const start = input.selectionStart ?? 0;
|
|
720
|
+
const end = input.selectionEnd ?? 0;
|
|
721
|
+
const processed = processedRef.current;
|
|
722
|
+
|
|
723
|
+
const modifier = e.metaKey || (e.ctrlKey && !e.altKey);
|
|
724
|
+
const key = e.key.toLowerCase();
|
|
725
|
+
|
|
726
|
+
if (modifier && key === 'z' && !e.shiftKey) {
|
|
727
|
+
e.preventDefault();
|
|
728
|
+
const prev = undoStackRef.current.pop();
|
|
729
|
+
if (!prev) {
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
redoStackRef.current.push({
|
|
733
|
+
rawValue: rawValueRef.current,
|
|
734
|
+
selectionStart: input.selectionStart ?? 0,
|
|
735
|
+
});
|
|
736
|
+
applyHistoryState(prev);
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (modifier && ((key === 'z' && e.shiftKey) || (key === 'y' && !e.shiftKey))) {
|
|
741
|
+
e.preventDefault();
|
|
742
|
+
const next = redoStackRef.current.pop();
|
|
743
|
+
if (!next) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
undoStackRef.current.push({
|
|
747
|
+
rawValue: rawValueRef.current,
|
|
748
|
+
selectionStart: input.selectionStart ?? 0,
|
|
749
|
+
});
|
|
750
|
+
applyHistoryState(next);
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (e.key === 'Backspace') {
|
|
755
|
+
e.preventDefault();
|
|
756
|
+
|
|
757
|
+
if (e.metaKey || (e.ctrlKey && !e.altKey)) {
|
|
758
|
+
const clampedStart = Math.min(start, processed.length);
|
|
759
|
+
const afterRaw = extractRaw(processed.slice(clampedStart), slots.slice(clampedStart));
|
|
760
|
+
const newValue = applyMaskToRaw(afterRaw, slots, slotChar, transform);
|
|
761
|
+
pushUndoState();
|
|
762
|
+
updateValue(newValue, 0);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
if (start !== end) {
|
|
767
|
+
const clampedEnd = Math.min(end, processed.length);
|
|
768
|
+
const before = processed.slice(0, start);
|
|
769
|
+
const afterRaw = extractRaw(processed.slice(clampedEnd), slots.slice(clampedEnd));
|
|
770
|
+
const newValue = applyMaskToRaw(
|
|
771
|
+
extractRaw(before, slots) + afterRaw,
|
|
772
|
+
slots,
|
|
773
|
+
slotChar,
|
|
774
|
+
transform,
|
|
775
|
+
);
|
|
776
|
+
pushUndoState();
|
|
777
|
+
updateValue(newValue, start);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (start === 0) {
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
let deletePos = start - 1;
|
|
786
|
+
while (deletePos >= 0 && slots[deletePos] && slots[deletePos].type === 'literal') {
|
|
787
|
+
deletePos--;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
if (deletePos < 0) {
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const beforeRaw = extractRaw(processed.slice(0, deletePos), slots.slice(0, deletePos));
|
|
795
|
+
const afterRaw = extractRaw(processed.slice(deletePos + 1), slots.slice(deletePos + 1));
|
|
796
|
+
const newValue = applyMaskToRaw(beforeRaw + afterRaw, slots, slotChar, transform);
|
|
797
|
+
pushUndoState();
|
|
798
|
+
updateValue(newValue, deletePos);
|
|
799
|
+
} else if (e.key === 'Delete') {
|
|
800
|
+
e.preventDefault();
|
|
801
|
+
|
|
802
|
+
if (start !== end) {
|
|
803
|
+
const clampedEnd = Math.min(end, processed.length);
|
|
804
|
+
const before = processed.slice(0, start);
|
|
805
|
+
const afterRaw = extractRaw(processed.slice(clampedEnd), slots.slice(clampedEnd));
|
|
806
|
+
const newValue = applyMaskToRaw(
|
|
807
|
+
extractRaw(before, slots) + afterRaw,
|
|
808
|
+
slots,
|
|
809
|
+
slotChar,
|
|
810
|
+
transform,
|
|
811
|
+
);
|
|
812
|
+
pushUndoState();
|
|
813
|
+
updateValue(newValue, start);
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
let deletePos = start;
|
|
818
|
+
while (
|
|
819
|
+
deletePos < slots.length &&
|
|
820
|
+
slots[deletePos] &&
|
|
821
|
+
slots[deletePos].type === 'literal'
|
|
822
|
+
) {
|
|
823
|
+
deletePos++;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
if (deletePos >= processed.length) {
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const beforeRaw = extractRaw(processed.slice(0, start), slots.slice(0, start));
|
|
831
|
+
const afterRaw = extractRaw(processed.slice(deletePos + 1), slots.slice(deletePos + 1));
|
|
832
|
+
const newValue = applyMaskToRaw(beforeRaw + afterRaw, slots, slotChar, transform);
|
|
833
|
+
pushUndoState();
|
|
834
|
+
updateValue(newValue, start);
|
|
835
|
+
} else if (e.key === 'ArrowRight' && !e.shiftKey) {
|
|
836
|
+
const nextPos = findNextEditablePosition(start + 1, slots, input.value);
|
|
837
|
+
if (nextPos !== start + 1) {
|
|
838
|
+
e.preventDefault();
|
|
839
|
+
input.setSelectionRange(nextPos, nextPos);
|
|
840
|
+
}
|
|
841
|
+
} else if (e.key === 'ArrowLeft' && !e.shiftKey) {
|
|
842
|
+
if (start > 0) {
|
|
843
|
+
const prevToken = findPrevTokenIndex(slots, start - 1);
|
|
844
|
+
if (prevToken >= 0 && prevToken !== start - 1) {
|
|
845
|
+
e.preventDefault();
|
|
846
|
+
input.setSelectionRange(prevToken + 1, prevToken + 1);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
} else if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
850
|
+
e.preventDefault();
|
|
851
|
+
|
|
852
|
+
let insertPos = Math.min(start, processed.length);
|
|
853
|
+
while (
|
|
854
|
+
insertPos < slots.length &&
|
|
855
|
+
slots[insertPos] &&
|
|
856
|
+
slots[insertPos].type === 'literal'
|
|
857
|
+
) {
|
|
858
|
+
insertPos++;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
if (insertPos >= slots.length) {
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
const slot = slots[insertPos];
|
|
866
|
+
const ch = transform ? transform(e.key) : e.key;
|
|
867
|
+
if (!slot.pattern!.test(ch)) {
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const beforeRaw = extractRaw(processed.slice(0, insertPos), slots.slice(0, insertPos));
|
|
872
|
+
const afterRaw =
|
|
873
|
+
start < end
|
|
874
|
+
? extractRaw(
|
|
875
|
+
processed.slice(Math.min(end, processed.length)),
|
|
876
|
+
slots.slice(Math.min(end, processed.length)),
|
|
877
|
+
)
|
|
878
|
+
: extractRaw(processed.slice(insertPos), slots.slice(insertPos));
|
|
879
|
+
const newValue = applyMaskToRaw(beforeRaw + ch + afterRaw, slots, slotChar, transform);
|
|
880
|
+
const newCursorPos = findNextEditablePosition(insertPos + 1, slots, newValue);
|
|
881
|
+
pushUndoState();
|
|
882
|
+
updateValue(newValue, newCursorPos);
|
|
883
|
+
}
|
|
884
|
+
},
|
|
885
|
+
[applyHistoryState, pushUndoState, rawValue, updateValue],
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
const handlePaste = useCallback(
|
|
889
|
+
(e: ClipboardEvent) => {
|
|
890
|
+
e.preventDefault();
|
|
891
|
+
const input = e.target as HTMLInputElement;
|
|
892
|
+
const opts = optionsRef.current;
|
|
893
|
+
|
|
894
|
+
const pastedText = e.clipboardData?.getData('text') ?? '';
|
|
895
|
+
const start = input.selectionStart ?? 0;
|
|
896
|
+
const end = input.selectionEnd ?? 0;
|
|
897
|
+
const processed = processedRef.current;
|
|
898
|
+
|
|
899
|
+
const { slots, slotChar, transform } = getResolvedOptions(opts, '');
|
|
900
|
+
const clampedStart = Math.min(start, processed.length);
|
|
901
|
+
const clampedEnd = Math.min(end, processed.length);
|
|
902
|
+
const beforeRaw = extractRaw(processed.slice(0, clampedStart), slots.slice(0, clampedStart));
|
|
903
|
+
const afterRaw = extractRaw(processed.slice(clampedEnd), slots.slice(clampedEnd));
|
|
904
|
+
const newValue = applyMaskToRaw(
|
|
905
|
+
beforeRaw + pastedText + afterRaw,
|
|
906
|
+
slots,
|
|
907
|
+
slotChar,
|
|
908
|
+
transform,
|
|
909
|
+
);
|
|
910
|
+
|
|
911
|
+
pushUndoState();
|
|
912
|
+
updateValue(newValue);
|
|
913
|
+
|
|
914
|
+
const maskedPrefix = applyMaskToRaw(beforeRaw + pastedText, slots, slotChar, transform);
|
|
915
|
+
const pasteEndPos = Math.min(maskedPrefix.length, slots.length);
|
|
916
|
+
if (input === document.activeElement) {
|
|
917
|
+
input.setSelectionRange(pasteEndPos, pasteEndPos);
|
|
918
|
+
}
|
|
919
|
+
},
|
|
920
|
+
[pushUndoState, updateValue],
|
|
921
|
+
);
|
|
922
|
+
|
|
923
|
+
const setAriaAttributes = useCallback((input: HTMLInputElement) => {
|
|
924
|
+
const opts = optionsRef.current;
|
|
925
|
+
|
|
926
|
+
if (opts.invalid) {
|
|
927
|
+
input.setAttribute('aria-invalid', 'true');
|
|
928
|
+
} else {
|
|
929
|
+
input.removeAttribute('aria-invalid');
|
|
930
|
+
}
|
|
931
|
+
}, []);
|
|
932
|
+
|
|
933
|
+
const refCallback = useCallback(
|
|
934
|
+
(node: HTMLInputElement | null) => {
|
|
935
|
+
const prevInput = inputRef.current;
|
|
936
|
+
|
|
937
|
+
if (prevInput) {
|
|
938
|
+
prevInput.removeEventListener('input', handleInput);
|
|
939
|
+
prevInput.removeEventListener('focus', handleFocus);
|
|
940
|
+
prevInput.removeEventListener('blur', handleBlur);
|
|
941
|
+
prevInput.removeEventListener('mousedown', handleMouseDown);
|
|
942
|
+
prevInput.removeEventListener('mouseup', handleMouseUp);
|
|
943
|
+
prevInput.removeEventListener('keydown', handleKeyDown as EventListener);
|
|
944
|
+
prevInput.removeEventListener('paste', handlePaste as EventListener);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
inputRef.current = node;
|
|
948
|
+
|
|
949
|
+
if (node) {
|
|
950
|
+
node.addEventListener('input', handleInput);
|
|
951
|
+
node.addEventListener('focus', handleFocus);
|
|
952
|
+
node.addEventListener('blur', handleBlur);
|
|
953
|
+
node.addEventListener('mousedown', handleMouseDown);
|
|
954
|
+
node.addEventListener('mouseup', handleMouseUp);
|
|
955
|
+
node.addEventListener('keydown', handleKeyDown as EventListener);
|
|
956
|
+
node.addEventListener('paste', handlePaste as EventListener);
|
|
957
|
+
|
|
958
|
+
setAriaAttributes(node);
|
|
959
|
+
|
|
960
|
+
const hasInitialValue = initializeInputValue(node);
|
|
961
|
+
|
|
962
|
+
if (options.alwaysShowMask && !hasInitialValue) {
|
|
963
|
+
const { slots, slotChar } = getResolvedOptions(options, '');
|
|
964
|
+
const display = buildDisplayValue('', slots, slotChar, true);
|
|
965
|
+
node.value = display;
|
|
966
|
+
displayValueRef.current = display;
|
|
967
|
+
setMaskedValue(display);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
},
|
|
971
|
+
[
|
|
972
|
+
handleInput,
|
|
973
|
+
handleFocus,
|
|
974
|
+
handleBlur,
|
|
975
|
+
handleMouseDown,
|
|
976
|
+
handleMouseUp,
|
|
977
|
+
handleKeyDown,
|
|
978
|
+
handlePaste,
|
|
979
|
+
initializeInputValue,
|
|
980
|
+
setAriaAttributes,
|
|
981
|
+
options,
|
|
982
|
+
],
|
|
983
|
+
);
|
|
984
|
+
|
|
985
|
+
useEffect(() => {
|
|
986
|
+
const input = inputRef.current;
|
|
987
|
+
if (!input) {
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
setAriaAttributes(input);
|
|
992
|
+
}, [options.invalid, setAriaAttributes]);
|
|
993
|
+
|
|
994
|
+
const isComplete = (() => {
|
|
995
|
+
const { slots } = getOptions();
|
|
996
|
+
return checkComplete(processedRef.current, slots);
|
|
997
|
+
})();
|
|
998
|
+
|
|
999
|
+
const reset = useCallback(() => {
|
|
1000
|
+
const opts = optionsRef.current;
|
|
1001
|
+
const input = inputRef.current;
|
|
1002
|
+
|
|
1003
|
+
processedRef.current = '';
|
|
1004
|
+
displayValueRef.current = '';
|
|
1005
|
+
rawValueRef.current = '';
|
|
1006
|
+
undoStackRef.current = [];
|
|
1007
|
+
redoStackRef.current = [];
|
|
1008
|
+
setMaskedValue('');
|
|
1009
|
+
setRawValue('');
|
|
1010
|
+
wasCompleteRef.current = false;
|
|
1011
|
+
|
|
1012
|
+
if (input) {
|
|
1013
|
+
if (opts.alwaysShowMask) {
|
|
1014
|
+
const { slots, slotChar } = getResolvedOptions(opts, '');
|
|
1015
|
+
const display = buildDisplayValue('', slots, slotChar, true);
|
|
1016
|
+
input.value = display;
|
|
1017
|
+
displayValueRef.current = display;
|
|
1018
|
+
setMaskedValue(display);
|
|
1019
|
+
} else {
|
|
1020
|
+
input.value = '';
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
if (opts.onChangeRaw) {
|
|
1025
|
+
opts.onChangeRaw('', '');
|
|
1026
|
+
}
|
|
1027
|
+
}, []);
|
|
1028
|
+
|
|
1029
|
+
return {
|
|
1030
|
+
ref: refCallback,
|
|
1031
|
+
value: maskedValue,
|
|
1032
|
+
rawValue,
|
|
1033
|
+
isComplete,
|
|
1034
|
+
reset,
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
function findNextEditablePosition(from: number, slots: MaskSlot[], value: string): number {
|
|
1039
|
+
let pos = from;
|
|
1040
|
+
while (pos < slots.length && pos < value.length && slots[pos] && slots[pos].type === 'literal') {
|
|
1041
|
+
pos++;
|
|
1042
|
+
}
|
|
1043
|
+
return pos;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
export namespace useMask {
|
|
1047
|
+
export type Options = UseMaskOptions;
|
|
1048
|
+
export type ReturnValue = UseMaskReturnValue;
|
|
1049
|
+
}
|