1- import { IconData } from '@gravity-ui/uikit' ;
1+ import * as React from 'react' ;
2+
23import { Meta , StoryFn } from '@storybook/react' ;
34
4- import { Col , Grid , Row } from '../../../grid' ;
5+ import { Col , Grid , GridAlignItems , Row } from '../../../grid' ;
56import Button , { ButtonProps } from '../Button' ;
67
78import { CONTRAST_THEMES , SIZES , THEMES } from './constants' ;
@@ -13,177 +14,134 @@ import './Button.stories.scss';
1314export default {
1415 component : Button ,
1516 title : 'Components/Links and buttons/Button' ,
16- args : {
17- text : 'Button' ,
18- size : 'l' ,
19- } ,
20- argTypes : {
21- size : {
22- control : {
23- type : 'select' ,
24- } ,
25- options : SIZES ,
26- } ,
27- } ,
2817} as Meta ;
2918
30- const createIconConfig = (
31- iconData : IconData ,
32- position ?: 'left' | 'right' ,
33- iconSize ?: number ,
34- className ?: string ,
35- ) => ( { iconData, position, iconSize, className} ) ;
19+ const THEMES_SIZES = [ ...THEMES , ...CONTRAST_THEMES ] . map ( ( theme ) =>
20+ SIZES . map ( ( size ) => ( {
21+ size,
22+ theme,
23+ ...data . themesSizes ,
24+ } ) ) ,
25+ ) ;
3626
37- const createButtonRow = ( label : string , buttons : React . ReactNode [ ] ) => (
38- < Row >
39- < Col style = { { margin : '20px 0' } } > { label } </ Col >
40- { buttons . map ( ( button , index ) => (
41- < Col key = { index } > { button } </ Col >
42- ) ) }
43- </ Row >
27+ const ICON_VARIANTS = [
28+ { iconData : 'Plus' } ,
29+ { iconData : data . iconExamples . signatureSvg } ,
30+ { url : data . iconExamples . customUrl } ,
31+ ] . map ( ( source ) =>
32+ [ { position : 'left' } , { position : 'right' } , { iconSize : 14 } , { iconSize : 20 } ] . map ( ( props ) => ( {
33+ img : {
34+ ...source ,
35+ ...props ,
36+ } ,
37+ ...data . withIcon ,
38+ } ) ) ,
4439) ;
40+ const ICON_SOURCES = [ 'Gravity UI' , 'Custom SVG' , 'Img URL' ] ;
4541
4642const DefaultTemplate : StoryFn < ButtonProps > = ( args ) => < Button { ...args } /> ;
4743
48- const SizesTemplate : StoryFn < ButtonProps > = ( args ) => (
49- < Row >
50- < Col style = { { margin : '20px 0' } } > { args . theme } </ Col >
51- { SIZES . map ( ( size ) => (
52- < Col key = { size } >
53- < Button { ...args } size = { size } />
44+ const SizesTemplate : StoryFn < Record < number , ButtonProps > > = ( args ) => (
45+ < Row
46+ alignItems = { GridAlignItems . End }
47+ style = { {
48+ background : CONTRAST_THEMES . includes ( args [ 0 ] . theme as ( typeof CONTRAST_THEMES ) [ number ] )
49+ ? 'rgb(58, 116, 255)'
50+ : 'transparent' ,
51+ padding : '10px 0' ,
52+ } }
53+ >
54+ < Col > { args [ 0 ] . theme } </ Col >
55+ { Object . values ( args ) . map ( ( itemArgs , index ) => (
56+ < Col key = { index } >
57+ < Button { ...itemArgs } />
5458 </ Col >
5559 ) ) }
5660 </ Row >
5761) ;
5862
59- const ThemesSizesTemplate : StoryFn < ButtonProps > = ( args ) => (
60- < Grid >
61- < Row >
62- < Col />
63- { SIZES . map ( ( size ) => (
64- < Col key = { size } > { size } </ Col >
65- ) ) }
66- </ Row >
67- { THEMES . map ( ( theme ) => (
68- < SizesTemplate key = { theme } { ...args } theme = { theme } />
69- ) ) }
70- < section style = { { color : '#fff' , background : '#3a74ff' } } >
71- { CONTRAST_THEMES . map ( ( theme ) => (
72- < SizesTemplate key = { theme } { ...args } theme = { theme } />
63+ const ThemesSizesTemplate : StoryFn < Record < number , Record < number , ButtonProps > > > = ( args ) => (
64+ < React . Fragment >
65+ < Grid >
66+ < Row >
67+ < Col />
68+ { Object . values ( args [ 0 ] ) . map ( ( arg , index ) => (
69+ < Col key = { index } > { arg . size } </ Col >
70+ ) ) }
71+ </ Row >
72+ { Object . values ( args ) . map ( ( arg , index ) => (
73+ < SizesTemplate key = { index } { ...arg } />
7374 ) ) }
74- </ section >
75- </ Grid >
75+ </ Grid >
76+ </ React . Fragment >
7677) ;
7778
78- const WidthTemplate : StoryFn < ButtonProps > = ( args ) => (
79+ const VariantsTemplate : StoryFn < Record < number , ButtonProps > > = ( args ) => (
7980 < Row >
80- < Col >
81- < Button { ...args } width = "auto" />
82- </ Col >
83- < Col >
84- < Button { ...args } width = "max" />
85- </ Col >
81+ { Object . values ( args ) . map ( ( arg , index ) => (
82+ < Col key = { index } >
83+ < Button { ...arg } />
84+ </ Col >
85+ ) ) }
8686 </ Row >
8787) ;
8888
89- const IconTemplate : StoryFn < ButtonProps > = ( args ) => {
90- const { iconExamples} = data ;
91-
92- return (
89+ const IconTemplate : StoryFn < Record < number , Record < number , ButtonProps > > > = ( args ) => (
90+ < React . Fragment >
9391 < Grid >
9492 < Row >
95- < Col > Source </ Col >
93+ < Col / >
9694 < Col > Left</ Col >
9795 < Col > Right</ Col >
9896 < Col > Small</ Col >
9997 < Col > Large</ Col >
10098 </ Row >
101-
102- { createButtonRow ( 'Gravity UI' , [
103- < Button key = "plus-left" { ...args } img = { createIconConfig ( 'Plus' ) } text = "Button" /> ,
104- < Button
105- key = "plus-right"
106- { ...args }
107- img = { createIconConfig ( 'Plus' , 'right' ) }
108- text = "Button"
109- /> ,
110- < Button
111- key = "plus-small"
112- { ...args }
113- img = { createIconConfig ( 'Plus' , 'left' , 14 ) }
114- text = "Button"
115- /> ,
116- < Button
117- key = "plus-large"
118- { ...args }
119- img = { createIconConfig ( 'Plus' , 'left' , 20 ) }
120- text = "Button"
121- /> ,
122- ] ) }
123-
124- { createButtonRow ( 'Custom SVG' , [
125- < Button
126- key = "signature-left"
127- { ...args }
128- img = { createIconConfig ( iconExamples . signatureSvg ) }
129- text = "Button"
130- /> ,
131- < Button
132- key = "signature-right"
133- { ...args }
134- img = { createIconConfig ( iconExamples . signatureSvg , 'right' ) }
135- text = "Button"
136- /> ,
137- < Button
138- key = "signature-small"
139- { ...args }
140- img = { createIconConfig ( iconExamples . signatureSvg , 'left' , 14 ) }
141- text = "Button"
142- /> ,
143- < Button
144- key = "signature-large"
145- { ...args }
146- img = { createIconConfig ( iconExamples . signatureSvg , 'left' , 20 ) }
147- text = "Button"
148- /> ,
149- ] ) }
150-
151- { createButtonRow ( 'Img URL' , [
152- < Button
153- key = "gravity-left"
154- { ...args }
155- img = { { url : iconExamples . customUrl , position : 'left' } }
156- text = "Button"
157- /> ,
158- < Button
159- key = "gravity-right"
160- { ...args }
161- img = { { url : iconExamples . customUrl , position : 'right' } }
162- text = "Button"
163- /> ,
164- < Button
165- key = "gravity-small"
166- { ...args }
167- img = { { url : iconExamples . customUrl , position : 'left' , iconSize : 14 } }
168- text = "Button"
169- /> ,
170- < Button
171- key = "gravity-large"
172- { ...args }
173- img = { { url : iconExamples . customUrl , position : 'left' , iconSize : 20 } }
174- text = "Button"
175- /> ,
176- ] ) }
99+ { Object . values ( args ) . map ( ( arg , index ) => (
100+ < Row
101+ key = { index }
102+ alignItems = { GridAlignItems . End }
103+ style = { {
104+ background : CONTRAST_THEMES . includes (
105+ arg [ 0 ] . theme as ( typeof CONTRAST_THEMES ) [ number ] ,
106+ )
107+ ? 'rgb(58, 116, 255)'
108+ : 'transparent' ,
109+ padding : '10px 0' ,
110+ } }
111+ >
112+ < Col > { ICON_SOURCES [ index ] } </ Col >
113+ { Object . values ( arg ) . map ( ( itemArgs , indexInner ) => (
114+ < Col key = { indexInner } >
115+ < Button { ...itemArgs } />
116+ </ Col >
117+ ) ) }
118+ </ Row >
119+ ) ) }
177120 </ Grid >
178- ) ;
179- } ;
121+ </ React . Fragment >
122+ ) ;
180123
181124export const Default = DefaultTemplate . bind ( { } ) ;
182- export const ThemesSizes = ThemesSizesTemplate . bind ( { } ) ;
183- export const Width = WidthTemplate . bind ( { } ) ;
184- export const Icons = IconTemplate . bind ( { } ) ;
125+ export const ThemesSizes = ThemesSizesTemplate . bind ( [ [ ] ] ) ;
126+ ThemesSizes . parameters = {
127+ controls : {
128+ include : Object . keys ( THEMES_SIZES ) ,
129+ } ,
130+ } ;
131+ export const Width = VariantsTemplate . bind ( [ ] ) ;
132+ Width . parameters = {
133+ controls : {
134+ include : Object . keys ( data . width ) ,
135+ } ,
136+ } ;
137+ export const Icons = IconTemplate . bind ( [ [ ] ] ) ;
138+ Icons . parameters = {
139+ controls : {
140+ include : Object . keys ( ICON_VARIANTS ) ,
141+ } ,
142+ } ;
185143
186- Default . args = data . default . content as ButtonProps ;
187- ThemesSizes . args = data . themesSizes . content as ButtonProps ;
188- Width . args = data . themesSizes . content as ButtonProps ;
189- Icons . args = data . withIcon . content as ButtonProps ;
144+ Default . args = data . default as ButtonProps ;
145+ ThemesSizes . args = THEMES_SIZES as ButtonProps [ ] [ ] ;
146+ Width . args = data . width as ButtonProps [ ] ;
147+ Icons . args = ICON_VARIANTS as ButtonProps [ ] [ ] ;
0 commit comments