Skip to content

Commit

Permalink
Components: Fix icon condition for Badge (#68588)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ciampo <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2025
1 parent ddfbdcc commit 79234bd
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions packages/components/src/badge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,46 @@ import type { BadgeProps } from './types';
import type { WordPressComponentProps } from '../context';
import Icon from '../icon';

/**
* Returns an icon based on the badge context.
*
* @return The corresponding icon for the provided context.
*/
function contextBasedIcon( intent: BadgeProps[ 'intent' ] = 'default' ) {
switch ( intent ) {
case 'info':
return info;
case 'success':
return published;
case 'warning':
return caution;
case 'error':
return error;
default:
return null;
}
}

function Badge( {
className,
intent = 'default',
children,
...props
}: WordPressComponentProps< BadgeProps, 'span', false > ) {
/**
* Returns an icon based on the badge context.
*
* @return The corresponding icon for the provided context.
*/
function contextBasedIcon() {
switch ( intent ) {
case 'info':
return info;
case 'success':
return published;
case 'warning':
return caution;
case 'error':
return error;
default:
return null;
}
}
const icon = contextBasedIcon( intent );
const hasIcon = !! icon;

return (
<span
className={ clsx(
'components-badge',
`is-${ intent }`,
intent !== 'default' && 'has-icon',
className
) }
className={ clsx( 'components-badge', className, {
[ `is-${ intent }` ]: intent,
'has-icon': hasIcon,
} ) }
{ ...props }
>
{ intent !== 'default' && (
{ hasIcon && (
<Icon
icon={ contextBasedIcon() }
icon={ icon }
size={ 16 }
fill="currentColor"
className="components-badge__icon"
Expand Down

0 comments on commit 79234bd

Please sign in to comment.