Change blur detection for dropdown

This commit is contained in:
John Furrow
2015-12-31 08:16:38 -05:00
parent ad34bf6eaa
commit 142e150f89
3 changed files with 51 additions and 42 deletions

View File

@@ -1,5 +1,7 @@
.dropdown { .dropdown {
cursor: pointer;
display: inline-block; display: inline-block;
outline: none;
position: relative; position: relative;
z-index: 2; z-index: 2;

View File

@@ -3,15 +3,15 @@ import CSSTransitionGroup from 'react-addons-css-transition-group';
import React from 'react'; import React from 'react';
const methodsToBind = [ const methodsToBind = [
'componentDidMount', 'getDropdownButton',
'componentWillUnmount',
'getDropdownMenu', 'getDropdownMenu',
'onItemSelect', 'handleDropdownBlur',
'onDropdownClick', 'handleDropdownClick',
'onExternalClick' 'handleDropdownFocus',
'handleItemSelect'
]; ];
export default class SortDropdown extends React.Component { export default class Dropdown extends React.Component {
constructor() { constructor() {
super(); super();
@@ -24,12 +24,37 @@ export default class SortDropdown extends React.Component {
}); });
} }
componentDidMount() { handleDropdownBlur() {
window.addEventListener('click', this.onExternalClick); this.setState({
isExpanded: false
});
} }
componentWillUnmount() { handleDropdownClick() {
window.removeEventListener('click', this.onExternalClick); if (this.state.isExpanded) {
this.refs.dropdown.blur();
} else {
this.refs.dropdown.focus();
}
}
handleDropdownFocus(event) {
this.setState({
isExpanded: true
});
}
handleItemSelect(item) {
this.refs.dropdown.blur();
this.props.handleItemSelect(item);
}
getDropdownButton() {
return (
<div className={this.props.dropdownButtonClass} onClick={this.handleDropdownClick}>
{this.props.header}
</div>
);
} }
getDropdownMenu() { getDropdownMenu() {
@@ -39,7 +64,7 @@ export default class SortDropdown extends React.Component {
'is-selected': this.props.selectedItem.property === property.property 'is-selected': this.props.selectedItem.property === property.property
}) })
return ( return (
<li className={classes} key={index} onClick={this.onItemSelect.bind(this, property)}> <li className={classes} key={index} onClick={this.handleItemSelect.bind(this, property)}>
{property.displayName} {property.displayName}
</li> </li>
); );
@@ -48,7 +73,7 @@ export default class SortDropdown extends React.Component {
return ( return (
<div className="dropdown__content"> <div className="dropdown__content">
<div className="dropdown__header"> <div className="dropdown__header">
{this.props.header} {this.getDropdownButton()}
</div> </div>
<ul className="dropdown__items"> <ul className="dropdown__items">
{menuItems} {menuItems}
@@ -57,33 +82,10 @@ export default class SortDropdown extends React.Component {
); );
} }
onDropdownClick(event) {
event.stopPropagation();
this.setState({
isExpanded: !this.state.isExpanded
});
}
onExternalClick() {
if (this.state.isExpanded) {
this.setState({
isExpanded: false
});
}
}
onItemSelect(item) {
this.setState({
isExpanded: false
});
this.props.handleItemSelect(item);
}
render() { render() {
let classes = classnames({ let dropdownWrapperClass = classnames({
'dropdown': true, [this.props.dropdownWrapperClass]: true,
'is-expanded': this.state.isExpanded, 'is-expanded': this.state.isExpanded
[this.props.dropdownClasses]: true
}); });
let menu = null; let menu = null;
@@ -93,8 +95,8 @@ export default class SortDropdown extends React.Component {
} }
return ( return (
<div className={classes} onClick={this.onDropdownClick}> <div className={dropdownWrapperClass} onFocus={this.handleDropdownFocus} onBlur={this.handleDropdownBlur} ref="dropdown" tabIndex="0">
{this.props.header} {this.getDropdownButton()}
<CSSTransitionGroup <CSSTransitionGroup
transitionName="dropdown__content" transitionName="dropdown__content"
transitionEnterTimeout={250} transitionEnterTimeout={250}
@@ -105,3 +107,8 @@ export default class SortDropdown extends React.Component {
); );
} }
} }
Dropdown.defaultProps = {
dropdownWrapperClass: 'dropdown',
dropdownButtonClass: 'dropdown__trigger'
};

View File

@@ -14,9 +14,9 @@ class Sidebar extends React.Component {
getDropdownHeader() { getDropdownHeader() {
return ( return (
<button className="client-stats client-stat--limits"> <a className="client-stats client-stat--limits">
<Icon icon="limits" /> Speed Limits <Icon icon="limits" /> Speed Limits
</button> </a>
); );
} }