| 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 |
120x
120x
120x
361x
24x
24x
1x
24x
1x
24x
1x
1x
1x
2x
2x
1x
1x
4x
4x
4x
4x
4x
4x
2x
2x
1x
1x
1x
1x
1x
1x
1x
1x
32x
32x
32x
26x
32x
| import React from 'react';
import _ from 'lodash';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, omitProps } from '../../util/component-types';
import reducers from './TextField.reducers';
import * as KEYCODE from '../../constants/key-code';
const cx = lucidClassNames.bind('&-TextField');
const {
bool,
string,
func,
number,
object,
oneOfType,
} = React.PropTypes;
/**
*
* {"categories": ["controls", "text"]}
*
* TextField should cover all your text input needs. It is able to handle
* single and multi line inputs.
*
*/
const TextField = createClass({
displayName: 'TextField',
reducers,
propTypes: {
/**
* Styles that are passed through to native control.
*/
style: object,
/**
* Set the TextField to multi line mode. Under the hood this will use a
* `textarea` instead of an `input` if set to `true`.
*/
isMultiLine: bool,
/**
* Disables the TextField by greying it out.
*/
isDisabled: bool,
/**
* Initial number of rows a multi line TextField should have. Ignored when
* not in multi-line mode.
*/
rows: number,
/**
* Class names that are appended to the defaults.
*/
className: string,
/**
* Fires an event every time the user types text into the TextField.
*
* Signature: `(value, { event, props }) => {}`
*/
onChange: func,
/**
* Fires an on the `input`'s onBlur.
*
* Signature: `(currentValue, { event, props }) => {}`
*/
onBlur: func,
/**
* Fires an event, debounced by `debounceLevel`, when the user types text
* into the TextField.
*
* Signature: `(value, { event, props }) => {}`
*/
onChangeDebounced: func,
/**
* Fires an event on every keydown
*
* Signature: `({ event, props }) => {}`
*/
onKeyDown: func,
/**
* Fires an event when the user hits "enter" from the TextField. You
* shouldn't use it if you're using `isMultiLine`.
*
* Signature: `(value, { event, props }) => {}`
*/
onSubmit: func,
/**
* Set the value of the input.
*/
value: oneOfType([
number,
string,
]),
/**
* Number of milliseconds to debounce the `onChangeDebounced` callback.
* Only useful if you provide an `onChangeDebounced` handler.
*/
debounceLevel: number,
/**
* Set the holding time, in milliseconds, that the component will wait if
* the user is typing and the component gets a new `value` prop.
*
* Any time the user hits a key, it starts a timer that prevents state
* changes from flowing in to the component until the timer has elapsed.
* This was heavily inspired by the [lazy-input][li] component.
*
* [li]: https://www.npmjs.com/package/lazy-input
*/
lazyLevel: number,
},
getDefaultProps() {
return {
style: null,
isDisabled: false,
isMultiLine: false,
onBlur: _.noop,
onChange: _.noop,
onChangeDebounced: _.noop,
onSubmit: _.noop,
rows: 5,
debounceLevel: 500,
lazyLevel: 1000,
};
},
getInitialState() {
return {
value: this.props.value,
}
},
componentWillMount() {
// Because we want the debounceLevel to be configurable, we can't put the
// debounced handler directly on the react class, so we set it up right
// before mount
this._handleChangeDebounced = _.debounce((...args) => {
this.props.onChangeDebounced(...args);
}, this.props.debounceLevel);
this._releaseHold = _.debounce(() => {
this.setState({ isHolding: false });
}, this.props.lazyLevel);
this._updateWhenReady = _.debounce((newValue) => {
Iif (this.state.isHolding) {
this._updateWhenReady(newValue);
} else Eif(newValue !== this.state.value) {
this.setState({ value: newValue });
}
}, this.props.lazyLevel);
},
componentWillReceiveProps(nextProps) {
// Allow consumer to optionally control state
Eif (_.has(nextProps, 'value')) {
if (this.state.isHolding) {
this._updateWhenReady(nextProps.value);
} else {
this.setState({ value: nextProps.value });
}
}
},
handleChange(event) {
const {
onChange,
onChangeDebounced,
} = this.props;
const value = _.get(event, 'target.value', '');
this.setState({ value, isHolding: true });
this._releaseHold();
onChange(value, { event, props: this.props });
// Also call the debounced handler in case the user wants debounced change
// events.
if (onChangeDebounced !== _.noop) {
event.persist(); // https://facebook.github.io/react/docs/events.html#event-pooling
this._handleChangeDebounced(value, { event, props: this.props });
}
},
handleBlur(event) {
const {
onBlur,
} = this.props;
const value = _.get(event, 'target.value', '');
onBlur(value, { event, props: this.props });
},
handleKeyDown(event) {
const {
props,
props: {
onSubmit,
onKeyDown,
},
} = this;
const value = _.get(event, 'target.value', '');
// If the consumer passed an onKeyDown, we call it
Iif (onKeyDown) {
onKeyDown({ event, props });
}
Eif (event.keyCode === KEYCODE.Enter) {
onSubmit(value, {event, props: this.props });
}
},
focus() {
this.refs.nativeElement.focus();
},
render() {
const {
className,
isDisabled,
isMultiLine,
rows,
style,
...passThroughs,
} = this.props;
const {
value,
} = this.state;
const finalProps = {
...omitProps(passThroughs, TextField, ['children']),
className: cx('&', {
'&-is-disabled': isDisabled,
'&-is-multi-line': isMultiLine,
'&-is-single-line': !isMultiLine,
}, className),
disabled: isDisabled,
onChange: this.handleChange,
onBlur: this.handleBlur,
onKeyDown: this.handleKeyDown,
style,
rows,
value,
ref: ref => this.refs = { nativeElement: ref },
};
return isMultiLine
? <textarea {...finalProps}/>
: <input type='text' {...finalProps}/>;
},
});
export default TextField;
|