clear-react-router 2.1.1 → 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 +51 -60
- package/dist/types.d.ts +6 -8
- package/dist/utils/commitState.d.ts +1 -1
- 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
|
|
@@ -30,13 +30,12 @@ var useGlobalState = ({ subscribe, getState, setState }) => {
|
|
|
30
30
|
};
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region utils/commitState.ts
|
|
33
|
-
var createCommitState = ({ routeItemDataState, statusState,
|
|
33
|
+
var createCommitState = ({ routeItemDataState, statusState, loaderStateRef, loaderState }) => (nextLocation, routeItem) => {
|
|
34
34
|
routeItemDataState.setState({
|
|
35
35
|
routeItem,
|
|
36
36
|
location: nextLocation
|
|
37
37
|
});
|
|
38
38
|
statusState.setState("active");
|
|
39
|
-
isOptimisticLoading.setState(false);
|
|
40
39
|
loaderState.setState(loaderStateRef.value);
|
|
41
40
|
if ((nextLocation.search ? `${nextLocation.pathname}${nextLocation.search}` : nextLocation.pathname) === window.location.pathname + window.location.search) return;
|
|
42
41
|
history.pushState(null, "", `${nextLocation.pathname}${nextLocation.search}`);
|
|
@@ -201,7 +200,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
201
200
|
let navigationSeq = 0;
|
|
202
201
|
let interval = 0;
|
|
203
202
|
let abortController = null;
|
|
204
|
-
const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState
|
|
203
|
+
const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState } = routerState;
|
|
205
204
|
const navigationExecutor = createCommitState(routerState);
|
|
206
205
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
207
206
|
const createSignal = () => {
|
|
@@ -253,7 +252,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
253
252
|
routeItem,
|
|
254
253
|
location
|
|
255
254
|
});
|
|
256
|
-
|
|
255
|
+
statusState.setState("optimistic");
|
|
257
256
|
const currentLoaderState = loaderMap.get(path)?.state;
|
|
258
257
|
if (currentLoaderState) loaderState.setState(currentLoaderState);
|
|
259
258
|
} else {
|
|
@@ -285,7 +284,8 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
285
284
|
}), getLoaderDurationPromise(routeItem, nextLocation)]);
|
|
286
285
|
if (result) loaderStateRef.set((prev) => ({
|
|
287
286
|
...prev,
|
|
288
|
-
|
|
287
|
+
data: result.data,
|
|
288
|
+
loaderError: result.error
|
|
289
289
|
}));
|
|
290
290
|
if (seq !== navigationSeq) return;
|
|
291
291
|
polling(routeItem, nextLocation);
|
|
@@ -527,7 +527,6 @@ var createRouterInstance = () => {
|
|
|
527
527
|
from: "",
|
|
528
528
|
to: ""
|
|
529
529
|
}),
|
|
530
|
-
isOptimisticLoading: create(false),
|
|
531
530
|
loaderState: create(EMPTY_LOADER_STATE),
|
|
532
531
|
loaderStateRef: new Cell(EMPTY_LOADER_STATE),
|
|
533
532
|
loaderMap: /* @__PURE__ */ new Map(),
|
|
@@ -537,7 +536,7 @@ var createRouterInstance = () => {
|
|
|
537
536
|
const invalidate = createInvalidate(routerState, revalidateCache);
|
|
538
537
|
const navigate = createNavigate(routerState, revalidateCache);
|
|
539
538
|
const prefetch = createPrefetch(routerState, revalidateCache);
|
|
540
|
-
const
|
|
539
|
+
const useGetCurrentAction = (actionKey) => {
|
|
541
540
|
const { routeItem } = routerState.routeItemDataState.getState();
|
|
542
541
|
const context = routerState.contextState.getState();
|
|
543
542
|
const setContext = routerState.contextState.setState;
|
|
@@ -551,10 +550,7 @@ var createRouterInstance = () => {
|
|
|
551
550
|
invalidate
|
|
552
551
|
})[actionKey];
|
|
553
552
|
if (!action) throw new Error(`Action "${actionKey}" not found`);
|
|
554
|
-
return
|
|
555
|
-
currentAction: action,
|
|
556
|
-
invalidate
|
|
557
|
-
};
|
|
553
|
+
return action;
|
|
558
554
|
};
|
|
559
555
|
return {
|
|
560
556
|
state: routerState,
|
|
@@ -569,7 +565,6 @@ var createRouterInstance = () => {
|
|
|
569
565
|
useScrollMap: () => useGlobalState(routerState.scrollMapState),
|
|
570
566
|
useStatus: () => useGlobalState(routerState.statusState),
|
|
571
567
|
useContextState: () => useGlobalState(routerState.contextState),
|
|
572
|
-
useOptimisticLoading: () => useGlobalState(routerState.isOptimisticLoading),
|
|
573
568
|
useLoaderState: () => useGlobalState(routerState.loaderState)[0],
|
|
574
569
|
useParams: () => getParamsObject(),
|
|
575
570
|
useNavigate: () => {
|
|
@@ -590,16 +585,23 @@ var createRouterInstance = () => {
|
|
|
590
585
|
} else if (JSON.stringify(arg) !== JSON.stringify(location)) await navigate(arg);
|
|
591
586
|
};
|
|
592
587
|
},
|
|
593
|
-
useGetAction,
|
|
594
588
|
useAction: (action, options = {}) => {
|
|
595
|
-
const
|
|
589
|
+
const currentAction = useGetCurrentAction(action);
|
|
596
590
|
return async (formData) => {
|
|
597
591
|
try {
|
|
598
|
-
const
|
|
599
|
-
await invalidate();
|
|
600
|
-
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
|
+
};
|
|
601
599
|
} catch (error) {
|
|
602
600
|
options.onError?.(error);
|
|
601
|
+
return {
|
|
602
|
+
data: null,
|
|
603
|
+
error
|
|
604
|
+
};
|
|
603
605
|
}
|
|
604
606
|
};
|
|
605
607
|
}
|
|
@@ -774,12 +776,12 @@ var IS_MOBILE = isMobile();
|
|
|
774
776
|
var MOBILE_CACHE_SIZE = 60;
|
|
775
777
|
var DESKTOP_CACHE_SIZE = 150;
|
|
776
778
|
var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, optimisticSpinner = true, defaultMinLoaderDuration = 0, maxCacheSize = IS_MOBILE ? MOBILE_CACHE_SIZE : DESKTOP_CACHE_SIZE, defaultPrefetch = IS_MOBILE ? "viewport" : "hover", defaultHoverPrefetchDelay = 150, defaultScrollRestorationBehavior = "auto", errorBoundary: ErrorBoundary = EmptyBoundary }) => {
|
|
777
|
-
const { useRouteItemData, useStatus
|
|
779
|
+
const { useRouteItemData, useStatus } = router.hooks;
|
|
778
780
|
const [routeItemData] = useRouteItemData();
|
|
779
781
|
const [status] = useStatus();
|
|
780
|
-
const [isOptimisticLoading] = useOptimisticLoading();
|
|
781
782
|
const loaderState = useLoaderState();
|
|
782
783
|
const isLoading = status === "pending";
|
|
784
|
+
const isOptimistic = status === "optimistic";
|
|
783
785
|
useNavigation();
|
|
784
786
|
useSetRouterConfig({
|
|
785
787
|
routes,
|
|
@@ -803,7 +805,7 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
803
805
|
if (showErrorElement) return renderElement(routeItem.errorElement || defaultErrorElement);
|
|
804
806
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
805
807
|
style: { viewTransitionName: "page" },
|
|
806
|
-
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, { children: renderElement(routeItem.element) }, location.pathname), optimisticSpinner &&
|
|
808
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, { children: renderElement(routeItem.element) }, location.pathname), optimisticSpinner && isOptimistic && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Spinner, {})]
|
|
807
809
|
});
|
|
808
810
|
};
|
|
809
811
|
//#endregion
|
|
@@ -916,42 +918,6 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
916
918
|
});
|
|
917
919
|
};
|
|
918
920
|
//#endregion
|
|
919
|
-
//#region context/FormContext.ts
|
|
920
|
-
var FormContext = createContext({ isSubmitting: false });
|
|
921
|
-
//#endregion
|
|
922
|
-
//#region provider/FormProvider.tsx
|
|
923
|
-
var FormProvider = ({ children, isSubmitting }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormContext.Provider, {
|
|
924
|
-
value: { isSubmitting },
|
|
925
|
-
children
|
|
926
|
-
});
|
|
927
|
-
//#endregion
|
|
928
|
-
//#region components/Form.tsx
|
|
929
|
-
var Form = ({ children, action, onSuccess, onError, autoReset = true }) => {
|
|
930
|
-
const { currentAction, invalidate } = router.hooks.useGetAction(action);
|
|
931
|
-
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
932
|
-
const onSubmit = async (evt) => {
|
|
933
|
-
evt.preventDefault();
|
|
934
|
-
try {
|
|
935
|
-
setIsSubmitting(true);
|
|
936
|
-
const result = await currentAction(new FormData(evt.target));
|
|
937
|
-
await invalidate();
|
|
938
|
-
if (autoReset) evt.target.reset();
|
|
939
|
-
onSuccess?.(result);
|
|
940
|
-
} catch (error) {
|
|
941
|
-
onError?.(error);
|
|
942
|
-
} finally {
|
|
943
|
-
setIsSubmitting(false);
|
|
944
|
-
}
|
|
945
|
-
};
|
|
946
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormProvider, {
|
|
947
|
-
isSubmitting,
|
|
948
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("form", {
|
|
949
|
-
onSubmit,
|
|
950
|
-
children
|
|
951
|
-
})
|
|
952
|
-
});
|
|
953
|
-
};
|
|
954
|
-
//#endregion
|
|
955
921
|
//#region hooks/useParams.ts
|
|
956
922
|
var useParams = router.hooks.useParams;
|
|
957
923
|
//#endregion
|
|
@@ -1080,8 +1046,33 @@ var useSearchParams = () => {
|
|
|
1080
1046
|
};
|
|
1081
1047
|
};
|
|
1082
1048
|
//#endregion
|
|
1083
|
-
//#region hooks/
|
|
1084
|
-
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
|
+
};
|
|
1085
1076
|
//#endregion
|
|
1086
1077
|
//#region utils/createLazyComponent.tsx
|
|
1087
1078
|
var createLazyComponent = (importFn, fallback) => {
|
|
@@ -1121,4 +1112,4 @@ var lazy = (importFn) => ({
|
|
|
1121
1112
|
importFn
|
|
1122
1113
|
});
|
|
1123
1114
|
//#endregion
|
|
1124
|
-
export {
|
|
1115
|
+
export { Link, Router, createRouter, lazy, useAction, useBlocker, useInvalidate, useIsDataLoading, useLoaderState, useLocation, useNavigate, useParams, useRouterContext, useSearchParams, useSubmitAction };
|
package/dist/types.d.ts
CHANGED
|
@@ -119,7 +119,7 @@ export type LoadingPromise = Promise<{
|
|
|
119
119
|
data: null;
|
|
120
120
|
error: unknown;
|
|
121
121
|
} | undefined>;
|
|
122
|
-
export type Status = 'idle' | 'pending' | 'active';
|
|
122
|
+
export type Status = 'idle' | 'pending' | 'active' | 'optimistic';
|
|
123
123
|
export type ScrollMap = Record<string, [string, number][]>;
|
|
124
124
|
export type RouterState = {
|
|
125
125
|
routeItemDataState: Store<RouteItemData>;
|
|
@@ -130,7 +130,6 @@ export type RouterState = {
|
|
|
130
130
|
from: string;
|
|
131
131
|
to: string;
|
|
132
132
|
}>;
|
|
133
|
-
isOptimisticLoading: Store<boolean>;
|
|
134
133
|
loaderState: Store<LoaderState>;
|
|
135
134
|
loaderStateRef: Cell<LoaderState>;
|
|
136
135
|
loaderMap: Map<string, LoaderStateItem>;
|
|
@@ -152,15 +151,13 @@ export type RouterType = {
|
|
|
152
151
|
useScrollMap: () => ReturnType<typeof useGlobalState<ScrollMap>>;
|
|
153
152
|
useStatus: () => ReturnType<typeof useGlobalState<Status>>;
|
|
154
153
|
useContextState: () => ReturnType<typeof useGlobalState<Record<string, unknown>>>;
|
|
155
|
-
useOptimisticLoading: () => ReturnType<typeof useGlobalState<boolean>>;
|
|
156
154
|
useLoaderState: <T>() => LoaderState<T>;
|
|
157
155
|
useParams: <T>() => T;
|
|
158
156
|
useNavigate: () => (arg: Location | string | -1) => Promise<void>;
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
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
|
+
}>;
|
|
164
161
|
};
|
|
165
162
|
};
|
|
166
163
|
export type InvalidateOptions = {
|
|
@@ -172,6 +169,7 @@ export type RevalidateCache = (args: RevalidateCacheArgs) => LoadingPromise;
|
|
|
172
169
|
export type Options = Partial<{
|
|
173
170
|
onSuccess: (args: unknown) => void;
|
|
174
171
|
onError: (args: unknown) => void;
|
|
172
|
+
withBeforeLoad?: boolean;
|
|
175
173
|
}> | undefined;
|
|
176
174
|
export type InvalidateResult = {
|
|
177
175
|
path: string;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Location, RouteItem, RouterState } from '../types';
|
|
2
|
-
export declare const createCommitState: ({ routeItemDataState, statusState,
|
|
2
|
+
export declare const createCommitState: ({ routeItemDataState, statusState, loaderStateRef, loaderState }: RouterState) => (nextLocation: Location, routeItem: RouteItem | undefined) => void;
|
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;
|