2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 import React, {Component} from 'react';
24 import {Button} from 'react-bootstrap';
25 import AutoSuggest from 'react-autosuggest';
26 import Highlighter from 'react-highlight-words';
27 import debounce from 'lodash.debounce';
28 import {ButtonGroup} from 'react-bootstrap';
29 import {Link} from 'react-router-dom';
31 import {changeUrlAddress} from 'utils/Routes.js';
38 SEARCH_PLACEHOLDER_TEXT
39 } from './AutoCompleteSearchBarConstants.js';
41 export default class AutoCompleteSearchBar extends Component {
43 value: React.PropTypes.string,
44 suggestions: React.PropTypes.array,
45 cachedSuggestions: React.PropTypes.array,
46 suggestionName: React.PropTypes.string
49 componentWillMount() {
50 this.debouncedLoadSuggestions =
51 debounce(this.props.onSuggestionsFetchRequested, SEARCH_DEBOUNCE_TIME);
54 onSuggestionsFetchRequested = ({value}) => {
55 this.debouncedLoadSuggestions({value});
58 isValidSearch(value) {
59 return (value && value !== NO_MATCHES_FOUND);
62 isValidSuggestionObject(suggestionObj) {
63 return (suggestionObj &&
64 Object.keys(suggestionObj).length > 0 &&
65 this.isValidSearch(suggestionObj.text));
68 getSelectedSuggestionObj(value, cachedSuggestions) {
69 let matchesSuggestion = {};
71 if (this.isValidSearch(value)) {
72 for (let suggestionIndex in cachedSuggestions) {
73 if (cachedSuggestions[suggestionIndex].text === value) {
74 matchesSuggestion = cachedSuggestions[suggestionIndex];
80 return matchesSuggestion;
84 cachedSuggestion, invalidSearchCallback, dispatchAnalytics, value) {
85 if (this.isValidSuggestionObject(cachedSuggestion)) {
86 changeUrlAddress(cachedSuggestion, this.props.history);
87 //Call analytics if defined
88 if (dispatchAnalytics) {
92 invalidSearchCallback(value);
99 suggestionName, cachedSuggestions,
100 onInputChange, onInvalidSearch,
101 onClearSuggestionsTextFieldRequested,
102 onSuggestionsClearRequested,
106 placeholder: SEARCH_PLACEHOLDER_TEXT,
108 onChange: onInputChange
111 let clearButtonClass = (value.length > 0)
112 ? 'auto-complete-clear-button'
113 : 'auto-complete-clear-button hidden';
115 <div className='auto-complete-search'>
117 suggestions={suggestions}
118 getSuggestionValue={suggestion => suggestion[suggestionName]}
119 onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
120 onSuggestionsClearRequested={onSuggestionsClearRequested}
121 onSuggestionSelected={(event, {suggestion}) => {
122 this.newSearchSelected(suggestion, onInvalidSearch, dispatchAnalytics, value);
124 renderSuggestion={this.renderSuggestion}
125 inputProps={inputProps}
126 focusFirstSuggestion={false}
127 renderSuggestionsContainer={this.renderSuggestionsContainer}/>
128 <ButtonGroup className='auto-complete-search-button-group'>
129 <Button type='submit' className={clearButtonClass}
130 onClick={onClearSuggestionsTextFieldRequested}>
131 <i className={ICON_CLASS_CLEAR} aria-hidden='true'/>
134 <Button type='submit' className='auto-complete-search-button' onClick={() => {
135 this.newSearchSelected(this.getSelectedSuggestionObj(value, cachedSuggestions),
136 onInvalidSearch, dispatchAnalytics, value);
138 <i className={ICON_CLASS_SEARCH} aria-hidden='true'/>
145 renderSuggestion(suggestion, {query}) {
146 let toHighLightArray = query.split(' ');
147 let suggestionTextArray = suggestion.text.split(' ');
150 if (suggestion.text !== NO_MATCHES_FOUND) {
151 // render the suggestion as a clickable link
153 <div className='suggestionFlexContainer'>
154 <span key={'sugSpan1'}
155 className='suggestionColumnTwo'>
156 <Link style={{textDecoration: 'none'}}
157 to={'/' + suggestion.route + '/' + suggestion.hashId}
159 {suggestionTextArray.map(
162 <span key={arrayIndex + 'sugSpan3'}>
163 <Highlighter key={arrayIndex + 'high'}
164 highlightClassName='highlight'
165 searchWords={toHighLightArray}
166 textToHighlight={suggestionTextArray[arrayIndex]}/>
167 { ++arrayIndex ? ' ' : ' '}
175 // render the suggestion as plain text
177 <div className='suggestionFlexContainer'>
178 <span key={'sugSpan1'}
179 className='suggestionColumnTwo'>
180 {suggestionTextArray.map(
183 <span key={arrayIndex + 'sugSpan3'}>
184 <Highlighter key={arrayIndex + 'high'}
185 highlightClassName='highlight'
186 searchWords={toHighLightArray}
187 textToHighlight={suggestionTextArray[arrayIndex]}/>
188 { ++arrayIndex ? ' ' : ' '}
198 renderSuggestionsContainer({children, ...rest}) {
199 if (children !== null && children.props.items.length < 5) {
200 rest.className = 'react-autosuggest__suggestions-containerCopy';