-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Re-usable dropdown component, 👏 Close dropdown when click outside, …
…actually open creator / community profile
- Loading branch information
1 parent
b1cb247
commit c6e7f84
Showing
5 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
import React, {Component, PropTypes} from 'react' | ||
import classNames from 'classnames' | ||
|
||
import Icon from './icon' | ||
import {map} from 'underline' | ||
|
||
import listensToClickOutside from 'react-onclickoutside/decorator' | ||
import {connect} from './connect' | ||
|
||
export class Dropdown extends Component { | ||
|
||
constructor () { | ||
super() | ||
this.state = {open: false} | ||
} | ||
|
||
render () { | ||
const {t, items, inner, className = ''} = this.props | ||
|
||
const {open} = this.state | ||
const dropdownClasses = classNames('dropdown', {active: open}) | ||
|
||
const children = items::map((item) => { | ||
const {label, icon, onClick} = item | ||
|
||
return <section key={label + '-' + icon} onClick={onClick}> | ||
<Icon icon={icon}/> | ||
{t.apply(null, label)} | ||
</section> | ||
}) | ||
|
||
return <div style={{position: 'relative'}} className={className}> | ||
<div onClick={this.toggle.bind(this)}>{inner}</div> | ||
<div className='dropdown-container'> | ||
<div className={dropdownClasses}> | ||
{children} | ||
</div> | ||
</div> | ||
</div> | ||
} | ||
|
||
toggle () { | ||
this.setState({...this.state, open: !this.state.open}) | ||
} | ||
|
||
close () { | ||
this.setState({...this.state, open: false}) | ||
} | ||
|
||
handleClickOutside () { | ||
this.close() | ||
} | ||
|
||
} | ||
|
||
Dropdown.propTypes = { | ||
inner: PropTypes.element, | ||
className: PropTypes.string, | ||
items: PropTypes.arrayOf(PropTypes.shape({ | ||
label: PropTypes.array.isRequired, | ||
icon: PropTypes.string | ||
})), | ||
|
||
t: PropTypes.func.isRequired | ||
} | ||
|
||
const listening = listensToClickOutside(Dropdown) | ||
|
||
const mapStateToProps = (state) => ({}) | ||
const mapDispatchToProps = (dispatch) => ({}) | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(listening) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters