forked from jquense/react-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboboxInput.jsx
81 lines (65 loc) · 1.8 KB
/
ComboboxInput.jsx
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
import React from 'react';
import caretPos from './util/caret';
import compat from './util/compat';
export default React.createClass({
displayName: 'ComboboxInput',
propTypes: {
value: React.PropTypes.string,
onChange: React.PropTypes.func.isRequired
},
componentDidUpdate() {
var input = compat.findDOMNode(this)
, val = this.props.value;
if ( this.isSuggesting() ){
var start = val.toLowerCase().indexOf(this._last.toLowerCase()) + this._last.length
, end = val.length - start
if ( start >= 0) {
caretPos(input, start, start + end)
}
}
},
getDefaultProps(){
return {
value: ''
}
},
render(){
return (
<input
{...this.props }
type='text'
aria-disabled={this.props.disabled}
aria-readonly={this.props.readOnly}
className={this.props.className + ' rw-input'}
onKeyDown={this.props.onKeyDown}
onChange={this._change}
value={this.props.value == null ? '' : this.props.value}
/>
)
},
isSuggesting() {
var val = this.props.value
, isSuggestion = this._last != null
&& val.toLowerCase().indexOf(this._last.toLowerCase()) !== -1;
return this.props.suggest && isSuggestion
},
accept(removeCaret) {
var val = compat.findDOMNode(this).value || ''
, end = val.length;
this._last = null
removeCaret && caretPos(compat.findDOMNode(this), end, end)
},
_change(e) {
var val = e.target.value
, pl = !!this.props.placeholder
// IE fires input events when setting/unsetting placeholders.
// issue #112
if ( pl && !val && val === (this.props.value || '') )
return
this._last = val;
this.props.onChange(e, val)
},
focus() {
compat.findDOMNode(this).focus()
}
});