remix-validated-form 1.1.1-beta.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.
Files changed (97) hide show
  1. package/.turbo/turbo-build.log +9 -0
  2. package/.turbo/turbo-dev.log +0 -0
  3. package/browser/ValidatedForm.d.ts +22 -0
  4. package/browser/ValidatedForm.js +4 -1
  5. package/browser/hooks.d.ts +28 -3
  6. package/browser/hooks.js +12 -1
  7. package/browser/index.d.ts +2 -0
  8. package/browser/index.js +1 -0
  9. package/browser/{flatten.d.ts → internal/flatten.d.ts} +2 -3
  10. package/browser/{flatten.js → internal/flatten.js} +2 -8
  11. package/browser/internal/formContext.d.ts +18 -0
  12. package/browser/internal/formContext.js +0 -0
  13. package/browser/internal/util.d.ts +0 -0
  14. package/browser/internal/util.js +0 -0
  15. package/browser/server.d.ts +5 -0
  16. package/browser/server.js +5 -0
  17. package/browser/test-data/testFormData.d.ts +15 -0
  18. package/browser/test-data/testFormData.js +46 -0
  19. package/browser/validation/createValidator.d.ts +5 -1
  20. package/browser/validation/createValidator.js +15 -4
  21. package/browser/validation/types.d.ts +9 -0
  22. package/browser/validation/types.js +0 -0
  23. package/browser/validation/validation.test.d.ts +0 -0
  24. package/browser/validation/validation.test.js +65 -0
  25. package/browser/validation/withYup.d.ts +3 -0
  26. package/browser/validation/withYup.js +3 -0
  27. package/browser/validation/withZod.d.ts +3 -0
  28. package/browser/validation/withZod.js +3 -0
  29. package/build/ValidatedForm.d.ts +22 -0
  30. package/build/ValidatedForm.js +4 -1
  31. package/build/hooks.d.ts +28 -3
  32. package/build/hooks.js +12 -1
  33. package/build/index.d.ts +2 -0
  34. package/build/index.js +1 -0
  35. package/build/{flatten.d.ts → internal/flatten.d.ts} +2 -3
  36. package/build/{flatten.js → internal/flatten.js} +4 -10
  37. package/build/internal/formContext.d.ts +18 -0
  38. package/build/internal/formContext.js +0 -0
  39. package/build/internal/util.d.ts +0 -0
  40. package/build/internal/util.js +0 -0
  41. package/build/server.d.ts +5 -0
  42. package/build/server.js +5 -0
  43. package/build/test-data/testFormData.d.ts +15 -0
  44. package/build/test-data/testFormData.js +50 -0
  45. package/build/validation/createValidator.d.ts +5 -1
  46. package/build/validation/createValidator.js +15 -4
  47. package/build/validation/types.d.ts +9 -0
  48. package/build/validation/types.js +0 -0
  49. package/build/validation/validation.test.d.ts +0 -0
  50. package/build/validation/validation.test.js +65 -0
  51. package/build/validation/withYup.d.ts +3 -0
  52. package/build/validation/withYup.js +3 -0
  53. package/build/validation/withZod.d.ts +3 -0
  54. package/build/validation/withZod.js +3 -0
  55. package/jest.config.js +5 -0
  56. package/package.json +7 -32
  57. package/src/ValidatedForm.tsx +151 -0
  58. package/src/hooks.ts +60 -0
  59. package/src/index.ts +8 -0
  60. package/src/internal/flatten.ts +48 -0
  61. package/src/internal/formContext.ts +36 -0
  62. package/src/internal/util.ts +23 -0
  63. package/src/server.ts +10 -0
  64. package/src/test-data/testFormData.ts +55 -0
  65. package/src/validation/createValidator.ts +24 -0
  66. package/src/validation/types.ts +26 -0
  67. package/src/validation/validation.test.ts +317 -0
  68. package/src/validation/withYup.ts +43 -0
  69. package/src/validation/withZod.ts +51 -0
  70. package/tsconfig.json +5 -0
  71. package/.eslintcache +0 -1
  72. package/.eslintignore +0 -1
  73. package/.prettierignore +0 -10
  74. package/LICENSE +0 -21
  75. package/README.md +0 -230
  76. package/sample-app/.env +0 -7
  77. package/sample-app/README.md +0 -53
  78. package/sample-app/app/components/ErrorBox.tsx +0 -34
  79. package/sample-app/app/components/FormInput.tsx +0 -40
  80. package/sample-app/app/components/FormSelect.tsx +0 -37
  81. package/sample-app/app/components/SubjectForm.tsx +0 -150
  82. package/sample-app/app/entry.client.tsx +0 -4
  83. package/sample-app/app/entry.server.tsx +0 -21
  84. package/sample-app/app/root.tsx +0 -92
  85. package/sample-app/app/routes/index.tsx +0 -5
  86. package/sample-app/app/routes/subjects/$id.edit.tsx +0 -100
  87. package/sample-app/app/routes/subjects/index.tsx +0 -112
  88. package/sample-app/app/routes/subjects/new.tsx +0 -48
  89. package/sample-app/app/services/db.server.ts +0 -23
  90. package/sample-app/app/types.ts +0 -6
  91. package/sample-app/package-lock.json +0 -4617
  92. package/sample-app/package.json +0 -36
  93. package/sample-app/prisma/dev.db +0 -0
  94. package/sample-app/prisma/schema.prisma +0 -34
  95. package/sample-app/public/favicon.ico +0 -0
  96. package/sample-app/remix.config.js +0 -10
  97. package/sample-app/remix.env.d.ts +0 -2
