clear-react-router 2.1.2 → 2.1.3
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 +60 -40
- package/dist/hooks/useAction.d.ts +4 -1
- package/dist/hooks/useSubmitAction.d.ts +18 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +43 -50
- package/dist/types.d.ts +5 -5
- package/package.json +1 -1
- package/dist/components/Form.d.ts +0 -9
- package/dist/context/FormContext.d.ts +0 -4
- package/dist/hooks/useFormContext.d.ts +0 -1
- package/dist/provider/FormProvider.d.ts +0 -3
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ It provides first-class support for:
|
|
|
12
12
|
|
|
13
13
|
* Predictable routing
|
|
14
14
|
* Built-in data loading
|
|
15
|
-
* Route actions
|
|
15
|
+
* Route actions
|
|
16
16
|
* Simple, provider-free architecture
|
|
17
17
|
* Small, explicit API
|
|
18
18
|
|
|
@@ -398,7 +398,7 @@ const routes = createRouter([
|
|
|
398
398
|
|
|
399
399
|
Defines route-specific actions for handling data mutations such as creating, updating, or deleting resources.
|
|
400
400
|
|
|
401
|
-
Actions are available through the `
|
|
401
|
+
Actions are available through the `useSubmitAction` and `useAction` hooks. After a successful action, the current route is automatically invalidated, causing `loader` to run again in the background.
|
|
402
402
|
|
|
403
403
|
```tsx
|
|
404
404
|
actions?: ({ context, params, invalidate, setContext }) => ({
|
|
@@ -425,66 +425,86 @@ actions?: ({ context, params, invalidate, setContext }) => ({
|
|
|
425
425
|
|
|
426
426
|
A record where each key is an action name and each value is a function accepting a `FormData` instance.
|
|
427
427
|
|
|
428
|
-
|
|
428
|
+
## useSubmitAction()
|
|
429
429
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
const save = useAction('save');
|
|
434
|
-
```
|
|
430
|
+
`useSubmitAction` wraps a route's `action` with submission state (`isSubmitting`, `data`, `error`) and
|
|
431
|
+
gives you two ways to trigger it — a ready-made `onSubmit` for native forms, or a raw `submit(formData)`
|
|
432
|
+
you can call from any form library.
|
|
435
433
|
|
|
434
|
+
```tsx
|
|
435
|
+
import { useSubmitAction } from 'clear-react-router';
|
|
436
436
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
## Form
|
|
437
|
+
const CreateUserForm = () => {
|
|
438
|
+
const { onSubmit, isSubmitting, error } = useSubmitAction('createUser');
|
|
440
439
|
|
|
441
|
-
|
|
440
|
+
return (
|
|
441
|
+
<form onSubmit={onSubmit}>
|
|
442
|
+
<input name="email" disabled={isSubmitting} />
|
|
443
|
+
<button disabled={isSubmitting}>Create</button>
|
|
444
|
+
{error && <span>{error.message}</span>}
|
|
445
|
+
</form>
|
|
446
|
+
);
|
|
447
|
+
};
|
|
448
|
+
```
|
|
442
449
|
|
|
443
|
-
`
|
|
450
|
+
`submit(formData)` accepts a plain `FormData`, so it works with any form library that gives you
|
|
451
|
+
validated values — not just native `<form>` submissions:
|
|
444
452
|
|
|
445
453
|
```tsx
|
|
446
|
-
import {
|
|
454
|
+
import { useForm } from 'react-hook-form';
|
|
447
455
|
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
return <button disabled={isSubmitting} type='submit'>Save</button>
|
|
451
|
-
}
|
|
456
|
+
const { register, handleSubmit } = useForm<FormValues>();
|
|
457
|
+
const { submit, isSubmitting } = useSubmitAction('createUser');
|
|
452
458
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
459
|
+
const onValid = (values: FormValues) => {
|
|
460
|
+
const formData = new FormData();
|
|
461
|
+
Object.entries(values).forEach(([key, value]) => formData.append(key, String(value)));
|
|
462
|
+
return submit(formData);
|
|
463
|
+
};
|
|
457
464
|
|
|
465
|
+
<form onSubmit={handleSubmit(onValid)}>
|
|
466
|
+
<input {...register('email')} disabled={isSubmitting} />
|
|
467
|
+
</form>
|
|
458
468
|
```
|
|
459
469
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
| Prop | Type | Description |
|
|
463
|
-
| ----------- | --------------------------- | -------------------------------------------------------------------------------- |
|
|
464
|
-
| `action` | `string` | Name of the route action to execute. |
|
|
465
|
-
| `onSuccess` | `(result: unknown) => void` | Called when the action completes successfully. Receives the action return value. |
|
|
466
|
-
| `onError` | `(error: unknown) => void` | Called when the action throws. |
|
|
467
|
-
| `autoReset` | `boolean` | Automatically resets the form after a successful submission. Default: `true`. |
|
|
470
|
+
Arguments:
|
|
468
471
|
|
|
469
|
-
|
|
472
|
+
| Argument | Type | Default | Description |
|
|
473
|
+
|---|---|---|---|
|
|
474
|
+
| `action` | `string` | — | The action key defined in the route's `actions` |
|
|
475
|
+
| `options.onSuccess` | `(data: unknown) => void \| undefined` | `undefined` | Called after a successful submission |
|
|
476
|
+
| `options.onError` | `(error: unknown) => void \| undefined` | `undefined` | Called if the action throws |
|
|
477
|
+
| `options.autoReset` | `boolean \| undefined` | `true` | Reset the form element after a successful native submission (`onSubmit` only — has no effect on `submit`) |
|
|
478
|
+
| `options.withBeforeLoad` | `boolean \| undefined` | `false` | Whether to run the `beforeLoad` hook on a successful action submit as well |
|
|
470
479
|
|
|
471
|
-
|
|
480
|
+
Return value:
|
|
472
481
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
482
|
+
| Field | Type | Description |
|
|
483
|
+
|---|---|---|
|
|
484
|
+
| `submit` | `(formData: FormData) => Promise<{ data: unknown; error: Error \| null }>` | Runs the action directly, from any form data source |
|
|
485
|
+
| `onSubmit` | `(evt: SubmitEvent<HTMLFormElement>) => Promise<void>` | Ready-made handler for a native `<form onSubmit={...}>` |
|
|
486
|
+
| `data` | `unknown` | Result of the last successful submission |
|
|
487
|
+
| `error` | `Error \| null` | Error from the last failed submission |
|
|
488
|
+
| `isSubmitting` | `boolean` | `true` while the action is in flight |
|
|
477
489
|
|
|
478
|
-
## useAction
|
|
490
|
+
## useAction()
|
|
479
491
|
|
|
480
|
-
`useAction` provides direct access to a route action
|
|
492
|
+
`useAction` provides direct access to a route action.
|
|
481
493
|
|
|
482
494
|
### Arguments
|
|
483
495
|
|
|
484
496
|
| Argument | Type | Description |
|
|
485
497
|
|----------|------|-------------|
|
|
486
498
|
| `action` | `string` | Name of the route action to execute. Must match a key returned from the route's `actions` configuration. |
|
|
487
|
-
| `options` | `
|
|
499
|
+
| `options` | `Options` | Optional callbacks invoked after the action succeeds or fails and `withBeforeLoad` value to set route's `beforeLoad` to run. |
|
|
500
|
+
|
|
501
|
+
```ts
|
|
502
|
+
type Options = Partial<{
|
|
503
|
+
onSuccess: (args: unknown) => void;
|
|
504
|
+
onError: (args: unknown) => void;
|
|
505
|
+
withBeforeLoad?: boolean;
|
|
506
|
+
}> | undefined;
|
|
507
|
+
```
|
|
488
508
|
|
|
489
509
|
```tsx
|
|
490
510
|
const save = useAction('save');
|
|
@@ -498,7 +518,7 @@ const handleClick = async () => {
|
|
|
498
518
|
<button onClick={handleClick}>Save</button>
|
|
499
519
|
```
|
|
500
520
|
|
|
501
|
-
`useAction` automatically invalidates the current route after a successful action, causing
|
|
521
|
+
`useAction` automatically invalidates the current route after a successful action, causing `loader` to run again in the background.
|
|
502
522
|
|
|
503
523
|
This hook is useful when the mutation is triggered programmatically, such as from dialogs, context menus, drag-and-drop interactions, keyboard shortcuts, or custom UI components.
|
|
504
524
|
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export declare const useAction: (action: string, options?: import("../types").Options) => (arg: FormData) => Promise<
|
|
1
|
+
export declare const useAction: (action: string, options?: import("../types").Options) => (arg: FormData) => Promise<{
|
|
2
|
+
data: unknown;
|
|
3
|
+
error: Error | null;
|
|
4
|
+
}>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SubmitEvent } from 'react';
|
|
2
|
+
type Options = {
|
|
3
|
+
onSuccess?(arg: unknown): void;
|
|
4
|
+
onError?(arg: unknown): void;
|
|
5
|
+
autoReset?: boolean;
|
|
6
|
+
withBeforeLoad?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const useSubmitAction: (action: string, options?: Options) => {
|
|
9
|
+
submit: (formData: FormData) => Promise<{
|
|
10
|
+
data: unknown;
|
|
11
|
+
error: Error | null;
|
|
12
|
+
}>;
|
|
13
|
+
onSubmit: (evt: SubmitEvent<HTMLFormElement>) => Promise<void>;
|
|
14
|
+
data: unknown;
|
|
15
|
+
error: Error | null;
|
|
16
|
+
isSubmitting: boolean;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { Router } from './components/Router';
|
|
2
2
|
export { Link } from './components/Link';
|
|
3
|
-
export { Form } from './components/Form';
|
|
4
3
|
export { useNavigate } from './hooks/useNavigate';
|
|
5
4
|
export { useParams } from './hooks/useParams';
|
|
6
5
|
export { useLocation } from './hooks/useLocation';
|
|
@@ -11,7 +10,7 @@ export { useAction } from './hooks/useAction';
|
|
|
11
10
|
export { useIsDataLoading } from './hooks/useIsDataLoading';
|
|
12
11
|
export { useRouterContext } from './hooks/useRouterContext';
|
|
13
12
|
export { useSearchParams } from './hooks/useSearchParams';
|
|
14
|
-
export {
|
|
13
|
+
export { useSubmitAction } from './hooks/useSubmitAction';
|
|
15
14
|
export { createRouter } from './utils/createRouter';
|
|
16
15
|
export { lazy } from './utils/lazy';
|
|
17
16
|
export type { RouteItem, BlockerState, Location, RouterProps, ElementProps } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Suspense,
|
|
1
|
+
import { Suspense, lazy as lazy$1, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
2
|
//#region \0rolldown/runtime.js
|
|
3
3
|
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
|
|
4
4
|
//#endregion
|
|
@@ -536,7 +536,7 @@ var createRouterInstance = () => {
|
|
|
536
536
|
const invalidate = createInvalidate(routerState, revalidateCache);
|
|
537
537
|
const navigate = createNavigate(routerState, revalidateCache);
|
|
538
538
|
const prefetch = createPrefetch(routerState, revalidateCache);
|
|
539
|
-
const
|
|
539
|
+
const useGetCurrentAction = (actionKey) => {
|
|
540
540
|
const { routeItem } = routerState.routeItemDataState.getState();
|
|
541
541
|
const context = routerState.contextState.getState();
|
|
542
542
|
const setContext = routerState.contextState.setState;
|
|
@@ -550,10 +550,7 @@ var createRouterInstance = () => {
|
|
|
550
550
|
invalidate
|
|
551
551
|
})[actionKey];
|
|
552
552
|
if (!action) throw new Error(`Action "${actionKey}" not found`);
|
|
553
|
-
return
|
|
554
|
-
currentAction: action,
|
|
555
|
-
invalidate
|
|
556
|
-
};
|
|
553
|
+
return action;
|
|
557
554
|
};
|
|
558
555
|
return {
|
|
559
556
|
state: routerState,
|
|
@@ -588,16 +585,23 @@ var createRouterInstance = () => {
|
|
|
588
585
|
} else if (JSON.stringify(arg) !== JSON.stringify(location)) await navigate(arg);
|
|
589
586
|
};
|
|
590
587
|
},
|
|
591
|
-
useGetAction,
|
|
592
588
|
useAction: (action, options = {}) => {
|
|
593
|
-
const
|
|
589
|
+
const currentAction = useGetCurrentAction(action);
|
|
594
590
|
return async (formData) => {
|
|
595
591
|
try {
|
|
596
|
-
const
|
|
597
|
-
await invalidate();
|
|
598
|
-
options.onSuccess?.(
|
|
592
|
+
const data = await currentAction(formData);
|
|
593
|
+
await invalidate("", { withBeforeLoad: options.withBeforeLoad });
|
|
594
|
+
options.onSuccess?.(data);
|
|
595
|
+
return {
|
|
596
|
+
data,
|
|
597
|
+
error: null
|
|
598
|
+
};
|
|
599
599
|
} catch (error) {
|
|
600
600
|
options.onError?.(error);
|
|
601
|
+
return {
|
|
602
|
+
data: null,
|
|
603
|
+
error
|
|
604
|
+
};
|
|
601
605
|
}
|
|
602
606
|
};
|
|
603
607
|
}
|
|
@@ -914,42 +918,6 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
914
918
|
});
|
|
915
919
|
};
|
|
916
920
|
//#endregion
|
|
917
|
-
//#region context/FormContext.ts
|
|
918
|
-
var FormContext = createContext({ isSubmitting: false });
|
|
919
|
-
//#endregion
|
|
920
|
-
//#region provider/FormProvider.tsx
|
|
921
|
-
var FormProvider = ({ children, isSubmitting }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormContext.Provider, {
|
|
922
|
-
value: { isSubmitting },
|
|
923
|
-
children
|
|
924
|
-
});
|
|
925
|
-
//#endregion
|
|
926
|
-
//#region components/Form.tsx
|
|
927
|
-
var Form = ({ children, action, onSuccess, onError, autoReset = true }) => {
|
|
928
|
-
const { currentAction, invalidate } = router.hooks.useGetAction(action);
|
|
929
|
-
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
930
|
-
const onSubmit = async (evt) => {
|
|
931
|
-
evt.preventDefault();
|
|
932
|
-
try {
|
|
933
|
-
setIsSubmitting(true);
|
|
934
|
-
const result = await currentAction(new FormData(evt.target));
|
|
935
|
-
await invalidate();
|
|
936
|
-
if (autoReset) evt.target.reset();
|
|
937
|
-
onSuccess?.(result);
|
|
938
|
-
} catch (error) {
|
|
939
|
-
onError?.(error);
|
|
940
|
-
} finally {
|
|
941
|
-
setIsSubmitting(false);
|
|
942
|
-
}
|
|
943
|
-
};
|
|
944
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormProvider, {
|
|
945
|
-
isSubmitting,
|
|
946
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("form", {
|
|
947
|
-
onSubmit,
|
|
948
|
-
children
|
|
949
|
-
})
|
|
950
|
-
});
|
|
951
|
-
};
|
|
952
|
-
//#endregion
|
|
953
921
|
//#region hooks/useParams.ts
|
|
954
922
|
var useParams = router.hooks.useParams;
|
|
955
923
|
//#endregion
|
|
@@ -1078,8 +1046,33 @@ var useSearchParams = () => {
|
|
|
1078
1046
|
};
|
|
1079
1047
|
};
|
|
1080
1048
|
//#endregion
|
|
1081
|
-
//#region hooks/
|
|
1082
|
-
var
|
|
1049
|
+
//#region hooks/useSubmitAction.ts
|
|
1050
|
+
var useSubmitAction = (action, options) => {
|
|
1051
|
+
const currentAction = router.hooks.useAction(action, options);
|
|
1052
|
+
const [data, setData] = useState();
|
|
1053
|
+
const [error, setError] = useState(null);
|
|
1054
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
1055
|
+
const submit = useCallback(async (formData) => {
|
|
1056
|
+
setIsSubmitting(true);
|
|
1057
|
+
const result = await currentAction(formData);
|
|
1058
|
+
setIsSubmitting(false);
|
|
1059
|
+
setData(result.data);
|
|
1060
|
+
setError(result.error);
|
|
1061
|
+
return result;
|
|
1062
|
+
}, [currentAction]);
|
|
1063
|
+
return {
|
|
1064
|
+
submit,
|
|
1065
|
+
onSubmit: useCallback(async (evt) => {
|
|
1066
|
+
evt.preventDefault();
|
|
1067
|
+
const target = evt.target;
|
|
1068
|
+
const { error } = await submit(new FormData(target));
|
|
1069
|
+
if (!(options?.autoReset === false) && !error) target.reset();
|
|
1070
|
+
}, [options?.autoReset, submit]),
|
|
1071
|
+
data,
|
|
1072
|
+
error,
|
|
1073
|
+
isSubmitting
|
|
1074
|
+
};
|
|
1075
|
+
};
|
|
1083
1076
|
//#endregion
|
|
1084
1077
|
//#region utils/createLazyComponent.tsx
|
|
1085
1078
|
var createLazyComponent = (importFn, fallback) => {
|
|
@@ -1119,4 +1112,4 @@ var lazy = (importFn) => ({
|
|
|
1119
1112
|
importFn
|
|
1120
1113
|
});
|
|
1121
1114
|
//#endregion
|
|
1122
|
-
export {
|
|
1115
|
+
export { Link, Router, createRouter, lazy, useAction, useBlocker, useInvalidate, useIsDataLoading, useLoaderState, useLocation, useNavigate, useParams, useRouterContext, useSearchParams, useSubmitAction };
|
package/dist/types.d.ts
CHANGED
|
@@ -154,11 +154,10 @@ export type RouterType = {
|
|
|
154
154
|
useLoaderState: <T>() => LoaderState<T>;
|
|
155
155
|
useParams: <T>() => T;
|
|
156
156
|
useNavigate: () => (arg: Location | string | -1) => Promise<void>;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
useAction: (action: string, options?: Options) => (arg: FormData) => Promise<void>;
|
|
157
|
+
useAction: (action: string, options?: Options) => (arg: FormData) => Promise<{
|
|
158
|
+
data: unknown;
|
|
159
|
+
error: Error | null;
|
|
160
|
+
}>;
|
|
162
161
|
};
|
|
163
162
|
};
|
|
164
163
|
export type InvalidateOptions = {
|
|
@@ -170,6 +169,7 @@ export type RevalidateCache = (args: RevalidateCacheArgs) => LoadingPromise;
|
|
|
170
169
|
export type Options = Partial<{
|
|
171
170
|
onSuccess: (args: unknown) => void;
|
|
172
171
|
onError: (args: unknown) => void;
|
|
172
|
+
withBeforeLoad?: boolean;
|
|
173
173
|
}> | undefined;
|
|
174
174
|
export type InvalidateResult = {
|
|
175
175
|
path: string;
|
package/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { PropsWithChildren } from 'react';
|
|
2
|
-
type FormProps = {
|
|
3
|
-
action: string;
|
|
4
|
-
onSuccess?(arg: unknown): void;
|
|
5
|
-
onError?(arg: unknown): void;
|
|
6
|
-
autoReset?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export declare const Form: ({ children, action, onSuccess, onError, autoReset }: PropsWithChildren<FormProps>) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useFormContext: () => import("../context/FormContext").FormContextProps;
|