Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor example code in README.md #408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,28 @@ import PlacesAutocomplete, {
class LocationSearchInput extends React.Component {
constructor(props) {
super(props);
this.state = { address: '' };
this.state = { address: '', showDropdown: false };
}

handleChange = address => {
this.setState({ address });
this.setState((prevState) => ({ ...prevState, address }));
};

handleSelect = address => {
this.handleChange(address);

// example usage
geocodeByAddress(address)
.then(results => getLatLng(results[0]))
.then(latLng => console.log('Success', latLng))
.catch(error => console.error('Error', error));

// this.props.onSelect(address);
};

render() {
const shouldShowDropdown = this.state.showDropdown && this.state.address;

return (
<PlacesAutocomplete
value={this.state.address}
Expand All @@ -98,10 +105,22 @@ class LocationSearchInput extends React.Component {
{...getInputProps({
placeholder: 'Search Places ...',
className: 'location-search-input',
onFocus: () => {
this.setState((s) => ({ ...s, showDropdown: true }));
},
onBlur: () => {
this.setState((s) => ({ ...s, showDropdown: false }));
},
})}
/>
<div className="autocomplete-dropdown-container">
<div
className="autocomplete-dropdown-container"
style={{
...(shouldShowDropdown && suggestions?.length > 0 ? {} : { display: 'none' }),
}}
>
{loading && <div>Loading...</div>}

{suggestions.map(suggestion => {
const className = suggestion.active
? 'suggestion-item--active'
Expand All @@ -110,6 +129,7 @@ class LocationSearchInput extends React.Component {
const style = suggestion.active
? { backgroundColor: '#fafafa', cursor: 'pointer' }
: { backgroundColor: '#ffffff', cursor: 'pointer' };

return (
<div
{...getSuggestionItemProps(suggestion, {
Expand Down