-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
233 lines (219 loc) · 5.28 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React from 'react'
import { Component } from 'react'
import * as PropTypes from 'prop-types'
import * as ReactDOM from 'react-dom'
import TextareaAutosize from 'react-textarea-autosize'
/**
* TextInput component <input> or <textArea> depending on property 'multiline'.
*/
class TextInput extends Component {
static propTypes = {
/**
* HTML 'id' attribute
*/
id: PropTypes.string,
/**
* HTML 'aria-label' attribute
*/
accessibilityLabel: PropTypes.string,
/**
* HTML 'autocomplete' attribute
*/
autoComplete: PropTypes.bool,
/**
* HTML 'autofocus' attribute
*/
autoFocus: PropTypes.bool,
/**
* Toggle whether to clear text when component is focused
*/
clearTextOnFocus: PropTypes.bool,
/**
* Value to display if empty when component is first initialized
*/
defaultValue: PropTypes.string,
/**
* Toggle whether this is readonly or not.
* HTML 'readonly' attribute
*/
editable: PropTypes.bool,
/**
* HTML 'type' attribute for <input>
*/
keyboardType: PropTypes.oneOf(['default', 'email-address', 'numeric',
'phone-pad', 'url']),
/**
* Maximum length of text field. <input> 'maxlength' attribute
*/
maxLength: PropTypes.number,
/**
* Maximum line of text area. <textarea> 'rows' attribute
*/
maxNumberOfLines: PropTypes.number,
/**
* Toggle whether to it is text field or text area
*/
multiline: PropTypes.bool,
/**
* Initial number of line to display for text area.
* Default is '2'
*/
numberOfLines: PropTypes.number,
/**
* Event handler for onBlur
*/
onBlur: PropTypes.func,
/**
* Event handler for onKeyDown
*/
onKeyDown: PropTypes.func,
/**
* Event handler for onChange
*/
onChange: PropTypes.func,
/**
* Same as onChange
*/
onChangeText: PropTypes.func,
/**
* Event handler for onFocus
*/
onFocus: PropTypes.func,
/**
* Event handler for onSelect
*/
onSelectionChange: PropTypes.func,
/**
* HTML 'placeholder' attribute
*/
placeholder: PropTypes.string,
/**
* Toggle whether to display value in text or as password
*/
secureTextEntry: PropTypes.bool,
/**
* Toggle whether to select text in text field
* when component is focused
*/
selectTextOnFocus: PropTypes.bool,
/**
* String value for this text field/text area
*/
value: PropTypes.string
}
_onBlur = (e) => {
const {onBlur} = this.props
if (onBlur) {
onBlur(e)
}
}
_onChange = (e) => {
const {onChange, onChangeText} = this.props
if (onChangeText) onChangeText(e.target.value)
if (onChange) onChange(e)
}
_onFocus = (e) => {
const {clearTextOnFocus, onFocus, selectTextOnFocus} = this.props
const node = ReactDOM.findDOMNode(this)
if (clearTextOnFocus) node.value = ''
if (selectTextOnFocus) node.select()
if (onFocus) onFocus(e)
}
_onSelectionChange = (e) => {
const {onSelectionChange} = this.props
const {selectionDirection, selectionEnd, selectionStart} = e.target
if (onSelectionChange) {
const event = {
selectionDirection,
selectionEnd,
selectionStart,
nativeEvent: e.nativeEvent
}
onSelectionChange(event)
}
}
_onKeyDown = (e) => {
const {onKeyDown} = this.props
if (onKeyDown) onKeyDown(e)
}
_onClear = () => {
const node = ReactDOM.findDOMNode(this)
node.value = ''
}
render () {
const {
id,
accessibilityLabel,
autoComplete,
autoFocus,
editable = true,
keyboardType = 'default',
maxLength,
maxNumberOfLines,
multiline = false,
numberOfLines = 2,
onBlur,
onChange,
onChangeText,
onKeyDown,
onSelectionChange,
placeholder,
secureTextEntry = false,
value
} = this.props
let type
switch (keyboardType) {
case 'default':
break
case 'email-address':
type = 'email'
break
case 'numeric':
type = 'number'
break
case 'phone-pad':
type = 'tel'
break
case 'url':
type = 'url'
break
default:
console.error('Unsupported keyboardType.', keyboardType)
break
}
if (secureTextEntry) {
type = 'password'
}
const propsCommon = {
id: id,
'aria-label': accessibilityLabel,
autoComplete: autoComplete && 'on',
autoFocus,
className: 'textInput',
maxLength,
onBlur: onBlur && this._onBlur,
onChange: (onChange || onChangeText) && this._onChange,
onFocus: this._onFocus,
onSelect: onSelectionChange && this._onSelectionChange,
onKeyDown: (onKeyDown) && this._onKeyDown,
placeholder,
readOnly: !editable,
value
}
if (multiline) {
const propsMultiline = {
...propsCommon,
maxRows: maxNumberOfLines || numberOfLines,
minRows: numberOfLines
}
return <TextareaAutosize {...propsMultiline} />
} else {
const propsSingleline = {
...propsCommon,
type
}
return <input {...propsSingleline} />
}
}
}
export default TextInput