@@ -1,100 +0,0 @@
1
- import { Box, Container, Heading } from "@chakra-ui/react";
2
- import {
3
- ActionFunction,
4
- json,
5
- LoaderFunction,
6
- redirect,
7
- useCatch,
8
- useLoaderData,
9
- useParams,
10
- } from "remix";
11
- import { z } from "zod";
12
- import { ErrorBox } from "~/components/ErrorBox";
13
- import { SubjectForm, subjectFormValidator } from "~/components/SubjectForm";
14
- import { db } from "~/services/db.server";
15
- import { SubjectComplete } from "~/types";
16
- import { validationError } from "../../../remix-validated-form";
17
-
18
- export const loader: LoaderFunction = async ({ params }) => {
19
- const { id } = z.object({ id: z.string() }).parse(params);
20
-
21
- const subject = await db.subject.findUnique({
22
- where: { id: +id },
23
- include: {
24
- teacher: true,
25
- subjectDays: { orderBy: { day: "asc" } },
26
- },
27
- });
28
-
29
- if (!subject) {
30
- throw new Response(`Subject ${id} doesn't exist`, {
31
- status: 404,
32
- });
33
- }
34
-
35
- return json(subject);
36
- };
37
-
38
- export const action: ActionFunction = async ({ request, params }) => {
39
- const { id } = z.object({ id: z.string() }).parse(params);
40
-
41
- const fieldValues = subjectFormValidator.validate(
42
- Object.fromEntries(await request.formData())
43
- );
44
- if (fieldValues.error) return validationError(fieldValues.error);
45
-
46
- const { teacher, subjectDays, ...updatedSubject } = fieldValues.data;
47
-
48
- await db.subject.update({
49
- where: { id: +id },
50
- data: {
51
- ...updatedSubject,
52
- teacher: {
53
- update: teacher,
54
- },
55
- subjectDays: {
56
- deleteMany: {},
57
- create: subjectDays,
58
- },
59
- },
60
- });
61
-
62
- return redirect("/subjects");
63
- };
64
-
65
- export default function NewSubject() {
66
- const subject = useLoaderData<SubjectComplete | null>();
67
- return (
68
- <>
69
- <Box bg="white" pt="4" pb="4" shadow="sm">
70
- <Container maxW="7xl">
71
- <Heading size="lg" mb="0">
72
- Create Subject
73
- </Heading>
74
- </Container>
75
- </Box>
76
-
77
- <Box as="main" py="8" flex="1">
78
- <Container maxW="7xl">
79
- <SubjectForm defaultValues={subject ?? undefined} />
80
- </Container>
81
- </Box>
82
- </>
83
- );
84
- }
85
-
86
- export function CatchBoundary() {
87
- const caught = useCatch();
88
- const params = useParams();
89
- if (caught.status === 404) {
90
- return <ErrorBox title={`Subject ${params.id} doesn't exist`} />;
91
- }
92
- throw new Error(`Unhandled error: ${caught.status}`);
93
- }
94
-
95
- export function ErrorBoundary() {
96
- const { id } = useParams();
97
- return (
98
- <ErrorBox title={`There was an error subject by the id ${id}. Sorry.`} />
99
- );
100
- }
@@ -1,112 +0,0 @@
1
- import {
2
- Box,
3
- Button,
4
- Container,
5
- Flex,
6
- Heading,
7
- Spacer,
8
- Table,
9
- Tbody,
10
- Td,
11
- Th,
12
- Thead,
13
- Tr,
14
- } from "@chakra-ui/react";
15
- import { Subject, SubjectDays, Teacher } from "@prisma/client";
16
- import {
17
- json,
18
- Link,
19
- LoaderFunction,
20
- useCatch,
21
- useLoaderData,
22
- useParams,
23
- } from "remix";
24
- import { db } from "~/services/db.server";
25
-
26
- export const loader: LoaderFunction = async () => {
27
- const subjects = await db.subject.findMany({
28
- include: { teacher: true, subjectDays: true },
29
- });
30
- return json(subjects);
31
- };
32
-
33
- export default function Subjects() {
34
- const subjects =
35
- useLoaderData<
36
- (Subject & { teacher: Teacher; subjectDays: SubjectDays[] })[]
37
- >();
38
- return (
39
- <>
40
- <Box bg="white" pt="4" pb="4" shadow="sm">
41
- <Container maxW="7xl">
42
- <Flex>
43
- <Heading size="lg" mb="0">
44
- Subjects
45
- </Heading>
46
- <Spacer />
47
- <Link to="new">
48
- <Button colorScheme="blue">New</Button>
49
- </Link>
50
- </Flex>
51
- </Container>
52
- </Box>
53
-
54
- <Box as="main" py="8" flex="1">
55
- <Container maxW="7xl">
56
- <Box bg="white" p="6" rounded="lg" shadow="base" overflowX="auto">
57
- <Table borderWidth="1px" fontSize="sm">
58
- <Thead bg="white">
59
- <Tr>
60
- <Th whiteSpace="nowrap" scope="col">
61
- Subject
62
- </Th>
63
- <Th whiteSpace="nowrap" scope="col">
64
- Teacher
65
- </Th>
66
- <Th whiteSpace="nowrap" scope="col"></Th>
67
- </Tr>
68
- </Thead>
69
- <Tbody>
70
- {subjects.map((subject) => (
71
- <Tr key={subject.id}>
72
- <Td whiteSpace="nowrap">{subject.name}</Td>
73
- <Td>
74
- {subject.teacher.name} ({subject.teacher.email})
75
- </Td>
76
- <Td textAlign="right">
77
- <Link to={`${subject.id}/edit`}>
78
- <Button variant="link" colorScheme="blue">
79
- Edit
80
- </Button>
81
- </Link>
82
- </Td>
83
- </Tr>
84
- ))}
85
- </Tbody>
86
- </Table>
87
- </Box>
88
- </Container>
89
- </Box>
90
- </>
91
- );
92
- }
93
-
94
- export function CatchBoundary() {
95
- const caught = useCatch();
96
- const params = useParams();
97
- if (caught.status === 404) {
98
- return (
99
- <div className="error-container">
100
- Huh? What the heck is "{params.id}"?
101
- </div>
102
- );
103
- }
104
- throw new Error(`Unhandled error: ${caught.status}`);
105
- }
106
-
107
- export function ErrorBoundary() {
108
- const { jokeId } = useParams();
109
- return (
110
- <div className="error-container">{`There was an error loading joke by the id ${jokeId}. Sorry.`}</div>
111
- );
112
- }
@@ -1,48 +0,0 @@
1
- import { Box, Container, Heading } from "@chakra-ui/react";
2
- import { ActionFunction, redirect } from "remix";
3
- import { SubjectForm, subjectFormValidator } from "~/components/SubjectForm";
4
- import { db } from "~/services/db.server";
5
- import { validationError } from "../../../remix-validated-form";
6
-
7
- export const action: ActionFunction = async ({ request }) => {
8
- const fieldValues = subjectFormValidator.validate(
9
- Object.fromEntries(await request.formData())
10
- );
11
- if (fieldValues.error) return validationError(fieldValues.error);
12
-
13
- const { teacher, subjectDays, ...newSubject } = fieldValues.data;
14
-
15
- await db.subject.create({
16
- data: {
17
- ...newSubject,
18
- teacher: {
19
- create: teacher,
20
- },
21
- subjectDays: {
22
- create: subjectDays,
23
- },
24
- },
25
- });
26
-
27
- return redirect("/subjects");
28
- };
29
-
30
- export default function NewSubject() {
31
- return (
32
- <>
33
- <Box bg="white" pt="4" pb="4" shadow="sm">
34
- <Container maxW="7xl">
35
- <Heading size="lg" mb="0">
36
- Create Subject
37
- </Heading>
38
- </Container>
39
- </Box>
40
-
41
- <Box as="main" py="8" flex="1">
42
- <Container maxW="7xl">
43
- <SubjectForm />
44
- </Container>
45
- </Box>
46
- </>
47
- );
48
- }
@@ -1,23 +0,0 @@
1
- import { PrismaClient } from "@prisma/client";
2
-
3
- let db: PrismaClient;
4
-
5
- declare global {
6
- var __db: PrismaClient | undefined;
7
- }
8
-
9
- // this is needed because in development we don't want to restart
10
- // the server with every change, but we want to make sure we don't
11
- // create a new connection to the DB with every change either.
12
- if (process.env.NODE_ENV === "production") {
13
- db = new PrismaClient();
14
- db.$connect();
15
- } else {
16
- if (!global.__db) {
17
- global.__db = new PrismaClient();
18
- global.__db.$connect();
19
- }
20
- db = global.__db;
21
- }
22
-
23
- export { db };
@@ -1,6 +0,0 @@
1
- import { Subject, SubjectDays, Teacher } from "@prisma/client";
2
-
3
- export type SubjectComplete = Subject & {
4
- teacher: Teacher;
5
- subjectDays: SubjectDays[];
6
- };