all files / src/explore/components/controls/ CollectionControl.jsx

58.14% Statements 25/43
0% Branches 0/4
0% Functions 0/11
60% Lines 24/40
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                                                                                                                                                                                                    
import React from 'react';
import PropTypes from 'prop-types';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
import shortid from 'shortid';
import {
  SortableContainer, SortableHandle, SortableElement, arrayMove,
} from 'react-sortable-hoc';
 
import InfoTooltipWithTrigger from '../../../components/InfoTooltipWithTrigger';
import ControlHeader from '../ControlHeader';
import controlMap from './';
 
const propTypes = {
  name: PropTypes.string.isRequired,
  label: PropTypes.string,
  description: PropTypes.string,
  placeholder: PropTypes.string,
  addTooltip: PropTypes.string,
  itemGenerator: PropTypes.func,
  keyAccessor: PropTypes.func,
  onChange: PropTypes.func,
  value: PropTypes.oneOfType([
    PropTypes.array,
  ]),
  isFloat: PropTypes.bool,
  isInt: PropTypes.bool,
  controlName: PropTypes.string.isRequired,
};
 
const defaultProps = {
  label: null,
  description: null,
  onChange: () => {},
  placeholder: 'Empty collection',
  itemGenerator: () => ({ key: shortid.generate() }),
  keyAccessor: o => o.key,
  value: [],
  addTooltip: 'Add an item',
};
const SortableListGroupItem = SortableElement(ListGroupItem);
const SortableListGroup = SortableContainer(ListGroup);
const SortableDragger = SortableHandle(() => (
  <i className="fa fa-bars text-primary" style={{ cursor: 'ns-resize' }} />));
 
export default class CollectionControl extends React.Component {
  constructor(props) {
    super(props);
    this.onAdd = this.onAdd.bind(this);
  }
  onChange(i, value) {
    Object.assign(this.props.value[i], value);
    this.props.onChange(this.props.value);
  }
  onAdd() {
    this.props.onChange(this.props.value.concat([this.props.itemGenerator()]));
  }
  onSortEnd({ oldIndex, newIndex }) {
    this.props.onChange(arrayMove(this.props.value, oldIndex, newIndex));
  }
  removeItem(i) {
    this.props.onChange(this.props.value.filter((o, ix) => i !== ix));
  }
  renderList() {
    if (this.props.value.length === 0) {
      return <div className="text-muted">{this.props.placeholder}</div>;
    }
    const Control = controlMap[this.props.controlName];
    return (
      <SortableListGroup
        useDragHandle
        lockAxis="y"
        onSortEnd={this.onSortEnd.bind(this)}
      >
        {this.props.value.map((o, i) => (
          <SortableListGroupItem
            className="clearfix"
            key={this.props.keyAccessor(o)}
            index={i}
          >
            <div className="pull-left m-r-5">
              <SortableDragger />
            </div>
            <div className="pull-left">
              <Control
                {...o}
                onChange={this.onChange.bind(this, i)}
              />
            </div>
            <div className="pull-right">
              <InfoTooltipWithTrigger
                icon="times"
                label="remove-item"
                tooltip="remove item"
                bsStyle="primary"
                onClick={this.removeItem.bind(this, i)}
              />
            </div>
          </SortableListGroupItem>))}
      </SortableListGroup>
    );
  }
  render() {
    return (
      <div>
        <ControlHeader {...this.props} />
        {this.renderList()}
        <InfoTooltipWithTrigger
          icon="plus-circle"
          label="add-item"
          tooltip={this.props.addTooltip}
          bsStyle="primary"
          className="fa-lg"
          onClick={this.onAdd}
        />
      </div>
    );
  }
}
 
CollectionControl.propTypes = propTypes;
CollectionControl.defaultProps = defaultProps;