remix-validated-form 2.0.0 → 2.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +9 -0
- package/.turbo/turbo-dev.log +0 -0
- package/browser/ValidatedForm.d.ts +0 -0
- package/browser/ValidatedForm.js +0 -0
- package/browser/hooks.d.ts +0 -0
- package/browser/hooks.js +0 -0
- package/browser/index.d.ts +0 -0
- package/browser/index.js +0 -0
- package/browser/internal/flatten.d.ts +0 -0
- package/browser/internal/flatten.js +0 -0
- package/browser/internal/formContext.d.ts +0 -0
- package/browser/internal/formContext.js +0 -0
- package/browser/internal/util.d.ts +0 -0
- package/browser/internal/util.js +0 -0
- package/browser/server.d.ts +0 -0
- package/browser/server.js +0 -0
- package/browser/test-data/testFormData.d.ts +0 -0
- package/browser/test-data/testFormData.js +0 -0
- package/browser/validation/createValidator.d.ts +0 -0
- package/browser/validation/createValidator.js +0 -0
- package/browser/validation/types.d.ts +0 -0
- package/browser/validation/types.js +0 -0
- package/browser/validation/validation.test.d.ts +0 -0
- package/browser/validation/validation.test.js +0 -0
- package/browser/validation/withYup.d.ts +0 -0
- package/browser/validation/withYup.js +0 -0
- package/browser/validation/withZod.d.ts +0 -0
- package/browser/validation/withZod.js +0 -0
- package/build/ValidatedForm.d.ts +0 -0
- package/build/ValidatedForm.js +0 -0
- package/build/hooks.d.ts +0 -0
- package/build/hooks.js +0 -0
- package/build/index.d.ts +0 -0
- package/build/index.js +0 -0
- package/build/internal/flatten.d.ts +0 -0
- package/build/internal/flatten.js +0 -0
- package/build/internal/formContext.d.ts +0 -0
- package/build/internal/formContext.js +0 -0
- package/build/internal/util.d.ts +0 -0
- package/build/internal/util.js +0 -0
- package/build/server.d.ts +0 -0
- package/build/server.js +0 -0
- package/build/test-data/testFormData.d.ts +0 -0
- package/build/test-data/testFormData.js +0 -0
- package/build/validation/createValidator.d.ts +0 -0
- package/build/validation/createValidator.js +0 -0
- package/build/validation/types.d.ts +0 -0
- package/build/validation/types.js +0 -0
- package/build/validation/validation.test.d.ts +0 -0
- package/build/validation/validation.test.js +0 -0
- package/build/validation/withYup.d.ts +0 -0
- package/build/validation/withYup.js +0 -0
- package/build/validation/withZod.d.ts +0 -0
- package/build/validation/withZod.js +0 -0
- package/jest.config.js +5 -0
- package/package.json +7 -32
- package/src/ValidatedForm.tsx +151 -0
- package/src/hooks.ts +60 -0
- package/src/index.ts +8 -0
- package/src/internal/flatten.ts +48 -0
- package/src/internal/formContext.ts +36 -0
- package/src/internal/util.ts +23 -0
- package/src/server.ts +10 -0
- package/src/test-data/testFormData.ts +55 -0
- package/src/validation/createValidator.ts +24 -0
- package/src/validation/types.ts +26 -0
- package/src/validation/validation.test.ts +317 -0
- package/src/validation/withYup.ts +43 -0
- package/src/validation/withZod.ts +51 -0
- package/tsconfig.json +5 -0
- package/.eslintcache +0 -1
- package/.eslintignore +0 -1
- package/.prettierignore +0 -10
- package/LICENSE +0 -21
- package/README.md +0 -235
- package/browser/flatten.d.ts +0 -4
- package/browser/flatten.js +0 -35
- package/build/flatten.d.ts +0 -4
- package/build/flatten.js +0 -43
- package/sample-app/.env +0 -7
- package/sample-app/README.md +0 -53
- package/sample-app/app/components/ErrorBox.tsx +0 -34
- package/sample-app/app/components/FormInput.tsx +0 -40
- package/sample-app/app/components/FormSelect.tsx +0 -37
- package/sample-app/app/components/SubjectForm.tsx +0 -150
- package/sample-app/app/entry.client.tsx +0 -4
- package/sample-app/app/entry.server.tsx +0 -21
- package/sample-app/app/root.tsx +0 -92
- package/sample-app/app/routes/index.tsx +0 -5
- package/sample-app/app/routes/subjects/$id.edit.tsx +0 -98
- package/sample-app/app/routes/subjects/index.tsx +0 -112
- package/sample-app/app/routes/subjects/new.tsx +0 -46
- package/sample-app/app/services/db.server.ts +0 -23
- package/sample-app/app/types.ts +0 -6
- package/sample-app/package-lock.json +0 -10890
- package/sample-app/package.json +0 -36
- package/sample-app/prisma/dev.db +0 -0
- package/sample-app/prisma/schema.prisma +0 -34
- package/sample-app/public/favicon.ico +0 -0
- package/sample-app/remix.config.js +0 -10
- package/sample-app/remix.env.d.ts +0 -2
package/README.md
DELETED
@@ -1,235 +0,0 @@
|
|
1
|
-
# Remix Validated Form
|
2
|
-
|
3
|
-
A form library built for [remix](https://remix.run) to make validation easy.
|
4
|
-
|
5
|
-
- Client-side, field-by-field validation (e.g. validate on blur) and form-level validation
|
6
|
-
- Set default values for the entire form in one place
|
7
|
-
- Re-use validation on the server
|
8
|
-
- Show validation errors from the server even without JS
|
9
|
-
- Detect if the current form is submitting when there are multiple forms on the page
|
10
|
-
- Supports nested objects and arrays
|
11
|
-
- Validation library agnostic
|
12
|
-
|
13
|
-
# Demo
|
14
|
-
|
15
|
-
https://user-images.githubusercontent.com/2811287/145734901-700a5085-a10b-4d89-88e1-5de9142b1e85.mov
|
16
|
-
|
17
|
-
To run `sample-app`:
|
18
|
-
|
19
|
-
```
|
20
|
-
git clone https://github.com/airjp73/remix-validated-form
|
21
|
-
cd remix-validated-form
|
22
|
-
npm i
|
23
|
-
cd sample-app
|
24
|
-
npm i
|
25
|
-
cd ..
|
26
|
-
npm run sample-app
|
27
|
-
```
|
28
|
-
|
29
|
-
# Getting started
|
30
|
-
|
31
|
-
## Install
|
32
|
-
|
33
|
-
```bash
|
34
|
-
npm install remix-validated-form
|
35
|
-
```
|
36
|
-
|
37
|
-
## Create an input component
|
38
|
-
|
39
|
-
In order to display field errors or do field-by-field validation,
|
40
|
-
it's recommended to incorporate this library into an input component using `useField`.
|
41
|
-
|
42
|
-
```tsx
|
43
|
-
import { useField } from "remix-validated-form";
|
44
|
-
|
45
|
-
type MyInputProps = {
|
46
|
-
name: string;
|
47
|
-
label: string;
|
48
|
-
};
|
49
|
-
|
50
|
-
export const MyInput = ({ name, label }: InputProps) => {
|
51
|
-
const { validate, clearError, defaultValue, error } = useField(name);
|
52
|
-
return (
|
53
|
-
<div>
|
54
|
-
<label htmlFor={name}>{label}</label>
|
55
|
-
<input
|
56
|
-
id={name}
|
57
|
-
name={name}
|
58
|
-
onBlur={validate}
|
59
|
-
onChange={clearError}
|
60
|
-
defaultValue={defaultValue}
|
61
|
-
/>
|
62
|
-
{error && <span className="my-error-class">{error}</span>}
|
63
|
-
</div>
|
64
|
-
);
|
65
|
-
};
|
66
|
-
```
|
67
|
-
|
68
|
-
## Create a submit button component
|
69
|
-
|
70
|
-
To best take advantage of the per-form submission detection, we can create a submit button component.
|
71
|
-
|
72
|
-
```tsx
|
73
|
-
import { useIsSubmitting } from "remix-validated-form";
|
74
|
-
|
75
|
-
export const MySubmitButton = () => {
|
76
|
-
const isSubmitting = useIsSubmitting();
|
77
|
-
return (
|
78
|
-
<button type="submit" disabled={isSubmitting}>
|
79
|
-
{isSubmitting ? "Submitting..." : "Submit"}
|
80
|
-
</button>
|
81
|
-
);
|
82
|
-
};
|
83
|
-
```
|
84
|
-
|
85
|
-
## Use the form!
|
86
|
-
|
87
|
-
Now that we have our components, making a form is easy!
|
88
|
-
|
89
|
-
```tsx
|
90
|
-
import { ActionFunction, LoaderFunction, redirect, useLoaderData } from "remix";
|
91
|
-
import * as yup from "yup";
|
92
|
-
import { validationError, ValidatedForm, withYup } from "remix-validated-form";
|
93
|
-
import { MyInput, MySubmitButton } from "~/components/Input";
|
94
|
-
|
95
|
-
// Using yup in this example, but you can use anything
|
96
|
-
const validator = withYup(
|
97
|
-
yup.object({
|
98
|
-
firstName: yup.string().label("First Name").required(),
|
99
|
-
lastName: yup.string().label("Last Name").required(),
|
100
|
-
email: yup.string().email().label("Email").required(),
|
101
|
-
})
|
102
|
-
);
|
103
|
-
|
104
|
-
export const action: ActionFunction = async ({ request }) => {
|
105
|
-
const fieldValues = validator.validate(await request.formData());
|
106
|
-
if (fieldValues.error) return validationError(fieldValues.error);
|
107
|
-
const { firstName, lastName, email } = fieldValues.data;
|
108
|
-
|
109
|
-
// Do something with correctly typed values;
|
110
|
-
|
111
|
-
return redirect("/");
|
112
|
-
};
|
113
|
-
|
114
|
-
export const loader: LoaderFunction = () => {
|
115
|
-
return {
|
116
|
-
defaultValues: {
|
117
|
-
firstName: "Jane",
|
118
|
-
lastName: "Doe",
|
119
|
-
email: "jane.doe@example.com",
|
120
|
-
},
|
121
|
-
};
|
122
|
-
};
|
123
|
-
|
124
|
-
export default function MyForm() {
|
125
|
-
const { defaultValues } = useLoaderData();
|
126
|
-
return (
|
127
|
-
<ValidatedForm
|
128
|
-
validator={validator}
|
129
|
-
method="post"
|
130
|
-
defaultValues={defaultValues}
|
131
|
-
>
|
132
|
-
<MyInput name="firstName" label="First Name" />
|
133
|
-
<MyInput name="lastName" label="Last Name" />
|
134
|
-
<MyInput name="email" label="Email" />
|
135
|
-
<MySubmitButton />
|
136
|
-
</ValidatedForm>
|
137
|
-
);
|
138
|
-
}
|
139
|
-
```
|
140
|
-
|
141
|
-
## Nested objects and arrays
|
142
|
-
|
143
|
-
You can use nested objects and arrays by using a period (`.`) or brackets (`[]`) for the field names.
|
144
|
-
|
145
|
-
```tsx
|
146
|
-
export default function MyForm() {
|
147
|
-
const { defaultValues } = useLoaderData();
|
148
|
-
return (
|
149
|
-
<ValidatedForm
|
150
|
-
validator={validator}
|
151
|
-
method="post"
|
152
|
-
defaultValues={defaultValues}
|
153
|
-
>
|
154
|
-
<MyInput name="firstName" label="First Name" />
|
155
|
-
<MyInput name="lastName" label="Last Name" />
|
156
|
-
<MyInput name="address.street" label="Street" />
|
157
|
-
<MyInput name="address.city" label="City" />
|
158
|
-
<MyInput name="phones[0].type" label="Phone 1 Type" />
|
159
|
-
<MyInput name="phones[0].number" label="Phone 1 Number" />
|
160
|
-
<MyInput name="phones[1].type" label="Phone 2 Type" />
|
161
|
-
<MyInput name="phones[1].number" label="Phone 2 Number" />
|
162
|
-
<MySubmitButton />
|
163
|
-
</ValidatedForm>
|
164
|
-
);
|
165
|
-
}
|
166
|
-
```
|
167
|
-
|
168
|
-
# Validation Library Support
|
169
|
-
|
170
|
-
This library currently includes an out-of-the-box adapter for `yup` and `zod`,
|
171
|
-
but you can easily support whatever library you want by creating your own adapter.
|
172
|
-
|
173
|
-
And if you create an adapter for a library, feel free to make a PR on this library to add official support 😊
|
174
|
-
|
175
|
-
## Creating an adapter
|
176
|
-
|
177
|
-
Any object that conforms to the `Validator` type can be passed into the the `ValidatedForm`'s `validator` prop.
|
178
|
-
|
179
|
-
```ts
|
180
|
-
type FieldErrors = Record<string, string>;
|
181
|
-
|
182
|
-
type ValidationResult<DataType> =
|
183
|
-
| { data: DataType; error: undefined }
|
184
|
-
| { error: FieldErrors; data: undefined };
|
185
|
-
|
186
|
-
type ValidateFieldResult = { error?: string };
|
187
|
-
|
188
|
-
type Validator<DataType> = {
|
189
|
-
validate: (unvalidatedData: unknown) => ValidationResult<DataType>;
|
190
|
-
validateField: (
|
191
|
-
unvalidatedData: unknown,
|
192
|
-
field: string
|
193
|
-
) => ValidateFieldResult;
|
194
|
-
};
|
195
|
-
```
|
196
|
-
|
197
|
-
In order to make an adapter for your validation library of choice,
|
198
|
-
you can create a function that accepts a schema from the validation library and turns it into a validator.
|
199
|
-
|
200
|
-
Note the use of `createValidator`.
|
201
|
-
It takes care of unflattening the data for nested objects and arrays
|
202
|
-
since the form doesn't know anything about object and arrays and this should be handled by the adapter.
|
203
|
-
For more on this you can check the implementations for `withZod` and `withYup`.
|
204
|
-
|
205
|
-
The out-of-the-box support for `yup` in this library works like this:
|
206
|
-
|
207
|
-
```ts
|
208
|
-
export const withYup = <Schema extends AnyObjectSchema>(
|
209
|
-
validationSchema: Schema
|
210
|
-
// For best result with Typescript, we should type the `Validator` we return based on the provided schema
|
211
|
-
): Validator<InferType<Schema>> =>
|
212
|
-
createValidator({
|
213
|
-
validate: (unvalidatedData) => {
|
214
|
-
// Validate with yup and return the validated & typed data or the error
|
215
|
-
|
216
|
-
if (isValid) return { data: { field1: "someValue" }, error: undefined };
|
217
|
-
else return { error: { field1: "Some error!" }, data: undefined };
|
218
|
-
},
|
219
|
-
validateField: (unvalidatedData, field) => {
|
220
|
-
// Validate the specific field with yup
|
221
|
-
|
222
|
-
if (isValid) return { error: undefined };
|
223
|
-
else return { error: "Some error" };
|
224
|
-
},
|
225
|
-
});
|
226
|
-
```
|
227
|
-
|
228
|
-
# Frequenty Asked Questions
|
229
|
-
|
230
|
-
## Why are my fields triggering the native HTML validations before `remix-validated-form` ones?
|
231
|
-
|
232
|
-
This is happening because you or the library you are using is passing the `required` attribute to the fields.
|
233
|
-
This library doesn't take care of eliminating them and it's up to the user how they want to manage the validation errors.
|
234
|
-
If you wan't to disable all native HTML validations you can add `noValidate` to `<ValidatedForm>`.
|
235
|
-
We recommend this approach since the validation will still work even if JS is disabled.
|
package/browser/flatten.d.ts
DELETED
@@ -1,4 +0,0 @@
|
|
1
|
-
import { GenericObject } from ".";
|
2
|
-
export declare const objectFromPathEntries: (entries: [string, any][]) => {};
|
3
|
-
/** Flatten an object so there are no nested objects or arrays */
|
4
|
-
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
package/browser/flatten.js
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
// `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
|
2
|
-
import assign from "lodash/assign";
|
3
|
-
import isArray from "lodash/isArray";
|
4
|
-
import isObject from "lodash/isObject";
|
5
|
-
import keys from "lodash/keys";
|
6
|
-
import mapKeys from "lodash/mapKeys";
|
7
|
-
import set from "lodash/set";
|
8
|
-
import transform from "lodash/transform";
|
9
|
-
export const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => set(acc, key, value), {});
|
10
|
-
/** Flatten an object so there are no nested objects or arrays */
|
11
|
-
export function flatten(obj, preserveEmpty = false) {
|
12
|
-
return transform(obj, function (result, value, key) {
|
13
|
-
if (isObject(value)) {
|
14
|
-
let flatMap = mapKeys(flatten(value, preserveEmpty), function (_mvalue, mkey) {
|
15
|
-
if (isArray(value)) {
|
16
|
-
let index = mkey.indexOf(".");
|
17
|
-
if (-1 !== index) {
|
18
|
-
return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
|
19
|
-
}
|
20
|
-
return `${key}[${mkey}]`;
|
21
|
-
}
|
22
|
-
return `${key}.${mkey}`;
|
23
|
-
});
|
24
|
-
assign(result, flatMap);
|
25
|
-
// Preverve Empty arrays and objects
|
26
|
-
if (preserveEmpty && keys(flatMap).length === 0) {
|
27
|
-
result[key] = value;
|
28
|
-
}
|
29
|
-
}
|
30
|
-
else {
|
31
|
-
result[key] = value;
|
32
|
-
}
|
33
|
-
return result;
|
34
|
-
}, {});
|
35
|
-
}
|
package/build/flatten.d.ts
DELETED
@@ -1,4 +0,0 @@
|
|
1
|
-
import { GenericObject } from ".";
|
2
|
-
export declare const objectFromPathEntries: (entries: [string, any][]) => {};
|
3
|
-
/** Flatten an object so there are no nested objects or arrays */
|
4
|
-
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
package/build/flatten.js
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.flatten = exports.objectFromPathEntries = void 0;
|
7
|
-
// `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
|
8
|
-
const assign_1 = __importDefault(require("lodash/assign"));
|
9
|
-
const isArray_1 = __importDefault(require("lodash/isArray"));
|
10
|
-
const isObject_1 = __importDefault(require("lodash/isObject"));
|
11
|
-
const keys_1 = __importDefault(require("lodash/keys"));
|
12
|
-
const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
|
13
|
-
const set_1 = __importDefault(require("lodash/set"));
|
14
|
-
const transform_1 = __importDefault(require("lodash/transform"));
|
15
|
-
const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => (0, set_1.default)(acc, key, value), {});
|
16
|
-
exports.objectFromPathEntries = objectFromPathEntries;
|
17
|
-
/** Flatten an object so there are no nested objects or arrays */
|
18
|
-
function flatten(obj, preserveEmpty = false) {
|
19
|
-
return (0, transform_1.default)(obj, function (result, value, key) {
|
20
|
-
if ((0, isObject_1.default)(value)) {
|
21
|
-
let flatMap = (0, mapKeys_1.default)(flatten(value, preserveEmpty), function (_mvalue, mkey) {
|
22
|
-
if ((0, isArray_1.default)(value)) {
|
23
|
-
let index = mkey.indexOf(".");
|
24
|
-
if (-1 !== index) {
|
25
|
-
return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
|
26
|
-
}
|
27
|
-
return `${key}[${mkey}]`;
|
28
|
-
}
|
29
|
-
return `${key}.${mkey}`;
|
30
|
-
});
|
31
|
-
(0, assign_1.default)(result, flatMap);
|
32
|
-
// Preverve Empty arrays and objects
|
33
|
-
if (preserveEmpty && (0, keys_1.default)(flatMap).length === 0) {
|
34
|
-
result[key] = value;
|
35
|
-
}
|
36
|
-
}
|
37
|
-
else {
|
38
|
-
result[key] = value;
|
39
|
-
}
|
40
|
-
return result;
|
41
|
-
}, {});
|
42
|
-
}
|
43
|
-
exports.flatten = flatten;
|
package/sample-app/.env
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
# Environment variables declared in this file are automatically made available to Prisma.
|
2
|
-
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
|
3
|
-
|
4
|
-
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
|
5
|
-
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
|
6
|
-
|
7
|
-
DATABASE_URL="file:./dev.db"
|
package/sample-app/README.md
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
# Welcome to Remix!
|
2
|
-
|
3
|
-
- [Remix Docs](https://remix.run/docs)
|
4
|
-
|
5
|
-
## Development
|
6
|
-
|
7
|
-
From your terminal:
|
8
|
-
|
9
|
-
```sh
|
10
|
-
npm run dev
|
11
|
-
```
|
12
|
-
|
13
|
-
This starts your app in development mode, rebuilding assets on file changes.
|
14
|
-
|
15
|
-
## Deployment
|
16
|
-
|
17
|
-
First, build your app for production:
|
18
|
-
|
19
|
-
```sh
|
20
|
-
npm run build
|
21
|
-
```
|
22
|
-
|
23
|
-
Then run the app in production mode:
|
24
|
-
|
25
|
-
```sh
|
26
|
-
npm start
|
27
|
-
```
|
28
|
-
|
29
|
-
Now you'll need to pick a host to deploy it to.
|
30
|
-
|
31
|
-
### DIY
|
32
|
-
|
33
|
-
If you're familiar with deploying node applications, the built-in Remix app server is production-ready.
|
34
|
-
|
35
|
-
Make sure to deploy the output of `remix build`
|
36
|
-
|
37
|
-
- `build/`
|
38
|
-
- `public/build/`
|
39
|
-
|
40
|
-
### Using a Template
|
41
|
-
|
42
|
-
When you ran `npx create-remix@latest` there were a few choices for hosting. You can run that again to create a new project, then copy over your `app/` folder to the new project that's pre-configured for your target server.
|
43
|
-
|
44
|
-
```sh
|
45
|
-
cd ..
|
46
|
-
# create a new project, and pick a pre-configured host
|
47
|
-
npx create-remix@latest
|
48
|
-
cd my-new-remix-app
|
49
|
-
# remove the new project's app (not the old one!)
|
50
|
-
rm -rf app
|
51
|
-
# copy your app over
|
52
|
-
cp -R ../my-old-remix-app/app app
|
53
|
-
```
|
@@ -1,34 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
Alert,
|
3
|
-
AlertDescription,
|
4
|
-
AlertIcon,
|
5
|
-
AlertTitle,
|
6
|
-
} from "@chakra-ui/react";
|
7
|
-
|
8
|
-
type ErrorProps = {
|
9
|
-
title?: React.ReactNode;
|
10
|
-
description?: React.ReactNode;
|
11
|
-
};
|
12
|
-
export function ErrorBox({ title, description }: ErrorProps) {
|
13
|
-
return (
|
14
|
-
<Alert
|
15
|
-
status="error"
|
16
|
-
variant="subtle"
|
17
|
-
flexDirection="column"
|
18
|
-
alignItems="center"
|
19
|
-
justifyContent="center"
|
20
|
-
textAlign="center"
|
21
|
-
height="200px"
|
22
|
-
>
|
23
|
-
<AlertIcon boxSize="40px" mr={0} />
|
24
|
-
{title && (
|
25
|
-
<AlertTitle mt={4} mb={1} fontSize="lg">
|
26
|
-
{title}
|
27
|
-
</AlertTitle>
|
28
|
-
)}
|
29
|
-
{description && (
|
30
|
-
<AlertDescription maxWidth="sm">{description}</AlertDescription>
|
31
|
-
)}
|
32
|
-
</Alert>
|
33
|
-
);
|
34
|
-
}
|
@@ -1,40 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
FormControl,
|
3
|
-
FormErrorMessage,
|
4
|
-
FormLabel,
|
5
|
-
Input,
|
6
|
-
InputProps,
|
7
|
-
} from "@chakra-ui/react";
|
8
|
-
import { useField } from "../../remix-validated-form";
|
9
|
-
|
10
|
-
type FormInputProps = {
|
11
|
-
name: string;
|
12
|
-
label: string;
|
13
|
-
isRequired?: boolean;
|
14
|
-
};
|
15
|
-
|
16
|
-
export const FormInput = ({
|
17
|
-
name,
|
18
|
-
label,
|
19
|
-
isRequired,
|
20
|
-
...rest
|
21
|
-
}: FormInputProps & InputProps) => {
|
22
|
-
const { validate, clearError, defaultValue, error } = useField(name);
|
23
|
-
|
24
|
-
return (
|
25
|
-
<>
|
26
|
-
<FormControl isInvalid={!!error} isRequired={isRequired}>
|
27
|
-
<FormLabel htmlFor={name}>{label}</FormLabel>
|
28
|
-
<Input
|
29
|
-
id={name}
|
30
|
-
name={name}
|
31
|
-
onBlur={validate}
|
32
|
-
onChange={clearError}
|
33
|
-
defaultValue={defaultValue}
|
34
|
-
{...rest}
|
35
|
-
/>
|
36
|
-
{error && <FormErrorMessage>{error}</FormErrorMessage>}
|
37
|
-
</FormControl>
|
38
|
-
</>
|
39
|
-
);
|
40
|
-
};
|
@@ -1,37 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
FormControl,
|
3
|
-
FormErrorMessage,
|
4
|
-
FormLabel,
|
5
|
-
Select,
|
6
|
-
SelectProps,
|
7
|
-
} from "@chakra-ui/react";
|
8
|
-
import { useField } from "../../remix-validated-form";
|
9
|
-
|
10
|
-
type FormSelectProps = {
|
11
|
-
name: string;
|
12
|
-
label: string;
|
13
|
-
isRequired?: boolean;
|
14
|
-
};
|
15
|
-
|
16
|
-
export const FormSelect = ({
|
17
|
-
name,
|
18
|
-
label,
|
19
|
-
isRequired,
|
20
|
-
...rest
|
21
|
-
}: FormSelectProps & SelectProps) => {
|
22
|
-
const { validate, clearError, defaultValue, error } = useField(name);
|
23
|
-
return (
|
24
|
-
<FormControl isInvalid={!!error} isRequired={isRequired}>
|
25
|
-
<FormLabel htmlFor={name}>{label}</FormLabel>
|
26
|
-
<Select
|
27
|
-
id={name}
|
28
|
-
name={name}
|
29
|
-
onBlur={validate}
|
30
|
-
onChange={clearError}
|
31
|
-
defaultValue={defaultValue}
|
32
|
-
{...rest}
|
33
|
-
/>
|
34
|
-
{error && <FormErrorMessage>{error}</FormErrorMessage>}
|
35
|
-
</FormControl>
|
36
|
-
);
|
37
|
-
};
|
@@ -1,150 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
Box,
|
3
|
-
Button,
|
4
|
-
Stack,
|
5
|
-
HStack,
|
6
|
-
Container,
|
7
|
-
VStack,
|
8
|
-
} from "@chakra-ui/react";
|
9
|
-
import { useState } from "react";
|
10
|
-
import { useNavigate } from "remix";
|
11
|
-
import * as z from "zod";
|
12
|
-
import {
|
13
|
-
ValidatedForm,
|
14
|
-
withZod,
|
15
|
-
useIsSubmitting,
|
16
|
-
} from "../../remix-validated-form";
|
17
|
-
import { FormInput } from "./FormInput";
|
18
|
-
import { FormSelect } from "./FormSelect";
|
19
|
-
|
20
|
-
const subjectSchema = z.object({
|
21
|
-
name: z.string().nonempty("Subject Name can't be empty"),
|
22
|
-
description: z.string().nonempty("Subject Description can't be empty"),
|
23
|
-
teacher: z.object({
|
24
|
-
name: z.string().nonempty("Teacher Name can't be empty"),
|
25
|
-
email: z
|
26
|
-
.string()
|
27
|
-
.email("Teacher Email is invalid")
|
28
|
-
.nonempty("Teacher Email can't be empty"),
|
29
|
-
}),
|
30
|
-
subjectDays: z
|
31
|
-
.object({
|
32
|
-
day: z.string().nonempty("Day can't be empty"),
|
33
|
-
})
|
34
|
-
.array(),
|
35
|
-
});
|
36
|
-
|
37
|
-
export const subjectFormValidator = withZod(subjectSchema);
|
38
|
-
|
39
|
-
export function SubjectForm({
|
40
|
-
defaultValues,
|
41
|
-
}: {
|
42
|
-
defaultValues?: Partial<z.infer<typeof subjectSchema>>;
|
43
|
-
}) {
|
44
|
-
let navigate = useNavigate();
|
45
|
-
const isSubmitting = useIsSubmitting();
|
46
|
-
const [daysKeys, setDaysKeys] = useState(
|
47
|
-
defaultValues?.subjectDays && defaultValues.subjectDays.length > 0
|
48
|
-
? Array.from(Array(defaultValues.subjectDays.length).keys())
|
49
|
-
: [0]
|
50
|
-
);
|
51
|
-
|
52
|
-
return (
|
53
|
-
<Box as="main" py="8" flex="1">
|
54
|
-
<Container maxW="7xl" id="xxx">
|
55
|
-
<Box bg="white" p="6" rounded="lg" shadow="base">
|
56
|
-
<Box px="10" maxWidth="7xl">
|
57
|
-
<ValidatedForm
|
58
|
-
validator={subjectFormValidator}
|
59
|
-
defaultValues={defaultValues}
|
60
|
-
method="post"
|
61
|
-
noValidate
|
62
|
-
>
|
63
|
-
<Stack spacing="6" direction="column">
|
64
|
-
<Stack direction="row" spacing="6" align="center" width="full">
|
65
|
-
<FormInput name="name" label="Name" isRequired />
|
66
|
-
<FormInput
|
67
|
-
name="description"
|
68
|
-
label="Description"
|
69
|
-
isRequired
|
70
|
-
/>
|
71
|
-
</Stack>
|
72
|
-
<Stack direction="row" spacing="6" align="center" width="full">
|
73
|
-
<FormInput
|
74
|
-
name="teacher.name"
|
75
|
-
label="Teacher Name"
|
76
|
-
isRequired
|
77
|
-
/>
|
78
|
-
<FormInput
|
79
|
-
name="teacher.email"
|
80
|
-
label="Teacher Email"
|
81
|
-
isRequired
|
82
|
-
/>
|
83
|
-
</Stack>
|
84
|
-
<VStack width="full" spacing="6" alignItems="flex-start">
|
85
|
-
{daysKeys.map((key, index) => (
|
86
|
-
<Stack direction="row" width="full" key={key}>
|
87
|
-
<FormSelect
|
88
|
-
name={`subjectDays[${index}].day`}
|
89
|
-
label="Subject Day"
|
90
|
-
isRequired
|
91
|
-
placeholder="Select Day"
|
92
|
-
>
|
93
|
-
<option value="Monday">Monday</option>
|
94
|
-
<option value="Tuesday">Tuesday</option>
|
95
|
-
<option value="Wednesday">Wednesday</option>
|
96
|
-
<option value="Thursday">Thursday</option>
|
97
|
-
<option value="Friday">Friday</option>
|
98
|
-
<option value="Saturday">Saturday</option>
|
99
|
-
<option value="Sunday">Sunday</option>
|
100
|
-
</FormSelect>
|
101
|
-
{daysKeys.length > 1 && (
|
102
|
-
<Box pt="8">
|
103
|
-
<Button
|
104
|
-
colorScheme="red"
|
105
|
-
onClick={() =>
|
106
|
-
setDaysKeys(
|
107
|
-
daysKeys.filter(
|
108
|
-
(key2, index2) => index !== index2
|
109
|
-
)
|
110
|
-
)
|
111
|
-
}
|
112
|
-
>
|
113
|
-
Delete
|
114
|
-
</Button>
|
115
|
-
</Box>
|
116
|
-
)}
|
117
|
-
</Stack>
|
118
|
-
))}
|
119
|
-
<Button
|
120
|
-
colorScheme="blue"
|
121
|
-
size="xs"
|
122
|
-
onClick={() =>
|
123
|
-
setDaysKeys([...daysKeys, Math.max(...daysKeys) + 1])
|
124
|
-
}
|
125
|
-
>
|
126
|
-
Add Day
|
127
|
-
</Button>
|
128
|
-
</VStack>
|
129
|
-
|
130
|
-
<HStack width="full" justifyContent="center" mt="8">
|
131
|
-
<Button
|
132
|
-
type="submit"
|
133
|
-
colorScheme="blue"
|
134
|
-
disabled={isSubmitting}
|
135
|
-
isLoading={isSubmitting}
|
136
|
-
>
|
137
|
-
{isSubmitting ? "Sending..." : "Send"}
|
138
|
-
</Button>
|
139
|
-
<Button variant="outline" onClick={() => navigate(-1)}>
|
140
|
-
Cancel
|
141
|
-
</Button>
|
142
|
-
</HStack>
|
143
|
-
</Stack>
|
144
|
-
</ValidatedForm>
|
145
|
-
</Box>
|
146
|
-
</Box>
|
147
|
-
</Container>
|
148
|
-
</Box>
|
149
|
-
);
|
150
|
-
}
|
@@ -1,21 +0,0 @@
|
|
1
|
-
import { renderToString } from "react-dom/server";
|
2
|
-
import { RemixServer } from "remix";
|
3
|
-
import type { EntryContext } from "remix";
|
4
|
-
|
5
|
-
export default function handleRequest(
|
6
|
-
request: Request,
|
7
|
-
responseStatusCode: number,
|
8
|
-
responseHeaders: Headers,
|
9
|
-
remixContext: EntryContext
|
10
|
-
) {
|
11
|
-
let markup = renderToString(
|
12
|
-
<RemixServer context={remixContext} url={request.url} />
|
13
|
-
);
|
14
|
-
|
15
|
-
responseHeaders.set("Content-Type", "text/html");
|
16
|
-
|
17
|
-
return new Response("<!DOCTYPE html>" + markup, {
|
18
|
-
status: responseStatusCode,
|
19
|
-
headers: responseHeaders,
|
20
|
-
});
|
21
|
-
}
|