|
| 1 | +import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; |
| 2 | +import { Loader } from '@ui5/webcomponents-react/lib/Loader'; |
| 3 | +import { LoaderType } from '@ui5/webcomponents-react/lib/LoaderType'; |
| 4 | +import { Card } from '@ui5/webcomponents-react/lib/Card'; |
| 5 | +import { Text } from '@ui5/webcomponents-react/lib/Text'; |
| 6 | +import { Icon } from '@ui5/webcomponents-react/lib/Icon'; |
| 7 | +import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox'; |
| 8 | +import { FlexBoxDirection } from '@ui5/webcomponents-react/lib/FlexBoxDirection'; |
| 9 | +import { createSelectArgTypes } from '@shared/stories/createSelectArgTypes'; |
| 10 | +import { DocsHeader } from '@shared/stories/DocsHeader'; |
| 11 | +import { DocsCommonProps } from '@shared/stories/DocsCommonProps'; |
| 12 | +import { spacing } from '@ui5/webcomponents-react-base'; |
| 13 | +import '@ui5/webcomponents-icons/dist/icons/activate.js'; |
| 14 | +import { useEffect, useRef, useState } from 'react'; |
| 15 | + |
| 16 | +<Meta |
| 17 | + title="Components / Loader" |
| 18 | + component={Loader} |
| 19 | + argTypes={{ |
| 20 | + ...createSelectArgTypes({ type: LoaderType }), |
| 21 | + ...DocsCommonProps |
| 22 | + }} |
| 23 | + args={{ |
| 24 | + type: LoaderType.Indeterminate, |
| 25 | + progress: '60%', |
| 26 | + style: {}, |
| 27 | + className: '', |
| 28 | + tooltip: '', |
| 29 | + slot: '', |
| 30 | + ref: null |
| 31 | + }} |
| 32 | +/> |
| 33 | + |
| 34 | +<DocsHeader /> |
| 35 | + |
| 36 | +<Canvas> |
| 37 | + <Story name="Default"> |
| 38 | + {(args) => { |
| 39 | + return <Loader {...args} />; |
| 40 | + }} |
| 41 | + </Story> |
| 42 | +</Canvas> |
| 43 | + |
| 44 | +<ArgsTable story="." /> |
| 45 | + |
| 46 | +<br /> |
| 47 | + |
| 48 | +# Stories |
| 49 | + |
| 50 | +<br /> |
| 51 | + |
| 52 | +## Card with Loader |
| 53 | + |
| 54 | +<Canvas> |
| 55 | + <Story name="with Card"> |
| 56 | + {(args) => { |
| 57 | + const initialText = |
| 58 | + 'This is a text that will be updated 5 seconds after the header is clicked. The text will be visible until the new text has been fetched. To let the user know that the text is updating, the Loader will be displayed.'; |
| 59 | + const [text, setText] = useState(initialText); |
| 60 | + const [loading, setLoading] = useState(false); |
| 61 | + const counter = useRef(0); |
| 62 | + const onHeaderClick = () => { |
| 63 | + setLoading(true); |
| 64 | + }; |
| 65 | + useEffect(() => { |
| 66 | + if (loading) { |
| 67 | + setTimeout(() => { |
| 68 | + if (counter.current < 10) { |
| 69 | + setText((prev) => (prev === initialText ? 'Updated (⌐■_■)' : initialText)); |
| 70 | + } else { |
| 71 | + setText('You really must be bored ಠ_ಠ'); |
| 72 | + } |
| 73 | + counter.current++; |
| 74 | + setLoading(false); |
| 75 | + }, 5000); |
| 76 | + } |
| 77 | + }, [loading]); |
| 78 | + return ( |
| 79 | + <Card |
| 80 | + avatar={<Icon name="activate" />} |
| 81 | + heading="Click the header to update the text below." |
| 82 | + headerInteractive |
| 83 | + style={{ width: '400px' }} |
| 84 | + onHeaderClick={onHeaderClick} |
| 85 | + > |
| 86 | + <FlexBox direction={FlexBoxDirection.Column}> |
| 87 | + {loading && <Loader {...args} />} |
| 88 | + <Text style={spacing.sapUiContentPadding}>{text}</Text> |
| 89 | + </FlexBox> |
| 90 | + </Card> |
| 91 | + ); |
| 92 | + }} |
| 93 | + </Story> |
| 94 | +</Canvas> |
| 95 | + |
| 96 | +```jsx |
| 97 | +const LoaderComponent = () => { |
| 98 | + const initialText = |
| 99 | + 'This is a text that will be updated 5 seconds after the header is clicked. The text will be visible until the new text has been fetched. To let the user know that the text is updating, the Loader will be displayed.'; |
| 100 | + const [text, setText] = useState(initialText); |
| 101 | + const [loading, setLoading] = useState(false); |
| 102 | + const onHeaderClick = () => { |
| 103 | + setLoading(true); |
| 104 | + }; |
| 105 | + useEffect(() => { |
| 106 | + if (loading) { |
| 107 | + setTimeout(() => { |
| 108 | + setText((prev) => (prev === initialText ? 'Updated (⌐■_■)' : initialText)); |
| 109 | + setLoading(false); |
| 110 | + }, 5000); |
| 111 | + } |
| 112 | + }, [loading]); |
| 113 | + return ( |
| 114 | + <Card |
| 115 | + avatar={<Icon name="activate" />} |
| 116 | + heading="Click the header to update the text below." |
| 117 | + headerInteractive |
| 118 | + style={{ width: '400px' }} |
| 119 | + onHeaderClick={onHeaderClick} |
| 120 | + > |
| 121 | + <FlexBox direction={FlexBoxDirection.Column}> |
| 122 | + {loading && <Loader />} |
| 123 | + <Text style={spacing.sapUiContentPadding}>{text}</Text> |
| 124 | + </FlexBox> |
| 125 | + </Card> |
| 126 | + ); |
| 127 | +}; |
| 128 | +``` |
0 commit comments