web-ui-pack
Version:
Web package with UI elements
109 lines (108 loc) • 4.24 kB
TypeScript
declare global {
namespace WUP.Text.Mask {
type TestFn = (str: string, charIndex: number) => boolean | {
value: string;
};
interface VarChunk {
index: number;
pattern: string;
isTouched?: true;
value: string;
test: TestFn;
/** Returns whether chunk is variable or static */
isVar: true;
min: number;
max: number;
isCompleted?: boolean;
}
interface StaticChunk {
index: number;
pattern: string;
isTouched?: true;
/** Returns whether chunk is variable or static */
isVar?: false;
value?: undefined;
}
type InputChunk = VarChunk | StaticChunk;
interface Options {
/** Add next constant-symbol to allow user don't worry about non-digit values @default true
* @WARN prediction:false is partially supported (delete behavior forces prediction logic) */
prediction?: boolean;
/** Add missed zero: for pattern '0000-00' and string '1 ' result will be "0001-" @default true */
lazy?: boolean;
}
interface HandledInput extends HTMLInputElement {
_maskPrev?: {
position: number;
value: string;
insertText: string | null;
};
}
}
}
export default class MaskTextInput {
#private;
pattern: string;
private options?;
/** Corrected result */
value: string;
/** Returns text of first chunk if it's static or "" */
prefix: string;
/** Pattern splits into chunks. With detailed info about process */
chunks: WUP.Text.Mask.InputChunk[];
/** Last processed chunk */
lastChunk: WUP.Text.Mask.InputChunk;
/** Returns whether value fits pattern (isComplete) `'#.#' + '2' => true` */
isCompleted: boolean;
/** Returns whether value completely fits pattern (isComplete) `'#.#' + '2.2' => true` */
isCompletedFull: boolean;
/** Returns count chars of pattern that missed in value (used to maskHolder) */
leftLength: number;
/** Format string according to pattern
* @param pattern
* * 0000-00-00 - for date yyyy-MM-dd
* * ##0.##0.##0.##0 - for IPaddress
* * \# - optional digit
* * 0 - required digit
* * \* - any char
* * *{1,5} - any 1..5 chars
* * //[a-zA-Z]// - regex > 1 letter
* * //[a-zA-Z]//{1,5} - regex > 1..5 letters
* * '|0' or '\x00' - static char '0'
* * '|#' or '\x01' - static char '#'
* * '|*' or '\x02' - static char '\*'
* * '|/' or '\x03' - static char '/'
* */
constructor(pattern: string, rawValue: string, options?: WUP.Text.Mask.Options | undefined);
/** Returns whether char is digit or not */
static testDigit(str: string, charIndex: number): boolean;
static testAnyChar(): boolean;
/** Splits pointed pattern to chunks */
static parsePattern(pattern: string): WUP.Text.Mask.InputChunk[];
/** Converts pointed value to masked-value and update internal state
* @returns new input-carret position */
parse(value: string, lazy?: boolean, caret?: number): number;
/** Update state based on chunks; call it when chunks are changed to re-calc value etc. */
updateState(): void;
handleBeforeInput(e: InputEvent): void;
handleInput(e: InputEvent): {
declinedAdd: number;
position: number;
};
/** Returns chunk according to pointed cursor position + position inside chunk */
findChunkByCaret(pos: number): {
chunk: WUP.Text.Mask.InputChunk;
posChunk: number;
};
/** Leap through static chunk if required */
adjustCaret(pos: number): number;
/** Clear chunk state */
resetChunk(c: WUP.Text.Mask.InputChunk): void;
private deleteChar;
/** Delete a char after pointed position: when user presses 'Delete';
* @returns next cursor position */
deleteAfter(position: number): number;
/** Delete a char before pointed position: when user presses 'Backspace'
* @returns next cursor position */
deleteBefore(position: number): number;
}