UNPKG

1.51 kBTypeScriptView Raw
1import * as React from "react";
2
3export type Mask = Array<string | RegExp> | false;
4
5export interface PipeConfig {
6 placeholder: string;
7 placeholderChar: string;
8 currentCaretPosition: number;
9 keepCharPositions: boolean;
10 rawValue: string;
11 guide: boolean | undefined;
12 previousConformedValue: string | undefined;
13}
14
15export type ConformToMaskConfig = Partial<Omit<PipeConfig, "rawValue">>;
16
17export interface MaskedInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
18 mask: Mask | ((value: string) => Mask);
19
20 guide?: boolean;
21
22 placeholderChar?: string;
23
24 keepCharPositions?: boolean;
25
26 pipe?: (
27 conformedValue: string,
28 config: PipeConfig,
29 ) => false | string | { value: string; indexesOfPipedChars: number[] };
30
31 showMask?: boolean;
32
33 render?: (
34 ref: (inputElement: HTMLElement) => void,
35 props: {
36 onChange: (event: React.ChangeEvent<HTMLElement>) => void;
37 onBlur: (event: React.FocusEvent<HTMLElement>) => void;
38 defaultValue: string | undefined;
39 },
40 ) => React.ReactNode;
41}
42
43export interface ConformToMaskResult {
44 conformedValue: string;
45 meta: {
46 someCharsRejected: boolean;
47 };
48}
49
50export default class MaskedInput extends React.Component<MaskedInputProps, any> {
51 inputElement: HTMLElement;
52}
53
54export function conformToMask(
55 text: string,
56 mask: Mask | ((value: string) => Mask),
57 config?: ConformToMaskConfig,
58): ConformToMaskResult;