-
Notifications
You must be signed in to change notification settings - Fork 14
/
InlineTextInput.js
167 lines (154 loc) · 4.12 KB
/
InlineTextInput.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
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
166
167
import React, { Component } from 'react'
import { View, Text, TextInput, StyleSheet } from 'react-native'
import PropTypes from 'prop-types'
export default class InlineTextInput extends Component {
componentDidMount() {
this.scrollTo = 0
}
handleLayout(event) {
this.scrollTo = event.nativeEvent.layout.y
}
handleFocus() {
const { onFocus } = this.props
onFocus && onFocus(this.scrollTo)
}
focus() {
this.refs.input.focus()
}
blur() {
this.refs.input.blur()
}
shouldDisplayMessage() {
const { value, valid, message } = this.props
return (value && value.length > 0 && !valid && message)
}
handleSubmitEditing() {
const { nextInput, onNextInputFocus } = this.props
onNextInputFocus && onNextInputFocus(nextInput, this)
}
renderIcon() {
const { icon, validIcon, invalidIcon, valid, value, iconStyle } = this.props
if (!icon)
return
let renderedIcon = null
if (value && value.length > 0) {
renderedIcon = (valid ? (validIcon ? validIcon : icon) : (invalidIcon ? invalidIcon : icon))
} else {
renderedIcon = icon
}
return (
<View
style={[{
marginLeft: 6,
}, iconStyle]}
>
{renderedIcon}
</View>
)
}
renderMessage() {
const { message, messageStyle } = this.props
const style = StyleSheet.flatten(this.props.style)
if (this.shouldDisplayMessage()) {
return(
<View style={{
backgroundColor: (style && style.backgroundColor ? style.backgroundColor : 'white')
}}>
<Text style={[{
color: 'red',
marginLeft: 10,
marginBottom: 10,
fontSize: 12,
}, messageStyle]}>
{ message }
</Text>
</View>
)
}
}
render() {
const { label, value, labelStyle, inputStyle, nextInput, onBlur, multiline } = this.props
const style = StyleSheet.flatten(this.props.style)
return (
<View
onLayout={this.handleLayout.bind(this)}
style={[{
backgroundColor: 'white',
borderTopWidth: 0.5,
borderBottomWidth: (nextInput ? 0 : 0.5),
borderColor: 'lightgray',
}, style]}
>
<View style={{
flexDirection: 'row',
alignItems: 'center',
paddingTop: 6,
paddingBottom: (this.shouldDisplayMessage() ? 0 : 6),
backgroundColor: (style && style.backgroundColor ? style.backgroundColor : 'white'),
}}>
{ this.renderIcon() }
<Text
style={[{
flex: 0.5,
fontSize: 14,
fontWeight: 'bold',
marginLeft: 6,
}, labelStyle]}
>
{label}
</Text>
<TextInput
clearButtonMode='while-editing'
underlineColorAndroid='transparent'
returnKeyType={ multiline ? 'default' : (nextInput ? 'next' : 'done') }
onSubmitEditing={this.handleSubmitEditing.bind(this)}
{ ...this.props }
onFocus={this.handleFocus.bind(this)}
onBlur={onBlur}
ref='input'
value={value}
style={[{
flex: 1,
height: 36,
fontSize: 14,
backgroundColor: 'white',
}, inputStyle]}
/>
</View>
{ this.renderMessage() }
</View>
)
}
}
const stylePropType = PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.object),
])
InlineTextInput.propTypes = {
label: PropTypes.string,
value: PropTypes.string,
valid: PropTypes.bool,
message: PropTypes.string,
style: stylePropType,
iconStyle: stylePropType,
labelStyle: stylePropType,
inputStyle: stylePropType,
messageStyle: stylePropType,
icon: PropTypes.element,
validIcon: PropTypes.element,
invalidIcon: PropTypes.element,
}
InlineTextInput.defaultProps = {
label: 'Use label prop',
value: null,
valid: false,
message: null,
style: {},
iconStyle: {},
labelStyle: {},
inputStyle: {},
messageStyle: {},
icon: null,
validIcon: null,
invalidIcon: null,
}