# All files / ship-components-tag-input/src Controls.js

    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

    30x
    30x

    4x
    4x

    89x

    89x
    88x

    7x

    1x

    89x

    89x
    89x

    89x

    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() {
        Eif (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'
              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.isDropdownOpen.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
              className={css['multi-selection-area']}
              onClick={this.handleOpenDropdown}
            >
              {this.props.invert ? this.filterHtml.call(this) : null}
              {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.props.invert ? null : this.filterHtml.call(this)}
            </div>
          );
        }

        return (
          <div
            className={css['selection-area']}
            onClick={this.props.selection.size === 0 ? this.handleOpenDropdown : null}
          >
            {this.props.invert ? this.filterHtml.call(this) : null}
            <span
              className={classNames(
                css.selection,
                {
                  empty: this.props.isEmpty,
                  hidden: (this.props.filterable && this.props.isDropdownOpen)
                }
              )}
            >
              {this.props.selection && this.props.selection.first() ? this.props.selection.first()[this.props.orderOptionsBy] : this.props.label}
            </span>
            {this.props.invert ? null : this.filterHtml.call(this)}
          </div>
        );
      }

      /**
       * Shows the toggle button
       *
       * @returns {react}
       * @memberof SelectControls
       */
      dropdownToggleHtml() {
        return (
          <div
            className={css['toggle-container']}
          >
            {this.props.toggleSwitch !== false ?
              <button
                tabIndex="-1"
                className={classNames(css['toggle-btn'], {
                  [css.hidden]: this.props.loading,
                  [css.darkTheme]: this.props.darkTheme,
                  [css.toggleRightPos]: 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 selectionDisplay = this.selectionDisplayHtml.call(this);
        let toggleBtn = this.dropdownToggleHtml.call(this);

        return (
          <div
            className={css.controls}
          >
            {this.props.togglePosition === 'right' ? null : toggleBtn}
            {selectionDisplay}
            {this.props.togglePosition === 'right' ? toggleBtn : null}
          </div>
        );
      }
    }

    // default props
    SelectControls.defaultProps = {
      open: false,
      loading: false,
      isDropdownOpen: false,
      isEmpty: false,
      multiple: false,
      filterable: false,
      invert: false,

      label: '',
      filterText: '',

      toggleSwitch: 'library_add',
      togglePosition: 'left'
    };

    // prop types checking
    SelectControls.propTypes = {
      open: PropTypes.bool,
      loading: PropTypes.bool,
      isDropdownOpen: PropTypes.bool,
      isEmpty: PropTypes.bool,
      multiple: PropTypes.bool,
      filterable: PropTypes.bool,
      darkTheme: PropTypes.bool.isRequired,
      invert: PropTypes.bool,

      label: PropTypes.string,
      filterText: PropTypes.string,
      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
    };