Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

[added] select item on arrow right key down #67

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions lib/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,12 @@ let Autocomplete = React.createClass({
})
},

ArrowRight (event) {
this.selectItemFromKeyEvent(event)
},

Enter (event) {
if (this.state.isOpen === false) {
// already selected this, do nothing
return
}
else if (this.state.highlightedIndex == null) {
// hit enter after focus but before typing anything so no autocomplete attempt yet
this.setState({
isOpen: false
}, () => {
React.findDOMNode(this.refs.input).select()
})
}
else {
var item = this.getFilteredItems()[this.state.highlightedIndex]
this.setState({
value: this.props.getItemValue(item),
isOpen: false,
highlightedIndex: null
}, () => {
//React.findDOMNode(this.refs.input).focus() // TODO: file issue
React.findDOMNode(this.refs.input).setSelectionRange(
this.state.value.length,
this.state.value.length
)
this.props.onSelect(this.state.value, item)
})
}
this.selectItemFromKeyEvent(event)
},

Escape (event) {
Expand Down Expand Up @@ -243,6 +221,36 @@ let Autocomplete = React.createClass({
})
},

selectItemFromKeyEvent (event) {
if (this.state.isOpen === false) {
// already selected this, do nothing
return
}
else if (this.state.highlightedIndex == null) {
// hit enter after focus but before typing anything so no autocomplete attempt yet
this.setState({
isOpen: false
}, () => {
React.findDOMNode(this.refs.input).select()
})
}
else {
var item = this.getFilteredItems()[this.state.highlightedIndex]
this.setState({
value: this.props.getItemValue(item),
isOpen: false,
highlightedIndex: null
}, () => {
//React.findDOMNode(this.refs.input).focus() // TODO: file issue
React.findDOMNode(this.refs.input).setSelectionRange(
this.state.value.length,
this.state.value.length
)
this.props.onSelect(this.state.value, item)
})
}
},

setIgnoreBlur (ignore) {
this._ignoreBlur = ignore
},
Expand Down Expand Up @@ -324,4 +332,3 @@ let Autocomplete = React.createClass({
})

module.exports = Autocomplete