# All files / components/Select Select.js

53.85% Statements 7/13
28.57% Branches 4/14
60% Functions 3/5
54.55% Lines 6/11

    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
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244

    13x

    13x

    13x
    13x
    13x

    13x

     import React from 'react';
    import PropTypes from 'prop-types';
    import LibSelect from 'react-select';
    import arrowImage from './arrow.png';
    import theme from '../../theme';
    import _ from 'lodash';
    import selectOptionPrimaryValue from '../../extensions/selectItemPrimaryValue';
    import Loader from '../Loader';
    import SelectWrapper from './styles/SelectWrapper';

    class Select extends React.Component {
      static propTypes = {
        /**
         * Whether to blur the component upon selection
         */
        autoBlur: PropTypes.bool,
        /**
         * Whether to focus the component on mount
         */
        autoFocus: PropTypes.bool,
        /**
         * Whether to close the menu when a value is selected
         */
        closeOnSelect: PropTypes.bool,
        /**
         * Whether pressing backspace will clear a value.
         */
        deleteRemoves: PropTypes.bool,
        /**
         * delimiter to use to join multiple values for the hidden field value
         */
        delimiter: PropTypes.string,
        /**
         * method to filter a single option (option, filterString)
         */
        filterOption: PropTypes.func,
        /**
         * boolean to enable default filtering or function to filter the options array ([options], filterString, [values])
         */
        filterOptions: PropTypes.func,
        /**
         * whether to strip diacritics when filtering
         */
        ignoreAccents: PropTypes.bool,
        /**
         * whether to perform case-insensitive filtering
         */
        ignoreCase: PropTypes.bool,
        /**
         * custom attributes for the Input
         */
        inputProps: PropTypes.object,
        /**
         * controls whether the component renders in a loading state
         */
        isLoading: PropTypes.bool,
        /**
         * Enables multi-select mode
         */
        multi: PropTypes.bool,
        /**
         * The name to apply to the input field
         */
        name: PropTypes.string,
        /**
         * Handler for blur events on the input
         */
        onBlur: PropTypes.func,
        /**
         * Handler for closing the options list
         */
        onClose: PropTypes.func,
        /**
         * Handler for focus events on the input
         */
        onFocus: PropTypes.func,
        /**
         * Handles raw input change events
         */
        onInputChange: PropTypes.func,
        /**
         * Handles raw input key down events
         */
        onInputKeyDown: PropTypes.func,
        /**
         * Handler for opening the options list
         */
        onOpen: PropTypes.func,
        /**
         * Handler for when an option is selected (value, event) => {}
         */
        onValueClick: PropTypes.func,
        /**
         * Text or node to show if none is selected
         */
        placeholder: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
        /**
         * Adds a class name to the element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the element.
         */
        id: PropTypes.string,
        /**
         * Indicates whether the user can interact with this field.
         */
        disabled: PropTypes.bool,
        /**
         * Indicates whether this field is required for form submission. Non-required fields allow none.
         */
        required: PropTypes.bool,
        /**
         * A list of selection options. Can be complex objects.
         */
        options: PropTypes.array.isRequired,
        /**
         * A function which takes an option and renders text for the select. Has
         * a sane default.
         */
        renderOption: PropTypes.func,
        /**
         * A function which takes an option and computes a single string value to
         * represent it. Has a sane default.
         */
        getOptionValue: PropTypes.func,
        /**
         * The currently selected value.
         */
        value: PropTypes.any,
        /**
         * Handler for change events on the select. It will be called with the new value
         * computed from getOptionValue.
         */
        onChange: PropTypes.func.isRequired,
        /**
         * Whether to enable searching to filter to the desired item.
         */
        searchable: PropTypes.bool,
        /**
         * Alias for isLoading
         */
        loading: PropTypes.bool,
        /**
         * DEPRECATED: use getOptionValue
         */
        selectOptionValue: PropTypes.func,
        /**
         * DEPRECATED: use required to prevent None, or omit it to allow None
         */
        allowNone: PropTypes.bool,
        /**
         * DEPRECATED: use placeholder
         */
        noneText: PropTypes.string,
        /**
         * A component that wraps the library select component, letting you
         * change styles.
         */
        Wrapper: PropTypes.func,
      };

      static defaultProps = {
        className: null,
        id: null,
        disabled: false,
        required: false,
        allowNone: false,
        placeholder: 'None',
        renderOption: selectOptionPrimaryValue,
        getOptionValue: selectOptionPrimaryValue,
        searchable: false,
        Wrapper: SelectWrapper,
      };

      /**
       * Takes the options provided and converts them into
       * react-select options using user-provided conversion
       * functions to generate a displayed label and a backing value
       *
       * @memberof Select
       */
      convertOptions = () => {
        const {
          options,
          renderOption,
          getOptionValue,
          selectOptionValue,
        } = this.props;

        return options.map(opt => ({
          label: renderOption(opt),
          value: (getOptionValue || selectOptionValue)(opt),
        }));
      };

      /**
       * Intercepts the change event and calls the onChange prop
       * with the value alone.
       *
       * @memberof Select
       */
      handleChange = newValue => {
        if (!newValue) {
          this.props.onChange(newValue);
        } else if (this.props.multi) {
          this.props.onChange(newValue.map(item => item.value));
        } else {
          this.props.onChange(newValue.value);
        }
      };

      render() {
        const {
          required,
          placeholder,
          noneText,
          allowNone,
          isLoading,
          loading,
          Wrapper,
        } = this.props;
        const combinedLoading = loading || isLoading;
        const combinedPlaceholder = placeholder || noneText;

        return (
          <Wrapper>
            <LibSelect
              {...this.props}
              options={this.convertOptions()}
              onChange={this.handleChange}
              clearable={!required || allowNone}
              placeholder={
                combinedLoading ? <Loader size="14px" /> : combinedPlaceholder
              }
              isLoading={combinedLoading}
            />
          </Wrapper>
        );
      }
    }

    export default Select;