This commit is contained in:
qiuchenfan 2025-11-17 19:36:29 +08:00
commit 39ac8592c3
3 changed files with 19 additions and 716 deletions

View File

@ -45,8 +45,6 @@ function WeatherIcon({ description, isDay, className = "w-16 h-16 md:w-20 md:h-2
} }
return isDaytime ? <Sun className={className} /> : <Moon className={className} />; return isDaytime ? <Sun className={className} /> : <Moon className={className} />;
} }
export function WeatherDisplay(){ export function WeatherDisplay(){
const {currentWeather} = useWeatherStore(); const {currentWeather} = useWeatherStore();
if(!currentWeather) return null; if(!currentWeather) return null;

View File

@ -8,10 +8,8 @@ import { useWeatherStore } from '@/store/weatherStore';
export function WeatherSearchForm() { export function WeatherSearchForm() {
const {isLoading, currentWeather, searchWeather, refreshWeather} = useWeatherStore(); const {isLoading, currentWeather, searchWeather, refreshWeather} = useWeatherStore();
const [city, setCity] = useState(''); const [city, setCity] = useState('');
const [inputError, setInputError] = useState(''); const [inputError, setInputError] = useState('');
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => { const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
setCity(e.target.value); setCity(e.target.value);
if (inputError) { if (inputError) {
@ -19,13 +17,22 @@ export function WeatherSearchForm() {
}; };
} }
const handleFormSubmit = async (e: FormEvent<HTMLFormElement>) => { const handleFormSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
const trimmedCity = city.trim(); const trimmedCity = city.trim();
if (!trimmedCity) { if (!trimmedCity) {
setInputError('请输入城市名称'); setInputError('请输入城市名称');
return; return;
} }
if (trimmedCity.length < 2) {
setInputError('请输入至少2个字符');
return;
}
if (!/^[a-zA-Z\s]+$/.test(trimmedCity)) {
setInputError('请输入字母和空格');
return;
}
await searchWeather(trimmedCity);
if (trimmedCity.length < 2) { if (trimmedCity.length < 2) {
setInputError('请输入至少2个字符'); setInputError('请输入至少2个字符');

File diff suppressed because it is too large Load Diff