@bgord/ui 0.8.3 → 0.8.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.
|
@@ -16,5 +16,5 @@ export function Dialog(props) {
|
|
|
16
16
|
useShortcuts({ Escape: locked ? noop : dialog.disable });
|
|
17
17
|
useScrollLock(props.on);
|
|
18
18
|
useClickOutside(ref, locked ? noop : dialog.disable);
|
|
19
|
-
return (_jsx("dialog", { ref: ref, tabIndex: 0, "aria-modal": "true", "data-disp": props.on ? "flex" : "none", "data-dir": "column", "data-mx": "auto", "data-p": "5", "data-position": "fixed", "data-z": "2", "data-bg": "neutral-900", "data-br": "xs", "data-backdrop": "stronger", "data-animation": "grow-fade-in", ...rest }));
|
|
19
|
+
return (_jsx("dialog", { ref: ref, tabIndex: 0, "aria-modal": "true", "data-disp": props.on ? "flex" : "none", "data-dir": "column", "data-mx": "auto", "data-p": "5", "data-position": "fixed", "data-z": "2", "data-bg": "neutral-900", "data-br": "xs", "data-backdrop": "stronger", "data-animation": "grow-fade-in", ...dialog.props.target, ...rest }));
|
|
20
20
|
}
|
|
@@ -5,10 +5,13 @@ export declare enum MutationState {
|
|
|
5
5
|
done = "done"
|
|
6
6
|
}
|
|
7
7
|
type MutationErrorType = unknown;
|
|
8
|
+
type MutationContextType = {
|
|
9
|
+
form?: HTMLFormElement;
|
|
10
|
+
};
|
|
8
11
|
type UseMutationOptions = {
|
|
9
12
|
perform: () => Promise<Response>;
|
|
10
|
-
onSuccess?: (response: Response) => void | Promise<void>;
|
|
11
|
-
onError?: (error: MutationErrorType) => void | Promise<void>;
|
|
13
|
+
onSuccess?: (response: Response, context: MutationContextType) => void | Promise<void>;
|
|
14
|
+
onError?: (error: MutationErrorType, context: MutationContextType) => void | Promise<void>;
|
|
12
15
|
autoResetDelayMs?: number;
|
|
13
16
|
};
|
|
14
17
|
type UseMutationReturnType = {
|
|
@@ -18,7 +21,7 @@ type UseMutationReturnType = {
|
|
|
18
21
|
isLoading: boolean;
|
|
19
22
|
isError: boolean;
|
|
20
23
|
isDone: boolean;
|
|
21
|
-
mutate: () => Promise<Response | undefined>;
|
|
24
|
+
mutate: (formElement?: HTMLFormElement) => Promise<Response | undefined>;
|
|
22
25
|
handleSubmit: React.FormEventHandler<HTMLFormElement>;
|
|
23
26
|
reset: () => void;
|
|
24
27
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useReducer } from "react";
|
|
2
2
|
export var MutationState;
|
|
3
3
|
(function (MutationState) {
|
|
4
4
|
MutationState["idle"] = "idle";
|
|
@@ -6,52 +6,55 @@ export var MutationState;
|
|
|
6
6
|
MutationState["error"] = "error";
|
|
7
7
|
MutationState["done"] = "done";
|
|
8
8
|
})(MutationState || (MutationState = {}));
|
|
9
|
+
function mutationReducer(_state, action) {
|
|
10
|
+
if (action.type === "START")
|
|
11
|
+
return { state: MutationState.loading, error: null };
|
|
12
|
+
if (action.type === "ERROR")
|
|
13
|
+
return { state: MutationState.error, error: action.error };
|
|
14
|
+
if (action.type === "DONE")
|
|
15
|
+
return { state: MutationState.done, error: null };
|
|
16
|
+
if (action.type === "RESET")
|
|
17
|
+
return { state: MutationState.idle, error: null };
|
|
18
|
+
throw new Error("Unknown useMutation action type");
|
|
19
|
+
}
|
|
9
20
|
export function useMutation(options) {
|
|
10
|
-
const [
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
setError(null);
|
|
14
|
-
setState(MutationState.idle);
|
|
15
|
-
}, []);
|
|
16
|
-
const mutate = useCallback(async () => {
|
|
17
|
-
if (state === MutationState.loading)
|
|
21
|
+
const [mutation, dispatch] = useReducer(mutationReducer, { state: MutationState.idle, error: null });
|
|
22
|
+
const mutate = async (form) => {
|
|
23
|
+
if (mutation.state === MutationState.loading)
|
|
18
24
|
return;
|
|
19
|
-
|
|
20
|
-
setState(MutationState.loading);
|
|
25
|
+
dispatch({ type: "START" });
|
|
21
26
|
try {
|
|
22
27
|
const response = await options.perform();
|
|
23
28
|
if (!response.ok) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
await options.onError?.(null);
|
|
29
|
+
dispatch({ type: "ERROR", error: null });
|
|
30
|
+
await options.onError?.(null, { form: form });
|
|
27
31
|
return;
|
|
28
32
|
}
|
|
29
|
-
|
|
30
|
-
await options.onSuccess?.(response);
|
|
33
|
+
dispatch({ type: "DONE" });
|
|
34
|
+
await options.onSuccess?.(response, { form: form });
|
|
31
35
|
if (options.autoResetDelayMs) {
|
|
32
|
-
setTimeout(() =>
|
|
36
|
+
setTimeout(() => dispatch({ type: "RESET" }), options.autoResetDelayMs);
|
|
33
37
|
}
|
|
34
38
|
return response;
|
|
35
39
|
}
|
|
36
40
|
catch (error) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
await options.onError?.(error);
|
|
41
|
+
dispatch({ type: "ERROR", error });
|
|
42
|
+
await options.onError?.(error, { form: form });
|
|
40
43
|
}
|
|
41
|
-
}
|
|
42
|
-
const handleSubmit =
|
|
44
|
+
};
|
|
45
|
+
const handleSubmit = async (event) => {
|
|
43
46
|
event.preventDefault();
|
|
44
|
-
await mutate();
|
|
45
|
-
}
|
|
47
|
+
await mutate(event.currentTarget);
|
|
48
|
+
};
|
|
46
49
|
return {
|
|
47
|
-
state,
|
|
48
|
-
error,
|
|
49
|
-
isIdle: state === MutationState.idle,
|
|
50
|
-
isLoading: state === MutationState.loading,
|
|
51
|
-
isError: state === MutationState.error,
|
|
52
|
-
isDone: state === MutationState.done,
|
|
50
|
+
state: mutation.state,
|
|
51
|
+
error: mutation.error,
|
|
52
|
+
isIdle: mutation.state === MutationState.idle,
|
|
53
|
+
isLoading: mutation.state === MutationState.loading,
|
|
54
|
+
isError: mutation.state === MutationState.error,
|
|
55
|
+
isDone: mutation.state === MutationState.done,
|
|
53
56
|
mutate,
|
|
54
57
|
handleSubmit,
|
|
55
|
-
reset,
|
|
58
|
+
reset: () => dispatch({ type: "RESET" }),
|
|
56
59
|
};
|
|
57
60
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bgord/ui",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@biomejs/biome": "2.3.
|
|
26
|
+
"@biomejs/biome": "2.3.4",
|
|
27
27
|
"@commitlint/cli": "20.1.0",
|
|
28
28
|
"@commitlint/config-conventional": "20.0.0",
|
|
29
29
|
"@happy-dom/global-registrator": "20.0.10",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"@types/js-cookie": "3.0.6",
|
|
36
36
|
"@types/react": "19.2.2",
|
|
37
37
|
"@types/react-dom": "19.2.2",
|
|
38
|
-
"cspell": "9.
|
|
39
|
-
"knip": "5.
|
|
38
|
+
"cspell": "9.3.0",
|
|
39
|
+
"knip": "5.68.0",
|
|
40
40
|
"lefthook": "2.0.2",
|
|
41
41
|
"only-allow": "1.2.1",
|
|
42
42
|
"shellcheck": "4.1.0"
|