All files / src/components/Form/Select index.jsx

75% Statements 12/16
66.67% Branches 4/6
55.56% Functions 5/9
75% Lines 12/16
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                        3x 16x 16x       16x         16x   16x             16x 12x   24x 12x           4x             3x                                      
import 'react-select/dist/react-select.css';
import styles from './style.postcss';
 
import React from 'react';
import ReactSelect from 'react-select';
import is from 'is_js';
import { HOC as formsyDecorator } from 'formsy-react';
import classnames from 'classnames';
import Icon from 'components/Icon';
import Field from '../Field';
import PropTypes from 'prop-types';
 
const Select = (props) => {
  const arrowRenderer = () =>
    <span className={styles.Select_icon}>
      <Icon id="chevron-small-down" />
    </span>;
 
  const classes = classnames(styles.Select_element,
      props.className,
      { [styles.__required]: props.showRequired() },
      { [styles.__error]: props.showError() });
 
  const select = <ReactSelect placeholder={props.placeholder || undefined}
      onChange={(e) => handleChange(e)}
      arrowRenderer={() => arrowRenderer()}
      optionRenderer={(option) => <div className={styles.Select_option}>
        {option.label || option.value}
      </div>}
      {...props}
      className={classes} />;
 
  if (is.not.undefined(props.label)) {
    return <Field label={props.label}
        getErrorMessage={() => props.getErrorMessage()}
        showError={() => props.showError()}
        showRequired={() => props.showRequired()}
        useLabel
        {...props.fieldOptions}>
      {select}
    </Field>;
  }
  return select;
 
  function handleChange(e) {
    props.setValue(e.value);
  }
};
 
Select.propTypes = {
  className: PropTypes.string,
  getErrorMessage: PropTypes.func.isRequired,
  showRequired: PropTypes.func.isRequired,
  setValue: PropTypes.func.isRequired,
  showError: PropTypes.func.isRequired,
  clearable: PropTypes.bool,   // default: true
  searchable: PropTypes.bool,  // default: true
  label: PropTypes.string,
  placeholder: PropTypes.string,
  fieldOptions: PropTypes.shape({
    className: PropTypes.string,
    labelTextClassName: PropTypes.string,
    neighborStackMode: PropTypes.oneOf(['default', 'horizontal']),
    innerStackMode: PropTypes.oneOf(['horizontal', 'vertical']),  // default: horizontal
  }),
};
 
export default formsyDecorator(Select);