2016-11-21 10:06:25 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
|
import { bindActionCreators } from 'redux'
|
|
|
|
|
import FontAwesome from 'react-fontawesome'
|
|
|
|
|
|
|
|
|
|
export default class DropdownField extends React.Component{
|
|
|
|
|
|
2017-10-10 13:05:28 +13:00
|
|
|
constructor(props){
|
2016-11-21 10:06:25 +13:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
expanded: false
|
|
|
|
|
}
|
2016-12-16 07:58:12 +13:00
|
|
|
this.handleClick = this.handleClick.bind(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
|
window.addEventListener("click", this.handleClick, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(){
|
|
|
|
|
window.removeEventListener("click", this.handleClick, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleClick(e){
|
|
|
|
|
// TODO: remove dependency on jQuery and explore the performance of this functionality
|
2017-10-10 13:05:28 +13:00
|
|
|
if ($(e.target).closest('.dropdown-field').data('key') != this.props.name.replace(' ','_').toLowerCase() && this.state.expanded){
|
2016-12-16 07:58:12 +13:00
|
|
|
this.setState({ expanded: false })
|
|
|
|
|
}
|
2016-11-21 10:06:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleChange(value){
|
|
|
|
|
this.setState({ expanded: !this.state.expanded })
|
2017-10-10 13:05:28 +13:00
|
|
|
return this.props.handleChange(value )
|
2016-11-21 10:06:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleToggle(){
|
|
|
|
|
this.setState({ expanded: !this.state.expanded })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render(){
|
2018-01-17 10:17:30 +13:00
|
|
|
if (!this.props.options){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-11-21 10:06:25 +13:00
|
|
|
|
2018-01-17 10:17:30 +13:00
|
|
|
var className = 'dropdown-field';
|
|
|
|
|
if (this.state.expanded){
|
|
|
|
|
className += ' expanded';
|
|
|
|
|
}
|
|
|
|
|
if (this.props.no_status_icon){
|
|
|
|
|
className += ' no-status-icon';
|
|
|
|
|
}
|
2018-04-29 21:10:57 +12:00
|
|
|
if (this.props.no_label){
|
|
|
|
|
className += ' no-label';
|
|
|
|
|
}
|
2018-01-23 11:34:03 +13:00
|
|
|
if (this.props.button){
|
|
|
|
|
className += ' buttonify';
|
|
|
|
|
}
|
2016-12-16 07:58:12 +13:00
|
|
|
var current_value = this.props.options[0].value
|
2018-01-17 10:17:30 +13:00
|
|
|
if (this.props.value){
|
|
|
|
|
current_value = this.props.value;
|
|
|
|
|
}
|
2017-01-24 08:02:30 +13:00
|
|
|
|
|
|
|
|
var icon = <FontAwesome name="check" />
|
2018-01-17 10:17:30 +13:00
|
|
|
if (this.props.reverse !== undefined){
|
2017-03-15 08:23:47 +13:00
|
|
|
if (this.props.reverse){
|
|
|
|
|
icon = <FontAwesome className="reverse" name="caret-down" />
|
|
|
|
|
} else {
|
|
|
|
|
icon = <FontAwesome className="reverse" name="caret-up" />
|
|
|
|
|
}
|
2017-01-24 08:02:30 +13:00
|
|
|
}
|
2016-12-16 07:58:12 +13:00
|
|
|
|
2016-11-21 10:06:25 +13:00
|
|
|
return (
|
2018-01-17 10:17:30 +13:00
|
|
|
<div className={className} data-key={this.props.name.replace(' ','_').toLowerCase()}>
|
2018-01-23 11:34:03 +13:00
|
|
|
<div className={"label"+(this.props.button ? " button "+this.props.button : "")} onClick={ () => this.handleToggle() }>
|
2017-03-24 08:46:20 +13:00
|
|
|
{this.props.icon ? <span><FontAwesome name={this.props.icon} /> </span> : null}
|
2017-03-23 21:20:25 +13:00
|
|
|
<span className="text">{ this.props.name }</span>
|
2016-11-21 10:06:25 +13:00
|
|
|
</div>
|
|
|
|
|
<div className="options">
|
|
|
|
|
{
|
2017-10-10 13:05:28 +13:00
|
|
|
this.props.options.map(option => {
|
2016-11-21 10:06:25 +13:00
|
|
|
return (
|
|
|
|
|
<div className="option" key={ option.value } onClick={ e => this.handleChange(option.value) }>
|
2018-01-17 10:17:30 +13:00
|
|
|
{!this.props.no_status_icon && (option.value == current_value) ? icon : null}
|
2017-01-24 08:02:30 +13:00
|
|
|
{option.label}
|
2016-11-21 10:06:25 +13:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|