|
| 1 | +import { isArrayExpression } from "@babel/types"; |
| 2 | +import { Accessor, createEffect, createSignal, onCleanup, onMount, Setter } from "solid-js"; |
| 3 | + |
| 4 | +export type InputSelection = { |
| 5 | + start: number; |
| 6 | + end: number; |
| 7 | +}; |
| 8 | + |
| 9 | +export const createInputSelection = ( |
| 10 | + ref?: HTMLInputElement | HTMLTextAreaElement |
| 11 | +): [selection: Accessor<InputSelection>, setSelection: Setter<InputSelection>] => { |
| 12 | + const [selection, setSelection] = createSignal({ |
| 13 | + start: ref?.selectionStart ?? -1, |
| 14 | + end: ref?.selectionEnd ?? -1 |
| 15 | + }); |
| 16 | + |
| 17 | + onMount(() => { |
| 18 | + const listener = () => { |
| 19 | + setSelection((last) => { |
| 20 | + const start = ref?.selectionStart ?? -1, |
| 21 | + end = ref?.selectionEnd ?? -1; |
| 22 | + return (last.start === start && last.end === end) |
| 23 | + ? last |
| 24 | + : { start, end }; |
| 25 | + }); |
| 26 | + } |
| 27 | + ref?.addEventListener('selectionchange', listener); |
| 28 | + onCleanup(() => ref?.removeEventListener('selectionChange', listener)); |
| 29 | + }); |
| 30 | + |
| 31 | + return [selection, (sel) => { |
| 32 | + const next = setSelection(sel) |
| 33 | + ref?.setSelectionRange(next.start, next.end); |
| 34 | + return next; |
| 35 | + }]; |
| 36 | +}; |
| 37 | + |
| 38 | +export type InputMaskFunc = ((value: string, sel: InputSelection) => |
| 39 | + [maskedValue: string, sel: InputSelection]); |
| 40 | +export type InputMask = (string | RegExp)[] | InputMaskFunc; |
| 41 | + |
| 42 | +const alignSelectionToRemovedPart = (selection: InputSelection, start: number, length: number): InputSelection => { |
| 43 | + if (selection.start > start) { |
| 44 | + if (selection.start > start + length) { |
| 45 | + selection.start -= length |
| 46 | + } else { |
| 47 | + selection.start = start; |
| 48 | + } |
| 49 | + } |
| 50 | + if (selection.end > start) { |
| 51 | + if (selection.end > start + length) { |
| 52 | + selection.end -= length |
| 53 | + } else { |
| 54 | + selection.end = start; |
| 55 | + } |
| 56 | + } |
| 57 | + return selection; |
| 58 | +} |
| 59 | + |
| 60 | +const inputMaskArrayToFunc = (def: InputMask): InputMaskFunc => { |
| 61 | + if (Array.isArray(def)) { |
| 62 | + return (value, selection) => { |
| 63 | + if (value) { |
| 64 | + let index = 0; |
| 65 | + def.forEach((part, id) => { |
| 66 | + // fixed part; add if not already in string and jump to next part |
| 67 | + if (typeof part === 'string') { |
| 68 | + if (value.substr(index, part.length) !== part) { |
| 69 | + value = `${value.slice(0, index)}${part}${value.slice(index)}`; |
| 70 | + } |
| 71 | + index += part.length; |
| 72 | + // regex part; delete before match and jump after match |
| 73 | + } else { |
| 74 | + const match = value.slice(index).match(part); |
| 75 | + const matchIndex = match?.index ?? -1; |
| 76 | + if (match && (matchIndex >= 0)) { |
| 77 | + // remove characters padding the match |
| 78 | + if (matchIndex > 0) { |
| 79 | + value = `${value.slice(0, index)}${value.slice(matchIndex)}}`; |
| 80 | + alignSelectionToRemovedPart(selection, index, matchIndex); |
| 81 | + } |
| 82 | + index += match[0].length; |
| 83 | + // cut additional characters after the last part |
| 84 | + const isLastPart = id === def.length - 1; |
| 85 | + if (isLastPart && value.length > index) { |
| 86 | + value = value.slice(0, index) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + return [value, selection]; |
| 93 | + } |
| 94 | + } |
| 95 | + return def |
| 96 | +} |
| 97 | + |
| 98 | +export const createInputMask = (ref?: HTMLInputElement, mask?: InputMask) => { |
| 99 | + if (!mask) { |
| 100 | + return; |
| 101 | + } |
| 102 | + const [selection, setSelection] = createInputSelection(ref); |
| 103 | + const inputMask = inputMaskArrayToFunc(mask); |
| 104 | + |
| 105 | + onMount(() => { |
| 106 | + const handler = () => { |
| 107 | + if (ref) { |
| 108 | + const [newValue, newSelection] = inputMask(ref.value, selection()); |
| 109 | + ref.value = newValue; |
| 110 | + setSelection(newSelection); |
| 111 | + } |
| 112 | + }; |
| 113 | + ref?.addEventListener('input', handler); |
| 114 | + onCleanup(() => ref?.removeEventListener('input', handler)); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
0 commit comments