-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
Copy pathEntryForm.tsx
66 lines (61 loc) · 1.88 KB
/
EntryForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"use client";
import cn from "classnames";
import { createEntryAction } from "@/actions/entry";
// @ts-ignore
import { useActionState } from "react";
// @ts-ignore
import { useFormStatus } from "react-dom";
import LoadingSpinner from "@/components/LoadingSpinner";
import SuccessMessage from "@/components/SuccessMessage";
import ErrorMessage from "@/components/ErrorMessage";
const inputClasses = cn(
"block py-2 bg-white dark:bg-gray-800",
"rounded-md border-gray-300 focus:ring-blue-500",
"focus:border-blue-500 text-gray-900 dark:text-gray-100",
);
const initialState = {
successMessage: null,
errorMessage: null,
};
export default function EntryForm() {
const [state, formAction] = useActionState(createEntryAction, initialState);
const { pending } = useFormStatus();
return (
<>
<form className="relative flex my-4" action={formAction}>
<input
required
className={cn(inputClasses, "w-1/3 mr-2 px-4")}
aria-label="Your name"
placeholder="Your name..."
name="name"
/>
<input
required
className={cn(inputClasses, "pl-4 pr-32 flex-grow")}
aria-label="Your message"
placeholder="Your message..."
name="message"
/>
<button
className={cn(
"flex items-center justify-center",
"absolute right-1 top-1 px-4 font-bold h-8",
"bg-gray-100 dark:bg-gray-700 text-gray-900",
"dark:text-gray-100 rounded w-28",
)}
type="submit"
disabled={pending}
>
{pending ? <LoadingSpinner /> : "Sign"}
</button>
</form>
{state?.successMessage ? (
<SuccessMessage>{state.successMessage}</SuccessMessage>
) : null}
{state?.errorMessage ? (
<ErrorMessage>{state.errorMessage}</ErrorMessage>
) : null}
</>
);
}