Skip to content

Commit adaf5d6

Browse files
committed
address map search review
1 parent 29f4f70 commit adaf5d6

2 files changed

Lines changed: 31 additions & 39 deletions

File tree

src/components/LocationSelect/LocationSelect.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,16 @@ async function getAreaNameMatch(
143143
longitude: number,
144144
latitude: number
145145
): Promise<AreaNameMatch | null> {
146-
const coordinates = await geocoding.reverse([longitude, latitude]);
146+
try {
147+
const coordinates = await geocoding.reverse([longitude, latitude]);
147148

148-
return getBestAreaNameMatchFromFeatures(
149-
coordinates.features as AreaNameFeature[]
150-
);
149+
return getBestAreaNameMatchFromFeatures(
150+
coordinates.features as AreaNameFeature[]
151+
);
152+
} catch (error) {
153+
console.warn("Could not reverse-geocode selected location:", error);
154+
return null;
155+
}
151156
}
152157

153158
// TODO: use this to build a custom component around the core geocoding API, using my nice own components for input and dropdown
@@ -223,7 +228,6 @@ export default function LocationSelect({
223228
(e: ChangeEvent<HTMLSelectElement>) => {
224229
onLocationInteract?.();
225230
setCountryCode(e.target.value);
226-
console.log("Country changed, focusing input...");
227231
setMapShown(false);
228232
inputRef.current?.focus();
229233
},
@@ -232,12 +236,10 @@ export default function LocationSelect({
232236

233237
const handleDragStart = useCallback(() => {
234238
inputRef.current?.blur(); // Close and blur the input if it's open
235-
console.log("handling drag start");
236239
}, []);
237240

238241
const handleDragEnd = useCallback(
239242
async (event: any) => {
240-
console.log("Drag end. Location:", event.lngLat);
241243
onLocationInteract?.();
242244

243245
const nextCoordinates = {
@@ -264,8 +266,6 @@ export default function LocationSelect({
264266
async (feature: GeocodingFeature) => {
265267
if (!feature.center) return;
266268

267-
// Otherwise continue as normal
268-
console.log("Picked:", feature, feature.center);
269269
onLocationInteract?.();
270270

271271
const nextCoordinates = {
@@ -288,16 +288,9 @@ export default function LocationSelect({
288288
inputRef.current?.blur();
289289

290290
if (!mapShown) {
291-
console.log("Map isnt shown yet, coming now...");
292291
setCoordinates(nextCoordinates);
293292
setMapShown(true);
294293
} else {
295-
console.log(
296-
"Map already shown, flying from",
297-
coordinates,
298-
"to",
299-
nextCoordinates
300-
);
301294
mapRef.current?.flyTo({
302295
center: [nextCoordinates.longitude, nextCoordinates.latitude],
303296
duration: 2800,

src/features/map/components/GeocodingSearch.tsx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const ResultList = styled.ul`
171171
padding: 0;
172172
`;
173173

174-
const ResultOption = styled.button<{ $active?: boolean }>`
174+
const ResultOption = styled.li<{ $active?: boolean }>`
175175
appearance: none;
176176
width: 100%;
177177
border: 0;
@@ -269,7 +269,7 @@ const GeocodingSearch = forwardRef<GeocodingSearchHandle, GeocodingSearchProps>(
269269
: "";
270270
const shouldRenderStatusMessage =
271271
variant === "palette" || !onStatusMessageChange;
272-
const activeFeature = features[activeIndex];
272+
const activeFeature = showResults ? features[activeIndex] : undefined;
273273

274274
useEffect(() => {
275275
onStatusMessageChange?.(statusMessage);
@@ -374,28 +374,27 @@ const GeocodingSearch = forwardRef<GeocodingSearchHandle, GeocodingSearchProps>(
374374
) : null}
375375

376376
{showResults && features.length > 0 ? (
377-
<ResultsPanel $variant={variant} id={listId}>
378-
<ResultList role="listbox">
377+
<ResultsPanel $variant={variant}>
378+
<ResultList id={listId} role="listbox">
379379
{features.map((feature, index) => (
380-
<li key={feature.id}>
381-
<ResultOption
382-
id={`${listId}-option-${index}`}
383-
role="option"
384-
type="button"
385-
$active={index === activeIndex}
386-
aria-selected={index === activeIndex}
387-
onMouseDown={(event) => event.preventDefault()}
388-
onMouseEnter={() => setActiveIndex(index)}
389-
onClick={() => pickFeature(feature)}
390-
>
391-
<ResultPrimary>
392-
{getFeaturePrimaryLabel(feature)}
393-
</ResultPrimary>
394-
<ResultSecondary>
395-
{getFeatureSecondaryLabel(feature)}
396-
</ResultSecondary>
397-
</ResultOption>
398-
</li>
380+
<ResultOption
381+
key={feature.id}
382+
id={`${listId}-option-${index}`}
383+
role="option"
384+
tabIndex={-1}
385+
$active={index === activeIndex}
386+
aria-selected={index === activeIndex}
387+
onMouseDown={(event) => event.preventDefault()}
388+
onMouseEnter={() => setActiveIndex(index)}
389+
onClick={() => pickFeature(feature)}
390+
>
391+
<ResultPrimary>
392+
{getFeaturePrimaryLabel(feature)}
393+
</ResultPrimary>
394+
<ResultSecondary>
395+
{getFeatureSecondaryLabel(feature)}
396+
</ResultSecondary>
397+
</ResultOption>
399398
))}
400399
</ResultList>
401400
</ResultsPanel>

0 commit comments

Comments
 (0)