import React, { useState, type FormEvent, type ChangeEvent } from 'react'; import { Search, Loader2, AlertCircle, RefreshCw } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { cn } from '@/lib/utils'; import { useWeatherStore } from '@/store/weatherStore'; export function WeatherSearchForm() { const { isLoading, currentWeather, searchWeather, refreshWeather } = useWeatherStore(); const [city, setCity] = useState(''); const [inputError, setInputError] = useState(''); const handleInputChange = (e: ChangeEvent) => { setCity(e.target.value); if (inputError) setInputError(''); }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); const trimmed = city.trim(); if (!trimmed) { setInputError('请输入城市名称'); return; } if (trimmed.length < 2) { setInputError('城市名称至少需要2个字符'); return; } if (!/^[\u4e00-\u9fa5a-zA-Z\s-]+$/.test(trimmed)) { setInputError('城市名称包含无效字符'); return; } await searchWeather(trimmed); }; return (
{inputError && (

{inputError}

)}
{currentWeather && ( )}
); }