Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Fix dropdown lists remain displayed after losing focus #1421
Browse files Browse the repository at this point in the history
  • Loading branch information
wiadev committed Dec 9, 2017
1 parent 76a29ff commit b559422
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/components/widget/List/RawList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import React, { Component } from 'react';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';

class RawList extends Component {
isFocused = false;

constructor(props) {
super(props);

Expand All @@ -13,8 +15,6 @@ class RawList extends Component {
}
}

considerBlur = false;

componentDidMount = () => {
const { autofocus, onRequestListData } = this.props;
if (this.dropdown && autofocus && onRequestListData) {
Expand Down Expand Up @@ -92,7 +92,6 @@ class RawList extends Component {
}

let dropdownList = dropdown.concat(list);

this.setState(
Object.assign(
{
Expand All @@ -113,8 +112,13 @@ class RawList extends Component {

const { isOpen } = this.state;

// trigger handleBlur action if dropdown is still opened
// after focus is lost
if (!this.isFocused && isOpen) {
this.handleBlur();
}
// no need for updating scroll
if (!isOpen || !list.length) {
if (!this.isFocused || !isOpen || !list.length) {
return;
}

Expand Down Expand Up @@ -207,7 +211,6 @@ class RawList extends Component {
navigateToAlphanumeric = (char) => {
const { list } = this.props;
const { isOpen, selected } = this.state;

if (!isOpen) {
this.setState({
isOpen: true
Expand All @@ -232,7 +235,6 @@ class RawList extends Component {

navigate = (up) => {
const { selected, dropdownList, isOpen } = this.state;

if (!isOpen) {
this.setState({
isOpen: true
Expand All @@ -257,18 +259,12 @@ class RawList extends Component {
}

handleBlur = () => {
if (!this.considerBlur) {
return;
}

this.considerBlur = false;

const { selected, doNotOpenOnFocus } = this.props;
this.isFocused = false;

if (!doNotOpenOnFocus && this.dropdown) {
this.dropdown.blur();
}

this.setState({
isOpen: false,
selected: selected || 0
Expand All @@ -280,20 +276,19 @@ class RawList extends Component {
* on focus.
*/
handleClick = (e) => {
this.considerBlur = true;

e.preventDefault();

const { onFocus } = this.props;

onFocus && onFocus();

this.setState({
isOpen: true
});
}

handleFocus = (e) => {
this.isFocused = true;

if (e) {
e.preventDefault();
}
Expand Down Expand Up @@ -321,7 +316,6 @@ class RawList extends Component {
} else {
onSelect(option);
}

this.setState({
selected: (option || 0)
}, () => this.handleBlur());
Expand Down Expand Up @@ -358,7 +352,7 @@ class RawList extends Component {
}

if (selected) {
this.considerBlur = true;
this.isFocused = true;
this.handleSelect(selected);
} else {
onSelect(null);
Expand Down Expand Up @@ -509,7 +503,7 @@ class RawList extends Component {
<i className="meta-icon-down-1 input-icon-sm"/>
</div>
</div>
{isOpen && (
{this.isFocused && isOpen && (
<div
className="input-dropdown-list"
ref="listScroll"
Expand Down

0 comments on commit b559422

Please sign in to comment.