Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
fix(Picky): Losing focus should close dropdown
Browse files Browse the repository at this point in the history
Added ref and handleOutsideClick

fix #22
  • Loading branch information
Aidurber committed Dec 30, 2017
1 parent 1ad43de commit 7b77afe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
21 changes: 21 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3028,6 +3028,7 @@ var Picky$1 = function (_React$Component) {
_this.onFilterChange = _this.onFilterChange.bind(_this);
_this.selectValue = _this.selectValue.bind(_this);
_this.allSelected = _this.allSelected.bind(_this);
_this.handleOutsideClick = _this.handleOutsideClick.bind(_this);
return _this;
}

Expand Down Expand Up @@ -3194,11 +3195,26 @@ var Picky$1 = function (_React$Component) {
}
});
}
}, {
key: 'handleOutsideClick',
value: function handleOutsideClick(e) {
if (this.node && this.node.contains(e.target)) {
return;
}
this.toggleDropDown();
}
}, {
key: 'toggleDropDown',
value: function toggleDropDown() {
var _this6 = this;

if (!this.state.open) {
// attach/remove event handler
document.addEventListener('click', this.handleOutsideClick, false);
} else {
document.removeEventListener('click', this.handleOutsideClick, false);
}

this.setState({
open: !this.state.open
}, function () {
Expand All @@ -3213,6 +3229,8 @@ var Picky$1 = function (_React$Component) {
}, {
key: 'render',
value: function render() {
var _this7 = this;

var _props2 = this.props,
placeholder = _props2.placeholder,
value = _props2.value,
Expand All @@ -3233,6 +3251,9 @@ var Picky$1 = function (_React$Component) {
return React__default.createElement(
'div',
{
ref: function ref(node) {
_this7.node = node;
},
className: 'picky',
id: this.state.id,
role: 'combobox',
Expand Down
18 changes: 18 additions & 0 deletions src/Picky.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Picky extends React.Component {
this.onFilterChange = this.onFilterChange.bind(this);
this.selectValue = this.selectValue.bind(this);
this.allSelected = this.allSelected.bind(this);
this.handleOutsideClick = this.handleOutsideClick.bind(this);
}
componentWillMount() {
const allSelected = this.allSelected();
Expand Down Expand Up @@ -206,7 +207,21 @@ class Picky extends React.Component {
}
);
}

handleOutsideClick(e) {
if (this.node && this.node.contains(e.target)) {
return;
}
this.toggleDropDown();
}
toggleDropDown() {
if (!this.state.open) {
// attach/remove event handler
document.addEventListener('click', this.handleOutsideClick, false);
} else {
document.removeEventListener('click', this.handleOutsideClick, false);
}

this.setState(
{
open: !this.state.open
Expand Down Expand Up @@ -241,6 +256,9 @@ class Picky extends React.Component {
}
return (
<div
ref={node => {
this.node = node;
}}
className="picky"
id={this.state.id}
role="combobox"
Expand Down
1 change: 0 additions & 1 deletion src/Placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Placeholder extends React.PureComponent {
constructor(props) {
super(props);
}

render() {
const {
placeholder,
Expand Down

0 comments on commit 7b77afe

Please sign in to comment.