Place Search Autocomplete
In a maps app, the search box is the front door: people type two or three letters and expect the place they mean to be one keystroke away. It also has to be gentle with the backend, because a request per keystroke from millions of phones is a denial of service the product inflicts on itself.
Build the place search box.
Requirements
- An input labelled
Search places, with the rolecombobox. - When the user stops typing for 300 ms, search with
GET /api/places?q=<text>(the trimmed text, URL-encoded). It answers{ "places": [{ "id", "name", "address" }] }. Typingcoffeein one go sends one request. - With fewer than two characters, nothing is searched and no suggestions are shown.
- While a search is in flight, show
Searching.... - Show the results as a
listboxofoptions, each with the place's name followed by its address. With no results, showNo places found. If the search fails, show an alert:Search is unavailable. Try again. - Keyboard:
ArrowDownandArrowUpmove the highlighted option (stopping at the first and last),Enterpicks the highlighted one andEscapecloses the list. The highlighted option hasaria-selected="true", and the input'saria-activedescendantis that option'sid. The input'saria-expandedsays whether the list is shown. - Clicking an option also picks it. Picking a place puts its name in the input, closes the list and shows
Selected: <name>, <address>, for exampleSelected: Blue Bottle Coffee, 66 Mint St. - Only the latest search counts: if an answer to an older query arrives after a newer one, it must not replace the newer results.
The setup
The files open on a skeleton. Build the behaviour the requirements describe.
- src/main.jsx
- src/App.jsx
- src/styles.css
The preview has no network. fetch is answered by this problem's API:
- GET /api/places
Follow-up: Most searches start with the same few letters. How would you cache results so that typing `cof`, deleting and typing it again costs no request, and how long may such a cache live?
- React with JavaScript and plain CSS; every file is editable. - Follow the ARIA combobox pattern: focus stays in the input while the list is navigated. - Do not search again when a place is picked and its name is put in the input.
- Views
- 2