//天气搜索表单组件 import { cn } from "@/components/lib/utils"; import React ,{useState,type FormEvent,type ChangeEvent, use} from "react"; import { Button } from "../ui/button"; import { Input} from "../ui/input"; import {useWeatherStore} from "@/store/weatherStore"; import { AlertCircle, Loader, RefreshCw, Search } from "lucide-react"; //城市输入验证搜索刷新 export function WeatherSearchForm() { const {isLoading,currentWeather,setCurrentWeather,refreshWeather} = useWeatherStore(); const [city, setCity] = useState(''); const [inputError, setInputError] = useState(''); //输入框内容 const handleInputChange = (event: ChangeEvent) => { setCity(event.target.value); 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 && ( ) }
); }