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
|
|
|
|
|
}
|
2018-05-04 09:08:01 +12:00
|
|
|
|
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
2016-12-16 07:58:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
2018-05-04 09:08:01 +12:00
|
|
|
window.addEventListener("click", this.handleClick, false);
|
2016-12-16 07:58:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(){
|
2018-05-04 09:08:01 +12:00
|
|
|
window.removeEventListener("click", this.handleClick, false);
|
2016-12-16 07:58:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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){
|
2018-05-04 09:08:01 +12:00
|
|
|
this.setState({
|
|
|
|
|
expanded: false
|
|
|
|
|
});
|
2016-12-16 07:58:12 +13:00
|
|
|
}
|
2016-11-21 10:06:25 +13:00
|
|
|
}
|
|
|
|
|
|
2018-05-04 09:08:01 +12:00
|
|
|
handleChange(value, is_selected){
|
|
|
|
|
this.setState({
|
|
|
|
|
expanded: !this.state.expanded
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var current_value = this.props.value;
|
|
|
|
|
if (current_value instanceof Array){
|
|
|
|
|
if (is_selected){
|
|
|
|
|
var index = current_value.indexOf(value);
|
|
|
|
|
current_value.splice(index, 1);
|
|
|
|
|
var new_value = current_value;
|
|
|
|
|
} else {
|
|
|
|
|
current_value.push(value);
|
|
|
|
|
var new_value = current_value;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
var new_value = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.props.handleChange(new_value);
|
2016-11-21 10:06:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleToggle(){
|
2018-05-04 09:08:01 +12:00
|
|
|
this.setState({
|
|
|
|
|
expanded: !this.state.expanded
|
|
|
|
|
});
|
2016-11-21 10:06:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
}
|
2018-04-30 20:34:15 +12:00
|
|
|
if (this.props.className){
|
|
|
|
|
className += ' '+this.props.className;
|
|
|
|
|
}
|
2018-05-04 09:08:01 +12:00
|
|
|
var current_value = null;
|
2018-01-17 10:17:30 +13:00
|
|
|
if (this.props.value){
|
|
|
|
|
current_value = this.props.value;
|
2018-05-04 09:08:01 +12:00
|
|
|
} else if (this.props.options.length > 0){
|
|
|
|
|
current_value = this.props.options[0].value;
|
2018-01-17 10:17:30 +13:00
|
|
|
}
|
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 => {
|
2018-05-04 09:08:01 +12:00
|
|
|
if (current_value instanceof Array){
|
|
|
|
|
var is_selected = current_value.indexOf(option.value) > -1;
|
|
|
|
|
} else {
|
|
|
|
|
var is_selected = current_value == option.value;
|
|
|
|
|
}
|
2016-11-21 10:06:25 +13:00
|
|
|
return (
|
2018-05-04 09:08:01 +12:00
|
|
|
<div className="option" key={option.value} onClick={e => this.handleChange(option.value, is_selected)}>
|
|
|
|
|
{!this.props.no_status_icon && is_selected ? icon : null}
|
2017-01-24 08:02:30 +13:00
|
|
|
{option.label}
|
2016-11-21 10:06:25 +13:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|