Repository navigation
Expand file tree
/
Copy pathInput.js
More file actions
executable file
·165 lines (155 loc) · 4.29 KB
/
Copy pathInput.js
File metadata and controls
executable file
·165 lines (155 loc) · 4.29 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright © SuperMap. All rights reserved.
Author: Yang Shanglong
E-mail: yangshanglong@supermap.com
*/
import React, { PureComponent } from 'react'
import {
View,
TextInput,
KeyboardAvoidingView,
Image,
TouchableOpacity,
} from 'react-native'
import { color } from '../../styles'
import styles from './styles'
export default class Input extends PureComponent {
props: {
accessible?: boolean,
accessibilityLabel?: string,
showClear?: boolean,
isKeyboardAvoiding?: boolean,
behavior?: string,
style?: Object,
inputStyle?: Object,
placeholder: string,
value: string,
defaultValue: string,
keyboardAppearance: string,
returnKeyType?: string,
placeholderTextColor?: string,
keyboardType?: string,
textContentType?: string,
secureTextEntry?: boolean,
onChangeText?: () => {},
onClear?: () => {},
}
static defaultProps = {
label: '',
keyboardAppearance: 'dark',
returnKeyType: 'done',
keyboardType: 'default',
placeholder: '',
placeholderTextColor: color.themePlaceHolder,
showClear: false,
isKeyboardAvoiding: false,
secureTextEntry: false,
}
constructor(props) {
super(props)
this.state = {
value: props.value || props.defaultValue || '',
}
this._value = props.value || props.defaultValue || '' // 实时输入的值
}
componentDidUpdate(prevProps) {
if (this.props.value !== undefined) {
if (
this.props.value !== prevProps.value ||
this.props.value !== this._value
) {
this._value = this.props.value
this.setState({
value: this.props.value,
})
}
} else if (this.props.defaultValue !== prevProps.defaultValue) {
this._value = this.props.defaultValue
this.setState({
value: this.props.defaultValue,
})
}
}
clear = () => {
if (this.props.onClear && typeof this.props.onClear === 'function') {
this.props.onClear()
}
this.setState({
value: '',
})
}
focus = () => {
this.textInput && this.textInput.focus()
}
blur = () => {
this.textInput && this.textInput.blur()
}
renderClearBtn = () => {
if (this.state.value === '') {
return <View style={styles.clearBtn} />
}
return (
<TouchableOpacity
activeOpacity={0.8}
style={styles.clearBtn}
onPress={this.clear}
>
<Image
style={styles.clearImg}
resizeMode={'contain'}
source={require('../../assets/public/icon_input_clear.png')}
/>
</TouchableOpacity>
)
}
renderInput = () => {
return (
<View style={[styles.inputContainer, this.props.style]}>
<TextInput
ref={ref => (this.textInput = ref)}
accessible={this.props.accessible}
accessibilityLabel={this.props.accessibilityLabel}
style={[styles.input, this.props.inputStyle]}
placeholder={this.props.placeholder}
underlineColorAndroid="transparent"
placeholderTextColor={this.props.placeholderTextColor}
textContentType={this.props.textContentType}
secureTextEntry={this.props.secureTextEntry}
defaultValue={this.props.defaultValue}
value={this.state.value + ''}
onChangeText={text => {
if (
this.props.keyboardType === 'number-pad' ||
this.props.keyboardType === 'decimal-pad' ||
this.props.keyboardType === 'numeric'
) {
if (isNaN(text) && text !== '' && text !== '-') {
text = this.state.value || ''
}
}
this.props.onChangeText && this.props.onChangeText(text)
this._value = text
this.setState({
value: text,
})
}}
keyboardAppearance={this.props.keyboardAppearance}
returnKeyType={this.props.returnKeyType}
keyboardType={this.props.keyboardType}
/>
{this.props.showClear && this.renderClearBtn()}
</View>
)
}
render() {
if (this.props.isKeyboardAvoiding) {
return (
<KeyboardAvoidingView behavior={this.props.behavior} enabled>
{this.renderInput()}
</KeyboardAvoidingView>
)
} else {
return this.renderInput()
}
}
}