react-form-action 0.1.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/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/client.d.mts +22 -0
- package/dist/client.mjs +1 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.mjs +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 MiroslavPetrik
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# react-form-action
|
|
2
|
+
|
|
3
|
+
Typesafe success & error state control for Next.js v14 form actions.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i react-form-action
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Server Action (usable in Next.js)
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// actions/updateUser.ts
|
|
17
|
+
"use server";
|
|
18
|
+
import { createFormAction } from "react-form-action";
|
|
19
|
+
|
|
20
|
+
// Define custom serializable error & success data types
|
|
21
|
+
type ErrorData = {
|
|
22
|
+
message: string;
|
|
23
|
+
validation?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type SuccessData = string;
|
|
27
|
+
|
|
28
|
+
export const updateUser = createFormAction<SuccessData, ErrorData>(
|
|
29
|
+
({ success, failure }) =>
|
|
30
|
+
// success and failure helper functions create wrappers for success & error data respectively
|
|
31
|
+
async (prevState, formData) => {
|
|
32
|
+
if (prevState.type === "initial") {
|
|
33
|
+
// use the initialData passed to <Form /> here
|
|
34
|
+
// prevState.data === "foobar"
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const { name } = updateUserSchema.parse({
|
|
39
|
+
name: formData.get("name"),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const user = await updateCurrentUser(name);
|
|
43
|
+
|
|
44
|
+
if (user) {
|
|
45
|
+
const successState = success("Your profile has been updated.");
|
|
46
|
+
// {type: "success", data: "Your profile has been updated.", error: null }
|
|
47
|
+
return successState;
|
|
48
|
+
} else {
|
|
49
|
+
const errorState = failure({ message: "No current user." });
|
|
50
|
+
// {type: "error", data: null, error: { message: "No current user." } }
|
|
51
|
+
return errorState;
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof ZodError) {
|
|
55
|
+
return failure({
|
|
56
|
+
validation: true,
|
|
57
|
+
message: error.issues[0]?.message ?? "Validation error",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return failure({ message: "Failed to update user." });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `<Form>` Component
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
"use client";
|
|
71
|
+
// Form connects the formAction to the formState & provides the meta state props via render prop
|
|
72
|
+
import { Form } from "react-form-action/client";
|
|
73
|
+
|
|
74
|
+
import { updateUser } from "@/actions";
|
|
75
|
+
|
|
76
|
+
export function UpdateUserForm() {
|
|
77
|
+
return (
|
|
78
|
+
<Form action={updateUser} initialData="foobar">
|
|
79
|
+
{({ error, data, isPending, isFailure, isSuccess, isInitial }) => (
|
|
80
|
+
<>
|
|
81
|
+
{/* safely access the data or error by checking the mutually exclusive boolean flags: */}
|
|
82
|
+
{isSuccess && <p className="success-message">{data}</p>}
|
|
83
|
+
|
|
84
|
+
{isFailure && !error.validation && (
|
|
85
|
+
<p className="error-message">{error.message}</p>
|
|
86
|
+
)}
|
|
87
|
+
|
|
88
|
+
<input type="text" name="name" disabled={isPending} />
|
|
89
|
+
{isFailure && error.validation && (
|
|
90
|
+
<span className="input-error">{error.message}</span>
|
|
91
|
+
)}
|
|
92
|
+
|
|
93
|
+
<button disabled={isPending}>
|
|
94
|
+
{isPending ? "Submitting..." : "Submit"}
|
|
95
|
+
</button>
|
|
96
|
+
</>
|
|
97
|
+
)}
|
|
98
|
+
</Form>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Bonus `<FormStatus>`
|
|
104
|
+
|
|
105
|
+
The `useFormStatus` hook data exposed via render prop:
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
import { FormStatus } from "react-form-action/client";
|
|
109
|
+
|
|
110
|
+
// <FormStatus> alleviates the need to create a separate <SubmitButton> component using the useFormStatus hook
|
|
111
|
+
|
|
112
|
+
return function MyForm() {
|
|
113
|
+
return (
|
|
114
|
+
<form action={action}>
|
|
115
|
+
<FormStatus>
|
|
116
|
+
{({ pending }) => (
|
|
117
|
+
<button type="submit">{pending ? "Submitting..." : "Submit"} </button>
|
|
118
|
+
)}
|
|
119
|
+
</FormStatus>
|
|
120
|
+
</form>
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default, { FormHTMLAttributes } from 'react';
|
|
3
|
+
import { RenderProp } from 'react-render-prop-type';
|
|
4
|
+
import { InitialState, FailureState, SuccessState, FormState } from './index.mjs';
|
|
5
|
+
import { useFormStatus } from 'react-dom';
|
|
6
|
+
|
|
7
|
+
type FormStateProps<Data, Error, Payload> = {
|
|
8
|
+
action: (state: FormState<Data, Error>, payload: Payload) => Promise<FormState<Data, Error>>;
|
|
9
|
+
initialData: Data;
|
|
10
|
+
permalink?: string;
|
|
11
|
+
};
|
|
12
|
+
type FormMetaState<T extends FormState<unknown, unknown>> = T & {
|
|
13
|
+
isPending: boolean;
|
|
14
|
+
isInitial: T["type"] extends "initial" ? true : false;
|
|
15
|
+
isFailure: T["type"] extends "failure" ? true : false;
|
|
16
|
+
isSuccess: T["type"] extends "success" ? true : false;
|
|
17
|
+
};
|
|
18
|
+
declare function Form<Data, Error>({ children, action, initialData, permalink, ...props }: Omit<FormHTMLAttributes<HTMLFormElement>, "action" | "children"> & FormStateProps<Data, Error, FormData> & RenderProp<FormMetaState<InitialState<Data>> | FormMetaState<FailureState<Error>> | FormMetaState<SuccessState<Data>>>): React__default.JSX.Element;
|
|
19
|
+
|
|
20
|
+
declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
|
|
21
|
+
|
|
22
|
+
export { Form, FormStatus };
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";import a from"react";import{useFormState as l}from"react-dom";import{useFormStatus as u}from"react-dom";function r({children:t}){return t(u())}function c(t){return{type:"initial",data:t,error:null}}function D({children:t,action:o,initialData:i,permalink:n,...s}){let[e,m]=l(o,c(i),n);return a.createElement("form",{action:m,...s},a.createElement(r,null,({pending:p})=>t({...e,isPending:p,isInitial:e.type==="initial",isFailure:e.type==="failure",isSuccess:e.type==="success"})))}export{D as Form,r as FormStatus};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type InitialState<T> = {
|
|
2
|
+
type: "initial";
|
|
3
|
+
data: T;
|
|
4
|
+
error: null;
|
|
5
|
+
};
|
|
6
|
+
type FailureState<T> = {
|
|
7
|
+
type: "failure";
|
|
8
|
+
data: null;
|
|
9
|
+
error: T;
|
|
10
|
+
};
|
|
11
|
+
type SuccessState<T> = {
|
|
12
|
+
type: "success";
|
|
13
|
+
data: T;
|
|
14
|
+
error: null;
|
|
15
|
+
};
|
|
16
|
+
type FormState<Data, Error> = InitialState<Data> | FailureState<Error> | SuccessState<Data>;
|
|
17
|
+
declare function createFormAction<Data, Error = Data, Payload = FormData>(formAction: (params: {
|
|
18
|
+
success: (data: Data) => SuccessState<Data>;
|
|
19
|
+
failure: (data: Error) => FailureState<Error>;
|
|
20
|
+
}) => (state: FormState<Data, Error>, payload: Payload) => Promise<FormState<Data, Error>>): (state: FormState<Data, Error>, payload: Payload) => Promise<FormState<Data, Error>>;
|
|
21
|
+
|
|
22
|
+
export { FailureState, FormState, InitialState, SuccessState, createFormAction };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function u(t){function r(a){return{type:"success",data:a,error:null}}function e(a){return{type:"failure",data:null,error:a}}return(a,o)=>t({success:r,failure:e})(a,o)}export{u as createFormAction};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-form-action",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "State management helpers for the react form actions.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/MiroslavPetrik/react-form-action.git"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"files": [
|
|
12
|
+
"/dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsup"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"module": "./dist/index.mjs",
|
|
21
|
+
"default": "./dist/index.mjs",
|
|
22
|
+
"types": "./dist/index.d.mts"
|
|
23
|
+
},
|
|
24
|
+
"./client": {
|
|
25
|
+
"import": "./dist/client.mjs",
|
|
26
|
+
"module": "./dist/client.mjs",
|
|
27
|
+
"default": "./dist/client.mjs",
|
|
28
|
+
"types": "./dist/client.d.mts"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"react",
|
|
33
|
+
"form",
|
|
34
|
+
"action",
|
|
35
|
+
"next",
|
|
36
|
+
"nextjs",
|
|
37
|
+
"server",
|
|
38
|
+
"action"
|
|
39
|
+
],
|
|
40
|
+
"author": "Miroslav Petrik",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": ">=18.2",
|
|
44
|
+
"react-dom": ">=18.2"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/react": "^18.2.37",
|
|
48
|
+
"@types/react-dom": "^18.2.15",
|
|
49
|
+
"next": "^14.0.2",
|
|
50
|
+
"react-render-prop-type": "^0.1.0",
|
|
51
|
+
"tsup": "^7.2.0",
|
|
52
|
+
"typescript": "^5.2.2"
|
|
53
|
+
}
|
|
54
|
+
}
|