Skip to content

Commit

Permalink
Feat: Add support for language selection (#113)
Browse files Browse the repository at this point in the history
* feat: add support for language configuration

* feat: update lib files

* feat: bump version to 2.3.0
  • Loading branch information
lacunadream authored May 29, 2021
1 parent 48c119b commit 0ba11a3
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ import Autocomplete from "react-google-autocomplete";

- `defaultValue` prop is used for setting up the default value e.g `defaultValue={'Amsterdam'}`.

- `language`: Set [language](https://developers.google.com/maps/documentation/places/web-service/details#PlaceDetailsRequests) to be used for the results. If not specified, Google defaults to load the most appropriate language based on the users location or browser setting.

You can pass any prop specified for the hmtl [input tag](https://www.w3schools.com/tags/tag_input.asp). You can also set [options.fields](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult) prop if you need extra information, now it defaults to basic data in order to control expenses.

## usePlacesWidget
Expand Down Expand Up @@ -160,6 +162,7 @@ The hook has only one config argument.
- `debounce`: Number of milliseconds to accumulate responses for.
- `options`: Default [options](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#QueryAutocompletionRequest) which will be passed to every request.
- `sessionToken`: If true then a [session token](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteSessionToken) will be attached to every request.
- `language`: If the language code is set, the results will be returned in the specificed [language](https://developers.google.com/maps/documentation/places/web-service/details#PlaceDetailsRequests)

### Returned value

Expand Down
9 changes: 6 additions & 3 deletions lib/ReactGoogleAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ function ReactGoogleAutocomplete(props) {
options = props.options,
googleMapsScriptBaseUrl = props.googleMapsScriptBaseUrl,
refProp = props.refProp,
rest = _objectWithoutProperties(props, ["onPlaceSelected", "apiKey", "inputAutocompleteValue", "options", "googleMapsScriptBaseUrl", "refProp"]);
language = props.language,
rest = _objectWithoutProperties(props, ["onPlaceSelected", "apiKey", "inputAutocompleteValue", "options", "googleMapsScriptBaseUrl", "refProp", "language"]);

var _usePlacesWidget = (0, _usePlacesWidget2.default)({
ref: refProp,
googleMapsScriptBaseUrl: googleMapsScriptBaseUrl,
onPlaceSelected: onPlaceSelected,
apiKey: apiKey,
inputAutocompleteValue: inputAutocompleteValue,
options: options
options: options,
language: language
}),
ref = _usePlacesWidget.ref;

Expand Down Expand Up @@ -68,7 +70,8 @@ ReactGoogleAutocomplete.propTypes = {
radius: _propTypes.default.number,
sessionToken: _propTypes.default.object,
types: _propTypes.default.arrayOf(_propTypes.default.string)
})
}),
language: _propTypes.default.string
};

var _default = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
Expand Down
6 changes: 4 additions & 2 deletions lib/usePlacesAutocompleteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ function usePlacesAutocompleteService(_ref) {
debounce = _ref$debounce === void 0 ? 300 : _ref$debounce,
_ref$options = _ref.options,
options = _ref$options === void 0 ? {} : _ref$options,
sessionToken = _ref.sessionToken;
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?key=").concat(apiKey, "&libraries=places");
sessionToken = _ref.sessionToken,
language = _ref.language;
var languageQueryParam = language ? "&language=".concat(language) : '';
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?key=").concat(apiKey, "&libraries=places").concat(languageQueryParam);

var _useState = (0, _react.useState)([]),
_useState2 = _slicedToArray(_useState, 2),
Expand Down
6 changes: 4 additions & 2 deletions lib/usePlacesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ function usePlacesWidget(props) {
bounds = _props$options.bounds,
options = _objectWithoutProperties(_props$options, ["types", "componentRestrictions", "fields", "bounds"]),
_props$googleMapsScri = props.googleMapsScriptBaseUrl,
googleMapsScriptBaseUrl = _props$googleMapsScri === void 0 ? _constants.GOOGLE_MAP_SCRIPT_BASE_URL : _props$googleMapsScri;
googleMapsScriptBaseUrl = _props$googleMapsScri === void 0 ? _constants.GOOGLE_MAP_SCRIPT_BASE_URL : _props$googleMapsScri,
language = props.language;

var inputRef = (0, _react.useRef)(null);
var event = (0, _react.useRef)(null);
var autocompleteRef = (0, _react.useRef)(null);
var observerHack = (0, _react.useRef)(null);
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?libraries=places&key=").concat(apiKey);
var languageQueryParam = language ? "&language=".concat(language) : '';
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?libraries=places&key=").concat(apiKey).concat(languageQueryParam);
var handleLoadScript = (0, _react.useCallback)(function () {
return (0, _utils.loadGoogleMapScript)(googleMapsScriptBaseUrl, googleMapsScriptUrl);
}, [googleMapsScriptBaseUrl, googleMapsScriptUrl]);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-google-autocomplete",
"version": "2.2.0",
"version": "2.3.0",
"description": "React component for google autocomplete.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/ReactGoogleAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function ReactGoogleAutocomplete(props) {
options,
googleMapsScriptBaseUrl,
refProp,
language,
...rest
} = props;

Expand All @@ -21,6 +22,7 @@ function ReactGoogleAutocomplete(props) {
apiKey,
inputAutocompleteValue,
options,
language
});

return <input ref={ref} {...rest} />;
Expand All @@ -47,6 +49,7 @@ ReactGoogleAutocomplete.propTypes = {
sessionToken: PropTypes.object,
types: PropTypes.arrayOf(PropTypes.string),
}),
language: PropTypes.string,
};

export default forwardRef((props, ref) => (
Expand Down
1 change: 1 addition & 0 deletions src/usePlacesAutocompleteService.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface usePlacesAutocompleteServiceConfig {
debounce?: number;
options?: google.maps.places.QueryAutocompletionRequest;
sessionToken?: boolean;
language?: string;
}

interface usePlacesAutocompleteServiceResponse {
Expand Down
4 changes: 3 additions & 1 deletion src/usePlacesAutocompleteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export default function usePlacesAutocompleteService({
debounce = 300,
options = {},
sessionToken,
language,
}) {
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?key=${apiKey}&libraries=places`;
const languageQueryParam = language ? `&language=${language}` : '';
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?key=${apiKey}&libraries=places${languageQueryParam}`;
const [placePredictions, setPlacePredictions] = useState([]);
const [isPlacePredsLoading, setIsPlacePredsLoading] = useState(false);
const [placeInputValue, setPlaceInputValue] = useState(null);
Expand Down
4 changes: 3 additions & 1 deletion src/usePlacesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export default function usePlacesWidget(props) {
...options
} = {},
googleMapsScriptBaseUrl = GOOGLE_MAP_SCRIPT_BASE_URL,
language,
} = props;
const inputRef = useRef(null);
const event = useRef(null);
const autocompleteRef = useRef(null);
const observerHack = useRef(null);
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?libraries=places&key=${apiKey}`;
const languageQueryParam = language ? `&language=${language}` : '';
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?libraries=places&key=${apiKey}${languageQueryParam}`;

const handleLoadScript = useCallback(
() => loadGoogleMapScript(googleMapsScriptBaseUrl, googleMapsScriptUrl),
Expand Down

0 comments on commit 0ba11a3

Please sign in to comment.