local-weather/components/LocationSearch.tsx

36 lines
984 B
TypeScript
Raw Normal View History

2024-05-05 20:55:51 +00:00
"use client";
import { useState } from "react";
import { getLocation } from "@/app/actions";
import { LocationType } from "@/types/types";
2024-05-04 21:04:30 +00:00
2024-05-05 20:55:51 +00:00
export default function LocationSearch(props: any) {
const [searchLocation, setSearchLocation] = useState("Sluseholmen");
const setLocation = props.setLocation;
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchLocation(e.target.value);
};
const handleKeyDown = (e: any) => {
if (e.key === "Enter") {
const location: LocationType = getLocation(searchLocation);
setLocation(location);
}
};
2024-05-04 21:04:30 +00:00
return (
<>
2024-05-05 20:55:51 +00:00
<input
id="location"
type="text"
placeholder="enter a location to get the weather"
value={searchLocation}
onChange={handleChange}
onKeyDown={handleKeyDown}
/>
<label htmlFor="location" className="hidden">
Enter location to get the weather!
</label>
<p>{searchLocation}</p>
</>
2024-05-04 21:04:30 +00:00
);
}