11import React , { FC , ReactNode , ButtonHTMLAttributes , Ref , useRef } from 'react' ;
22import classnames from 'classnames' ;
3- import { Icon } from './Icon' ;
3+ import { SvgIcon , IconCategory } from './Icon' ;
44import { Spinner } from './Spinner' ;
55import { useEventCallback , useMergeRefs } from './hooks' ;
66
77export type ButtonType =
88 | 'neutral'
99 | 'brand'
10+ | 'outline-brand'
1011 | 'destructive'
12+ | 'text-destructive'
13+ | 'success'
1114 | 'inverse'
1215 | 'icon'
13- | 'icon-bare'
1416 | 'icon-container'
1517 | 'icon-inverse'
1618 | 'icon-more'
@@ -31,6 +33,7 @@ export type ButtonIconMoreSize = 'x-small' | 'small' | 'medium' | 'large';
3133 */
3234export type ButtonIconProps = {
3335 className ?: string ;
36+ category ?: IconCategory ;
3437 icon : string ;
3538 align ?: ButtonIconAlign ;
3639 size ?: ButtonIconSize ;
@@ -43,9 +46,9 @@ export type ButtonIconProps = {
4346 */
4447export const ButtonIcon : FC < ButtonIconProps > = ( {
4548 icon,
49+ category = 'utility' ,
4650 align,
4751 size,
48- inverse,
4952 className,
5053 style,
5154 ...props
@@ -56,19 +59,22 @@ export const ButtonIcon: FC<ButtonIconProps> = ({
5659 : null ;
5760 const sizeClassName =
5861 size && ICON_SIZES . indexOf ( size ) >= 0 ? `slds-button__icon_${ size } ` : null ;
59- const inverseClassName = inverse ? 'slds-button__icon_inverse' : null ;
6062 const iconClassNames = classnames (
6163 'slds-button__icon' ,
6264 alignClassName ,
6365 sizeClassName ,
64- inverseClassName ,
6566 className
6667 ) ;
68+
69+ if ( icon . indexOf ( ':' ) > 0 ) {
70+ [ category , icon ] = icon . split ( ':' ) as [ IconCategory , string ] ;
71+ }
72+
6773 return (
68- < Icon
74+ < SvgIcon
6975 className = { iconClassNames }
7076 icon = { icon }
71- textColor = { null }
77+ category = { category }
7278 pointerEvents = 'none'
7379 style = { style }
7480 { ...props }
@@ -118,6 +124,7 @@ export const Button: FC<ButtonProps> = (props) => {
118124 buttonRef : buttonRef_ ,
119125 iconMoreSize : iconMoreSize_ ,
120126 onClick : onClick_ ,
127+ tabIndex,
121128 ...rprops
122129 } = props ;
123130
@@ -135,19 +142,24 @@ export const Button: FC<ButtonProps> = (props) => {
135142 onClick_ ?.( e ) ;
136143 } ) ;
137144
145+ const content = children || label ;
146+ const isIconOnly = type && / ^ i c o n - / . test ( type ) && icon && ! content ;
147+
138148 const typeClassName = type ? `slds-button_${ type } ` : null ;
139149 const btnClassNames = classnames ( className , 'slds-button' , typeClassName , {
140150 'slds-is-selected' : selected ,
151+ [ 'slds-button_icon' ] : / ^ i c o n - / . test ( type ?? '' ) ,
141152 [ `slds-button_icon-${ size ?? '' } ` ] :
142153 / ^ ( x - s m a l l | s m a l l ) $ / . test ( size ?? '' ) && / ^ i c o n - / . test ( type ?? '' ) ,
143154 } ) ;
144155
145- const buttonContent = (
146- // eslint-disable-next-line react/button-has-type
156+ return (
147157 < button
148158 ref = { buttonRef }
149159 className = { btnClassNames }
150160 type = { htmlType }
161+ title = { isIconOnly || alt ? alt : undefined }
162+ tabIndex = { tabIndex ?? - 1 }
151163 { ...rprops }
152164 onClick = { onClick }
153165 >
@@ -159,7 +171,7 @@ export const Button: FC<ButtonProps> = (props) => {
159171 inverse = { inverse }
160172 />
161173 ) : undefined }
162- { children || label }
174+ { content }
163175 { icon && iconAlign === 'right' ? (
164176 < ButtonIcon
165177 icon = { icon }
@@ -171,22 +183,10 @@ export const Button: FC<ButtonProps> = (props) => {
171183 { iconMore ? (
172184 < ButtonIcon icon = { iconMore } align = 'right' size = { iconMoreSize } />
173185 ) : undefined }
174- { alt ? < span className = 'slds-assistive-text' > { alt } </ span > : undefined }
186+ { isIconOnly || alt ? (
187+ < span className = 'slds-assistive-text' > { alt ?? icon } </ span >
188+ ) : undefined }
175189 { loading ? < Spinner /> : undefined }
176190 </ button >
177191 ) ;
178-
179- if ( props . tabIndex != null ) {
180- return (
181- < span
182- className = 'react-slds-button-focus-wrapper'
183- style = { { outline : 0 } }
184- tabIndex = { - 1 }
185- >
186- { buttonContent }
187- </ span >
188- ) ;
189- }
190-
191- return buttonContent ;
192192} ;
0 commit comments