diff --git a/packages/main/src/components/Loader/Loader.test.tsx b/packages/main/src/components/Loader/Loader.test.tsx index 6e868e137be..11767c54843 100644 --- a/packages/main/src/components/Loader/Loader.test.tsx +++ b/packages/main/src/components/Loader/Loader.test.tsx @@ -28,4 +28,9 @@ describe('Loader', () => { const wrapper = renderThemedComponent(); expect(wrapper).toMatchSnapshot(); }); + + test('with delay', () => { + const wrapper = renderThemedComponent(); + expect(wrapper).toMatchSnapshot(); + }); }); diff --git a/packages/main/src/components/Loader/__snapshots__/Loader.test.tsx.snap b/packages/main/src/components/Loader/__snapshots__/Loader.test.tsx.snap index b32e4cceaa0..61bf60552d1 100644 --- a/packages/main/src/components/Loader/__snapshots__/Loader.test.tsx.snap +++ b/packages/main/src/components/Loader/__snapshots__/Loader.test.tsx.snap @@ -54,3 +54,5 @@ exports[`Loader with Custom Class Name 1`] = ` title="Please wait" /> `; + +exports[`Loader with delay 1`] = `null`; diff --git a/packages/main/src/components/Loader/demo.stories.tsx b/packages/main/src/components/Loader/demo.stories.tsx index d9432f45f42..91061fd342b 100644 --- a/packages/main/src/components/Loader/demo.stories.tsx +++ b/packages/main/src/components/Loader/demo.stories.tsx @@ -1,10 +1,14 @@ -import { select, text } from '@storybook/addon-knobs'; -import React from 'react'; +import { number, select, text } from '@storybook/addon-knobs'; import { Loader } from '@ui5/webcomponents-react/lib/Loader'; import { LoaderType } from '@ui5/webcomponents-react/lib/LoaderType'; +import React from 'react'; export const renderLoader = () => ( - + ); renderLoader.story = { name: 'Default' diff --git a/packages/main/src/components/Loader/index.tsx b/packages/main/src/components/Loader/index.tsx index feab0fd8085..dec2c3252a3 100644 --- a/packages/main/src/components/Loader/index.tsx +++ b/packages/main/src/components/Loader/index.tsx @@ -1,12 +1,16 @@ import { StyleClassHelper } from '@ui5/webcomponents-react-base/lib/StyleClassHelper'; import { LoaderType } from '@ui5/webcomponents-react/lib/LoaderType'; -import React, { CSSProperties, FC, forwardRef, RefObject, useMemo } from 'react'; +import React, { CSSProperties, FC, forwardRef, RefObject, useEffect, useMemo, useState } from 'react'; import { createUseStyles } from 'react-jss'; import { CommonProps } from '../../interfaces/CommonProps'; import { JSSTheme } from '../../interfaces/JSSTheme'; import { styles } from './Loader.jss'; export interface LoaderProps extends CommonProps { + /* + * Delay in ms until the Loader will be displayed + */ + delay?: number; type?: LoaderType; progress?: CSSProperties['width']; } @@ -14,8 +18,9 @@ export interface LoaderProps extends CommonProps { const useStyles = createUseStyles>(styles, { name: 'Loader' }); const Loader: FC = forwardRef((props: LoaderProps, ref: RefObject) => { - const { className, type, progress, tooltip, slot, style } = props; + const { className, type, progress, tooltip, slot, style, delay } = props; const classes = useStyles(props); + const [isVisible, setIsVisible] = useState(delay === 0); const loaderClasses = StyleClassHelper.of(classes.loader); if (className) { @@ -31,6 +36,18 @@ const Loader: FC = forwardRef((props: LoaderProps, ref: RefObject { + if (delay > 0) { + setTimeout(() => { + setIsVisible(true); + }, delay); + } + }, []); + + if (!isVisible) { + return null; + } + return (
= forwardRef((props: LoaderProps, ref: RefObject { const wrapper = renderThemedComponent(); expect(wrapper).toMatchSnapshot(); }); + + test('with delay', () => { + const wrapper = renderThemedComponent(); + expect(wrapper).toMatchSnapshot(); + }); }); diff --git a/packages/main/src/components/Spinner/__snapshots__/Spinner.test.tsx.snap b/packages/main/src/components/Spinner/__snapshots__/Spinner.test.tsx.snap index 3c010a975a8..c05baea20d1 100644 --- a/packages/main/src/components/Spinner/__snapshots__/Spinner.test.tsx.snap +++ b/packages/main/src/components/Spinner/__snapshots__/Spinner.test.tsx.snap @@ -59,3 +59,5 @@ exports[`Spinner with custom class name 1`] = ` Loading...
`; + +exports[`Spinner with delay 1`] = `null`; diff --git a/packages/main/src/components/Spinner/demo.stories.tsx b/packages/main/src/components/Spinner/demo.stories.tsx index 1d3b9c53151..761da6066e3 100644 --- a/packages/main/src/components/Spinner/demo.stories.tsx +++ b/packages/main/src/components/Spinner/demo.stories.tsx @@ -1,9 +1,22 @@ -import { select } from '@storybook/addon-knobs'; -import React from 'react'; +import { number, select } from '@storybook/addon-knobs'; import { Size } from '@ui5/webcomponents-react/lib/Size'; import { Spinner } from '@ui5/webcomponents-react/lib/Spinner'; +import React, { useMemo } from 'react'; -export const renderSpinner = () => ; +let spinnerKey = 0; + +const Demo = (props) => { + const { delay } = props; + + const spinner = useMemo(() => { + spinnerKey++; + return ; + }, [delay]); + + return spinner; +}; + +export const renderSpinner = () => ; renderSpinner.story = { name: 'Default' }; diff --git a/packages/main/src/components/Spinner/index.tsx b/packages/main/src/components/Spinner/index.tsx index bcc196c9bd4..0fa6f6b3698 100644 --- a/packages/main/src/components/Spinner/index.tsx +++ b/packages/main/src/components/Spinner/index.tsx @@ -1,19 +1,24 @@ import { StyleClassHelper } from '@ui5/webcomponents-react-base/lib/StyleClassHelper'; -import React, { forwardRef, RefObject, FC } from 'react'; +import { Size } from '@ui5/webcomponents-react/lib/Size'; +import React, { FC, forwardRef, RefObject, useEffect, useState } from 'react'; import { createUseStyles } from 'react-jss'; import { CommonProps } from '../../interfaces/CommonProps'; -import { Size } from '@ui5/webcomponents-react/lib/Size'; import { styles } from './Spinner.jss'; export interface SpinnerProps extends CommonProps { + /* + * Delay in ms until the Spinner will be displayed + */ + delay?: number; size?: Size; } const useStyles = createUseStyles(styles, { name: 'Spinner' }); const Spinner: FC = forwardRef((props: SpinnerProps, ref: RefObject) => { - const { className, size, tooltip, slot, style } = props; + const { className, size, tooltip, slot, style, delay } = props; const classes = useStyles(); + const [isVisible, setIsVisible] = useState(delay === 0); const spinnerClasses = StyleClassHelper.of(classes.spinner); if (className) { @@ -22,6 +27,18 @@ const Spinner: FC = forwardRef((props: SpinnerProps, ref: RefObjec spinnerClasses.put(classes[`spinner${size}`]); + useEffect(() => { + if (delay > 0) { + setTimeout(() => { + setIsVisible(true); + }, delay); + } + }, []); + + if (!isVisible) { + return null; + } + return (
= forwardRef((props: SpinnerProps, ref: RefObjec }); Spinner.defaultProps = { + delay: 0, size: Size.Medium };