react-server-actions 1.0.0 → 1.0.4
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/README.md +161 -4
- package/dist/client/helpers.d.ts +28 -1
- package/dist/client/helpers.d.ts.map +1 -1
- package/dist/client/helpers.js +56 -4
- package/dist/client/helpers.js.map +1 -1
- package/dist/client/index.d.ts +9 -4
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +39 -13
- package/dist/client/index.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/server/actions.d.ts +8 -4
- package/dist/server/actions.d.ts.map +1 -1
- package/dist/server/actions.js +2 -2
- package/dist/server/actions.js.map +1 -1
- package/dist/server/helpers.d.ts +21 -11
- package/dist/server/helpers.d.ts.map +1 -1
- package/dist/server/helpers.js +22 -17
- package/dist/server/helpers.js.map +1 -1
- package/dist/server/types.d.ts +8 -4
- package/dist/server/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,161 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
# Next Native Actions
|
|
2
|
+
|
|
3
|
+
A lightweight library for handling form actions in Next.js applications using native browser capabilities and server actions.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
### Server-Side Features
|
|
8
|
+
|
|
9
|
+
#### Direct React Server Actions Integration
|
|
10
|
+
|
|
11
|
+
- Seamlessly works with Next.js and React Server Actions
|
|
12
|
+
- No additional server-side middleware required
|
|
13
|
+
- Type-safe action handling
|
|
14
|
+
|
|
15
|
+
#### Structured Action Responses
|
|
16
|
+
|
|
17
|
+
- Standardized response shape for consistent error and success handling
|
|
18
|
+
- Type-safe responses with proper error typing
|
|
19
|
+
- Built-in support for validation errors and server errors
|
|
20
|
+
|
|
21
|
+
#### Form Data Processing
|
|
22
|
+
|
|
23
|
+
- Built-in form data decoder for easy access to form fields
|
|
24
|
+
- Support for complex form structures including nested objects and arrays
|
|
25
|
+
- Automatic type inference from your Zod schemas
|
|
26
|
+
|
|
27
|
+
#### Zod Schema Validation
|
|
28
|
+
|
|
29
|
+
- Server-side validation using Zod schemas
|
|
30
|
+
- Type inference for both client and server
|
|
31
|
+
- Detailed validation error messages
|
|
32
|
+
- Custom validation rules support
|
|
33
|
+
|
|
34
|
+
#### Form State Management
|
|
35
|
+
|
|
36
|
+
- Optional form data persistence between submissions
|
|
37
|
+
- Ability to reset form data after successful submission
|
|
38
|
+
- State management utilities for handling loading and error states
|
|
39
|
+
|
|
40
|
+
### Client-Side Features
|
|
41
|
+
|
|
42
|
+
#### Framework Agnostic
|
|
43
|
+
|
|
44
|
+
- Works with any form management library of your choice
|
|
45
|
+
- Native support for React Hook Form, Formik, or plain HTML forms
|
|
46
|
+
- Zero client-side dependencies required
|
|
47
|
+
|
|
48
|
+
#### Native HTML Validation
|
|
49
|
+
|
|
50
|
+
- Automatic HTML5 validation attributes from Zod schemas
|
|
51
|
+
- Client-side validation before server submission
|
|
52
|
+
- Improved user experience with instant feedback
|
|
53
|
+
- Accessibility-friendly validation messages
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
bash
|
|
58
|
+
npm install next-native-actions
|
|
59
|
+
|
|
60
|
+
## Basic Usage
|
|
61
|
+
|
|
62
|
+
### 1. Define your schema and action
|
|
63
|
+
|
|
64
|
+
typescript:next-native-actions/README.md
|
|
65
|
+
import { z } from 'zod';
|
|
66
|
+
import { createAction } from 'next-native-actions';
|
|
67
|
+
const userSchema = z.object({
|
|
68
|
+
name: z.string().min(2),
|
|
69
|
+
email: z.string().email(),
|
|
70
|
+
age: z.number().min(18)
|
|
71
|
+
});
|
|
72
|
+
export const createUser = createAction(userSchema, async (data) => {
|
|
73
|
+
// Your server logic here
|
|
74
|
+
return { success: true, data };
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
### 2. Use in your form component
|
|
78
|
+
|
|
79
|
+
typescript
|
|
80
|
+
'use client';
|
|
81
|
+
import { useAction } from 'next-native-actions/client';
|
|
82
|
+
export function UserForm() {
|
|
83
|
+
const { action, isLoading } = useAction(createUser);
|
|
84
|
+
return (
|
|
85
|
+
|
|
86
|
+
<form action={action}>
|
|
87
|
+
<input name="name" type="text" required minLength={2} />
|
|
88
|
+
<input name="email" type="email" required />
|
|
89
|
+
<input name="age" type="number" required min={18} />
|
|
90
|
+
<button disabled={isLoading}>Submit</button>
|
|
91
|
+
</form>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
## Advanced Features
|
|
96
|
+
|
|
97
|
+
### Custom Form Libraries Integration
|
|
98
|
+
|
|
99
|
+
The library works seamlessly with popular form management libraries:
|
|
100
|
+
|
|
101
|
+
typescript
|
|
102
|
+
// With React Hook Form
|
|
103
|
+
import { useForm } from 'react-hook-form';
|
|
104
|
+
import { zodResolver } from '@hookform/resolvers/zod';
|
|
105
|
+
export function UserFormWithRHF() {
|
|
106
|
+
const form = useForm({
|
|
107
|
+
resolver: zodResolver(userSchema)
|
|
108
|
+
});
|
|
109
|
+
// ... rest of your form implementation
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
### Error Handling
|
|
113
|
+
|
|
114
|
+
typescript
|
|
115
|
+
'use client';
|
|
116
|
+
export function UserForm() {
|
|
117
|
+
const { action, isLoading, error } = useAction(createUser);
|
|
118
|
+
return (
|
|
119
|
+
|
|
120
|
+
<form action={action}>
|
|
121
|
+
{error && <div className="error">{error.message}</div>}
|
|
122
|
+
{/ form fields /}
|
|
123
|
+
</form>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
## Caveats
|
|
128
|
+
|
|
129
|
+
### datetime-local input type
|
|
130
|
+
|
|
131
|
+
TODO: Explain how to use it
|
|
132
|
+
|
|
133
|
+
### select defaultValue
|
|
134
|
+
|
|
135
|
+
In react 19 there is an open issue https://github.com/facebook/react/issues/30580 that prevents the defaultValue to correctly set the select value.
|
|
136
|
+
According to this comment https://github.com/facebook/react/issues/30580#issuecomment-2537962675 there is a workaround by setting the 'key' attribute of the select
|
|
137
|
+
|
|
138
|
+
## Best Practices
|
|
139
|
+
|
|
140
|
+
1. Always define your schemas in a separate file for better reusability
|
|
141
|
+
2. Use type inference from your schemas for better type safety
|
|
142
|
+
3. Implement proper error handling both on client and server
|
|
143
|
+
4. Consider using progressive enhancement for better user experience
|
|
144
|
+
5. Follow accessibility guidelines when displaying validation messages
|
|
145
|
+
|
|
146
|
+
## TypeScript Support
|
|
147
|
+
|
|
148
|
+
The library is written in TypeScript and provides full type safety:
|
|
149
|
+
|
|
150
|
+
- Automatic type inference from Zod schemas
|
|
151
|
+
- Type-safe action responses
|
|
152
|
+
- Proper error typing
|
|
153
|
+
- IDE autocompletion support
|
|
154
|
+
|
|
155
|
+
## Contributing
|
|
156
|
+
|
|
157
|
+
Contributions are welcome! Please read our contributing guidelines before submitting a pull request.
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
MIT
|
package/dist/client/helpers.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Get the html5 validation attributes from a Zod schema field
|
|
4
|
+
* @param schema - The Zod schema
|
|
5
|
+
* @param path - The path to the field in the schema
|
|
6
|
+
* @returns The validation attributes for the field
|
|
7
|
+
* @note It would be cool to infer also the "type" attribute of the field, but this would not be consistent because a zod rule is not a 1-1 relation with an html input.
|
|
8
|
+
* For example, a zod.number() could be an <input type="number" /> but also a <select>. A z.date() could be represented by a <input type="date" /> but also a <input type="datetime-local" />.
|
|
9
|
+
* We could make a "guess" based on the zod rule, and then could be overriden by the user, but this would lead to confusion.
|
|
10
|
+
* So we leave it as a parameter for now.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getZodValidationAttributes(schema: z.ZodTypeAny, path: string[], options?: {
|
|
13
|
+
inferTypeAttr?: boolean;
|
|
14
|
+
}): {
|
|
15
|
+
type: 'string' | 'number' | 'date' | 'boolean' | 'enum';
|
|
16
|
+
attrs: Record<string, string | number | boolean>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Convert a date to an <input type="date"> default value
|
|
20
|
+
* @param date - The date to convert
|
|
21
|
+
* @returns The input default value
|
|
22
|
+
*/
|
|
23
|
+
export declare const dateToInputDefaultValue: (date: Date) => string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Convert a date to an <input type="datetime-local"> default value
|
|
26
|
+
* @param date - The date to convert
|
|
27
|
+
* @returns The input default value
|
|
28
|
+
*/
|
|
29
|
+
export declare const datetimeToInputDefaultValue: (date: Date) => string;
|
|
3
30
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/client/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,CAAC,CAAC,UAAU,EACpB,IAAI,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/client/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;GASG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,CAAC,CAAC,UAAU,EACpB,IAAI,EAAE,MAAM,EAAE,EACd,OAAO,CAAC,EAAE;IACR,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,GACA;IACD,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC;IACxD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;CAClD,CAgHA;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,SAAU,IAAI,uBAKjD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,SAAU,IAAI,WAKrD,CAAC"}
|
package/dist/client/helpers.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Get the html5 validation attributes from a Zod schema field
|
|
4
|
+
* @param schema - The Zod schema
|
|
5
|
+
* @param path - The path to the field in the schema
|
|
6
|
+
* @returns The validation attributes for the field
|
|
7
|
+
* @note It would be cool to infer also the "type" attribute of the field, but this would not be consistent because a zod rule is not a 1-1 relation with an html input.
|
|
8
|
+
* For example, a zod.number() could be an <input type="number" /> but also a <select>. A z.date() could be represented by a <input type="date" /> but also a <input type="datetime-local" />.
|
|
9
|
+
* We could make a "guess" based on the zod rule, and then could be overriden by the user, but this would lead to confusion.
|
|
10
|
+
* So we leave it as a parameter for now.
|
|
11
|
+
*/
|
|
12
|
+
export function getZodValidationAttributes(schema, path, options) {
|
|
3
13
|
const def = schema._def;
|
|
14
|
+
let type = 'string';
|
|
4
15
|
const attrs = {};
|
|
5
16
|
// First handle object type to get to the actual field
|
|
6
17
|
if (def.typeName === 'ZodObject' && path.length > 0) {
|
|
7
18
|
const shape = def.shape();
|
|
8
19
|
const field = shape[path[0]];
|
|
9
|
-
return field
|
|
20
|
+
return field
|
|
21
|
+
? getZodValidationAttributes(field, path.slice(1))
|
|
22
|
+
: { type, attrs };
|
|
10
23
|
}
|
|
11
24
|
// Now we're at the actual field, check if it's optional/nullable
|
|
12
25
|
const isOptionalType = schema instanceof z.ZodOptional;
|
|
@@ -14,12 +27,13 @@ export function getZodValidationAttributes(schema, path) {
|
|
|
14
27
|
// If it's an optional/nullable type, get attributes from the inner type but don't set required
|
|
15
28
|
if (isOptionalType || isNullableType) {
|
|
16
29
|
const innerAttrs = getZodValidationAttributes(def.innerType, path);
|
|
17
|
-
delete innerAttrs.required;
|
|
30
|
+
delete innerAttrs.attrs.required;
|
|
18
31
|
return innerAttrs;
|
|
19
32
|
}
|
|
20
33
|
// Set required by default for non-optional/nullable fields
|
|
21
34
|
attrs.required = true;
|
|
22
35
|
if (def.typeName === 'ZodString') {
|
|
36
|
+
type = 'string';
|
|
23
37
|
attrs.type = 'text';
|
|
24
38
|
if (def.checks) {
|
|
25
39
|
for (const check of def.checks) {
|
|
@@ -40,6 +54,7 @@ export function getZodValidationAttributes(schema, path) {
|
|
|
40
54
|
}
|
|
41
55
|
if (def.typeName === 'ZodNumber' ||
|
|
42
56
|
(def.typeName === 'ZodCoerce' && def.schema._def.typeName === 'ZodNumber')) {
|
|
57
|
+
type = 'number';
|
|
43
58
|
attrs.type = 'number';
|
|
44
59
|
if (def.checks || (def.schema && def.schema._def.checks)) {
|
|
45
60
|
const checks = def.checks || def.schema._def.checks;
|
|
@@ -58,6 +73,7 @@ export function getZodValidationAttributes(schema, path) {
|
|
|
58
73
|
}
|
|
59
74
|
if (def.typeName === 'ZodDate' ||
|
|
60
75
|
(def.typeName === 'ZodCoerce' && def.schema._def.typeName === 'ZodDate')) {
|
|
76
|
+
type = 'date';
|
|
61
77
|
attrs.type = 'date';
|
|
62
78
|
if (def.checks || (def.schema && def.schema._def.checks)) {
|
|
63
79
|
const checks = def.checks || def.schema._def.checks;
|
|
@@ -71,6 +87,42 @@ export function getZodValidationAttributes(schema, path) {
|
|
|
71
87
|
}
|
|
72
88
|
}
|
|
73
89
|
}
|
|
74
|
-
|
|
90
|
+
if (def.typeName === 'ZodBoolean' ||
|
|
91
|
+
(def.typeName === 'ZodCoerce' && def.schema._def.typeName === 'ZodBoolean')) {
|
|
92
|
+
type = 'boolean';
|
|
93
|
+
attrs.type = 'checkbox';
|
|
94
|
+
}
|
|
95
|
+
if (def.typeName === 'ZodEnum' ||
|
|
96
|
+
(def.typeName === 'ZodCoerce' && def.schema._def.typeName === 'ZodEnum')) {
|
|
97
|
+
type = 'enum';
|
|
98
|
+
attrs.type = 'radio';
|
|
99
|
+
}
|
|
100
|
+
// If not specified, remove the type attribute
|
|
101
|
+
if (!options?.inferTypeAttr) {
|
|
102
|
+
delete attrs.type;
|
|
103
|
+
}
|
|
104
|
+
return { type, attrs };
|
|
75
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Convert a date to an <input type="date"> default value
|
|
108
|
+
* @param date - The date to convert
|
|
109
|
+
* @returns The input default value
|
|
110
|
+
*/
|
|
111
|
+
export const dateToInputDefaultValue = (date) => {
|
|
112
|
+
const newDate = date ? new Date(date) : new Date();
|
|
113
|
+
return new Date(newDate.getTime() - newDate.getTimezoneOffset() * 60000)
|
|
114
|
+
.toISOString()
|
|
115
|
+
.split('T')[0];
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Convert a date to an <input type="datetime-local"> default value
|
|
119
|
+
* @param date - The date to convert
|
|
120
|
+
* @returns The input default value
|
|
121
|
+
*/
|
|
122
|
+
export const datetimeToInputDefaultValue = (date) => {
|
|
123
|
+
const newDate = date ? new Date(date) : new Date();
|
|
124
|
+
return new Date(newDate.getTime() - newDate.getTimezoneOffset() * 60000)
|
|
125
|
+
.toISOString()
|
|
126
|
+
.slice(0, -1);
|
|
127
|
+
};
|
|
76
128
|
//# sourceMappingURL=helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/client/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,UAAU,0BAA0B,CACxC,MAAoB,EACpB,IAAc;
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/client/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;GASG;AACH,MAAM,UAAU,0BAA0B,CACxC,MAAoB,EACpB,IAAc,EACd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AAC,CAAC;AAClB,CAAC,CAAC"}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import type { ActionResult } from '../index.js';
|
|
4
|
-
|
|
4
|
+
type UseFieldReturn = {
|
|
5
5
|
invalid: string[] | undefined;
|
|
6
6
|
value: any;
|
|
7
7
|
input: {
|
|
8
8
|
id: string;
|
|
9
9
|
name: string;
|
|
10
|
-
defaultValue: any;
|
|
11
10
|
'aria-invalid': boolean;
|
|
11
|
+
autoComplete: 'on' | 'off' | undefined;
|
|
12
|
+
defaultValue?: string;
|
|
13
|
+
defaultChecked?: boolean;
|
|
12
14
|
};
|
|
13
15
|
};
|
|
14
|
-
export declare
|
|
16
|
+
export declare const useField: <Schema extends z.AnyZodObject>() => UseFieldReturn;
|
|
17
|
+
export declare function Form<Schema extends z.AnyZodObject>({ children, action, state, schema, className, reset, onSuccess, onError, }: {
|
|
15
18
|
children: React.ReactNode;
|
|
16
19
|
action: (payload: FormData) => void;
|
|
17
20
|
state: ActionResult<Schema>;
|
|
18
21
|
schema: Schema;
|
|
22
|
+
className?: string;
|
|
19
23
|
reset?: boolean;
|
|
20
|
-
onSuccess?: (
|
|
24
|
+
onSuccess?: (successData: any, formData: z.TypeOf<Schema> | undefined) => void;
|
|
21
25
|
onError?: (error: string) => void;
|
|
22
26
|
}): React.JSX.Element;
|
|
23
27
|
export declare function FormField<Schema extends z.AnyZodObject>({ render, name, }: {
|
|
24
28
|
render: (field: ReturnType<typeof useField>) => React.ReactNode;
|
|
25
29
|
name: keyof z.TypeOf<Schema>;
|
|
26
30
|
}): React.JSX.Element;
|
|
31
|
+
export {};
|
|
27
32
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA4B,MAAM,OAAO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,EAChB,QAAQ,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,SAAS,KACnC,IAAI,CAAC;IACV,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,qBAsBA;AAED,wBAAgB,SAAS,CAAC,MAAM,SAAS,CAAC,CAAC,YAAY,EAAE,EACvD,MAAM,EACN,IAAI,GACL,EAAE;IACD,MAAM,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC,KAAK,KAAK,CAAC,SAAS,CAAC;IAChE,IAAI,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;CAC9B,qBAOA"}
|
package/dist/client/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import React, { useEffect, useRef } from 'react';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
-
import { getZodValidationAttributes } from './helpers.js';
|
|
4
|
+
import { dateToInputDefaultValue, getZodValidationAttributes, } from './helpers.js';
|
|
5
5
|
const FormContext = React.createContext({ state: undefined, schema: undefined });
|
|
6
6
|
const useForm = () => {
|
|
7
7
|
const context = React.useContext(FormContext);
|
|
@@ -12,29 +12,54 @@ const useForm = () => {
|
|
|
12
12
|
schema: context.schema,
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
|
-
const FieldContext = React.createContext({
|
|
15
|
+
const FieldContext = React.createContext({
|
|
16
|
+
name: '',
|
|
17
|
+
id: '',
|
|
18
|
+
});
|
|
16
19
|
export const useField = () => {
|
|
17
20
|
'use no memo'; // the useField hook should not be memoized because the value will change
|
|
18
|
-
const { name } = React.useContext(FieldContext);
|
|
21
|
+
const { name, id } = React.useContext(FieldContext);
|
|
19
22
|
const { state, schema } = useForm();
|
|
20
23
|
if (!state || !schema)
|
|
21
24
|
throw new Error('<FormField> must be used within a <Form>');
|
|
22
|
-
const id = React.useId();
|
|
23
25
|
// Get validation attributes from schema
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
+
const { type, attrs } = getZodValidationAttributes(schema, [name]);
|
|
27
|
+
// Create the field object
|
|
28
|
+
const field = {
|
|
26
29
|
invalid: state.invalid?.[name],
|
|
27
|
-
value: state.
|
|
30
|
+
value: state.formData?.[name],
|
|
28
31
|
input: {
|
|
29
32
|
id: id,
|
|
30
33
|
name: name,
|
|
31
|
-
defaultValue: state.data?.[name],
|
|
32
34
|
'aria-invalid': !!state.invalid?.[name],
|
|
33
|
-
|
|
35
|
+
autoComplete: undefined,
|
|
36
|
+
...attrs,
|
|
34
37
|
},
|
|
35
38
|
};
|
|
39
|
+
// Set the default value for mantaining the state across submissions
|
|
40
|
+
let defaultValue = state.formData?.[name];
|
|
41
|
+
if (defaultValue && defaultValue instanceof Date) {
|
|
42
|
+
defaultValue = dateToInputDefaultValue(defaultValue);
|
|
43
|
+
}
|
|
44
|
+
if (type === 'enum') {
|
|
45
|
+
field.input.defaultValue = defaultValue;
|
|
46
|
+
// TODO: This is not working if the input is a radio
|
|
47
|
+
// if the input is a radio, we don't know which of the inputs is this one
|
|
48
|
+
}
|
|
49
|
+
else if (type === 'boolean') {
|
|
50
|
+
field.input.defaultChecked = defaultValue;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
field.input.defaultValue = defaultValue;
|
|
54
|
+
}
|
|
55
|
+
// Set autocomplete for string inputs
|
|
56
|
+
if (type === 'string') {
|
|
57
|
+
field.input.autoComplete = 'on';
|
|
58
|
+
}
|
|
59
|
+
console.log(field);
|
|
60
|
+
return field;
|
|
36
61
|
};
|
|
37
|
-
export function Form({ children, action, state, schema, reset, onSuccess, onError, }) {
|
|
62
|
+
export function Form({ children, action, state, schema, className, reset, onSuccess, onError, }) {
|
|
38
63
|
const formRef = useRef(null);
|
|
39
64
|
if (reset !== false) {
|
|
40
65
|
if (formRef.current && state.success) {
|
|
@@ -43,17 +68,18 @@ export function Form({ children, action, state, schema, reset, onSuccess, onErro
|
|
|
43
68
|
}
|
|
44
69
|
useEffect(() => {
|
|
45
70
|
if (state.success) {
|
|
46
|
-
onSuccess?.(state.
|
|
71
|
+
onSuccess?.(state.successData, state.formData);
|
|
47
72
|
}
|
|
48
73
|
else if (state.error) {
|
|
49
74
|
onError?.(state.error);
|
|
50
75
|
}
|
|
51
76
|
}, [state, onSuccess, onError]);
|
|
52
77
|
return (React.createElement(FormContext.Provider, { value: { state, schema } },
|
|
53
|
-
React.createElement("form", { action: action, ref: formRef }, children)));
|
|
78
|
+
React.createElement("form", { action: action, ref: formRef, className: className }, children)));
|
|
54
79
|
}
|
|
55
80
|
export function FormField({ render, name, }) {
|
|
56
|
-
|
|
81
|
+
const id = React.useId();
|
|
82
|
+
return (React.createElement(FieldContext.Provider, { value: { name: name, id } },
|
|
57
83
|
React.createElement(FormFieldRenderer, { render: render })));
|
|
58
84
|
}
|
|
59
85
|
function FormFieldRenderer({ render, }) {
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACL,uBAAuB,EACvB,0BAA0B,GAC3B,MAAM,cAAc,CAAC;AAEtB,MAAM,WAAW,GAAG,KAAK,CAAC,aAAa,CAGpC,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AAE5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,SAAS,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;QACjD,CAAC;aAAM,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;YACvB,OAAO,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzB,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;IAEhC,OAAO,CACL,oBAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE;QAC5C,8BAAM,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,IACrD,QAAQ,CACJ,CACc,CACxB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAgC,EACvD,MAAM,EACN,IAAI,GAIL;IACC,MAAM,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IACzB,OAAO,CACL,oBAAC,YAAY,CAAC,QAAQ,IAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAc,EAAE,EAAE,EAAE;QACxD,oBAAC,iBAAiB,IAAC,MAAM,EAAE,MAAM,GAAI,CACf,CACzB,CAAC;AACJ,CAAC;AAED,SAAS,iBAAiB,CAAgC,EACxD,MAAM,GAGP;IACC,MAAM,KAAK,GAAG,QAAQ,EAAU,CAAC;IACjC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { dateToInputDefaultValue, datetimeToInputDefaultValue } from './client/helpers.js';
|
|
1
2
|
import { Form, FormField, useField } from './client/index.js';
|
|
2
3
|
import { action, actionWithParam } from './server/actions.js';
|
|
3
4
|
import { initialState, setInvalid } from './server/helpers.js';
|
|
4
5
|
import { type ActionResult, type ErrorActionResult, type FieldErrors, type IdleActionResult, type InvalidActionResult, type SuccessActionResult } from './server/types.js';
|
|
5
|
-
export { action, actionWithParam,
|
|
6
|
+
export { Form, FormField, action, actionWithParam, dateToInputDefaultValue, datetimeToInputDefaultValue, initialState, setInvalid, useField, };
|
|
6
7
|
export type { ActionResult, ErrorActionResult, FieldErrors, IdleActionResult, InvalidActionResult, SuccessActionResult, };
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACzB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,eAAe,EACf,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACzB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,IAAI,EACJ,SAAS,EACT,MAAM,EACN,eAAe,EACf,uBAAuB,EACvB,2BAA2B,EAC3B,YAAY,EACZ,UAAU,EACV,QAAQ,GACT,CAAC;AACF,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,WAAW,EACX,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,GACpB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { dateToInputDefaultValue, datetimeToInputDefaultValue, } from './client/helpers.js';
|
|
1
2
|
import { Form, FormField, useField } from './client/index.js';
|
|
2
3
|
import { action, actionWithParam } from './server/actions.js';
|
|
3
4
|
import { initialState, setInvalid } from './server/helpers.js';
|
|
4
5
|
import {} from './server/types.js';
|
|
5
|
-
export { action, actionWithParam,
|
|
6
|
+
export { Form, FormField, action, actionWithParam, dateToInputDefaultValue, datetimeToInputDefaultValue, initialState, setInvalid, useField, };
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAON,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,eAAe,EACf,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,2BAA2B,GAC5B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAON,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,IAAI,EACJ,SAAS,EACT,MAAM,EACN,eAAe,EACf,uBAAuB,EACvB,2BAA2B,EAC3B,YAAY,EACZ,UAAU,EACV,QAAQ,GACT,CAAC"}
|
package/dist/server/actions.d.ts
CHANGED
|
@@ -2,22 +2,26 @@ import { z } from 'zod';
|
|
|
2
2
|
import { type InvalidActionResult } from './types.js';
|
|
3
3
|
export declare const action: <Schema extends z.ZodTypeAny>(schema: Schema, fn: (data: z.infer<Schema>) => unknown) => (_prevState: unknown, data: FormData) => Promise<InvalidActionResult<Schema> | {
|
|
4
4
|
success: true;
|
|
5
|
-
|
|
5
|
+
formData: z.TypeOf<Schema>;
|
|
6
|
+
successData: any;
|
|
6
7
|
invalid: undefined;
|
|
7
8
|
error: undefined;
|
|
8
9
|
} | {
|
|
9
|
-
|
|
10
|
+
formData: z.TypeOf<Schema>;
|
|
11
|
+
successData: undefined;
|
|
10
12
|
success: false;
|
|
11
13
|
invalid: undefined;
|
|
12
14
|
error: string;
|
|
13
15
|
}>;
|
|
14
16
|
export declare const actionWithParam: <Schema extends z.ZodTypeAny>(schema: Schema, fn: (param: string, data: z.infer<Schema>) => unknown) => (param: string, _prevState: unknown, data: FormData) => Promise<InvalidActionResult<Schema> | {
|
|
15
17
|
success: true;
|
|
16
|
-
|
|
18
|
+
formData: z.TypeOf<Schema>;
|
|
19
|
+
successData: any;
|
|
17
20
|
invalid: undefined;
|
|
18
21
|
error: undefined;
|
|
19
22
|
} | {
|
|
20
|
-
|
|
23
|
+
formData: z.TypeOf<Schema>;
|
|
24
|
+
successData: undefined;
|
|
21
25
|
success: false;
|
|
22
26
|
invalid: undefined;
|
|
23
27
|
error: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../src/server/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAoB,KAAK,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAExE,eAAO,MAAM,MAAM,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,UACxC,MAAM,MACV,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,OAAO,kBAEZ,OAAO,QAAQ,QAAQ
|
|
1
|
+
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../src/server/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAoB,KAAK,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAExE,eAAO,MAAM,MAAM,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,UACxC,MAAM,MACV,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,OAAO,kBAEZ,OAAO,QAAQ,QAAQ;;;;;;;;;;;;EA2BlD,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,UACjD,MAAM,MACV,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,OAAO,aAEhC,MAAM,cAAc,OAAO,QAAQ,QAAQ;;;;;;;;;;;;EA0BjE,CAAC"}
|
package/dist/server/actions.js
CHANGED
|
@@ -16,7 +16,7 @@ export const action = (schema, fn) => {
|
|
|
16
16
|
// Permit to return a FailureActionResult from the action for custom validations
|
|
17
17
|
return actionResponse;
|
|
18
18
|
}
|
|
19
|
-
return success(serialize(parsedData.data));
|
|
19
|
+
return success(serialize(parsedData.data), actionResponse);
|
|
20
20
|
}
|
|
21
21
|
catch (e) {
|
|
22
22
|
if (e instanceof Error &&
|
|
@@ -41,7 +41,7 @@ export const actionWithParam = (schema, fn) => {
|
|
|
41
41
|
if (actionResponse && isFailureActionResult(actionResponse)) {
|
|
42
42
|
return actionResponse;
|
|
43
43
|
}
|
|
44
|
-
return success(serialize(parsedData.data));
|
|
44
|
+
return success(serialize(parsedData.data), actionResponse);
|
|
45
45
|
}
|
|
46
46
|
catch (e) {
|
|
47
47
|
if (e instanceof Error &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.js","sourceRoot":"","sources":["../../src/server/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,qBAAqB,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAA8C,MAAM,YAAY,CAAC;AACxE,oBAAoB;AACpB,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,MAAc,EACd,EAAsC,EACtC,EAAE;IACF,OAAO,KAAK,EAAE,UAAmB,EAAE,IAAc,EAAE,EAAE;QACnD,MAAM,QAAQ,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QACtC,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QACzD,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;YACxB,OAAO,OAAO,CACZ,SAAS,CAAC,QAAQ,CAAC,EACnB,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,WAAkC,CAC9D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC;YACH,MAAM,cAAc,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YACjD,IAAI,cAAc,IAAI,qBAAqB,CAAC,cAAc,CAAC,EAAE,CAAC;gBAC5D,gFAA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AM,CAAC;gBACN,OAAO,KAAK,CAAS,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/server/helpers.d.ts
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
|
-
import type { FieldErrors,
|
|
3
|
-
export declare const success: <Schema extends z.ZodTypeAny>(
|
|
2
|
+
import type { FieldErrors, InvalidActionResult } from './types.js';
|
|
3
|
+
export declare const success: <Schema extends z.ZodTypeAny>(formData: z.infer<Schema>, successData: any) => {
|
|
4
4
|
success: true;
|
|
5
|
-
|
|
5
|
+
formData: z.TypeOf<Schema>;
|
|
6
|
+
successData: any;
|
|
6
7
|
invalid: undefined;
|
|
7
8
|
error: undefined;
|
|
8
9
|
};
|
|
9
|
-
export declare const failure: <Schema extends z.ZodTypeAny>(
|
|
10
|
+
export declare const failure: <Schema extends z.ZodTypeAny>(formData: z.infer<Schema>, invalid: FieldErrors<Schema>) => {
|
|
10
11
|
success: false;
|
|
11
|
-
|
|
12
|
+
formData: z.TypeOf<Schema>;
|
|
13
|
+
successData: undefined;
|
|
12
14
|
invalid: FieldErrors<Schema>;
|
|
13
15
|
error: undefined;
|
|
14
16
|
};
|
|
15
|
-
export declare const error: <Schema extends z.ZodTypeAny>(
|
|
16
|
-
|
|
17
|
+
export declare const error: <Schema extends z.ZodTypeAny>(formData: z.infer<Schema>, error: unknown) => {
|
|
18
|
+
formData: z.TypeOf<Schema>;
|
|
19
|
+
successData: undefined;
|
|
17
20
|
success: false;
|
|
18
21
|
invalid: undefined;
|
|
19
22
|
error: string;
|
|
20
23
|
};
|
|
21
|
-
export declare function setInvalid<Schema extends z.ZodTypeAny>(
|
|
24
|
+
export declare function setInvalid<Schema extends z.ZodTypeAny>(formData: z.infer<Schema>, field: keyof z.TypeOf<Schema>, error: string): {
|
|
22
25
|
invalid: FieldErrors<Schema>;
|
|
23
26
|
success: false;
|
|
24
27
|
error: undefined;
|
|
25
|
-
|
|
28
|
+
formData: z.TypeOf<Schema>;
|
|
29
|
+
successData: undefined;
|
|
30
|
+
};
|
|
31
|
+
export declare const isFailureActionResult: <Schema extends z.ZodTypeAny>(actionResult: unknown) => actionResult is InvalidActionResult<Schema>;
|
|
32
|
+
export declare const initialState: <Schema extends z.ZodTypeAny>(formData: z.infer<Schema> | undefined) => {
|
|
33
|
+
success: false;
|
|
34
|
+
formData: z.TypeOf<Schema> | undefined;
|
|
35
|
+
successData: undefined;
|
|
36
|
+
invalid: undefined;
|
|
37
|
+
error: undefined;
|
|
26
38
|
};
|
|
27
|
-
export declare const isFailureActionResult: <Schema extends z.ZodTypeAny>(data: unknown) => data is InvalidActionResult<Schema>;
|
|
28
|
-
export declare const initialState: <Schema extends z.ZodTypeAny>(data: z.infer<Schema> | undefined) => IdleActionResult<Schema>;
|
|
29
39
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/server/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAEV,WAAW,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/server/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAEV,WAAW,EAEX,mBAAmB,EAEpB,MAAM,YAAY,CAAC;AAGpB,eAAO,MAAM,OAAO,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,YACvC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,eACZ,GAAG;;;;;;CAQwB,CAAC;AAE3C,eAAO,MAAM,OAAO,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,YACvC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,WAChB,WAAW,CAAC,MAAM,CAAC;;;;;;CAQY,CAAC;AAE3C,eAAO,MAAM,KAAK,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,YACrC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,SAClB,OAAO;;;;;;CAQwB,CAAC;AAEzC,wBAAgB,UAAU,CAAC,MAAM,SAAS,CAAC,CAAC,UAAU,EACpD,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EACzB,KAAK,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAC7B,KAAK,EAAE,MAAM;aAKK,WAAW,CAAC,MAAM,CAAC;;;;;EAMtC;AAGD,eAAO,MAAM,qBAAqB,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,gBACjD,OAAO,KACpB,YAAY,IAAI,mBAAmB,CAAC,MAAM,CAS5C,CAAC;AAGF,eAAO,MAAM,YAAY,GAAI,MAAM,SAAS,CAAC,CAAC,UAAU,YAC5C,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS;;;;;;CAQA,CAAC"}
|
package/dist/server/helpers.js
CHANGED
|
@@ -1,45 +1,50 @@
|
|
|
1
1
|
// ** Action result constructors helpers
|
|
2
|
-
export const success = (
|
|
2
|
+
export const success = (formData, successData) => ({
|
|
3
3
|
success: true,
|
|
4
|
-
|
|
4
|
+
formData,
|
|
5
|
+
successData,
|
|
5
6
|
invalid: undefined,
|
|
6
7
|
error: undefined,
|
|
7
8
|
});
|
|
8
|
-
export const failure = (
|
|
9
|
+
export const failure = (formData, invalid) => ({
|
|
9
10
|
success: false,
|
|
10
|
-
|
|
11
|
+
formData, // pass down the data even if there are errors to leave the form filled
|
|
12
|
+
successData: undefined,
|
|
11
13
|
invalid,
|
|
12
14
|
error: undefined,
|
|
13
15
|
});
|
|
14
|
-
export const error = (
|
|
15
|
-
|
|
16
|
+
export const error = (formData, error) => ({
|
|
17
|
+
formData, // pass down the data even if there are errors to leave the form filled
|
|
18
|
+
successData: undefined,
|
|
16
19
|
success: false,
|
|
17
20
|
invalid: undefined,
|
|
18
21
|
error: error instanceof Error ? error.message : JSON.stringify(error),
|
|
19
22
|
});
|
|
20
|
-
export function setInvalid(
|
|
23
|
+
export function setInvalid(formData, field, error) {
|
|
21
24
|
return {
|
|
22
25
|
invalid: {
|
|
23
26
|
[field]: error,
|
|
24
27
|
},
|
|
25
28
|
success: false,
|
|
26
29
|
error: undefined,
|
|
27
|
-
|
|
30
|
+
formData, // pass down the data even if there are errors to leave the form filled
|
|
31
|
+
successData: undefined,
|
|
28
32
|
};
|
|
29
33
|
}
|
|
30
34
|
// ** Action result typeguards
|
|
31
|
-
export const isFailureActionResult = (
|
|
32
|
-
return (typeof
|
|
33
|
-
|
|
34
|
-
'success' in
|
|
35
|
-
|
|
36
|
-
'invalid' in
|
|
37
|
-
|
|
35
|
+
export const isFailureActionResult = (actionResult) => {
|
|
36
|
+
return (typeof actionResult === 'object' &&
|
|
37
|
+
actionResult !== null &&
|
|
38
|
+
'success' in actionResult &&
|
|
39
|
+
actionResult.success === false &&
|
|
40
|
+
'invalid' in actionResult &&
|
|
41
|
+
actionResult.invalid !== undefined);
|
|
38
42
|
};
|
|
39
43
|
// ** Initial state helper
|
|
40
|
-
export const initialState = (
|
|
44
|
+
export const initialState = (formData) => ({
|
|
41
45
|
success: false,
|
|
42
|
-
|
|
46
|
+
formData,
|
|
47
|
+
successData: undefined,
|
|
43
48
|
invalid: undefined,
|
|
44
49
|
error: undefined,
|
|
45
50
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/server/helpers.ts"],"names":[],"mappings":"AASA,wCAAwC;AACxC,MAAM,CAAC,MAAM,OAAO,GAAG,
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/server/helpers.ts"],"names":[],"mappings":"AASA,wCAAwC;AACxC,MAAM,CAAC,MAAM,OAAO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AS,CACnC,CAAC;AACJ,CAAC,CAAC;AAEF,0BAA0B;AAC1B,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,QAAqC,EACrC,EAAE,CACF,CAAC;IACC,OAAO,EAAE,KAAK;IACd,QAAQ;IACR,WAAW,EAAE,SAAS;IACtB,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,SAAS;CACjB,CAAoC,CAAC"}
|
package/dist/server/types.d.ts
CHANGED
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
export type IdleActionResult<Schema extends z.ZodTypeAny> = {
|
|
3
3
|
success: false;
|
|
4
|
-
|
|
4
|
+
formData: z.infer<Schema> | undefined;
|
|
5
|
+
successData: undefined;
|
|
5
6
|
invalid: undefined;
|
|
6
7
|
error: undefined;
|
|
7
8
|
};
|
|
8
9
|
export type SuccessActionResult<Schema extends z.ZodTypeAny> = {
|
|
9
10
|
success: true;
|
|
10
|
-
|
|
11
|
+
formData: z.infer<Schema> | undefined;
|
|
12
|
+
successData: any;
|
|
11
13
|
invalid: undefined;
|
|
12
14
|
error: undefined;
|
|
13
15
|
};
|
|
14
16
|
export type InvalidActionResult<Schema extends z.ZodTypeAny> = {
|
|
15
17
|
success: false;
|
|
16
|
-
|
|
18
|
+
formData: z.infer<Schema> | undefined;
|
|
19
|
+
successData: undefined;
|
|
17
20
|
invalid: FieldErrors<Schema> | undefined;
|
|
18
21
|
error: undefined;
|
|
19
22
|
};
|
|
20
23
|
export type ErrorActionResult<Schema extends z.ZodTypeAny> = {
|
|
21
24
|
success: false;
|
|
22
|
-
|
|
25
|
+
formData: z.infer<Schema> | undefined;
|
|
26
|
+
successData: undefined;
|
|
23
27
|
invalid: undefined;
|
|
24
28
|
error: string;
|
|
25
29
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/server/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM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