Compare commits

..

2 Commits

Author SHA1 Message Date
qiuchenfan 39ac8592c3 Merge branch 'main' of http://113.45.67.59:3003/lzq/test 2025-11-17 19:36:29 +08:00
qiuchenfan d42096efed 1115 2025-11-17 19:36:05 +08:00
1 changed files with 70 additions and 44 deletions

View File

@ -14,6 +14,7 @@ export function WeatherSearchForm() {
setCity(e.target.value); setCity(e.target.value);
if (inputError) { if (inputError) {
setInputError(''); setInputError('');
};
} }
const handleFormSubmit = async (e: FormEvent<HTMLFormElement>) => { const handleFormSubmit = async (e: FormEvent<HTMLFormElement>) => {
@ -33,6 +34,18 @@ export function WeatherSearchForm() {
} }
await searchWeather(trimmedCity); await searchWeather(trimmedCity);
if (trimmedCity.length < 2) {
setInputError('请输入至少2个字符');
return;
}
if (!/^[a-zA-Z\s]+$/.test(trimmedCity)) {
setInputError('请输入字母和空格');
return;
}
await searchWeather(trimmedCity);
}
return ( return (
<div className='p-6 border-6 border-b-slate-200/50 '> <div className='p-6 border-6 border-b-slate-200/50 '>
<form onSubmit={handleFormSubmit} className="space-by-3"> <form onSubmit={handleFormSubmit} className="space-by-3">
@ -54,10 +67,16 @@ export function WeatherSearchForm() {
<AlertCircle className="inline w-4 h-4 text-red-500 mt-0.5" /> <AlertCircle className="inline w-4 h-4 text-red-500 mt-0.5" />
<p className="text-red-500 text-xs">{inputError}</p> <p className="text-red-500 text-xs">{inputError}</p>
</div> </div>
) )
} }
<div className='flex gap-2'> <div className='flex gap-2'>
<Button type="submit" disabled={isLoading || !city.trim()} className="mt-4 w-full"> <Button type="submit" disabled={isLoading || !city.trim()} className="mt-4 w-full">
{isLoading ? <> {isLoading ? <>
<Loader2 className="animate-spin w-5 h-5 text-white" /> <Loader2 className="animate-spin w-5 h-5 text-white" />
<span>...</span> <span>...</span>
@ -66,15 +85,22 @@ export function WeatherSearchForm() {
<span></span> <span></span>
</>)} </>)}
</Button> </Button>
{currentWeather && ( {currentWeather && (
<Button type="button" variant="outline" onClick={refreshWeather} disabled={isLoading} className="mt-4 w-full" title='刷新数据'> <Button type="button" variant="outline" onClick={refreshWeather} disabled={isLoading} className="mt-4 w-full" title='刷新数据'>
<RefreshCw className={cn("w-5 h-5", isLoading && "animate-spin")} /> <RefreshCw className={cn("w-5 h-5", isLoading && "animate-spin")} />
</Button> </Button>
)} )}
</div> </div>
</form> </form>
</div> </div>
) )
} }
}
}