From 936c753ddbbb979b0cd59cdfc03d5ee40392e3f2 Mon Sep 17 00:00:00 2001 From: dmitrsavk Date: Tue, 27 Apr 2021 09:00:27 +0300 Subject: [PATCH 1/2] feat(button): add loader minimal display interval --- packages/button/src/Component.stories.mdx | 22 ++++++++--- packages/button/src/Component.tsx | 45 +++++++++++++++++++++-- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/packages/button/src/Component.stories.mdx b/packages/button/src/Component.stories.mdx index af1c41ef5f..8b634bc9cc 100644 --- a/packages/button/src/Component.stories.mdx +++ b/packages/button/src/Component.stories.mdx @@ -270,29 +270,39 @@ import { Button } from '@alfalab/core-components-button'; С помощью свойства `loading` можно отобразить состояние загрузки. +Минимальное время отображения лоадера - 500мс, чтобы при быстрых ответах от сервера кнопка не «моргала». + {React.createElement(() => { const [loading, setLoading] = React.useState({ primary: false, secondary: false }); - const handleClick = (buttonName) => { + const handleClick = (buttonName, timeout) => { setLoading({...loading, [buttonName]: true}); setTimeout(() => { setLoading({...loading, [buttonName]: false}); - }, 500) + }, timeout) } return ( - - diff --git a/packages/button/src/Component.tsx b/packages/button/src/Component.tsx index cbc4737349..f1700d4ab5 100644 --- a/packages/button/src/Component.tsx +++ b/packages/button/src/Component.tsx @@ -1,4 +1,10 @@ -import React, { AnchorHTMLAttributes, ButtonHTMLAttributes, useRef } from 'react'; +import React, { + AnchorHTMLAttributes, + ButtonHTMLAttributes, + useEffect, + useRef, + useState, +} from 'react'; import cn from 'classnames'; import mergeRefs from 'react-merge-refs'; @@ -61,8 +67,15 @@ export type ComponentProps = { type AnchorButtonProps = ComponentProps & AnchorHTMLAttributes; type NativeButtonProps = ComponentProps & ButtonHTMLAttributes; + export type ButtonProps = Partial; +/** + * Минимальное время отображения лоадера - 500мс, + * чтобы при быстрых ответах от сервера кнопка не «моргала». + */ +const LOADER_MIN_DISPLAY_INTERVAL = 500; + export const Button = React.forwardRef( ( { @@ -85,6 +98,12 @@ export const Button = React.forwardRef )} - {loading && } + + {showLoader && } + {rightAddons && {rightAddons}} ); + useEffect(() => { + if (loading) { + setLoaderTimePassed(false); + + timerId.current = window.setTimeout(() => { + setLoaderTimePassed(true); + }, LOADER_MIN_DISPLAY_INTERVAL); + } + }, [loading]); + + useEffect(() => { + return () => { + window.clearTimeout(timerId.current); + }; + }, []); + if (href) { const { target } = restProps as AnchorHTMLAttributes; @@ -146,7 +183,7 @@ export const Button = React.forwardRef {buttonChildren} From bc4761dfb5c80bc2100ec027137e233caffe5eac Mon Sep 17 00:00:00 2001 From: dmitrsavk Date: Tue, 27 Apr 2021 13:38:01 +0300 Subject: [PATCH 2/2] test(button): add some tests --- packages/button/src/Component.test.tsx | 81 +++++++++++++++++++++++--- packages/button/src/Component.tsx | 2 +- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/packages/button/src/Component.test.tsx b/packages/button/src/Component.test.tsx index 4f9fbc0eab..f0a43f0852 100644 --- a/packages/button/src/Component.test.tsx +++ b/packages/button/src/Component.test.tsx @@ -1,7 +1,29 @@ -import React, { MouseEvent } from 'react'; -import { render, fireEvent } from '@testing-library/react'; +import React, { MouseEvent, useState, FC } from 'react'; +import { render, fireEvent, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; -import { Button } from './index'; +import { Button, ButtonProps, LOADER_MIN_DISPLAY_INTERVAL } from './index'; + +const dataTestId = 'test-id'; + +const ButtonWithLoader: FC = ({ timeout, ...restProps }) => { + const [loading, setLoading] = useState(false); + + return ( + + ); +}; describe('Button', () => { describe('Snapshots tests', () => { @@ -36,7 +58,6 @@ describe('Button', () => { describe('Attributes tests', () => { it('should set `data-test-id` attribute', () => { - const dataTestId = 'test-id'; const { getByTestId } = render(