Skip to content

Commit 2516b1e

Browse files
Valent1325Maksim MalofeevMaksim MalofeevMaksim MalofeevMaksim Malofeev
authored
feat(Button): add support for string icon names from @gravity-ui/icons (#1260)
* feat(Button): add svg icon to button * feat(Button): update memory-bank, add screnshot test, change name props * feat(Button): update screnshot test * feat(Button): update screnshots * feat(Button): update story icon * feat(Button): update screnshot test * feat(Button): update story, screenshot tests and memory-bank * feat(Button): update imports * feat(Button): add support for string icon names from @gravity-ui/icons --------- Co-authored-by: Maksim Malofeev <[email protected]> Co-authored-by: Maksim Malofeev <[email protected]> Co-authored-by: Maksim Malofeev <[email protected]> Co-authored-by: Maksim Malofeev <[email protected]> Co-authored-by: Maksim Malofeev <[email protected]> Co-authored-by: Maksim Malofeev <[email protected]> Co-authored-by: Maksim Malofeev <[email protected]>
1 parent 34eed61 commit 2516b1e

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

src/components/Button/Button.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {useAnalytics} from '../../hooks';
1313
import {Github} from '../../icons';
1414
import {ButtonProps as ButtonParams, DefaultEventNames, QAProps} from '../../models';
1515
import {block, setUrlTld} from '../../utils';
16+
import {getGravityIcon} from '../../utils/icons';
1617

1718
import {i18n} from './i18n';
1819
import {ICON_QA, OldButtonSize, OldButtonTheme, toCommonSize, toCommonView} from './utils';
@@ -73,8 +74,10 @@ const Button = (props: ButtonProps) => {
7374
}
7475
: {url: img, position: defaultImgPosition};
7576

77+
const {position, iconData, iconSize, className: iconClassName, url: imgUrl, alt} = buttonImg;
78+
7679
const buttonClass = img
77-
? b({position: buttonImg.position, ...buttonModifiers}, className)
80+
? b({position, ...buttonModifiers}, className)
7881
: b({...buttonModifiers}, className);
7982

8083
const buttonProps = {
@@ -92,25 +95,33 @@ const Button = (props: ButtonProps) => {
9295
let icon;
9396
let image;
9497

95-
if (img && buttonImg.iconData) {
96-
const iconSize = buttonImg.iconSize;
97-
const iconClassName = buttonImg.className ? b('icon', buttonImg.className) : b('icon');
98+
if (img && iconData) {
99+
let finalIconData = iconData;
100+
101+
if (typeof iconData === 'string') {
102+
const gravityIcon = getGravityIcon(iconData);
103+
if (gravityIcon) {
104+
finalIconData = gravityIcon;
105+
}
106+
}
107+
108+
const finalIconClassName = iconClassName ? b('icon', iconClassName) : b('icon');
98109
icon = (
99110
<Icon
100-
className={iconClassName}
101-
data={buttonImg.iconData}
111+
className={finalIconClassName}
112+
data={finalIconData}
102113
size={iconSize}
103114
qa={ICON_QA}
104115
/>
105116
);
106-
} else if (img && buttonImg.url) {
117+
} else if (img && imgUrl) {
107118
image = (
108119
<img
109120
className={b('image')}
110-
width={buttonImg.iconSize || DEFAULT_IMG_SIZE}
111-
height={buttonImg.iconSize || DEFAULT_IMG_SIZE}
112-
src={buttonImg.url}
113-
alt={buttonImg.alt || i18n('image-alt')}
121+
width={iconSize || DEFAULT_IMG_SIZE}
122+
height={iconSize || DEFAULT_IMG_SIZE}
123+
src={imgUrl}
124+
alt={alt || i18n('image-alt')}
114125
/>
115126
);
116127
}
@@ -132,13 +143,13 @@ const Button = (props: ButtonProps) => {
132143
width={width}
133144
{...(buttonProps as UIKitButtonProps)}
134145
>
135-
{icon && buttonImg.position === 'left' ? icon : null}
146+
{icon && position === 'left' ? icon : null}
136147
<span className={b('content')}>
137-
{image && buttonImg.position === 'left' ? image : null}
148+
{image && position === 'left' ? image : null}
138149
<span className={b('text')}>{text}</span>
139-
{image && buttonImg.position === 'right' ? image : null}
150+
{image && position === 'right' ? image : null}
140151
</span>
141-
{icon && buttonImg.position === 'right' ? icon : null}
152+
{icon && position === 'right' ? icon : null}
142153
</CommonButton>
143154
);
144155
};

src/components/Button/__stories__/Button.stories.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {Plus} from '@gravity-ui/icons';
21
import {IconData} from '@gravity-ui/uikit';
32
import {Meta, StoryFn} from '@storybook/react';
43

@@ -101,23 +100,23 @@ const IconTemplate: StoryFn<ButtonProps> = (args) => {
101100
</Row>
102101

103102
{createButtonRow('Gravity UI', [
104-
<Button key="plus-left" {...args} img={createIconConfig(Plus)} text="Button" />,
103+
<Button key="plus-left" {...args} img={createIconConfig('Plus')} text="Button" />,
105104
<Button
106105
key="plus-right"
107106
{...args}
108-
img={createIconConfig(Plus, 'right')}
107+
img={createIconConfig('Plus', 'right')}
109108
text="Button"
110109
/>,
111110
<Button
112111
key="plus-small"
113112
{...args}
114-
img={createIconConfig(Plus, 'left', 14)}
113+
img={createIconConfig('Plus', 'left', 14)}
115114
text="Button"
116115
/>,
117116
<Button
118117
key="plus-large"
119118
{...args}
120-
img={createIconConfig(Plus, 'left', 20)}
119+
img={createIconConfig('Plus', 'left', 20)}
121120
text="Button"
122121
/>,
123122
])}

src/utils/icons.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as GravityIcons from '@gravity-ui/icons';
2+
import {IconData} from '@gravity-ui/uikit';
3+
4+
export function getGravityIcon(iconName: string): IconData | undefined {
5+
if (hasGravityIcon(iconName)) {
6+
return (GravityIcons as Record<string, IconData>)[iconName];
7+
}
8+
9+
return undefined;
10+
}
11+
12+
export function hasGravityIcon(iconName: string): boolean {
13+
return iconName in GravityIcons;
14+
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './url';
55
export * from './cn';
66
export * from './url';
77
export * from './theme';
8+
export * from './icons';
89

910
export type {HubspotEventData, HubspotEventHandlers, HubspotEventName} from './hubspot';
1011
export {isHubspotEventData} from './hubspot';

0 commit comments

Comments
 (0)