import React, { useState, type FormEvent, type ChangeEvent } from 'react'; import { Search, Loader2, AlertCircle, RefreshCw } from 'lucide-react'; import { Button } from '@/ui/button'; import { Input } from '@/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 handleFormSubmit = async (e: FormEvent) => { e.preventDefault(); const trimmedCity = city.trim(); if (!trimmedCity) { setInputError('请输入城市名称'); return; } if (!trimmedCity) { setInputError('请输入有效的城市名称'); return; } if (trimmedCity.length < 2) { setInputError('请输入至少2个字符'); return; } if (!/^[a-zA-Z\s]+$/.test(trimmedCity)) { setInputError('请输入字母和空格'); return; } await searchWeather(trimmedCity); } return (
setCity(e.target.value)} placeholder="请输入城市名称" disabled={isLoading} className={cn('pl-10 pr-4 py-2 w-full', inputError && 'border-red-500')} />
{inputError && (

{inputError}

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