Files
Iris/src/js/components/Fields/DropdownField.js

150 lines
3.8 KiB
JavaScript
Raw Normal View History

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
}
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){
this.setState({
expanded: false
});
}
2016-11-21 10:06:25 +13:00
}
handleChange(value, is_selected){
2018-05-06 20:25:59 +12:00
if (this.isMultiSelect()){
if (value == 'select-all'){
var new_value = [];
for (var i = 0; i < this.props.options.length; i++){
new_value.push(this.props.options[i].value);
}
return this.props.handleChange(new_value);
}
} else {
this.setState({
expanded: !this.state.expanded
});
}
var current_value = this.props.value;
2018-05-06 20:25:59 +12:00
if (this.isMultiSelect()){
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(){
this.setState({
expanded: !this.state.expanded
});
2016-11-21 10:06:25 +13:00
}
2018-05-06 20:25:59 +12:00
isMultiSelect(){
return this.props.value instanceof Array;
}
2016-11-21 10:06:25 +13:00
render(){
if (!this.props.options){
return null;
}
2016-11-21 10:06:25 +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;
}
var current_value = null;
if (this.props.value){
current_value = this.props.value;
} else if (this.props.options.length > 0){
current_value = this.props.options[0].value;
}
2018-05-06 20:25:59 +12:00
var selected_icon = <FontAwesome name="check" />
if (this.props.selected_icon){
selected_icon = <FontAwesome name={this.props.selected_icon} />
}
var options = Object.assign([], this.props.options);
if (this.isMultiSelect()){
options.push({
value: 'select-all',
label: 'Select all',
className: 'grey-text'
});
}
2016-11-21 10:06:25 +13:00
return (
<div className={className} data-key={this.props.name.replace(' ','_').toLowerCase()}>
2018-05-06 20:25:59 +12:00
<div className={"label"+(this.props.button ? " button "+this.props.button : "")} onClick={e => this.handleToggle()}>
{this.props.icon ? <span><FontAwesome name={this.props.icon} />&nbsp; </span> : null}
2018-05-06 20:25:59 +12:00
<span className="text">
{this.props.name}
{this.isMultiSelect() ? ' ('+current_value.length+')' : null}
</span>
2016-11-21 10:06:25 +13:00
</div>
<div className="options">
{
2018-05-06 20:25:59 +12:00
options.map(option => {
if (this.isMultiSelect()){
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-06 20:25:59 +12:00
<div className={"option "+(option.className ? option.className : '')} key={option.value} onClick={e => this.handleChange(option.value, is_selected)}>
{!this.props.no_status_icon && is_selected ? selected_icon : null}
{option.label}
2016-11-21 10:06:25 +13:00
</div>
)
})
}
</div>
</div>
)
}
}