clear-react-router 1.6.7 → 1.6.8
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 +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +91 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -272,7 +272,7 @@ Actions can be executed declaratively with `<Form />` or imperatively with `useA
|
|
|
272
272
|
import { Form, useFormContext } from '../clear-router';
|
|
273
273
|
|
|
274
274
|
const SubmitButton = () => {
|
|
275
|
-
const {isSubmitting} = useFormContext()
|
|
275
|
+
const { isSubmitting } = useFormContext()
|
|
276
276
|
return <button disabled={isSubmitting} type='submit'>Save</button>
|
|
277
277
|
}
|
|
278
278
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export { RouterProvider } from './components/RouterProvider';
|
|
2
2
|
export { Router } from './components/Router';
|
|
3
3
|
export { Link } from './components/Link';
|
|
4
|
+
export { Form } from './components/Form';
|
|
4
5
|
export { useNavigate } from './hooks/useNavigate';
|
|
5
6
|
export { useParams } from './hooks/useParams';
|
|
6
7
|
export { useLocation } from './hooks/useLocation';
|
|
7
8
|
export { useLoaderState } from './hooks/useLoaderState';
|
|
8
9
|
export { useInvalidate } from './hooks/useInvalidate';
|
|
9
10
|
export { useBlocker } from './hooks/useBlocker';
|
|
11
|
+
export { useAction } from './hooks/useAction';
|
|
10
12
|
export { useBeforeUnload } from './hooks/useBeforeUnload';
|
|
11
13
|
export { useRouterContext } from './hooks/useRouterContext';
|
|
12
14
|
export { useQueryParam } from './hooks/useQueryParam';
|
package/dist/index.js
CHANGED
|
@@ -704,6 +704,12 @@ var Link = ({ children, to, prefetch: prefetchLink, hoverPrefetchDelay }) => {
|
|
|
704
704
|
});
|
|
705
705
|
};
|
|
706
706
|
//#endregion
|
|
707
|
+
//#region hooks/useInvalidate.ts
|
|
708
|
+
var useInvalidate = () => {
|
|
709
|
+
const { invalidate } = useRouterActions();
|
|
710
|
+
return invalidate;
|
|
711
|
+
};
|
|
712
|
+
//#endregion
|
|
707
713
|
//#region hooks/useParams.ts
|
|
708
714
|
var useParams = () => {
|
|
709
715
|
const [routeItemData] = useRouteItemData();
|
|
@@ -715,18 +721,61 @@ var useParams = () => {
|
|
|
715
721
|
});
|
|
716
722
|
};
|
|
717
723
|
//#endregion
|
|
724
|
+
//#region context/FormContext.ts
|
|
725
|
+
var FormContext = createContext({ isSubmitting: false });
|
|
726
|
+
//#endregion
|
|
727
|
+
//#region provider/FormProvider.tsx
|
|
728
|
+
var FormProvider = ({ children, isSubmitting }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormContext.Provider, {
|
|
729
|
+
value: { isSubmitting },
|
|
730
|
+
children
|
|
731
|
+
});
|
|
732
|
+
//#endregion
|
|
733
|
+
//#region components/Form.tsx
|
|
734
|
+
var Form = ({ children, action: actionKey, onSuccess, onError, autoReset = true }) => {
|
|
735
|
+
const invalidate = useInvalidate();
|
|
736
|
+
const [routeItemData] = useRouteItemData();
|
|
737
|
+
const [context, setContext] = useContextState();
|
|
738
|
+
const params = useParams();
|
|
739
|
+
const { routeItem } = routeItemData;
|
|
740
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
741
|
+
const onSubmit = async (evt) => {
|
|
742
|
+
evt.preventDefault();
|
|
743
|
+
if (!routeItem) throw new Error("route not found");
|
|
744
|
+
if (!routeItem.actions) throw new Error("route action creator not found");
|
|
745
|
+
const action = routeItem.actions({
|
|
746
|
+
context,
|
|
747
|
+
setContext,
|
|
748
|
+
params,
|
|
749
|
+
invalidate
|
|
750
|
+
})[actionKey];
|
|
751
|
+
if (!action) throw new Error("action not found");
|
|
752
|
+
try {
|
|
753
|
+
setIsSubmitting(true);
|
|
754
|
+
const result = await action(new FormData(evt.target));
|
|
755
|
+
await invalidate();
|
|
756
|
+
if (autoReset) evt.target.reset();
|
|
757
|
+
onSuccess?.(result);
|
|
758
|
+
} catch (error) {
|
|
759
|
+
onError?.(error);
|
|
760
|
+
} finally {
|
|
761
|
+
setIsSubmitting(false);
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormProvider, {
|
|
765
|
+
isSubmitting,
|
|
766
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("form", {
|
|
767
|
+
onSubmit,
|
|
768
|
+
children
|
|
769
|
+
})
|
|
770
|
+
});
|
|
771
|
+
};
|
|
772
|
+
//#endregion
|
|
718
773
|
//#region hooks/useLoaderState.ts
|
|
719
774
|
var useLoaderState = () => {
|
|
720
775
|
const [loaderState] = useCurrentLoaderState();
|
|
721
776
|
return loaderState;
|
|
722
777
|
};
|
|
723
778
|
//#endregion
|
|
724
|
-
//#region hooks/useInvalidate.ts
|
|
725
|
-
var useInvalidate = () => {
|
|
726
|
-
const { invalidate } = useRouterActions();
|
|
727
|
-
return invalidate;
|
|
728
|
-
};
|
|
729
|
-
//#endregion
|
|
730
779
|
//#region hooks/useBlocker.ts
|
|
731
780
|
var useBlocker = (blockerFn) => {
|
|
732
781
|
const [blockedRoute, setBlockedRoute] = useBlockedRoute();
|
|
@@ -771,6 +820,41 @@ var useBlocker = (blockerFn) => {
|
|
|
771
820
|
};
|
|
772
821
|
};
|
|
773
822
|
//#endregion
|
|
823
|
+
//#region hooks/useAction.ts
|
|
824
|
+
var useAction = (actionKey, onError) => {
|
|
825
|
+
const invalidate = useInvalidate();
|
|
826
|
+
const [routeItemData] = useRouteItemData();
|
|
827
|
+
const [context, setContext] = useContextState();
|
|
828
|
+
const params = useParams();
|
|
829
|
+
const { routeItem } = routeItemData;
|
|
830
|
+
const latestContext = useLatest(context);
|
|
831
|
+
return useCallback(async (formData) => {
|
|
832
|
+
if (!routeItem) throw new Error("route not found");
|
|
833
|
+
if (!routeItem.actions) throw new Error("route action creator not found");
|
|
834
|
+
const action = routeItem.actions({
|
|
835
|
+
context: latestContext.current,
|
|
836
|
+
setContext,
|
|
837
|
+
params,
|
|
838
|
+
invalidate
|
|
839
|
+
})[actionKey];
|
|
840
|
+
if (!action) throw new Error("action not found");
|
|
841
|
+
try {
|
|
842
|
+
await action(formData);
|
|
843
|
+
await invalidate();
|
|
844
|
+
} catch (error) {
|
|
845
|
+
onError?.(error);
|
|
846
|
+
}
|
|
847
|
+
}, [
|
|
848
|
+
actionKey,
|
|
849
|
+
latestContext,
|
|
850
|
+
invalidate,
|
|
851
|
+
onError,
|
|
852
|
+
params,
|
|
853
|
+
routeItem,
|
|
854
|
+
setContext
|
|
855
|
+
]);
|
|
856
|
+
};
|
|
857
|
+
//#endregion
|
|
774
858
|
//#region hooks/useBeforeUnload.ts
|
|
775
859
|
var useBeforeUnload = (callback) => {
|
|
776
860
|
useEffect(() => {
|
|
@@ -892,9 +976,6 @@ var useHistoricalTrail = () => {
|
|
|
892
976
|
return trail;
|
|
893
977
|
};
|
|
894
978
|
//#endregion
|
|
895
|
-
//#region context/FormContext.ts
|
|
896
|
-
var FormContext = createContext({ isSubmitting: false });
|
|
897
|
-
//#endregion
|
|
898
979
|
//#region hooks/useFormContext.ts
|
|
899
980
|
var useFormContext = () => useContext(FormContext);
|
|
900
981
|
//#endregion
|
|
@@ -960,4 +1041,4 @@ var adapter = {
|
|
|
960
1041
|
})
|
|
961
1042
|
};
|
|
962
1043
|
//#endregion
|
|
963
|
-
export { Link, Router, RouterProvider, adapter, createRouter, useBeforeUnload, useBlocker, useFormContext, useHistoricalTrail, useInvalidate, useLoaderState, useLocation, useNavigate, useParams, useQueryParam, useRouterContext, useSearchParams };
|
|
1044
|
+
export { Form, Link, Router, RouterProvider, adapter, createRouter, useAction, useBeforeUnload, useBlocker, useFormContext, useHistoricalTrail, useInvalidate, useLoaderState, useLocation, useNavigate, useParams, useQueryParam, useRouterContext, useSearchParams };
|