7
7
8
8
import React , { useReducer } from 'react' ;
9
9
import PropTypes from 'prop-types' ;
10
+ import Autosuggest from 'react-autosuggest' ;
10
11
import { SearchTypeaheadAPI } from '@ibmdotcom/services' ;
11
12
import { escapeRegExp } from '@ibmdotcom/utilities' ;
12
- import { MastheadSearchInput , MastheadSearchSuggestion } from './' ;
13
-
13
+ import MastheadSearchInput from './MastheadSearchInput ' ;
14
+ import MastheadSearchSuggestion from './MastheadSearchSuggestion' ;
14
15
15
16
/**
16
17
* Converts the string to lower case and trims extra white space
@@ -26,7 +27,7 @@ const _trimAndLower = valueString => valueString.toLowerCase().trim();
26
27
* @param {object } suggestion The individual object from the data
27
28
* @returns {* } The name val
28
29
*/
29
- const _getSuggestionValue = suggestion => suggestion . name ;
30
+ const _getSuggestionValue = suggestion => suggestion [ 0 ] ;
30
31
31
32
/**
32
33
* Initial state of the autocomplete component
@@ -52,17 +53,19 @@ const _initialState = {
52
53
function _reducer ( state , action ) {
53
54
switch ( action . type ) {
54
55
case 'setVal' :
55
- return Object . assign ( { } , state , { val : action . payload . val } ) ;
56
+ return Object . assign ( { } , state , { val : action . payload . val } ) ;
56
57
case 'emptySuggestions' :
57
- return Object . assign ( { } , state , { suggestions : [ ] } ) ;
58
+ return Object . assign ( { } , state , { suggestions : [ ] } ) ;
58
59
case 'setPrevSuggestions' :
59
- return Object . assign ( { } , state , { prevSuggestions : action . payload . prevSuggestions } ) ;
60
+ return Object . assign ( { } , state , {
61
+ prevSuggestions : action . payload . prevSuggestions ,
62
+ } ) ;
60
63
case 'setSuggestionsToPrevious' :
61
- return Object . assign ( { } , state , { suggestions : state . prevSuggestions } ) ;
64
+ return Object . assign ( { } , state , { suggestions : state . prevSuggestions } ) ;
62
65
case 'showSuggestionsContainer' :
63
- return Object . assign ( { } , state , { suggestionContainerVisible : true } ) ;
66
+ return Object . assign ( { } , state , { suggestionContainerVisible : true } ) ;
64
67
case 'hideSuggestionsContainer' :
65
- return Object . assign ( { } , state , { suggestionContainerVisible : false } ) ;
68
+ return Object . assign ( { } , state , { suggestionContainerVisible : false } ) ;
66
69
default :
67
70
return state ;
68
71
}
@@ -76,15 +79,12 @@ function _reducer(state, action) {
76
79
* http://react-autosuggest.js.org/
77
80
* https://github.com/moroshko/react-autosuggest
78
81
*
79
- * @param {string } placeHolderText Placeholder text for the search field
80
- * @param {number } renderValue Number of characters to begin showing suggestions
81
- * @param {Function } getSuggestionValue Function for getting the suggestion value
82
+ * @param {object } props Incoming props
83
+ * @param {string } props.placeHolderText Placeholder text for the search field
84
+ * @param {number } props.renderValue Number of characters to begin showing suggestions
82
85
* @class
83
86
*/
84
- const MastheadSearch = ( {
85
- placeHolderText,
86
- renderValue,
87
- } ) => {
87
+ const MastheadSearch = ( { placeHolderText, renderValue } ) => {
88
88
const [ state , dispatch ] = useReducer ( _reducer , _initialState ) ;
89
89
90
90
/**
@@ -94,7 +94,7 @@ const MastheadSearch = ({
94
94
* @param {string } newValue The new val of the input
95
95
*/
96
96
function onChange ( event , { newValue } ) {
97
- dispatch ( { type : 'setVal' , payload : { val : newValue } } ) ;
97
+ dispatch ( { type : 'setVal' , payload : { val : newValue } } ) ;
98
98
}
99
99
100
100
/**
@@ -106,8 +106,12 @@ const MastheadSearch = ({
106
106
placeholder : placeHolderText ,
107
107
value : state . val ,
108
108
onChange,
109
- onFocus : ( e ) => { e . target . placeholder = '' ; } ,
110
- onBlur : ( e ) => { e . target . placeholder = placeHolderText ; } ,
109
+ onFocus : e => {
110
+ e . target . placeholder = '' ;
111
+ } ,
112
+ onBlur : e => {
113
+ e . target . placeholder = placeHolderText ;
114
+ } ,
111
115
} ;
112
116
113
117
/**
@@ -128,12 +132,13 @@ const MastheadSearch = ({
128
132
/**
129
133
* Renders the Suggestion Value with the function for the adding the suggestion
130
134
*
131
- * @param {object } suggestion The suggestion
132
- * @param {string } query The query being searched for
133
- * @param {boolean } isHighlighted Whether the suggestion is currently highlighted by the user
135
+ * @param {object } suggestion The suggestion to render
136
+ * @param {object } properties The property object of the incoming suggestion
137
+ * @param {string } properties.query The query being searched for
138
+ * @param {boolean } properties.isHighlighted Whether the suggestion is currently highlighted by the user
134
139
* @returns {* } The suggestion value
135
140
*/
136
- function renderSuggestionValue ( suggestion , { query, isHighlighted} ) {
141
+ function renderSuggestion ( suggestion , { query, isHighlighted } ) {
137
142
return (
138
143
< MastheadSearchSuggestion
139
144
suggestion = { suggestion }
@@ -156,27 +161,35 @@ const MastheadSearch = ({
156
161
* @param {string } request.value the current value of the input
157
162
* @param {string } request.reason string describing why onSuggestionsFetchRequested was called
158
163
*/
159
- function onSuggestionsFetchRequest ( request ) {
164
+ async function onSuggestionsFetchRequest ( request ) {
160
165
const searchValue = _trimAndLower ( escapeRegExp ( request . value ) ) ;
161
166
162
- if ( request . reason === 'input-changed' ) { // if the search input has changed
163
- SearchTypeaheadAPI . getResults ( searchValue ) . then ( ( response ) => {
164
- dispatch ( { type : 'setPrevSuggestions' , payload : { prevSuggestions : response } } ) ;
165
- dispatch ( { type : 'setSuggestionsToPrevious' } ) ;
166
- dispatch ( { type : 'showSuggestionsContainer' } ) ;
167
- } ) ;
167
+ if ( request . reason === 'input-changed' ) {
168
+ // if the search input has changed
169
+ let response = await SearchTypeaheadAPI . getResults ( searchValue ) ;
170
+ console . log ( 'response' , response ) ;
171
+
172
+ if ( response !== undefined ) {
173
+ console . log ( 'response' , response ) ;
174
+ dispatch ( {
175
+ type : 'setPrevSuggestions' ,
176
+ payload : { prevSuggestions : response } ,
177
+ } ) ;
178
+ dispatch ( { type : 'setSuggestionsToPrevious' } ) ;
179
+ dispatch ( { type : 'showSuggestionsContainer' } ) ;
180
+ }
168
181
} else {
169
- dispatch ( { type : 'setSuggestionsToPrevious' } ) ;
170
- dispatch ( { type : 'showSuggestionsContainer' } ) ;
182
+ dispatch ( { type : 'setSuggestionsToPrevious' } ) ;
183
+ dispatch ( { type : 'showSuggestionsContainer' } ) ;
171
184
}
172
185
}
173
186
174
187
/**
175
188
* Called every time we clear suggestions
176
189
*/
177
190
function onSuggestionsClearedRequested ( ) {
178
- dispatch ( { type : 'emptySuggestions' } ) ;
179
- dispatch ( { type : 'hideSuggestionsContainer' } ) ;
191
+ dispatch ( { type : 'emptySuggestions' } ) ;
192
+ dispatch ( { type : 'hideSuggestionsContainer' } ) ;
180
193
}
181
194
182
195
/**
@@ -190,15 +203,13 @@ const MastheadSearch = ({
190
203
}
191
204
192
205
return (
193
- < div
194
- data-autoid = "masthead__search"
195
- >
206
+ < div data-autoid = "masthead__search" >
196
207
< Autosuggest
197
208
suggestions = { state . suggestions } // The state value of suggestion
198
209
onSuggestionsFetchRequested = { onSuggestionsFetchRequest } // Method to fetch data (should be async call)
199
210
onSuggestionsClearRequested = { onSuggestionsClearedRequested } // When input bar loses focus
200
211
getSuggestionValue = { _getSuggestionValue } // Name of suggestion
201
- renderSuggestion = { renderSuggestionValue } // How to display a suggestion
212
+ renderSuggestion = { renderSuggestion } // How to display a suggestion
202
213
onSuggestionSelected = { null } // When a suggestion is selected
203
214
highlightFirstSuggestion // First suggestion is highlighted by default
204
215
inputProps = { inputProps }
@@ -212,16 +223,16 @@ const MastheadSearch = ({
212
223
/**
213
224
* @property propTypes
214
225
* @description Defined property types for component
215
- * @type {{items: * } }
226
+ * @type {{placeHolderText: shim, renderValue: shim } }
216
227
*/
217
228
MastheadSearch . propTypes = {
218
229
placeHolderText : PropTypes . string ,
219
230
renderValue : PropTypes . number ,
220
231
} ;
221
232
222
233
/**
223
- *
224
- * @type {{setSuggestionValue: (function(*=): {name: *}), placeHolderText: string, renderValue: number} }
234
+ * @property defaultProps
235
+ * @type {{placeHolderText: string, renderValue: number} }
225
236
*/
226
237
MastheadSearch . defaultProps = {
227
238
placeHolderText : '' ,
0 commit comments