@@ -18,6 +18,7 @@ export class ReactAutosuggestGeocoder extends React.Component {
1818 } ) ,
1919 bounds : React . PropTypes . array ,
2020
21+ onSearchSelected : React . PropTypes . func ,
2122 onSuggestionSelected : React . PropTypes . func ,
2223 onReverseSelected : React . PropTypes . func ,
2324 getSuggestionValue : React . PropTypes . func . isRequired ,
@@ -59,13 +60,15 @@ export class ReactAutosuggestGeocoder extends React.Component {
5960 this . input = this . autosuggest . input ;
6061 }
6162
62- reverse ( center , bounds ) {
63- const url = this . props . url + '/reverse' ;
63+ queryParameters ( { apiKey, sources, focus, center, bounds } , extra = { } ) {
6464 const data = {
65- api_key : this . props . apiKey ,
66- layers : 'address' ,
67- size : 1
65+ api_key : apiKey ,
66+ sources : sources
6867 } ;
68+ if ( focus ) {
69+ data [ 'focus.point.lat' ] = focus . latitude ;
70+ data [ 'focus.point.lon' ] = focus . longitude ;
71+ }
6972 if ( center ) {
7073 data [ 'point.lat' ] = center . latitude ;
7174 data [ 'point.lon' ] = center . longitude ;
@@ -76,6 +79,20 @@ export class ReactAutosuggestGeocoder extends React.Component {
7679 data [ 'boundary.rect.max_lon' ] = bounds [ 2 ] ;
7780 data [ 'boundary.rect.max_lat' ] = bounds [ 3 ] ;
7881 }
82+ return _ . assign ( { } , data , extra ) ;
83+ }
84+
85+ reverse ( center , bounds ) {
86+ const url = this . props . url + '/reverse' ;
87+ const { apiKey } = this . props ;
88+ const data = this . queryParameters ( {
89+ apiKey,
90+ center,
91+ bounds
92+ } , {
93+ layers : 'address' ,
94+ size : 1
95+ } ) ;
7996 return fetch ( url + '?' + stringify ( data ) , {
8097 method : 'get' ,
8198 headers : {
@@ -87,11 +104,16 @@ export class ReactAutosuggestGeocoder extends React.Component {
87104
88105 search ( text ) {
89106 const url = this . props . url + '/search' ;
90- return fetch ( url + '?' + stringify ( {
91- api_key : this . props . apiKey ,
92- sources : this . props . sources ,
107+ const { apiKey, sources, center, bounds } = this . props ;
108+ const data = this . queryParameters ( {
109+ apiKey,
110+ sources,
111+ focus : center ,
112+ bounds
113+ } , {
93114 text : text
94- } ) , {
115+ } ) ;
116+ return fetch ( url + '?' + stringify ( data ) , {
95117 method : 'get' ,
96118 headers : {
97119 'Accept' : 'application/json' ,
@@ -102,21 +124,15 @@ export class ReactAutosuggestGeocoder extends React.Component {
102124
103125 autocomplete ( text ) {
104126 const url = this . props . url + '/autocomplete' ;
105- const data = {
106- api_key : this . props . apiKey ,
107- sources : this . props . sources ,
127+ const { apiKey, sources, center, bounds } = this . props ;
128+ const data = this . queryParameters ( {
129+ apiKey,
130+ sources,
131+ focus : center ,
132+ bounds
133+ } , {
108134 text : text
109- } ;
110- if ( this . props . center ) {
111- data [ 'focus.point.lat' ] = this . props . center . latitude ;
112- data [ 'focus.point.lon' ] = this . props . center . longitude ;
113- }
114- if ( this . props . bounds ) {
115- data [ 'boundary.rect.min_lon' ] = this . props . bounds [ 0 ] ;
116- data [ 'boundary.rect.min_lat' ] = this . props . bounds [ 1 ] ;
117- data [ 'boundary.rect.max_lon' ] = this . props . bounds [ 2 ] ;
118- data [ 'boundary.rect.max_lat' ] = this . props . bounds [ 3 ] ;
119- }
135+ } ) ;
120136 return fetch ( url + '?' + stringify ( data ) , {
121137 method : 'get' ,
122138 headers : {
@@ -193,14 +209,52 @@ export class ReactAutosuggestGeocoder extends React.Component {
193209 } ;
194210
195211 onSuggestionSelected = ( event , { suggestion, suggestionValue, suggestionIndex, sectionIndex, method } ) => {
212+ if ( this . props . onSuggestionSelected ) {
213+ return this . props . onSuggestionSelected ( event , { suggestion, suggestionValue, suggestionIndex, sectionIndex, method } ) ;
214+ }
215+ } ;
216+
217+ onEnterCapture = ( event ) => {
218+ if ( event . keyCode !== 13 ) {
219+ return ;
220+ }
221+
222+ if ( ! this . autosuggest ) {
223+ return ;
224+ }
225+
226+ if ( this . autosuggest . getHighlightedSuggestion ( ) !== null ) {
227+ return ;
228+ }
229+
230+ return this . onEnterWithoutHighlight ( event ) ;
231+ } ;
232+
233+ onEnterWithoutHighlight = ( event ) => {
234+ let { value } = this . state ;
235+ let suggestionValue = value ;
236+
196237 return this . search ( suggestionValue ) . then ( ( data ) => {
238+ if ( ! data ) {
239+ return ;
240+ }
241+
242+ if ( ! data . features ) {
243+ return ;
244+ }
245+
246+ if ( ! data . features . length ) {
247+ return ;
248+ }
249+
250+ let suggestion = data . features [ 0 ] ;
197251 this . setState ( {
198252 selected : true ,
199- value : suggestionValue
253+ value : suggestion . properties . label
200254 } ) ;
201255
202- if ( this . props . onSuggestionSelected ) {
203- return this . props . onSuggestionSelected ( event , { search : data , suggestion, suggestionValue, suggestionIndex , sectionIndex , method } ) ;
256+ if ( this . props . onSearchSelected ) {
257+ return this . props . onSearchSelected ( event , { search : data , suggestion, suggestionValue, method : 'enter' } ) ;
204258 }
205259 } ) ;
206260 } ;
@@ -215,7 +269,7 @@ export class ReactAutosuggestGeocoder extends React.Component {
215269 fetchDelay,
216270 ...props
217271 } = this . props ;
218- const { onFocus, onBlur, ...restOfInputProps } = ( inputProps || { } ) ;
272+ const { onFocus, onBlur, onKeyDownCapture , ...restOfInputProps } = ( inputProps || { } ) ;
219273
220274 return (
221275 < Autosuggest
@@ -227,7 +281,8 @@ export class ReactAutosuggestGeocoder extends React.Component {
227281 value : value ,
228282 onChange : this . onChange ,
229283 onFocus : e => _ . isFunction ( onFocus ) ? onFocus ( e ) : undefined ,
230- onBlur : e => _ . isFunction ( onBlur ) ? onBlur ( e ) : undefined
284+ onBlur : e => _ . isFunction ( onBlur ) ? onBlur ( e ) : undefined ,
285+ onKeyDownCapture : this . onEnterCapture
231286 } ) }
232287 ref = { ( autosuggestRef ) => {
233288 if ( autosuggestRef !== null ) {
0 commit comments