Skip to content

feat: Migrate and modernize React Place Autocomplete sample - #1602

Open
willum070 wants to merge 16 commits into
mainfrom
migrate-rgm-autocomplete
Open

feat: Migrate and modernize React Place Autocomplete sample#1602
willum070 wants to merge 16 commits into
mainfrom
migrate-rgm-autocomplete

Conversation

@willum070

@willum070 willum070 commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR overhauls the rgm-autocomplete sample to use the modern Google Maps Places API and the new <gmp-place-autocomplete> Web Component. It serves as a definitive guide for developers on how to correctly integrate the new Places API and Web Components within a React (@vis.gl/react-google-maps) environment while maintaining strict type safety.

Key Changes

  • Modern Web Component Integration: Replaced the legacy Autocomplete widget with the modern PlaceAutocompleteElement via useMapsLibrary('places').
  • Programmatic Instantiation: Instantiated the Web Component programmatically via a React ref to reliably capture custom gmp-select events, avoiding React's synthetic event system quirks with Custom Elements.
  • New Place Class Usage: Demonstrated the core design principle of the new API by capturing the PlacePredictionSelectEvent, calling placePrediction.toPlace(), and explicitly requesting data via fetchFields() to ensure users only request the data they need.
  • Location Restriction Fix: Manually synced the map's viewport to the element's locationRestriction property. Crucially, this uses .toJSON() (e.g., map.getBounds().toJSON()) to safely bypass React cross-context instanceof object limitations.
  • Strict Type Safety: Resolved all @typescript-eslint complaints, including correctly typing custom event listeners, avoiding floating promises on fetchFields(), and strictly typing catch blocks.
  • Documentation Expansion: Considerably expanded the README.md to act as a comprehensive guide for developers, specifically calling out common "gotchas" when bridging React and the new Places API.
  • Memory leak prevention: Demonstrates correct cleanup using google.maps.event.removeListener(listener). This prevents "zombie" event listeners from persisting when the component remounts.

Testing

  • Verified all TypeScript and ESLint checks pass (npm run build).
  • Verified the sample successfully runs locally (npm start), successfully biases predictions to the map's current bounds, and correctly fetches Place details/geometry upon selection.

Change-Id: Ibd4d5bc42aa695d03d54b69ad16cd21e1fc64ea8
Change-Id: I0ef57901fd67ae7152217516f956e4b59b9207e3
Change-Id: I0cce1dda39c055dfbac6ccce8958f4f4d226562f
Change-Id: Ifff9d52a47f7b405a4c2b645c2eb600a47b2354c
Change-Id: Ifb8641ba611c95d5d3232e123b3431e56ff54cdd
…es docs

Change-Id: Id19072e7159ffef4df6cd9a5a8026a3ed1af8ae0
Change-Id: Ie115a8493247cde3fc3231221876f3b176e8c859
Change-Id: Ic29906ff21ab6322d4577c87c66e7ff18e1fddcf
Change-Id: I081ccefcc4ba6e960216ebf4f70734052f92aea6
Change-Id: I76e65cbd7110c633d1e623ab54b4f28e95cf2f7a
Change-Id: Icf6ae6e266fd7d5939ef7975e6f4a148ef0159b1
Change-Id: I8a86b395397d95b6b75e82ae48779eb978454213
Change-Id: Ib0e14557304be28023be5d45284b8c7d3dbabe6a
Change-Id: I93bf9f4c2b98d2ea6669b16e550f4e49427cf0c1
@snippet-bot

snippet-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 5 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Change-Id: Ic4d6705f0b11ce28b60ee4cd589bd56a1da6e9c3
Change-Id: I4acaa1da9fc41b16dd990e8cf77fd78cf0e8f080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants