2024-04-06 15:57:26 +08:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import { useFormState } from "react-dom";
|
2024-04-06 18:08:51 +08:00
|
|
|
import { addSong, searchSongs } from "./actions";
|
2024-04-06 15:57:26 +08:00
|
|
|
|
|
|
|
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);
|
2024-04-06 15:57:26 +08:00
|
|
|
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
|
|
|
添加
|
2024-04-06 15:57:26 +08:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|