From de06056596c0ededd811cc76100747ef33d95c0d Mon Sep 17 00:00:00 2001 From: zv Date: Sun, 14 Jun 2026 10:26:40 +0200 Subject: [PATCH] feat: select first location suggestion on enter --- components/weather/location-search.tsx | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/components/weather/location-search.tsx b/components/weather/location-search.tsx index 6ce9888..ad5bd5a 100644 --- a/components/weather/location-search.tsx +++ b/components/weather/location-search.tsx @@ -1,12 +1,13 @@ "use client"; -import { useMemo, useState } from "react"; +import { useMemo, useState, type KeyboardEvent } from "react"; import { LoaderCircle, MapPin, Search, X } from "lucide-react"; import { useLocationSearch } from "@/hooks/use-location-search"; import { useWeatherStore } from "@/lib/store"; import { findNearestSynopStation, locateSynopStations } from "@/lib/location-utils"; import { useI18n } from "@/lib/i18n"; import type { MeteoStationPosition, SynopStation } from "@/types/imgw"; +import type { SelectedLocation } from "@/types/location"; import { CurrentLocationControl } from "@/components/weather/current-location-control"; export function LocationSearch({ stations, positions }: { stations: SynopStation[]; positions: MeteoStationPosition[] }) { @@ -24,6 +25,19 @@ export function LocationSearch({ stations, positions }: { stations: SynopStation const showSuggestions = isFocused && query.trim().length >= 2; const isPreparingStations = positions.length === 0; + function chooseLocation(location: SelectedLocation) { + selectLocation(location); + setQuery(""); + setIsFocused(false); + } + + function handleSearchKeyDown(event: KeyboardEvent) { + const firstSuggestion = suggestions[0]?.nearest; + if (event.key !== "Enter" || !showSuggestions || !firstSuggestion) return; + event.preventDefault(); + chooseLocation(firstSuggestion); + } + return (
@@ -38,6 +52,7 @@ export function LocationSearch({ stations, positions }: { stations: SynopStation setQuery(event.target.value)} + onKeyDown={handleSearchKeyDown} onFocus={() => setIsFocused(true)} onBlur={() => window.setTimeout(() => setIsFocused(false), 150)} placeholder={t("location.placeholder")} @@ -55,11 +70,7 @@ export function LocationSearch({ stations, positions }: { stations: SynopStation