This repository was archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 530
[added] Allow Tab to select option #186
Open
Daniel15
wants to merge
3
commits into
reactjs:master
Choose a base branch
from
Daniel15:tabselect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ let Autocomplete = React.createClass({ | |
value: React.PropTypes.any, | ||
onChange: React.PropTypes.func, | ||
onSelect: React.PropTypes.func, | ||
selectOnTab: React.PropTypes.bool.isRequired, | ||
shouldItemRender: React.PropTypes.func, | ||
sortItems: React.PropTypes.func, | ||
getItemValue: React.PropTypes.func.isRequired, | ||
|
@@ -34,10 +35,11 @@ let Autocomplete = React.createClass({ | |
}, | ||
inputProps: {}, | ||
onChange () {}, | ||
onSelect (value, item) {}, | ||
onSelect (value, item, selectionMethod) {}, | ||
renderMenu (items, value, style) { | ||
return <div style={{...style, ...this.menuStyle}} children={items}/> | ||
}, | ||
selectOnTab: true, | ||
shouldItemRender () { return true }, | ||
menuStyle: { | ||
borderRadius: '3px', | ||
|
@@ -164,42 +166,57 @@ let Autocomplete = React.createClass({ | |
}, | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code was moved to a new method |
||
Enter (event) { | ||
if (this.state.isOpen === false) { | ||
// menu is closed so there is no selection to accept -> do nothing | ||
return | ||
} | ||
else if (this.state.highlightedIndex == null) { | ||
// input has focus but no menu item is selected + enter is hit -> close the menu, highlight whatever's in input | ||
this.setState({ | ||
isOpen: false | ||
}, () => { | ||
this.refs.input.select() | ||
}) | ||
} | ||
else { | ||
// text entered + menu item has been highlighted + enter is hit -> update value to that of selected menu item, close the menu | ||
event.preventDefault() | ||
var item = this.getFilteredItems()[this.state.highlightedIndex] | ||
var value = this.props.getItemValue(item) | ||
this.setState({ | ||
isOpen: false, | ||
highlightedIndex: null | ||
}, () => { | ||
//this.refs.input.focus() // TODO: file issue | ||
this.refs.input.setSelectionRange( | ||
value.length, | ||
value.length | ||
) | ||
this.props.onSelect(value, item) | ||
}) | ||
} | ||
this.handleKeyboardSelection(event); | ||
}, | ||
|
||
Escape (event) { | ||
this.setState({ | ||
highlightedIndex: null, | ||
isOpen: false | ||
}) | ||
}, | ||
|
||
Tab (event) { | ||
if (this.props.selectOnTab) { | ||
this.handleKeyboardSelection(event); | ||
} | ||
} | ||
}, | ||
|
||
handleKeyboardSelection (event) { | ||
var key = event.key; | ||
if (this.state.isOpen === false) { | ||
// menu is closed so there is no selection to accept -> do nothing | ||
return | ||
} | ||
else if (this.state.highlightedIndex == null) { | ||
// input has focus but no menu item is selected + enter is hit -> close the menu, highlight whatever's in input | ||
this.setState({ | ||
isOpen: false | ||
}, () => { | ||
this.refs.input.select() | ||
}) | ||
} | ||
else { | ||
// text entered + menu item has been highlighted + enter is hit -> update value to that of selected menu item, close the menu | ||
if (key === 'Enter') { | ||
// If enter was pressed, we want to prevent the default event handler from executing. | ||
// However, if tab was pressed, we *do* want the default handler to kick in. | ||
event.preventDefault() | ||
} | ||
var item = this.getFilteredItems()[this.state.highlightedIndex] | ||
var value = this.props.getItemValue(item) | ||
this.setState({ | ||
isOpen: false, | ||
highlightedIndex: null | ||
}, () => { | ||
//this.refs.input.focus() // TODO: file issue | ||
this.refs.input.setSelectionRange( | ||
value.length, | ||
value.length | ||
) | ||
this.props.onSelect(value, item, key) | ||
}) | ||
} | ||
}, | ||
|
||
|
@@ -262,7 +279,7 @@ let Autocomplete = React.createClass({ | |
isOpen: false, | ||
highlightedIndex: null | ||
}, () => { | ||
this.props.onSelect(value, item) | ||
this.props.onSelect(value, item, 'click') | ||
this.refs.input.focus() | ||
}) | ||
}, | ||
|
@@ -373,4 +390,3 @@ let Autocomplete = React.createClass({ | |
}) | ||
|
||
module.exports = Autocomplete | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best practice is to mark props as
isRequired
if they have a default, as they'll never not be set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this backwards compatible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, as it defaults to
false
on line 42.