# All files / components/SearchBox SearchBox.js

26.67% Statements 4/15
0% Branches 0/4
0% Functions 0/9
26.67% Lines 4/15

    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

    4x
    4x
    4x
    4x

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import Autosuggest from 'react-autosuggest';
    import DefaultInput from '../Input';
    import SearchBoxSuggestion from './Suggestion';
    import SearchBoxSuggestionsList from './styles/SearchBoxSuggestionsList';
    import SearchBoxSuggestionsSectionTitle from './styles/SearchBoxSuggestionsSectionTitle';
    import SearchBoxSearchButton from './styles/SearchBoxSearchButton';
    import SearchBoxWrapper from './styles/SearchBoxWrapper';
    import _ from 'lodash';

    class SearchBox extends React.Component {
      static propTypes = {
        /**
         * An array of suggestions. Suggestions can be any shape you like.
         */
        suggestions: PropTypes.array.isRequired,
        /**
         * Called when the component needs to refresh suggestions. ({ value, reson }) => {}
         */
        onSuggestionsFetchRequested: PropTypes.func.isRequired,
        /**
         * Called when suggestions need to be reset.
         */
        onSuggestionsClearRequested: PropTypes.func.isRequired,
        /**
         * Computes a value from a single suggestion item. (suggestion) => value
         */
        getSuggestionValue: PropTypes.func.isRequired,
        /**
         * Turns a suggestion into a renderable item. Can be a string or a node.
         * (suggestion, filterValue) => node
         */
        renderSuggestionContent: PropTypes.func,
        /**
         * Called when the value changes. (event, { newValue, method }) => {}
         */
        onChange: PropTypes.func.isRequired,
        /**
         * The current value in the search field.
         */
        value: PropTypes.any.isRequired,
        /**
         * Whether suggestions should always be shown
         */
        alwaysRenderSuggestions: PropTypes.bool,
        /**
         * Computes a title from a section object. Has a default (section) => seciton.title
         */
        getSectionTitle: PropTypes.func,
        /**
         * Computes the suggestions for a section. Has a default (section) => section.suggestions
         */
        getSectionSuggestions: PropTypes.func,
        /**
         * Additional props to pass to the input element
         */
        inputProps: PropTypes.object,
        /**
         * Called when the user selects something
         */
        onSuggestionSelected: PropTypes.func,
        /**
         * Invoked with the current input value and the content of a suggestion
         * as parameters. Must return an array [startIndex, endIndex] where
         * the value matches the suggestion content, so that the suggestion
         * knows which part of the string to highlight.
         *
         * Example, matchSuggestionContent('rg', 'corgi') = [2,4]
         *
         * The default for this will match case-insensitive
         */
        matchSuggestionContent: PropTypes.func,
        /**
         * Whether to render a button for submitting the search
         */
        showSubmitButton: PropTypes.bool,
        /**
         * Called when the user activates the search button
         */
        onSubmit: PropTypes.func,
        /**
         * Passed the current search value, calculates whether the submit button
         * should be enabled.
         */
        shouldShowSubmitButton: PropTypes.func,
        /**
         * A component to render a wrapper around the box
         */
        Wrapper: PropTypes.func,
        /**
         * A component to render the list of suggestions
         */
        SuggestionsList: PropTypes.func,
        /**
         * A component to render a search button integrated into the field
         */
        SearchButton: PropTypes.func,
        /**
         * A component to render a section title in the suggestions list
         */
        SuggestionsSectionTitle: PropTypes.func,
        /**
         * A component to render a suggestion item
         */
        Suggestion: PropTypes.func,
        /**
         * A component to render the input element (defaults Input)
         */
        Input: PropTypes.func,
      };

      static defaultProps = {
        renderSuggestionContent: _.identity,
        alwaysRenderSuggestions: false,
        getSectionTitle: sec => sec.title,
        getSectionSuggestions: sec => sec.suggestions,
        inputProps: {},
        onSuggestionSelected: null,
        matchSuggestionContent: undefined,
        showSubmitButton: false,
        onSubmit: () => null,
        shouldShowSubmitButton: () => false,
        Wrapper: SearchBoxWrapper,
        SuggestionsList: SearchBoxSuggestionsList,
        SuggestionsSectionTitle: SearchBoxSuggestionsSectionTitle,
        SearchButton: SearchBoxSearchButton,
        Suggestion: SearchBoxSuggestion,
        Input: DefaultInput,
      };

      renderInput = ({ ref, ...inputProps }) => {
        const self = this;
        return <this.props.Input {...inputProps} inputRef={ref} />;
      };

      renderSuggestion = (suggestion, { query, isHighlighted }) => (
        <this.props.Suggestion
          query={query}
          isHighlighted={isHighlighted}
          matchSuggestionContent={this.props.matchSuggestionContent}
        >
          {this.props.renderSuggestionContent(suggestion, query)}
        </this.props.Suggestion>
      );

      renderSuggestionsContainer = ({ containerProps, children }) =>
        children ? (
          <this.props.SuggestionsList {...containerProps}>
            {children}
          </this.props.SuggestionsList>
        ) : null;

      renderSectionTitle = section => (
        <this.props.SuggestionsSectionTitle>
          {this.props.getSectionTitle(section)}
        </this.props.SuggestionsSectionTitle>
      );

      render() {
        const {
          inputProps,
          onChange,
          value,
          showSubmitButton,
          onSubmit,
          shouldShowSubmitButton,
          Wrapper,
          SearchButton,
        } = this.props;

        return (
          <Wrapper>
            <Autosuggest
              {...this.props}
              inputProps={{
                ...inputProps,
                onChange,
                value,
              }}
              renderInputComponent={this.renderInput}
              renderSuggestion={this.renderSuggestion}
              renderSuggestionsContainer={this.renderSuggestionsContainer}
              renderSectionTitle={this.renderSectionTitle}
            />
            {/* Using the button as a glyph if submit is disabled */
            showSubmitButton ? (
              <SearchButton
                onClick={onSubmit}
                disabled={!shouldShowSubmitButton(value)}
              />
            ) : (
              <SearchButton disabled />
            )}
          </Wrapper>
        );
      }
    }

    SearchBox.Suggestion = SearchBoxSuggestion;
    SearchBox.SuggestionsList = SearchBoxSuggestionsList;
    SearchBox.SuggestionsSectionTitle = SearchBoxSuggestionsSectionTitle;
    SearchBox.Button = SearchBoxSearchButton;

    export default SearchBox;