Created
June 9, 2017 15:52
-
-
Save iegik/65fa034bf027980b7981e1ffe9ed5244 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Autocomplete Input for Google Places API | |
* @flow | |
*/ | |
import React, {Component} from 'react'; | |
import { Input } from 'native-base'; | |
import {get} from '../utils/request'; | |
class GooglePlacesAutocomplete extends Component { | |
state = { | |
text: this.props.value | |
}; | |
/** | |
* @returns {Promise.<*>} | |
*/ | |
async _request(data) { | |
try { | |
return await get('https://maps.googleapis.com/maps/api/place/autocomplete/json', data); | |
} catch (e) { | |
return e | |
} | |
} | |
async _onChangeText(text) { | |
const {onChangeData, onSuggestionError} = this.props; | |
try { | |
let response = await this._request({input: text, ...this.props.query}); | |
if(onChangeData){ | |
onChangeData(response.predictions); | |
} | |
} catch (e) { | |
if(onSuggestionError) { | |
onSuggestionError(e) | |
} | |
} | |
} | |
async _handleChangeText(text) { | |
this._onChangeText(text).catch(() => {}); | |
const {onChangeText} = this.props; | |
if (onChangeText) { | |
onChangeText(text); | |
} | |
} | |
render = () => ( | |
<Input {...{ | |
...this.props, | |
...this.state, | |
onChangeText: text => this._handleChangeText(text), | |
value: this.state.text, | |
}} /> | |
); | |
} | |
GooglePlacesAutocomplete.defaultProps = { | |
query: { | |
key: 'missing api key', | |
language: 'en', | |
types: 'geocode', | |
}, | |
data: [] | |
}; | |
export default GooglePlacesAutocomplete; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment