@springmicro/forms 0.7.15 → 0.7.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@springmicro/forms",
3
- "version": "0.7.15",
3
+ "version": "0.7.16",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -20,16 +20,17 @@
20
20
  "@emotion/styled": "^11.11.5",
21
21
  "@mui/icons-material": "^5.15.18",
22
22
  "@mui/material": "^5.15.20",
23
- "@rjsf/core": "^5.10.0",
24
- "@rjsf/utils": "^5.10.0",
25
- "@rjsf/validator-ajv8": "^5.10.0",
26
- "@springmicro/utils": "^0.7.15",
23
+ "@rjsf/core": "^6.2.5",
24
+ "@rjsf/shadcn": "^6.2.5",
25
+ "@rjsf/utils": "^6.2.5",
26
+ "@rjsf/validator-ajv8": "^6.2.5",
27
+ "@springmicro/utils": "^0.7.16",
27
28
  "lodash": "^4.17.21",
28
29
  "nanoid": "^5.0.7",
29
- "react": "^18.2.0",
30
+ "react": "^19.2.4",
30
31
  "react-beautiful-dnd": "^13.1.1",
31
32
  "react-datepicker": "^4.16.0",
32
- "react-dom": "^18.2.0",
33
+ "react-dom": "^19.2.4",
33
34
  "react-select": "^5.7.4",
34
35
  "uuid": "^9.0.1"
35
36
  },
@@ -49,5 +50,5 @@
49
50
  "vite": "^5.2.0",
50
51
  "vite-plugin-dts": "^3.9.0"
51
52
  },
52
- "gitHead": "999f4d500555ee2474404a1baa353945a82781db"
53
+ "gitHead": "3969208c879eb53f646849f05369b1ee2811f8ac"
53
54
  }
@@ -0,0 +1,184 @@
1
+ import React from "react";
2
+ import Form from "@rjsf/shadcn";
3
+ import validator from "@rjsf/validator-ajv8";
4
+ import type { RJSFSchema, SubmitButtonProps } from "@rjsf/utils";
5
+ import { getSubmitButtonOptions } from "@rjsf/utils";
6
+ import type { FormProps, IChangeEvent } from "@rjsf/core";
7
+ import { Button } from "./ui/button";
8
+ import { Spinner } from "./ui/spinner";
9
+
10
+ import { Alert, AlertDescription, AlertTitle } from "./ui/alert";
11
+ import { AlertTriangleIcon, CheckCircle2Icon } from "lucide-react";
12
+
13
+ type FormResult = {
14
+ has_error: boolean;
15
+ detail: string;
16
+ };
17
+
18
+ type FormStateContextProps = {
19
+ isSubmitting: boolean;
20
+ formResult: FormResult | null;
21
+ };
22
+
23
+ const FormStateContext = React.createContext<FormStateContextProps>({
24
+ isSubmitting: false,
25
+ formResult: null,
26
+ });
27
+
28
+ const SubmissionProvider = ({
29
+ children,
30
+ isSubmitting,
31
+ formResult,
32
+ }: React.PropsWithChildren<FormStateContextProps>) => {
33
+ return (
34
+ <FormStateContext.Provider value={{ isSubmitting, formResult }}>
35
+ {children}
36
+ </FormStateContext.Provider>
37
+ );
38
+ };
39
+
40
+ const useSubmission = () => {
41
+ return React.useContext(FormStateContext);
42
+ };
43
+
44
+ function FormAlert(formResult: FormResult) {
45
+ const classes = formResult.has_error
46
+ ? "mt-2 max-w-md border-red-200 bg-red-50 text-red-900"
47
+ : "mt-2 max-w-md border-green-200 bg-green-50 text-green-900";
48
+ return (
49
+ <Alert className={classes}>
50
+ {formResult.has_error ? <AlertTriangleIcon /> : <CheckCircle2Icon />}
51
+ <AlertTitle>{formResult.detail}</AlertTitle>
52
+ </Alert>
53
+ );
54
+ }
55
+
56
+ // https://rjsf-team.github.io/react-jsonschema-form/docs/advanced-customization/custom-templates/#submitbutton
57
+ function SubmitButton(props: SubmitButtonProps) {
58
+ const { uiSchema } = props;
59
+ const { norender } = getSubmitButtonOptions(uiSchema);
60
+ const { isSubmitting, formResult } = useSubmission();
61
+ if (norender) {
62
+ return null;
63
+ }
64
+ if (formResult !== null) {
65
+ return <FormAlert {...formResult} />;
66
+ }
67
+ return (
68
+ <Button
69
+ type="submit"
70
+ className="mt-2 cursor-pointer"
71
+ disabled={isSubmitting || formResult !== null}
72
+ >
73
+ {isSubmitting ? (
74
+ <>
75
+ <Spinner /> Submitting...
76
+ </>
77
+ ) : (
78
+ "Submit"
79
+ )}
80
+ </Button>
81
+ );
82
+ }
83
+
84
+ const requestAQuote_HARDCODED = {
85
+ schema: {
86
+ $id: "94648a2a-7561-4082-b4d9-31e7a71bceb4",
87
+ type: "object",
88
+ properties: {
89
+ name: { type: "string", maxLength: 70, title: "Name" },
90
+ email: {
91
+ type: "string",
92
+ format: "email",
93
+ maxLength: 255,
94
+ title: "Email",
95
+ },
96
+ message: { type: "string", title: "Message" },
97
+ phone: { type: "string" },
98
+ },
99
+ required: ["name", "email", "message"],
100
+ },
101
+ uiSchema: {
102
+ name: { "ui:autofocus": true },
103
+ email: {},
104
+ message: { "ui:options": { widget: "textarea", rows: 3 } },
105
+ phone: { "ui:widget": "hidden" },
106
+ },
107
+ formData: {},
108
+ };
109
+
110
+ export function FormRenderer({
111
+ id,
112
+ test = false,
113
+ }: {
114
+ id: string;
115
+ test?: boolean;
116
+ }) {
117
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
118
+ const [formResult, setFormResult] = React.useState<FormResult | null>(null);
119
+ const [form, setForm] = React.useState<
120
+ Omit<FormProps<any, RJSFSchema, any>, "validator"> | undefined
121
+ >();
122
+
123
+ React.useEffect(() => {
124
+ if (id === null) return;
125
+ if (test) {
126
+ // @ts-ignore
127
+ setForm(requestAQuote_HARDCODED);
128
+ return;
129
+ }
130
+
131
+ fetch(`/api/forms/forms/${id}`).then((res) => {
132
+ res.json().then((data) => {
133
+ data.schema = data.jsonSchema;
134
+ delete data.jsonSchema;
135
+ setForm(data);
136
+ });
137
+ });
138
+ }, [test, id]);
139
+
140
+ const onSubmit = (
141
+ data: IChangeEvent<any, RJSFSchema, any>,
142
+ event: React.FormEvent<any>,
143
+ ) => {
144
+ setIsSubmitting(true);
145
+ fetch(`/api/forms/submit/${id}`, {
146
+ headers: {
147
+ "content-type": "application/json",
148
+ },
149
+ method: "POST",
150
+ body: JSON.stringify({ data: data.formData }),
151
+ })
152
+ .then((res) => {
153
+ setIsSubmitting(false);
154
+ // console.log(res);
155
+ res.json().then((data) => [
156
+ setFormResult({
157
+ has_error: !res.ok,
158
+ detail: data.detail,
159
+ }),
160
+ ]);
161
+ })
162
+ .catch((err) => {
163
+ setIsSubmitting(false);
164
+ // console.error(err);
165
+ setFormResult({
166
+ has_error: true,
167
+ detail: err,
168
+ });
169
+ });
170
+ };
171
+
172
+ if (id === null) return <>No form selected.</>;
173
+ else if (form === undefined) return <></>;
174
+ return (
175
+ <SubmissionProvider isSubmitting={isSubmitting} formResult={formResult}>
176
+ <Form
177
+ {...form}
178
+ validator={validator}
179
+ onSubmit={onSubmit}
180
+ templates={{ ButtonTemplates: { SubmitButton } }}
181
+ />
182
+ </SubmissionProvider>
183
+ );
184
+ }
package/src/index.tsx CHANGED
@@ -6,6 +6,7 @@ import type { IChangeEvent } from "@rjsf/core";
6
6
  import type { RJSFSchema } from "@rjsf/utils";
7
7
  import validator from "@rjsf/validator-ajv8";
8
8
  import { emailOnlySG, emailOnlySGJSON } from "@springmicro/utils/forms";
9
+ import { FormRenderer } from "./FormRenderer";
9
10
 
10
11
  const rjsfDaisyUiTheme: ThemeProps = {
11
12
  fields,
@@ -23,4 +24,5 @@ export {
23
24
  BaseForm,
24
25
  emailOnlySG,
25
26
  emailOnlySGJSON,
27
+ FormRenderer,
26
28
  };
@@ -0,0 +1,6 @@
1
+ import { clsx, type ClassValue } from "clsx"
2
+ import { twMerge } from "tailwind-merge"
3
+
4
+ export function cn(...inputs: ClassValue[]) {
5
+ return twMerge(clsx(inputs))
6
+ }
@@ -0,0 +1,66 @@
1
+ import * as React from "react";
2
+ import { cva, type VariantProps } from "class-variance-authority";
3
+
4
+ import { cn } from "../lib/utils";
5
+
6
+ const alertVariants = cva(
7
+ "relative w-full rounded-lg border px-4 py-3 text-sm grid has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] grid-cols-[0_1fr] has-[>svg]:gap-x-3 gap-y-0.5 items-start [&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current",
8
+ {
9
+ variants: {
10
+ variant: {
11
+ default: "bg-card text-card-foreground",
12
+ destructive:
13
+ "text-destructive bg-card [&>svg]:text-current *:data-[slot=alert-description]:text-destructive/90",
14
+ },
15
+ },
16
+ defaultVariants: {
17
+ variant: "default",
18
+ },
19
+ },
20
+ );
21
+
22
+ function Alert({
23
+ className,
24
+ variant,
25
+ ...props
26
+ }: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
27
+ return (
28
+ <div
29
+ data-slot="alert"
30
+ role="alert"
31
+ className={cn(alertVariants({ variant }), className)}
32
+ {...props}
33
+ />
34
+ );
35
+ }
36
+
37
+ function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
38
+ return (
39
+ <div
40
+ data-slot="alert-title"
41
+ className={cn(
42
+ "col-start-2 line-clamp-1 min-h-4 font-medium tracking-tight",
43
+ className,
44
+ )}
45
+ {...props}
46
+ />
47
+ );
48
+ }
49
+
50
+ function AlertDescription({
51
+ className,
52
+ ...props
53
+ }: React.ComponentProps<"div">) {
54
+ return (
55
+ <div
56
+ data-slot="alert-description"
57
+ className={cn(
58
+ "text-muted-foreground col-start-2 grid justify-items-start gap-1 text-sm [&_p]:leading-relaxed",
59
+ className,
60
+ )}
61
+ {...props}
62
+ />
63
+ );
64
+ }
65
+
66
+ export { Alert, AlertTitle, AlertDescription };
@@ -0,0 +1,64 @@
1
+ import * as React from "react";
2
+ import { Slot } from "@radix-ui/react-slot";
3
+ import { cva, type VariantProps } from "class-variance-authority";
4
+
5
+ import { cn } from "../lib/utils";
6
+
7
+ const buttonVariants = cva(
8
+ "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 shrink-0 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
9
+ {
10
+ variants: {
11
+ variant: {
12
+ default: "bg-primary text-primary-foreground hover:bg-primary/90",
13
+ destructive:
14
+ "bg-destructive text-white hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
15
+ outline:
16
+ "border bg-background shadow-xs hover:bg-accent hover:text-accent-foreground dark:bg-input/30 dark:border-input dark:hover:bg-input/50",
17
+ secondary:
18
+ "bg-secondary text-secondary-foreground hover:bg-secondary/80",
19
+ ghost:
20
+ "hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50",
21
+ link: "text-primary underline-offset-4 hover:underline",
22
+ },
23
+ size: {
24
+ default: "h-9 px-4 py-2 has-[>svg]:px-3",
25
+ xs: "h-6 gap-1 rounded-md px-2 text-xs has-[>svg]:px-1.5 [&_svg:not([class*='size-'])]:size-3",
26
+ sm: "h-8 rounded-md gap-1.5 px-3 has-[>svg]:px-2.5",
27
+ lg: "h-10 rounded-md px-6 has-[>svg]:px-4",
28
+ icon: "size-9",
29
+ "icon-xs": "size-6 rounded-md [&_svg:not([class*='size-'])]:size-3",
30
+ "icon-sm": "size-8",
31
+ "icon-lg": "size-10",
32
+ },
33
+ },
34
+ defaultVariants: {
35
+ variant: "default",
36
+ size: "default",
37
+ },
38
+ },
39
+ );
40
+
41
+ function Button({
42
+ className,
43
+ variant = "default",
44
+ size = "default",
45
+ asChild = false,
46
+ ...props
47
+ }: React.ComponentProps<"button"> &
48
+ VariantProps<typeof buttonVariants> & {
49
+ asChild?: boolean;
50
+ }) {
51
+ const Comp = asChild ? Slot : "button";
52
+
53
+ return (
54
+ <Comp
55
+ data-slot="button"
56
+ data-variant={variant}
57
+ data-size={size}
58
+ className={cn(buttonVariants({ variant, size, className }))}
59
+ {...props}
60
+ />
61
+ );
62
+ }
63
+
64
+ export { Button, buttonVariants };
@@ -0,0 +1,16 @@
1
+ import { Loader2Icon } from "lucide-react";
2
+
3
+ import { cn } from "../lib/utils";
4
+
5
+ function Spinner({ className, ...props }: React.ComponentProps<"svg">) {
6
+ return (
7
+ <Loader2Icon
8
+ role="status"
9
+ aria-label="Loading"
10
+ className={cn("size-4 animate-spin", className)}
11
+ {...props}
12
+ />
13
+ );
14
+ }
15
+
16
+ export { Spinner };