clear-react-router 1.7.0 → 1.7.1

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 CHANGED
@@ -264,12 +264,12 @@ Actions can be executed declaratively with `<Form />` or imperatively with `useA
264
264
 
265
265
  ## Form
266
266
 
267
- `Form` automatically creates a `FormData` object, executes the specified route action, invalidates the current route, and optionally resets the form.
267
+ `Form` automatically creates a `FormData` object on submit event, executes the specified route action, invalidates the current route, and optionally resets the form, if fields are uncontrolled.
268
268
 
269
269
  `isSubmitting` value available inside the `Form` component from the `useFormContext` hook
270
270
 
271
271
  ```tsx
272
- import { Form, useFormContext } from '../clear-router';
272
+ import { Form, useFormContext } from 'clear-react-router';
273
273
 
274
274
  const SubmitButton = () => {
275
275
  const {isSubmitting} = useFormContext()
@@ -317,17 +317,11 @@ const save = useAction('save');
317
317
 
318
318
  const handleClick = async () => {
319
319
  const data = new FormData();
320
-
321
320
  data.append('title', 'Hello');
322
-
323
321
  await save(data);
324
322
  };
325
- ```
326
323
 
327
- ```tsx
328
- <button onClick={handleClick}>
329
- Save
330
- </button>
324
+ <button onClick={handleClick}>Save</button>
331
325
  ```
332
326
 
333
327
  `useAction` automatically invalidates the current route after a successful action, causing both `beforeLoad` and `loader` to run again in the background.
@@ -5,5 +5,5 @@ type FormProps = {
5
5
  onError?(arg: unknown): void;
6
6
  autoReset?: boolean;
7
7
  };
8
- export declare const Form: ({ children, action: actionKey, onSuccess, onError, autoReset, }: PropsWithChildren<FormProps>) => import("react/jsx-runtime").JSX.Element;
8
+ export declare const Form: ({ children, action, onSuccess, onError, autoReset }: PropsWithChildren<FormProps>) => import("react/jsx-runtime").JSX.Element;
9
9
  export {};
@@ -0,0 +1,4 @@
1
+ export declare const useGetAction: (actionKey: string) => {
2
+ currentAction: (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown;
3
+ invalidate: (path?: string) => Promise<void>;
4
+ };
package/dist/index.js CHANGED
@@ -729,40 +729,38 @@ var useParams = () => {
729
729
  }) : void 0, [pathname, routeItem]);
730
730
  };
731
731
  //#endregion
732
- //#region hooks/useActionParams.ts
733
- var useActionParams = () => {
732
+ //#region hooks/useGetAction.ts
733
+ var useGetAction = (actionKey) => {
734
734
  const invalidate = useInvalidate();
735
735
  const [routeItemData] = useRouteItemData();
736
736
  const [context, setContext] = useContextState();
737
737
  const params = useParams();
738
738
  const { routeItem } = routeItemData;
739
- return {
740
- invalidate,
741
- routeItem,
742
- latestContext: useLatest(context),
739
+ const latestContext = useLatest(context);
740
+ if (!routeItem) throw new Error("Route not found");
741
+ if (!routeItem.actions) throw new Error("Route action creator not found");
742
+ const action = routeItem.actions({
743
+ context: latestContext.current,
744
+ setContext,
743
745
  params,
744
- setContext
746
+ invalidate
747
+ })[actionKey];
748
+ if (!action) throw new Error(`Action "${actionKey}" not found`);
749
+ return {
750
+ currentAction: action,
751
+ invalidate
745
752
  };
746
753
  };
747
754
  //#endregion
748
755
  //#region components/Form.tsx
749
- var Form = ({ children, action: actionKey, onSuccess, onError, autoReset = true }) => {
750
- const { invalidate, routeItem, latestContext, params, setContext } = useActionParams();
756
+ var Form = ({ children, action, onSuccess, onError, autoReset = true }) => {
757
+ const { currentAction, invalidate } = useGetAction(action);
751
758
  const [isSubmitting, setIsSubmitting] = useState(false);
752
759
  const onSubmit = async (evt) => {
753
760
  evt.preventDefault();
754
- if (!routeItem) throw new Error("Route not found");
755
- if (!routeItem.actions) throw new Error("Route action creator not found");
756
- const action = routeItem.actions({
757
- context: latestContext.current,
758
- setContext,
759
- params,
760
- invalidate
761
- })[actionKey];
762
- if (!action) throw new Error(`Action "${actionKey}" not found`);
763
761
  try {
764
762
  setIsSubmitting(true);
765
- const result = await action(new FormData(evt.target));
763
+ const result = await currentAction(new FormData(evt.target));
766
764
  await invalidate();
767
765
  if (autoReset) evt.target.reset();
768
766
  onSuccess?.(result);
@@ -833,19 +831,10 @@ var useBlocker = (blockerFn) => {
833
831
  //#endregion
834
832
  //#region hooks/useAction.ts
835
833
  var useAction = (action, options = {}) => {
836
- const { invalidate, routeItem, latestContext, params, setContext } = useActionParams();
834
+ const { currentAction, invalidate } = useGetAction(action);
837
835
  const latestOnSuccess = useLatest(options?.onSuccess);
838
836
  const latestOnError = useLatest(options?.onError);
839
837
  return useCallback(async (formData) => {
840
- if (!routeItem) throw new Error("Route not found");
841
- if (!routeItem.actions) throw new Error("Route action creator not found");
842
- const currentAction = routeItem.actions({
843
- context: latestContext.current,
844
- setContext,
845
- params,
846
- invalidate
847
- })[action];
848
- if (!currentAction) throw new Error(`Action "${action}" not found`);
849
838
  try {
850
839
  const result = await currentAction(formData);
851
840
  await invalidate();
@@ -854,14 +843,10 @@ var useAction = (action, options = {}) => {
854
843
  latestOnError.current?.(error);
855
844
  }
856
845
  }, [
857
- action,
846
+ currentAction,
858
847
  invalidate,
859
- latestContext,
860
848
  latestOnError,
861
- latestOnSuccess,
862
- params,
863
- routeItem,
864
- setContext
849
+ latestOnSuccess
865
850
  ]);
866
851
  };
867
852
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {
@@ -1,7 +0,0 @@
1
- export declare const useActionParams: () => {
2
- invalidate: (path?: string) => Promise<void>;
3
- routeItem: import("..").RouteItem | undefined;
4
- latestContext: import("react").RefObject<Record<string, unknown>>;
5
- params: Record<string, string>;
6
- setContext: (action: Record<string, unknown> | ((prevState: Record<string, unknown>) => Record<string, unknown>)) => void;
7
- };