-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNTextInput.js
105 lines (97 loc) · 2.73 KB
/
RNTextInput.js
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { View, TextInput, Text } from 'react-native'
import styles from './Styles'
import { Colors } from './Themes'
class RNTextInput extends Component {
constructor (props) {
super(props)
this.state = {
validate: false
}
}
onBlur () {
if (this.props.validate === 'onBlur') {
this.setState({ validate: true })
}
}
onChangeText (text) {
if (this.props.validate === 'onChangeText') {
this.setState({ validate: true })
}
this.props.onChangeText(text)
}
render () {
const {
onRef,
value,
valid,
label,
editable,
errorText,
iconElement,
autoCorrect,
keyboardType,
returnKeyType,
errorTextStyle,
autoCapitalize,
onSubmitEditing,
secureTextEntry,
borderBottomColor,
editableTextStyle,
readOnlyTextStyle
} = this.props
const showError = this.state.validate && !valid
const finalErrorText = errorText || `${label} is not valid.`
const borderColor = showError ? Colors.error : borderBottomColor
const inputStyle = editable ? editableTextStyle : readOnlyTextStyle
return (
<View style={styles.container}>
<View style={[ styles.row, { borderBottomColor: borderColor } ]}>
{
iconElement ? (
<View style={styles.iconContainer}>
{iconElement}
</View>
) : null
}
<TextInput
ref={r => onRef(r)}
value={value}
style={[{ flex: 1 }, inputStyle]}
editable={editable}
autoCorrect={autoCorrect}
keyboardType={keyboardType}
returnKeyType={returnKeyType}
autoCapitalize={autoCapitalize}
onBlur={this.onBlur.bind(this)}
secureTextEntry={secureTextEntry}
onChangeText={this.onChangeText.bind(this)}
underlineColorAndroid='transparent'
onSubmitEditing={onSubmitEditing}
placeholder={label}
/>
</View>
{ showError ? <Text style={errorTextStyle}>{finalErrorText}</Text> : null }
</View>
)
}
}
RNTextInput.defaultProps = {
validate: 'onBlur',
autoCorrect: false,
returnKeyType: 'go',
label: 'Label text',
autoCapitalize: 'none',
keyboardType: 'default',
errorTextStyle: styles.errorText,
editableTextStyle: styles.editableText,
readOnlyTextStyle: styles.readOnlyText,
borderBottomColor: Colors.panther
}
RNTextInput.propTypes = {
// You can ensure that your prop is limited to specific values by treating
// it as an enum.
validate: PropTypes.oneOf(['onBlur', 'onChangeText'])
}
export default RNTextInput