forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.tsx
194 lines (184 loc) · 5.59 KB
/
root.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import type { ActionFunction, LinksFunction, LoaderFunction } from "remix";
import {
json,
Links,
LiveReload,
Meta,
Scripts,
ScrollRestoration,
useActionData,
useLoaderData
} from "remix";
import stylesUrl from "./index.css";
export const links: LinksFunction = () => {
return [
{
rel: "stylesheet",
href: stylesUrl
}
];
};
export const action: ActionFunction = async ({ request }) => {
const form = await request.formData();
const message = `Successfully submitted data:
- Required text: ${form.get("required-text")}
- Required checkbox: ${form.get("required-checkbox")}
- Text with regex: ${form.get("text-with-regex")}
- Number with min max: ${form.get("number-with-min-max")}
- Text with minlength maxlength: ${form.get(
"text-with-minlength-maxlength"
)}
- Date with min max: ${form.get("date-with-min-max")}
`;
return json({ message }, { status: 200 });
};
type LoaderData = {
todayString: string;
tomorrowString: string;
};
export const loader: LoaderFunction = async () => {
const date = new Date();
// today string in "YYYY-MM-DD" format
const todayString = `${date.getFullYear()}-${(
"00" +
(date.getMonth() + 1)
).slice(-2)}-${("00" + date.getDate()).slice(-2)}`;
// tomorrow string in "YYYY-MM-DD" format
const tomorrowString = `${date.getFullYear()}-${(
"00" +
(date.getMonth() + 1)
).slice(-2)}-${("00" + (date.getDate() + 1)).slice(-2)}`;
return { todayString, tomorrowString };
};
export default function App() {
const actionData = useActionData();
const data = useLoaderData<LoaderData>();
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<div className="root">
<h1>Client-Side Validation Example</h1>
<form method="post">
<div className="form-control">
<label>
Required text
<abbr title="This field is mandatory" aria-label="required">
*
</abbr>
<input type="text" name="required-text" required />
</label>
</div>
<div className="form-control">
<fieldset>
<legend>
Required checkbox
<abbr title="This field is mandatory" aria-label="required">
*
</abbr>
</legend>
<label>
<input
type="radio"
required
name="required-checkbox"
value="yes"
/>
Yes
</label>
<label>
<input
type="radio"
required
name="required-checkbox"
value="maybe"
/>
Maybe
</label>
<label>
<input
type="radio"
required
name="required-checkbox"
value="no"
/>
No
</label>
</fieldset>
</div>
<div className="form-control">
<label>
Text with regex validation (only allow [Bb]anana or [Oo]range)
<input
type="text"
name="text-with-regex"
list="list1"
pattern="[Bb]anana|[Oo]range"
/>
<datalist id="list1">
<option>Banana</option>
<option>Cherry</option>
<option>Apple</option>
<option>Strawberry</option>
<option>Lemon</option>
<option>Orange</option>
</datalist>
</label>
</div>
<div className="form-control">
<label>
Number with min (12) and max (120) validation
<input
type="number"
name="number-with-min-max"
min="12"
max="120"
step="1"
pattern="\d+"
/>
</label>
</div>
<div className="form-control">
<label>
Email
<input name="email" type="email" />
</label>
</div>
<div className="form-control">
<label>Text with minLength(10) and maxLength(140)</label>
<textarea
name="text-with-minlength-maxlength"
minLength={10}
maxLength={140}
rows={3}
></textarea>
</div>
<div className="form-control">
<label>Date with min(today) and max(tomorrow)</label>
<input
type="date"
name="date-with-min-max"
min={data.todayString}
max={data.tomorrowString}
/>
</div>
<div className="form-control">
<button>Submit</button>
</div>
</form>
{actionData?.message && (
<div className="result">{actionData.message}</div>
)}
</div>
<ScrollRestoration />
<Scripts />
{process.env.NODE_ENV === "development" && <LiveReload />}
</body>
</html>
);
}