Skip to content

Commit

Permalink
Fix initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Cuevas committed Mar 28, 2016
1 parent cc7da97 commit f3578f2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
18 changes: 12 additions & 6 deletions dist/react-select-google-places.js
Original file line number Diff line number Diff line change
Expand Up @@ -1524,8 +1524,8 @@ var SelectGooglePlaces = React.createClass({
formatName: React.PropTypes.func, // Receives the result placesService.getDetails() and returns a formatted name
// See https://developers.google.com/maps/documentation/javascript/3.exp/reference#PlaceResult
onChange: React.PropTypes.func, // onChange handler: function (newValue) {}
optionsForSelect: React.PropTypes.object // See https://github.com/JedWatson/react-select#further-options

optionsForSelect: React.PropTypes.object, // See https://github.com/JedWatson/react-select#further-options
initialValue: React.PropTypes.any
},

getDefaultProps: function getDefaultProps() {
Expand All @@ -1540,13 +1540,15 @@ var SelectGooglePlaces = React.createClass({
multi: true,
cache: false,
name: 'places'
}
},
onChange: function onChange(value) {},
initialValue: null
};
},

getInitialState: function getInitialState() {
return {
value: null,
value: this.props.initialValue,
autocompleteService: null,
placesService: null
};
Expand Down Expand Up @@ -1605,7 +1607,7 @@ var SelectGooglePlaces = React.createClass({

this.state.placesService.getDetails({ placeId: autocompletePrediction.place_id }, function (placeResult) {
autocompletePrediction = _extends(autocompletePrediction, placeResult);
autocompletePrediction.description = _this3.props.formatName(placeResult);
autocompletePrediction.name = _this3.props.formatName(placeResult);
callback();
});
},
Expand All @@ -1620,6 +1622,10 @@ var SelectGooglePlaces = React.createClass({
}
};
this.state.autocompleteService.getPlacePredictions(geocoderRequest, function (data) {
// Copy description into the name attribute
data.map(function (result) {
result.name = result.description;
});
callback(null, { options: data, complete: false });
});
}
Expand All @@ -1630,7 +1636,7 @@ var SelectGooglePlaces = React.createClass({
return React.createElement(
'div',
null,
React.createElement(_reactSelect2['default'].Async, _extends({ value: this.state.value, valueKey: 'description', labelKey: 'description', loadOptions: this.getPredictions, onChange: this.onChange }, this.props.optionsForSelect)),
React.createElement(_reactSelect2['default'].Async, _extends({ value: this.state.value, valueKey: 'name', labelKey: 'name', loadOptions: this.getPredictions, onChange: this.onChange }, this.props.optionsForSelect)),
React.createElement('div', { ref: 'attributions' })
);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/react-select-google-places.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var App = React.createClass({
return (
<div>
<h2>Demo Single selection</h2>
<SelectGooglePlaces optionsForSelect={{multi: false, cache: false}} onChange={this.onChange} />
<SelectGooglePlaces optionsForSelect={{multi: false, cache: false}} initialValue={{name: 'New York'}} onChange={this.onChange} />
<h2>Demo Multiple selection</h2>
<SelectGooglePlaces optionsForSelect={{multi: true, cache: false}} onChange={this.onChange} />
<SelectGooglePlaces optionsForSelect={{multi: true, cache: false }} initialValue={[{name: 'New York'}, {name: 'Chicago'}]} onChange={this.onChange} />
</div>
);
}
Expand Down
18 changes: 12 additions & 6 deletions lib/SelectGooglePlaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ var SelectGooglePlaces = React.createClass({
formatName: React.PropTypes.func, // Receives the result placesService.getDetails() and returns a formatted name
// See https://developers.google.com/maps/documentation/javascript/3.exp/reference#PlaceResult
onChange: React.PropTypes.func, // onChange handler: function (newValue) {}
optionsForSelect: React.PropTypes.object // See https://github.com/JedWatson/react-select#further-options

optionsForSelect: React.PropTypes.object, // See https://github.com/JedWatson/react-select#further-options
initialValue: React.PropTypes.any
},

getDefaultProps: function getDefaultProps() {
Expand All @@ -46,13 +46,15 @@ var SelectGooglePlaces = React.createClass({
multi: true,
cache: false,
name: 'places'
}
},
onChange: function onChange(value) {},
initialValue: null
};
},

getInitialState: function getInitialState() {
return {
value: null,
value: this.props.initialValue,
autocompleteService: null,
placesService: null
};
Expand Down Expand Up @@ -111,7 +113,7 @@ var SelectGooglePlaces = React.createClass({

this.state.placesService.getDetails({ placeId: autocompletePrediction.place_id }, function (placeResult) {
autocompletePrediction = _extends(autocompletePrediction, placeResult);
autocompletePrediction.description = _this3.props.formatName(placeResult);
autocompletePrediction.name = _this3.props.formatName(placeResult);
callback();
});
},
Expand All @@ -126,6 +128,10 @@ var SelectGooglePlaces = React.createClass({
}
};
this.state.autocompleteService.getPlacePredictions(geocoderRequest, function (data) {
// Copy description into the name attribute
data.map(function (result) {
result.name = result.description;
});
callback(null, { options: data, complete: false });
});
}
Expand All @@ -136,7 +142,7 @@ var SelectGooglePlaces = React.createClass({
return React.createElement(
'div',
null,
React.createElement(_reactSelect2['default'].Async, _extends({ value: this.state.value, valueKey: 'description', labelKey: 'description', loadOptions: this.getPredictions, onChange: this.onChange }, this.props.optionsForSelect)),
React.createElement(_reactSelect2['default'].Async, _extends({ value: this.state.value, valueKey: 'name', labelKey: 'name', loadOptions: this.getPredictions, onChange: this.onChange }, this.props.optionsForSelect)),
React.createElement('div', { ref: 'attributions' })
);
}
Expand Down
18 changes: 12 additions & 6 deletions src/SelectGooglePlaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const SelectGooglePlaces = React.createClass({
formatName: React.PropTypes.func, // Receives the result placesService.getDetails() and returns a formatted name
// See https://developers.google.com/maps/documentation/javascript/3.exp/reference#PlaceResult
onChange: React.PropTypes.func, // onChange handler: function (newValue) {}
optionsForSelect: React.PropTypes.object // See https://github.com/JedWatson/react-select#further-options

optionsForSelect: React.PropTypes.object, // See https://github.com/JedWatson/react-select#further-options
initialValue: React.PropTypes.any
},

getDefaultProps () {
Expand All @@ -31,13 +31,15 @@ const SelectGooglePlaces = React.createClass({
multi: true,
cache: false,
name: 'places'
}
},
onChange: function(value) {},
initialValue: null
};
},

getInitialState () {
return {
value: null,
value: this.props.initialValue,
autocompleteService: null,
placesService: null
};
Expand Down Expand Up @@ -91,7 +93,7 @@ const SelectGooglePlaces = React.createClass({
processPlace (autocompletePrediction, callback) {
this.state.placesService.getDetails({placeId: autocompletePrediction.place_id}, (placeResult) => {
autocompletePrediction = Object.assign(autocompletePrediction, placeResult);
autocompletePrediction.description = this.props.formatName(placeResult);
autocompletePrediction.name = this.props.formatName(placeResult);
callback();
});
},
Expand All @@ -106,6 +108,10 @@ const SelectGooglePlaces = React.createClass({
}
};
this.state.autocompleteService.getPlacePredictions(geocoderRequest, function(data){
// Copy description into the name attribute
data.map(function(result) {
result.name = result.description;
});
callback(null, {options: data, complete: false});
});
}
Expand All @@ -115,7 +121,7 @@ const SelectGooglePlaces = React.createClass({
render () {
return (
<div>
<Select.Async value={this.state.value} valueKey="description" labelKey="description" loadOptions={this.getPredictions} onChange={this.onChange} {...this.props.optionsForSelect} />
<Select.Async value={this.state.value} valueKey="name" labelKey="name" loadOptions={this.getPredictions} onChange={this.onChange} {...this.props.optionsForSelect} />
<div ref="attributions"></div>
</div>
);
Expand Down

0 comments on commit f3578f2

Please sign in to comment.