From 66a25b3b1c6241c10fa0db24c19dd9911cb51214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D1=81=D0=B5=D0=BD=D0=B8=D1=8F?= <31247233+kseniya57@users.noreply.github.com> Date: Tue, 14 Nov 2023 20:30:40 +0300 Subject: [PATCH] feat(useAsyncActionHandler): add useAsyncActionHandler hook (#1095) Co-authored-by: kseniyakuzina --- src/hooks/index.ts | 1 + src/hooks/useAsyncActionHandler/README.md | 89 +++++++++++++++++++ .../__stories__/ProgressButton.tsx | 16 ++++ .../UseAsyncActionHandlerDemo.classname.ts | 3 + .../UseAsyncActionHandlerDemo.scss | 11 +++ .../__stories__/UseAsyncActionHandlerDemo.tsx | 42 +++++++++ .../UseAsyncActionHandlerStories.stories.tsx | 11 +++ src/hooks/useAsyncActionHandler/index.ts | 1 + .../useAsyncActionHandler.ts | 36 ++++++++ 9 files changed, 210 insertions(+) create mode 100644 src/hooks/useAsyncActionHandler/README.md create mode 100644 src/hooks/useAsyncActionHandler/__stories__/ProgressButton.tsx create mode 100644 src/hooks/useAsyncActionHandler/__stories__/UseAsyncActionHandlerDemo.classname.ts create mode 100644 src/hooks/useAsyncActionHandler/__stories__/UseAsyncActionHandlerDemo.scss create mode 100644 src/hooks/useAsyncActionHandler/__stories__/UseAsyncActionHandlerDemo.tsx create mode 100644 src/hooks/useAsyncActionHandler/__stories__/UseAsyncActionHandlerStories.stories.tsx create mode 100644 src/hooks/useAsyncActionHandler/index.ts create mode 100644 src/hooks/useAsyncActionHandler/useAsyncActionHandler.ts diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 9cd1ead75d..8c733a9c24 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,4 +1,5 @@ export * from './useActionHandlers'; +export * from './useAsyncActionHandler'; export * from './useBodyScrollLock'; export * from './useFileInput'; export * from './useFocusWithin'; diff --git a/src/hooks/useAsyncActionHandler/README.md b/src/hooks/useAsyncActionHandler/README.md new file mode 100644 index 0000000000..5fcff146df --- /dev/null +++ b/src/hooks/useAsyncActionHandler/README.md @@ -0,0 +1,89 @@ + + +# useAsyncActionHandler + + + +```tsx +import {useAsyncActionHandler} from '@gravity-ui/uikit'; +``` + +The `useAsyncActionHandler` hook wraps an asynchronous action handler to add a loading state to it. +Starts the loading process before executing the passed action and terminates the process after executing the action. +Returns the loading state and the wrapped action handler + +## Properties + +| Name | Description | Type | Default | +| :------ | :------------- | :---------------------------------------------: | :-----: | +| handler | action handler | `(...args: unknown[]) => PromiseLike;` | | + +## Result + +```ts +{ + isLoading: boolean; + handler: typeof handler; +} +``` + +Usage: + +```tsx +const action = useCallback(() => Promise.resolve(), []); + +const {isLoading, handler: handleAction} = useAsyncActionHandler({handler: action}); +``` + +### Examples + +Button with automatic loading during click handler execution + +```tsx +type ProgressButtonProps = { + onClick: (event: React.MouseEvent) => PromiseLike; +} & Exclude; + +export const ProgressButton = (props: ProgressButtonProps) => { + const {onClick, loading: providedIsLoading} = props; + + const {isLoading, handler: handleClick} = useAsyncActionHandler({handler: onClick}); + + return