'use client'; import classnames from 'classnames'; import React from 'react'; import MdIconCheckCircle from '../icons-material/MdIconCheckCircle'; import MdIconDangerous from '../icons-material/MdIconDangerous'; import MdIconInfo from '../icons-material/MdIconInfo'; import MdIconWarning from '../icons-material/MdIconWarning'; import { MdTooltip } from '../tooltip/MdTooltip'; export type MdTagThemePrimary = 'primary' | 'secondary'; export type MdTagThemeOther = 'success' | 'warning' | 'info' | 'error'; interface MdTagCommon extends React.HTMLAttributes { type?: 'base' | 'light' | 'outlined'; label?: string; labels?: string[]; showIcon?: boolean; /** * Let you apply your own icon component. * Note: When `theme` is set to `primary` or `secondary`, this property is required. * * @example * customIcon={} * * @type {React.ReactNode} * */ customIcon?: React.ReactNode; tooltipOnly?: boolean; } // When `theme` is explicitly set to `primary` or `secondary`, `customIcon` is required. export type MdTagProps = | (MdTagCommon & { theme: MdTagThemePrimary; customIcon: React.ReactNode }) | (MdTagCommon & { theme?: MdTagThemeOther | undefined; customIcon?: React.ReactNode }); export const MdTag: React.FC = ({ theme = 'primary', type = 'base', label, showIcon = false, customIcon, tooltipOnly = false, ...otherProps }: MdTagProps) => { const classNames = classnames( 'md-tag', { 'md-tag-theme--primary': theme === 'primary', 'md-tag-theme--secondary': theme === 'secondary', 'md-tag-theme--success': theme === 'success', 'md-tag-theme--warning': theme === 'warning', 'md-tag-theme--info': theme === 'info', 'md-tag-theme--error': theme === 'error', 'md-tag-type--outline': type === 'outlined', 'md-tag-type--light': type === 'light', 'md-tag--nolabel': !label, }, otherProps.className, ); const renderIcon = () => { let icon = (<>) as React.ReactNode; if (theme === 'primary' || theme === 'secondary') { icon = customIcon; return icon; } if (theme === 'success') { icon = ; } else if (theme === 'warning') { icon = ; } else if (theme === 'info') { icon = ; } else if (theme === 'error') { icon = ; } return icon; }; return (
{showIcon && !tooltipOnly &&
{renderIcon()}
} {tooltipOnly && showIcon && ( {renderIcon()} )} {!tooltipOnly &&
{label}
}
); }; export default MdTag;