-
Notifications
You must be signed in to change notification settings - Fork 85
/
components.tsx
156 lines (149 loc) · 3.48 KB
/
components.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type { FieldMetadata } from '@conform-to/react';
import type { ReactNode } from 'react';
interface PlaygroundProps {
title: string;
description?: ReactNode;
form?: string;
result?: Record<string, unknown>;
formAction?: string;
formMethod?: string;
formEncType?: string;
children?: ReactNode;
}
export function Playground({
title,
description,
form,
result,
formAction,
formMethod,
formEncType,
children,
}: PlaygroundProps) {
return (
<section
className="lg:grid lg:grid-cols-2 lg:gap-6 py-8"
data-playground={title}
>
<aside className="flex flex-col">
<header className="px-4 lg:px-0">
<h3 className="text-lg font-medium leading-6 text-gray-900">
{title}
</h3>
<div className="mt-4 mb-2 text-sm text-gray-600">{description}</div>
</header>
{result ? (
<pre
className={`m-4 border-l-4 overflow-x-scroll ${
result.status === 'error'
? 'border-pink-600'
: 'border-emerald-500'
} pl-4 py-2 mt-4`}
>
{JSON.stringify(result, null, 2)}
</pre>
) : null}
</aside>
<div>
<main className="shadow lg:rounded-md">
<div className="mt-5 lg:mt-0 lg:col-span-2 px-4 py-5 bg-white lg:p-6">
{children}
</div>
<footer className="px-4 py-3 bg-gray-50 flex flex-row lg:px-6 gap-2">
<button
type="reset"
className="inline-flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-gray-700 bg-gray-50 hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
form={form}
>
Reset
</button>
<button
type="submit"
className="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
form={form}
formAction={formAction}
formMethod={formMethod}
formEncType={formEncType}
>
Submit
</button>
</footer>
</main>
</div>
</section>
);
}
interface FieldProps {
label: string;
inline?: boolean;
meta?: FieldMetadata<any, any>;
allErrors?: boolean;
children: ReactNode;
}
export function Field({
label,
inline,
meta,
allErrors,
children,
}: FieldProps) {
const errors =
meta && allErrors
? Object.values(meta.allErrors).flat()
: meta?.errors ?? [];
return (
<div className="mb-4">
<div
className={
inline
? 'flex flex-row justify-between items-center gap-8 whitespace-nowrap'
: ''
}
>
<label
htmlFor={meta?.id}
className="block text-sm font-medium text-gray-700"
>
{label}
</label>
{children}
</div>
<div id={meta?.errorId} className="my-1 space-y-0.5">
{errors.length === 0 ? (
<p className="text-pink-600 text-sm" />
) : (
errors.map((message) => (
<p className="text-pink-600 text-sm" key={message}>
{message}
</p>
))
)}
</div>
</div>
);
}
interface AlertProps {
id?: string;
errors: string[] | undefined;
}
export function Alert({ id, errors }: AlertProps) {
return (
<div
id={id}
className={
errors?.length
? 'flex gap-4 bg-red-100 text-red-500 text-sm p-4 mb-4 rounded align-top'
: 'hidden'
}
>
<span>❌</span>
<div className="space-y-1">
{!errors?.length ? (
<p></p>
) : (
errors.map((message, index) => <p key={index}>{message}</p>)
)}
</div>
</div>
);
}