danmu-sim/app/songs/search-form.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

"use client";
import { useFormState } from "react-dom";
2024-04-06 18:08:51 +08:00
import { addSong, searchSongs } from "./actions";
export default function SearchForm({
className,
}: {
className?: string | undefined;
}) {
const [items, dispatch] = useFormState(searchSongs, []);
2024-04-06 18:08:51 +08:00
const [_, dispatchAdd] = useFormState(addSong, undefined);
return (
<form className={className} action={dispatch}>
<input
name="name"
type="text"
placeholder="Search songs..."
className="w-full border rounded-md px-4 py-2"
/>
{items ? (
<div className="bg-white rounded shadow-md p-4">
{items.map((item) => (
<div
className="flex justify-between items-center py-2 border-b"
key={item.id}
>
<span>
{item.name} - {item.artist}
</span>
2024-04-06 18:08:51 +08:00
<button
className="text-green-500 hover:text-green-700"
onClick={() => {
dispatchAdd(item);
}}
>
2024-04-06 17:11:27 +08:00
</button>
</div>
))}
</div>
) : null}
</form>
);
}