Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoComplete] Fix openOnFocus and item click #3669

Merged
merged 1 commit into from
Mar 12, 2016
Merged
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
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