All files / javascripts/explorev2/components FieldSet.jsx

40.74% Statements 11/27
0% Branches 0/8
50% Functions 1/2
40.74% Lines 11/27
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 881x 1x 1x 1x 1x 1x   1x                               1x         1x                                                                                                                 1x 1x  
import React, { PropTypes } from 'react';
import TextField from './TextField';
import CheckboxField from './CheckboxField';
import TextAreaField from './TextAreaField';
import SelectField from './SelectField';
import { fieldTypes } from '../stores/fields';
 
const propTypes = {
  name: PropTypes.string.isRequired,
  type: PropTypes.oneOf(fieldTypes).isRequired,
  label: PropTypes.string.isRequired,
  choices: PropTypes.arrayOf(PropTypes.array),
  description: PropTypes.string,
  places: PropTypes.number,
  validators: PropTypes.any,
  onChange: React.PropTypes.func,
  value: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.bool,
    PropTypes.array]).isRequired,
};
 
const defaultProps = {
  choices: null,
  description: null,
  places: null,
  validators: null,
  onChange: () => {},
};
 
export default class FieldSet extends React.Component {
  renderCheckBoxField() {
    return (
      <CheckboxField
        {...this.props}
      />);
  }
 
  renderTextAreaField() {
    return (
      <TextAreaField
        {...this.props}
      />);
  }
 
  renderSelectField(selectProps) {
    return (
      <SelectField
        {...this.props}
        {...selectProps}
      />);
  }
 
  renderTextField() {
    return (
      <TextField
        {...this.props}
      />);
  }
 
  render() {
    const type = this.props.type;
    const selectProps = {
      SelectCustomMultiField: { multi: true, freeForm: true },
      SelectMultipleSortableField: { multi: true, freeForm: false },
      SelectField: { multi: false, freeForm: false },
      FreeFormSelectField: { multi: false, freeForm: true },
    };
    let field;
 
    if (type === 'CheckboxField') {
      field = this.renderCheckBoxField();
    } else if (Object.keys(selectProps).includes(type)) {
      field = this.renderSelectField(selectProps[type]);
    } else if (['TextField', 'IntegerField'].includes(type)) {
      field = this.renderTextField();
    } else if (type === 'TextAreaField') {
      field = this.renderTextAreaField();
    }
 
    return field;
  }
}
 
FieldSet.propTypes = propTypes;
FieldSet.defaultProps = defaultProps;