Skip to content

Commit

Permalink
Merge pull request #3669 from oliviertassinari/auto-complete-open-on-…
Browse files Browse the repository at this point in the history
…focus

[AutoComplete] Fix openOnFocus and item click
  • Loading branch information
mbrookes committed Mar 12, 2016
2 parents ac49447 + e644d37 commit 376a29f
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/auto-complete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const AutoComplete = React.createClass({
onUpdateInput: () => {},
onNewRequest: () => {},
searchText: '',
menuCloseDelay: 200,
menuCloseDelay: 300,
targetOrigin: {
vertical: 'top',
horizontal: 'left',
Expand Down Expand Up @@ -254,15 +254,9 @@ const AutoComplete = React.createClass({

componentWillUnmount() {
clearTimeout(this.timerTouchTapCloseId);
clearTimeout(this.timerBlurCloseId);
},

componentClickAway() {
this.close();
},

timerTouchTapCloseId: undefined,
timerBlurCloseId: undefined,
timerTouchTapCloseId: null,

close() {
this.setState({
Expand All @@ -271,6 +265,14 @@ const AutoComplete = React.createClass({
});
},

handleRequestClose() {
// Only take into account the Popover clickAway when we are
// not focusing the TextField.
if (!this.state.focusTextField) {
this.close();
}
},

setValue(textValue) {
warning(false, 'setValue() is deprecated, use the searchText property.');

Expand All @@ -285,6 +287,11 @@ const AutoComplete = React.createClass({
return this.state.searchText;
},

handleMouseDown(event) {
// Keep the TextField focused
event.preventDefault();
},

handleItemTouchTap(event, child) {
const dataSource = this.props.dataSource;
let chosenRequest;
Expand All @@ -304,12 +311,12 @@ const AutoComplete = React.createClass({

this.props.onNewRequest(chosenRequest, index);

clearTimeout(this.timerBlurCloseId);
this.timerTouchTapCloseId = setTimeout(() => {
this.setState({
searchText: searchText,
});
this.close();
this.timerTouchTapCloseId = null;
}, this.props.menuCloseDelay);
},

Expand Down Expand Up @@ -360,12 +367,8 @@ const AutoComplete = React.createClass({
},

handleBlur(event) {
// Run asynchronously to wait for a potential handleItemTouchTap() call.
// The blur event occurs first on a click device and after on a touch device.
if (this.state.focusTextField) {
this.timerBlurCloseId = setTimeout(() => {
this.close();
}, 0);
if (this.state.focusTextField && this.timerTouchTapCloseId === null) {
this.close();
}

if (this.props.onBlur) {
Expand Down Expand Up @@ -464,6 +467,7 @@ const AutoComplete = React.createClass({
onEscKeyDown={this.close}
initiallyKeyboardFocused={false}
onItemTouchTap={this.handleItemTouchTap}
onMouseDown={this.handleMouseDown}
listStyle={Object.assign(styles.list, listStyle)}
style={Object.assign(styles.menu, menuStyle)}
>
Expand Down Expand Up @@ -526,7 +530,7 @@ const AutoComplete = React.createClass({
open={open}
anchorEl={anchorEl}
useLayerForClickAway={false}
onRequestClose={this.close}
onRequestClose={this.handleRequestClose}
animated={animated}
>
{menu}
Expand Down

0 comments on commit 376a29f

Please sign in to comment.