-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.tsx
58 lines (47 loc) · 1.85 KB
/
index.tsx
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
import * as React from 'react';
import { Component, ReactNode } from 'react';
import { KeyboardTypeOptions, TextInput, TextInputProps } from 'react-native';
import { createInputProcessor, InputProcessorFunction, UserInputType } from './internals/inputProcessor';
type OnTextChangeListener = ((text: string, complete: boolean) => void);
interface IMaskedInputProps extends TextInputProps {
mask: string;
value?: string;
onTextChange?: OnTextChangeListener;
}
interface IMaskedInputState {
value: string;
}
export default class MaskedInput extends Component<IMaskedInputProps, IMaskedInputState> {
private userInputProcessorFunction: InputProcessorFunction;
public constructor(props: IMaskedInputProps) {
super(props);
this.onTextChange = this.onTextChange.bind(this);
this.state = {value: props.value || ""};
this.userInputProcessorFunction = createInputProcessor(props.mask);
}
private onTextChange(text: string): void {
this.updateMaskedValue(text);
}
public componentWillReceiveProps(nextProps: Readonly<IMaskedInputProps>, nextContext: any): void {
this.userInputProcessorFunction = createInputProcessor(nextProps.mask);
this.updateMaskedValue(nextProps.value || "");
}
private updateMaskedValue(inputValue: string): void {
const maskResult = this.userInputProcessorFunction(inputValue, UserInputType.INSERTION);
const previousValue = this.state.value;
const currentValue = maskResult.text;
this.setState({ value: currentValue });
if (this.props.onTextChange && currentValue !== previousValue) {
this.props.onTextChange(maskResult.text, maskResult.complete);
}
}
public render(): ReactNode {
let { mask, value, onTextChange, ...attributes } = this.props;
return (
<TextInput
value={this.state.value}
onChangeText={(text) => this.onTextChange(text)}
{...attributes}/>
);
}
}