|
1 | | -import React, { useCallback, useState, useRef } from 'react'; |
2 | 1 | import type { ComponentProps } from 'react'; |
3 | | -import { action } from '@storybook/addon-actions'; |
4 | | -import type { StoryObj, Meta } from '@storybook/react'; |
5 | | -import { IconMic } from '@salutejs/plasma-icons'; |
6 | | -import { disableProps, getConfigVariations, InSpacingDecorator } from '@salutejs/plasma-sb-utils'; |
| 2 | +import type { Meta } from '@storybook/react'; |
| 3 | +import { getButtonStories } from '@salutejs/plasma-sb-utils'; |
7 | 4 |
|
8 | 5 | import { config } from './Button.config'; |
9 | 6 |
|
10 | 7 | import { Button } from '.'; |
11 | 8 |
|
12 | 9 | type ButtonProps = ComponentProps<typeof Button>; |
13 | 10 |
|
14 | | -const { views, sizes } = getConfigVariations(config); |
15 | | - |
16 | | -const stretchingValues = ['auto', 'filled', 'fixed']; |
17 | | -const pinValues = [ |
18 | | - '', |
19 | | - 'square-square', |
20 | | - 'square-clear', |
21 | | - 'clear-square', |
22 | | - 'clear-clear', |
23 | | - 'clear-circle', |
24 | | - 'circle-clear', |
25 | | - 'circle-circle', |
26 | | -]; |
27 | | -const contentPlacingValues = ['default', 'relaxed']; |
28 | | - |
29 | | -const onClick = action('onClick'); |
30 | | -const onFocus = action('onFocus'); |
31 | | -const onBlur = action('onBlur'); |
| 11 | +const { meta: META, Default } = getButtonStories({ component: Button, componentConfig: config }); |
32 | 12 |
|
33 | 13 | const meta: Meta<ButtonProps> = { |
| 14 | + ...META, |
34 | 15 | title: 'Data Entry/Button', |
35 | | - decorators: [InSpacingDecorator], |
36 | | - component: Button, |
37 | | - args: { |
38 | | - view: 'default', |
39 | | - size: 'm', |
40 | | - contentPlacing: 'default', |
41 | | - stretching: 'auto', |
42 | | - text: 'Hello', |
43 | | - value: 'Value', |
44 | | - disabled: false, |
45 | | - focused: true, |
46 | | - square: false, |
47 | | - isLoading: false, |
48 | | - }, |
49 | | - argTypes: { |
50 | | - view: { |
51 | | - options: views, |
52 | | - control: { |
53 | | - type: 'select', |
54 | | - }, |
55 | | - }, |
56 | | - size: { |
57 | | - options: sizes, |
58 | | - control: { |
59 | | - type: 'select', |
60 | | - }, |
61 | | - }, |
62 | | - value: { |
63 | | - control: { |
64 | | - type: 'text', |
65 | | - }, |
66 | | - }, |
67 | | - contentPlacing: { |
68 | | - options: contentPlacingValues, |
69 | | - control: { |
70 | | - type: 'select', |
71 | | - }, |
72 | | - }, |
73 | | - stretching: { |
74 | | - options: stretchingValues, |
75 | | - control: { |
76 | | - type: 'select', |
77 | | - }, |
78 | | - }, |
79 | | - pin: { |
80 | | - options: pinValues, |
81 | | - control: { |
82 | | - type: 'select', |
83 | | - }, |
84 | | - table: { defaultValue: { summary: 'bottom' } }, |
85 | | - }, |
86 | | - ...disableProps([ |
87 | | - 'theme', |
88 | | - 'loader', |
89 | | - 'onClick', |
90 | | - 'onFocus', |
91 | | - 'onBlur', |
92 | | - 'outlined', |
93 | | - 'contentLeft', |
94 | | - 'contentRight', |
95 | | - 'shiftLeft', |
96 | | - 'shiftRight', |
97 | | - 'stretch', |
98 | | - 'as', |
99 | | - 'forwardedAs', |
100 | | - 'pin', |
101 | | - 'focused', |
102 | | - ]), |
103 | | - }, |
104 | 16 | }; |
105 | 17 |
|
106 | 18 | export default meta; |
107 | 19 |
|
108 | | -type StoryPropsDefault = ComponentProps<typeof Button> & { |
109 | | - enableContentLeft: boolean; |
110 | | - enableContentRight: boolean; |
111 | | -}; |
112 | | - |
113 | | -const StoryDefault = ({ enableContentLeft, enableContentRight, size, ...rest }: StoryPropsDefault) => { |
114 | | - const iconSize = size === 'xs' || size === 'xxs' ? 'xs' : 's'; |
115 | | - |
116 | | - return ( |
117 | | - <Button |
118 | | - contentLeft={enableContentLeft && size !== 'xxs' ? <IconMic size={iconSize} color="inherit" /> : undefined} |
119 | | - contentRight={ |
120 | | - enableContentRight && size !== 'xxs' ? <IconMic size={iconSize} color="inherit" /> : undefined |
121 | | - } |
122 | | - size={size} |
123 | | - onClick={onClick} |
124 | | - onFocus={onFocus} |
125 | | - onBlur={onBlur} |
126 | | - {...rest} |
127 | | - /> |
128 | | - ); |
129 | | -}; |
130 | | - |
131 | | -export const Default: StoryObj<StoryPropsDefault> = { |
132 | | - args: { |
133 | | - enableContentLeft: false, |
134 | | - enableContentRight: false, |
135 | | - value: '', |
136 | | - }, |
137 | | - argTypes: { |
138 | | - ...disableProps(['value']), |
139 | | - }, |
140 | | - render: (args) => <StoryDefault {...args} />, |
141 | | -}; |
142 | | - |
143 | | -export const WithValue: StoryObj<StoryPropsDefault> = { |
144 | | - args: { |
145 | | - enableContentLeft: false, |
146 | | - }, |
147 | | - argTypes: { |
148 | | - ...disableProps(['enableContentRight']), |
149 | | - }, |
150 | | - render: (args) => <StoryDefault {...args} />, |
151 | | -}; |
152 | | - |
153 | | -export const Anchor: StoryObj<StoryPropsDefault> = { |
154 | | - args: { |
155 | | - as: 'a', |
156 | | - enableContentLeft: false, |
157 | | - enableContentRight: false, |
158 | | - value: '', |
159 | | - }, |
160 | | - argTypes: { |
161 | | - ...disableProps(['value']), |
162 | | - }, |
163 | | - render: (args) => <StoryDefault {...args} />, |
164 | | -}; |
165 | | - |
166 | | -const StoryLoading = ({ |
167 | | - enableContentLeft, |
168 | | - enableContentRight, |
169 | | - size, |
170 | | - isLoading, |
171 | | - onClick: _onClick, |
172 | | - ...rest |
173 | | -}: StoryPropsDefault) => { |
174 | | - const [loading, setLoading] = useState(isLoading); |
175 | | - const [count, setCount] = useState(0); |
176 | | - const intervalId = useRef<number | undefined>(); |
177 | | - |
178 | | - const iconSize = size === 'xs' || size === 'xxs' ? 'xs' : 's'; |
179 | | - |
180 | | - const onClickHandle = useCallback( |
181 | | - (event) => { |
182 | | - event.persist(); |
183 | | - |
184 | | - _onClick?.(event); |
185 | | - |
186 | | - setLoading(true); |
187 | | - |
188 | | - intervalId.current = window.setInterval(() => { |
189 | | - setCount((prev) => { |
190 | | - return prev + 1; |
191 | | - }); |
192 | | - }, 1_000); |
193 | | - }, |
194 | | - [_onClick], |
195 | | - ); |
196 | | - |
197 | | - if (count === 6) { |
198 | | - setLoading(false); |
199 | | - setCount(0); |
200 | | - window.clearInterval(intervalId.current); |
201 | | - } |
202 | | - |
203 | | - return ( |
204 | | - <Button |
205 | | - autoFocus |
206 | | - onClick={onClickHandle} |
207 | | - contentLeft={enableContentLeft && size !== 'xxs' ? <IconMic size={iconSize} color="inherit" /> : undefined} |
208 | | - contentRight={ |
209 | | - enableContentRight && size !== 'xxs' ? <IconMic size={iconSize} color="inherit" /> : undefined |
210 | | - } |
211 | | - isLoading={loading} |
212 | | - size={size} |
213 | | - loader={<div>Loading - {count}</div>} |
214 | | - {...rest} |
215 | | - /> |
216 | | - ); |
217 | | -}; |
218 | | - |
219 | | -export const Loading: StoryObj<StoryPropsDefault> = { |
220 | | - args: { |
221 | | - ...Default.args, |
222 | | - text: 'Start loading', |
223 | | - value: '', |
224 | | - }, |
225 | | - argTypes: { |
226 | | - ...disableProps(['text', 'value']), |
227 | | - }, |
228 | | - render: (args) => <StoryLoading {...args} />, |
229 | | -}; |
| 20 | +export { Default }; |
0 commit comments