All files / javascripts/explorev2/components Filters.jsx

0% Statements 0/27
0% Branches 0/6
0% Functions 0/5
0% Lines 0/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                                                                                                                                                                                                                                                               
import React from 'react';
// import { Tab, Row, Col, Nav, NavItem } from 'react-bootstrap';
import Select from 'react-select';
import { Button } from 'react-bootstrap';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as actions from '../actions/exploreActions';
import shortid from 'shortid';
 
const propTypes = {
  actions: React.PropTypes.object,
  filterColumnOpts: React.PropTypes.array,
  filters: React.PropTypes.array,
};
 
const defaultProps = {
  filterColumnOpts: [],
  filters: [],
};
 
class Filters extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      opOpts: ['in', 'not in'],
    };
  }
  changeField(filter, fieldOpt) {
    const val = (fieldOpt) ? fieldOpt.value : null;
    this.props.actions.changeFilterField(filter, val);
  }
  changeOp(filter, opOpt) {
    const val = (opOpt) ? opOpt.value : null;
    this.props.actions.changeFilterOp(filter, val);
  }
  changeValue(filter, value) {
    this.props.actions.changeFilterValue(filter, value);
  }
  removeFilter(filter) {
    this.props.actions.removeFilter(filter);
  }
  addFilter() {
    this.props.actions.addFilter({
      id: shortid.generate(),
      field: null,
      op: null,
      value: null,
    });
  }
  render() {
    const filters = this.props.filters.map((filter) => (
      <div>
        <Select
          className="row"
          multi={false}
          name="select-column"
          placeholder="Select column"
          options={this.props.filterColumnOpts}
          value={filter.field}
          autosize={false}
          onChange={this.changeField.bind(this, filter)}
        />
        <div className="row">
          <Select
            className="col-sm-3"
            multi={false}
            name="select-op"
            placeholder="Select operator"
            options={this.state.opOpts.map((o) => ({ value: o, label: o }))}
            value={filter.op}
            autosize={false}
            onChange={this.changeOp.bind(this, filter)}
          />
          <div className="col-sm-6">
            <input
              type="text"
              onChange={this.changeValue.bind(this, filter)}
              className="form-control input-sm"
              placeholder="Filter value"
            />
          </div>
          <div className="col-sm-3">
            <Button
              bsStyle="primary"
              onClick={this.removeFilter.bind(this, filter)}
            >
              <i className="fa fa-minus" />
            </Button>
          </div>
        </div>
      </div>
      )
    );
    return (
      <div className="panel space-1">
        <div className="panel-header">Filters</div>
        <div className="panel-body">
          {filters}
          <Button
            bsStyle="primary"
            onClick={this.addFilter.bind(this)}
          >
            <i className="fa fa-plus" />Add Filter
          </Button>
        </div>
      </div>
    );
  }
}
 
Filters.propTypes = propTypes;
Filters.defaultProps = defaultProps;
 
function mapStateToProps(state) {
  return {
    filterColumnOpts: state.filterColumnOpts,
    filters: state.filters,
  };
}
 
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(actions, dispatch),
  };
}
 
export default connect(mapStateToProps, mapDispatchToProps)(Filters);