remix-validated-form 1.1.0 → 1.1.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/.eslintcache +1 -1
- package/.prettierignore +2 -0
- package/README.md +51 -3
- package/browser/ValidatedForm.js +1 -1
- package/browser/flatten.d.ts +5 -0
- package/browser/flatten.js +41 -0
- package/browser/hooks.js +5 -1
- package/browser/index.d.ts +1 -0
- package/browser/index.js +1 -0
- package/browser/validation/createValidator.d.ts +3 -0
- package/browser/validation/createValidator.js +8 -0
- package/browser/validation/types.d.ts +5 -2
- package/browser/validation/validation.test.js +92 -8
- package/browser/validation/withYup.js +28 -25
- package/browser/validation/withZod.js +16 -4
- package/build/ValidatedForm.js +1 -1
- package/build/flatten.d.ts +5 -0
- package/build/flatten.js +49 -0
- package/build/hooks.js +8 -1
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/validation/createValidator.d.ts +3 -0
- package/build/validation/createValidator.js +12 -0
- package/build/validation/types.d.ts +5 -2
- package/build/validation/validation.test.js +92 -8
- package/build/validation/withYup.js +28 -25
- package/build/validation/withZod.js +19 -4
- package/package.json +18 -13
- package/sample-app/.env +7 -0
- package/sample-app/README.md +53 -0
- package/sample-app/app/components/ErrorBox.tsx +34 -0
- package/sample-app/app/components/FormInput.tsx +40 -0
- package/sample-app/app/components/FormSelect.tsx +37 -0
- package/sample-app/app/components/SubjectForm.tsx +150 -0
- package/sample-app/app/entry.client.tsx +4 -0
- package/sample-app/app/entry.server.tsx +21 -0
- package/sample-app/app/root.tsx +92 -0
- package/sample-app/app/routes/index.tsx +5 -0
- package/sample-app/app/routes/subjects/$id.edit.tsx +100 -0
- package/sample-app/app/routes/subjects/index.tsx +112 -0
- package/sample-app/app/routes/subjects/new.tsx +48 -0
- package/sample-app/app/services/db.server.ts +23 -0
- package/sample-app/app/types.ts +6 -0
- package/sample-app/package-lock.json +4617 -0
- package/sample-app/package.json +36 -0
- package/sample-app/prisma/dev.db +0 -0
- package/sample-app/prisma/schema.prisma +34 -0
- package/sample-app/public/favicon.ico +0 -0
- package/sample-app/remix.config.js +10 -0
- package/sample-app/remix.env.d.ts +2 -0
@@ -30,6 +30,17 @@ const validationTestCases = [
|
|
30
30
|
firstName: yup.string().required(),
|
31
31
|
lastName: yup.string().required(),
|
32
32
|
age: yup.number(),
|
33
|
+
address: yup
|
34
|
+
.object({
|
35
|
+
streetAddress: yup.string().required(),
|
36
|
+
city: yup.string().required(),
|
37
|
+
country: yup.string().required(),
|
38
|
+
})
|
39
|
+
.required(),
|
40
|
+
pets: yup.array().of(yup.object({
|
41
|
+
animal: yup.string().required(),
|
42
|
+
name: yup.string().required(),
|
43
|
+
})),
|
33
44
|
})),
|
34
45
|
},
|
35
46
|
{
|
@@ -38,6 +49,18 @@ const validationTestCases = [
|
|
38
49
|
firstName: zod_1.z.string().nonempty(),
|
39
50
|
lastName: zod_1.z.string().nonempty(),
|
40
51
|
age: zod_1.z.optional(zod_1.z.number()),
|
52
|
+
address: zod_1.z.preprocess((value) => (value == null ? {} : value), zod_1.z.object({
|
53
|
+
streetAddress: zod_1.z.string().nonempty(),
|
54
|
+
city: zod_1.z.string().nonempty(),
|
55
|
+
country: zod_1.z.string().nonempty(),
|
56
|
+
})),
|
57
|
+
pets: zod_1.z
|
58
|
+
.object({
|
59
|
+
animal: zod_1.z.string().nonempty(),
|
60
|
+
name: zod_1.z.string().nonempty(),
|
61
|
+
})
|
62
|
+
.array()
|
63
|
+
.optional(),
|
41
64
|
})),
|
42
65
|
},
|
43
66
|
];
|
@@ -47,41 +70,102 @@ describe("Validation", () => {
|
|
47
70
|
describe.each(validationTestCases)("Adapter for $name", ({ validator }) => {
|
48
71
|
describe("validate", () => {
|
49
72
|
it("should return the data when valid", () => {
|
50
|
-
const
|
73
|
+
const person = {
|
51
74
|
firstName: "John",
|
52
75
|
lastName: "Doe",
|
53
76
|
age: 30,
|
77
|
+
address: {
|
78
|
+
streetAddress: "123 Main St",
|
79
|
+
city: "Anytown",
|
80
|
+
country: "USA",
|
81
|
+
},
|
82
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
54
83
|
};
|
55
|
-
expect(validator.validate(
|
56
|
-
data:
|
84
|
+
expect(validator.validate(person)).toEqual({
|
85
|
+
data: person,
|
57
86
|
error: undefined,
|
58
87
|
});
|
59
88
|
});
|
60
89
|
it("should return field errors when invalid", () => {
|
61
|
-
const obj = { age: "hi!" };
|
90
|
+
const obj = { age: "hi!", pets: [{ animal: "dog" }] };
|
62
91
|
expect(validator.validate(obj)).toEqual({
|
63
92
|
data: undefined,
|
64
93
|
error: {
|
65
94
|
firstName: anyString,
|
66
95
|
lastName: anyString,
|
67
96
|
age: anyString,
|
97
|
+
"address.city": anyString,
|
98
|
+
"address.country": anyString,
|
99
|
+
"address.streetAddress": anyString,
|
100
|
+
"pets[0].name": anyString,
|
68
101
|
},
|
69
102
|
});
|
70
103
|
});
|
71
104
|
});
|
72
105
|
describe("validateField", () => {
|
73
106
|
it("should not return an error if field is valid", () => {
|
74
|
-
const
|
107
|
+
const person = {
|
75
108
|
firstName: "John",
|
76
109
|
lastName: {}, // invalid, but we should only be validating firstName
|
77
110
|
};
|
78
|
-
expect(validator.validateField(
|
111
|
+
expect(validator.validateField(person, "firstName")).toEqual({
|
112
|
+
error: undefined,
|
113
|
+
});
|
114
|
+
});
|
115
|
+
it("should not return an error if a nested field is valid", () => {
|
116
|
+
const person = {
|
117
|
+
firstName: "John",
|
118
|
+
lastName: {},
|
119
|
+
address: {
|
120
|
+
streetAddress: "123 Main St",
|
121
|
+
city: "Anytown",
|
122
|
+
country: "USA",
|
123
|
+
},
|
124
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
125
|
+
};
|
126
|
+
expect(validator.validateField(person, "address.streetAddress")).toEqual({
|
127
|
+
error: undefined,
|
128
|
+
});
|
129
|
+
expect(validator.validateField(person, "address.city")).toEqual({
|
130
|
+
error: undefined,
|
131
|
+
});
|
132
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
133
|
+
error: undefined,
|
134
|
+
});
|
135
|
+
expect(validator.validateField(person, "pets[0].animal")).toEqual({
|
136
|
+
error: undefined,
|
137
|
+
});
|
138
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
79
139
|
error: undefined,
|
80
140
|
});
|
81
141
|
});
|
82
142
|
it("should return an error if field is invalid", () => {
|
83
|
-
const
|
84
|
-
|
143
|
+
const person = {
|
144
|
+
firstName: "John",
|
145
|
+
lastName: {},
|
146
|
+
address: {
|
147
|
+
streetAddress: "123 Main St",
|
148
|
+
city: 1234,
|
149
|
+
},
|
150
|
+
};
|
151
|
+
expect(validator.validateField(person, "lastName")).toEqual({
|
152
|
+
error: anyString,
|
153
|
+
});
|
154
|
+
});
|
155
|
+
it("should return an error if a nested field is invalid", () => {
|
156
|
+
const person = {
|
157
|
+
firstName: "John",
|
158
|
+
lastName: {},
|
159
|
+
address: {
|
160
|
+
streetAddress: "123 Main St",
|
161
|
+
city: 1234,
|
162
|
+
},
|
163
|
+
pets: [{ animal: "dog" }],
|
164
|
+
};
|
165
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
166
|
+
error: anyString,
|
167
|
+
});
|
168
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
85
169
|
error: anyString,
|
86
170
|
});
|
87
171
|
});
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.withYup = void 0;
|
4
|
+
const createValidator_1 = require("./createValidator");
|
4
5
|
const validationErrorToFieldErrors = (error) => {
|
5
6
|
const fieldErrors = {};
|
6
7
|
error.inner.forEach((innerError) => {
|
@@ -10,29 +11,31 @@ const validationErrorToFieldErrors = (error) => {
|
|
10
11
|
});
|
11
12
|
return fieldErrors;
|
12
13
|
};
|
13
|
-
const withYup = (validationSchema) =>
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
}
|
14
|
+
const withYup = (validationSchema) => {
|
15
|
+
return (0, createValidator_1.createValidator)({
|
16
|
+
validate: (data) => {
|
17
|
+
try {
|
18
|
+
const validated = validationSchema.validateSync(data, {
|
19
|
+
abortEarly: false,
|
20
|
+
});
|
21
|
+
return { data: validated, error: undefined };
|
22
|
+
}
|
23
|
+
catch (err) {
|
24
|
+
return {
|
25
|
+
error: validationErrorToFieldErrors(err),
|
26
|
+
data: undefined,
|
27
|
+
};
|
28
|
+
}
|
29
|
+
},
|
30
|
+
validateField: (data, field) => {
|
31
|
+
try {
|
32
|
+
validationSchema.validateSyncAt(field, data);
|
33
|
+
return {};
|
34
|
+
}
|
35
|
+
catch (err) {
|
36
|
+
return { error: err.message };
|
37
|
+
}
|
38
|
+
},
|
39
|
+
});
|
40
|
+
};
|
38
41
|
exports.withYup = withYup;
|
@@ -1,6 +1,12 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
6
|
exports.withZod = void 0;
|
7
|
+
const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
8
|
+
const toPath_1 = __importDefault(require("lodash/toPath"));
|
9
|
+
const createValidator_1 = require("./createValidator");
|
4
10
|
const getIssuesForError = (err) => {
|
5
11
|
return err.issues.flatMap((issue) => {
|
6
12
|
if ("unionErrors" in issue) {
|
@@ -11,15 +17,21 @@ const getIssuesForError = (err) => {
|
|
11
17
|
}
|
12
18
|
});
|
13
19
|
};
|
20
|
+
function pathToString(array) {
|
21
|
+
return array.reduce(function (string, item) {
|
22
|
+
var prefix = string === "" ? "" : ".";
|
23
|
+
return string + (isNaN(Number(item)) ? prefix + item : "[" + item + "]");
|
24
|
+
}, "");
|
25
|
+
}
|
14
26
|
function withZod(zodSchema) {
|
15
|
-
return {
|
27
|
+
return (0, createValidator_1.createValidator)({
|
16
28
|
validate: (value) => {
|
17
29
|
const result = zodSchema.safeParse(value);
|
18
30
|
if (result.success)
|
19
31
|
return { data: result.data, error: undefined };
|
20
32
|
const fieldErrors = {};
|
21
33
|
getIssuesForError(result.error).forEach((issue) => {
|
22
|
-
const path = issue.path
|
34
|
+
const path = pathToString(issue.path);
|
23
35
|
if (!fieldErrors[path])
|
24
36
|
fieldErrors[path] = issue.message;
|
25
37
|
});
|
@@ -31,9 +43,12 @@ function withZod(zodSchema) {
|
|
31
43
|
if (result.success)
|
32
44
|
return { error: undefined };
|
33
45
|
return {
|
34
|
-
error: (_a = getIssuesForError(result.error).find((issue) =>
|
46
|
+
error: (_a = getIssuesForError(result.error).find((issue) => {
|
47
|
+
const allPathsAsString = issue.path.map((p) => `${p}`);
|
48
|
+
return (0, isEqual_1.default)(allPathsAsString, (0, toPath_1.default)(field));
|
49
|
+
})) === null || _a === void 0 ? void 0 : _a.message,
|
35
50
|
};
|
36
51
|
},
|
37
|
-
};
|
52
|
+
});
|
38
53
|
}
|
39
54
|
exports.withZod = withZod;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "remix-validated-form",
|
3
|
-
"version": "1.1.0",
|
3
|
+
"version": "1.1.1-beta.0",
|
4
4
|
"description": "Form component and utils for easy form validation in remix",
|
5
5
|
"browser": "./browser/index.js",
|
6
6
|
"main": "./build/index.js",
|
@@ -14,7 +14,10 @@
|
|
14
14
|
"build:browser": "tsc --project tsconfig.json --module ESNext --outDir ./browser",
|
15
15
|
"build:main": "tsc --project tsconfig.json --module CommonJS --outDir ./build",
|
16
16
|
"build:tests": "tsc --project tsconfig.json --module CommonJS --outDir ./test-app/remix-validated-form",
|
17
|
+
"build:sample": "tsc --project tsconfig.json --module CommonJS --outDir ./sample-app/remix-validated-form",
|
18
|
+
"sample-app": "npm run build:sample && cd sample-app && npm run dev",
|
17
19
|
"test": "jest src",
|
20
|
+
"test:watch": "jest src --watch",
|
18
21
|
"lint": "eslint .",
|
19
22
|
"prettier": "prettier . --write",
|
20
23
|
"prepare": "husky install",
|
@@ -35,22 +38,23 @@
|
|
35
38
|
"validation"
|
36
39
|
],
|
37
40
|
"peerDependencies": {
|
38
|
-
"@remix-run/react": "^1.0.
|
39
|
-
"@remix-run/server-runtime": "^1.0.
|
41
|
+
"@remix-run/react": "^1.0.6",
|
42
|
+
"@remix-run/server-runtime": "^1.0.6",
|
40
43
|
"react": "^17.0.2"
|
41
44
|
},
|
42
45
|
"devDependencies": {
|
43
|
-
"@remix-run/react": "^1.0.
|
44
|
-
"@remix-run/server-runtime": "^1.0.
|
46
|
+
"@remix-run/react": "^1.0.6",
|
47
|
+
"@remix-run/server-runtime": "^1.0.6",
|
45
48
|
"@types/jest": "^27.0.3",
|
46
|
-
"@types/
|
47
|
-
"@
|
48
|
-
"@typescript-eslint/
|
49
|
+
"@types/lodash": "^4.14.178",
|
50
|
+
"@types/react": "^17.0.37",
|
51
|
+
"@typescript-eslint/eslint-plugin": "^5.6.0",
|
52
|
+
"@typescript-eslint/parser": "^5.6.0",
|
49
53
|
"babel-eslint": "^10.1.0",
|
50
54
|
"eslint": "^7.32.0",
|
51
55
|
"eslint-config-react-app": "^6.0.0",
|
52
56
|
"eslint-plugin-cypress": "^2.12.1",
|
53
|
-
"eslint-plugin-flowtype": "^
|
57
|
+
"eslint-plugin-flowtype": "^8.0.3",
|
54
58
|
"eslint-plugin-import": "^2.25.3",
|
55
59
|
"eslint-plugin-jsx-a11y": "^6.5.1",
|
56
60
|
"eslint-plugin-prettier": "^4.0.0",
|
@@ -58,16 +62,17 @@
|
|
58
62
|
"eslint-plugin-react-hooks": "^4.3.0",
|
59
63
|
"fetch-blob": "^3.1.3",
|
60
64
|
"husky": "^7.0.4",
|
61
|
-
"jest": "^27.
|
65
|
+
"jest": "^27.4.4",
|
62
66
|
"lint-staged": "^12.1.2",
|
63
|
-
"prettier": "^2.5.
|
67
|
+
"prettier": "^2.5.1",
|
64
68
|
"react": "^17.0.2",
|
65
|
-
"ts-jest": "^27.
|
66
|
-
"typescript": "^4.5.
|
69
|
+
"ts-jest": "^27.1.1",
|
70
|
+
"typescript": "^4.5.3",
|
67
71
|
"yup": "^0.32.11",
|
68
72
|
"zod": "^3.11.6"
|
69
73
|
},
|
70
74
|
"dependencies": {
|
75
|
+
"lodash": "^4.17.21",
|
71
76
|
"tiny-invariant": "^1.2.0"
|
72
77
|
},
|
73
78
|
"lint-staged": {
|
package/sample-app/.env
ADDED
@@ -0,0 +1,7 @@
|
|
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"
|
@@ -0,0 +1,53 @@
|
|
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
|
+
```
|
@@ -0,0 +1,34 @@
|
|
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
|
+
}
|
@@ -0,0 +1,40 @@
|
|
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
|
+
};
|
@@ -0,0 +1,37 @@
|
|
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
|
+
};
|
@@ -0,0 +1,150 @@
|
|
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
|
+
}
|