All files / javascripts/explorev2/components SelectField.jsx

72.5% Statements 29/40
38.46% Branches 10/26
62.5% Functions 5/8
72.97% Lines 27/37
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 1151x 1x   1x                         1x               1x         12x   4x 4x 4x     1x   1x     1x   4x 4x 8x 8x 8x   8x           4x   2x 1x                           4x                             4x                       4x   4x                     1x 1x  
import React, { PropTypes } from 'react';
import Select, { Creatable } from 'react-select';
 
const propTypes = {
  choices: PropTypes.array,
  clearable: PropTypes.bool,
  description: PropTypes.string,
  editUrl: PropTypes.string,
  freeForm: PropTypes.bool,
  label: PropTypes.string,
  multi: PropTypes.bool,
  name: PropTypes.string.isRequired,
  onChange: PropTypes.func,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array]),
};
 
const defaultProps = {
  choices: [],
  clearable: true,
  description: null,
  editUrl: null,
  freeForm: false,
  label: null,
  multi: false,
  onChange: () => {},
  value: '',
};
 
export default class SelectField extends React.Component {
  constructor(props) {
    super(props);
    this.state = { options: this.getOptions() };
    this.onChange = this.onChange.bind(this);
    this.renderOption = this.renderOption.bind(this);
  }
  onChange(opt) {
    let optionValue = opt ? opt.value : null;
    // if multi, return options values as an array
    Iif (this.props.multi) {
      optionValue = opt ? opt.map((o) => o.value) : null;
    }
    this.props.onChange(optionValue);
  }
  getOptions() {
    const options = this.props.choices.map((c) => {
      let label = c[0];
      Eif (c.length > 1) {
        label = c[1];
      }
      return {
        value: c[0],
        label,
        imgSrc: c[2],
      };
    });
    if (this.props.freeForm) {
      // For FreeFormSelect, insert value into options if not exist
      const values = this.props.choices.map((c) => c[0]);
      Iif (this.props.value) {
        if (typeof this.props.value === 'object') {
          this.props.value.forEach((v) => {
            if (values.indexOf(v) === -1) {
              options.push({ value: v, label: v });
            }
          });
        } else {
          if (values.indexOf(this.props.value) === -1) {
            options.push({ value: this.props.value, label: this.props.value });
          }
        }
      }
    }
    return options;
  }
  renderOption(opt) {
    if (opt.imgSrc) {
      return (
        <div>
          <img className="viz-thumb-option" src={opt.imgSrc} alt={opt.value} />
          <span>{opt.label}</span>
        </div>
      );
    }
    return opt.label;
  }
  render() {
    //  Tab, comma or Enter will trigger a new option created for FreeFormSelect
    const selectProps = {
      multi: this.props.multi,
      name: `select-${this.props.name}`,
      placeholder: `Select (${this.state.options.length})`,
      options: this.state.options,
      value: this.props.value,
      autosize: false,
      clearable: this.props.clearable,
      onChange: this.onChange,
      optionRenderer: this.renderOption,
    };
    //  Tab, comma or Enter will trigger a new option created for FreeFormSelect
    const selectWrap = this.props.freeForm ?
      (<Creatable {...selectProps} />) : (<Select {...selectProps} />);
    return (
      <div>
        {selectWrap}
        {this.props.editUrl &&
          <a href={`${this.props.editUrl}/${this.props.value}`}>edit</a>
        }
      </div>
    );
  }
}
 
SelectField.propTypes = propTypes;
SelectField.defaultProps = defaultProps;