All files Controls.js

88.46% Statements 23/26
84.62% Branches 22/26
88.89% Functions 8/9
88.46% Lines 23/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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                        35x 35x                 6x 5x                   2x 1x                                               93x                                                   93x 86x               10x                           7x                                                         93x                                                         93x 93x 93x   93x 69x 69x   24x 24x     93x                 3x                             3x                                              
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Immutable from 'immutable';
 
import Tag from './Tag';
import Loader from './Loader';
 
import css from './Controls.css';
 
export default class SelectControls extends React.Component {
  constructor(props) {
    super(props);
    this.handleOpenDropdown = this.handleOpenDropdown.bind(this);
  }
 
  /**
   * Enables the focus on input DOM
   *
   * @memberof SelectControls
   */
  focusInput() {
    if (this.props.filterable) {
      this.refs.filterInput.focus();
    }
  }
 
  /**
   * Enables the blur on input DOM
   *
   * @memberof SelectControls
   */
  blurInput() {
    if (this.props.filterable) {
      this.refs.filterInput.blur();
    }
  }
 
  /**
   * Opens the dropdown menu
   *
   * @memberof SelectControls
   */
  handleOpenDropdown() {
    if (this.props.multiple) {
      this.focusInput.call(this);
    }
 
    this.props.onFocus();
  }
 
  /**
   * Search / Filter functionality
   *
   * @returns {any} DOM || null
   * @memberof SelectControls
   */
  filterHtml() {
    return this.props.filterable ?
      (
        <input
          ref='filterInput'
          label={this.props.label}
          onKeyDown={this.props.onKeyDown}
          onChange={this.props.onChange}
          value={this.props.filterText}
          className={classNames(css.filter, {
            empty: this.props.isEmpty,
            hidden: !this.props.multiple && !this.props.isActive.array,
            [css.withTags]: this.props.selection.size > 0
          })}
          type='text'
        />
      )
      : null;
  }
 
  /**
   * Shows the list of options
   *
   * @returns {react}
   * @memberof SelectControls
   */
  selectionDisplayHtml() {
    if (this.props.multiple) {
      return (
        <div
          key={1}
          className={css['multi-selection-area']}
          onClick={this.handleOpenDropdown}
        >
          {this.props.selection
            .map(item => (
              <Tag
                key={`ship-select-tag--${item.key || item[this.props.orderOptionsBy]}`}
                icon={item.icon}
                title={item[this.props.orderOptionsBy]}
                // eslint-disable-next-line react/jsx-no-bind
                onClear={this.props.onClear.bind(this, item)}
              />
            ))
          }
          {this.filterHtml.call(this)}
        </div>
      );
    }
 
    return (
      <div
        key={1}
        className={css['selection-area']}
        onClick={this.handleOpenDropdown}
      >
        <span
          className={classNames(
            css.selection,
            {
              empty: this.props.isEmpty,
              hidden: (this.props.filterable && this.props.isActive)
            }
          )}
        >
          {this.props.selection ? this.props.selection.get(this.props.orderOptionsBy) : this.props.label}
        </span>
        {this.filterHtml.call(this)}
      </div>
    );
  }
 
  /**
   * Shows the toggle button
   *
   * @returns {react}
   * @memberof SelectControls
   */
  dropdownToggleHtml() {
    return (
      <div
        key={0}
        className={css['toggle-container']}
      >
        {this.props.toggleSwitch !== false ?
          <button
            className={classNames(css['toggle-btn'], {
              [css.hidden]: this.props.loading,
              [css.darkTheme]: this.props.darkTheme,
              [css.toggleRightPos]: this.props.togglePosition && this.props.togglePosition === 'right'
            })}
            onClick={this.props.onToggle}
          >
            {this.props.toggleSwitch}
          </button>
          : null}
        <Loader
          visible={this.props.loading}
          className={classNames(css.loader, {
            [css.toggleRightPos]: this.props.togglePosition && this.props.togglePosition === 'right'
          })}
          spinnerClassName={css['loader-spinner']}
        />
      </div>
    );
  }
 
  render() {
    let children = [];
    let selectionDisplay = this.selectionDisplayHtml.call(this);
    let toggleBtn = this.dropdownToggleHtml.call(this);
 
    if (this.props.togglePosition === 'right') {
      children[0] = selectionDisplay;
      children[1] = toggleBtn;
    } else {
      children[0] = toggleBtn;
      children[1] = selectionDisplay;
    }
 
    return (
      <div className={css.controls}>
        {children}
      </div>
    );
  }
}
 
// default props
SelectControls.defaultProps = {
  open:          false,
  loading:       false,
  isActive:      false,
  isEmpty:       false,
  multiple:      false,
  filterable:     false,
 
  filterText:      '',
 
  toggleSwitch:   'library_add',
  togglePosition: 'left'
};
 
// prop types checking
SelectControls.propTypes = {
  open:           PropTypes.bool,
  loading:        PropTypes.bool,
  isActive:       PropTypes.bool,
  isEmpty:        PropTypes.bool,
  multiple:       PropTypes.bool,
  filterable:      PropTypes.bool,
  darkTheme:      PropTypes.bool.isRequired,
 
  filterText:      PropTypes.string,
  label:          PropTypes.string.isRequired,
  orderOptionsBy: PropTypes.string.isRequired,
 
  toggleSwitch:   PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
  togglePosition: PropTypes.oneOf(['right', 'left']),
  selection:      PropTypes.instanceOf(Immutable.List).isRequired,
 
  onFocus:        PropTypes.func.isRequired,
  onChange:       PropTypes.func.isRequired,
  onKeyDown:      PropTypes.func.isRequired,
  onClear:        PropTypes.func.isRequired,
  onToggle:       PropTypes.func.isRequired
};