import React, { HTMLAttributes, MouseEvent, KeyboardEvent, Ref, FC, } from 'react'; import classnames from 'classnames'; import { Icon, IconCategory } from './Icon'; import { Button } from './Button'; import { useEventCallback } from './hooks'; /** * */ export type PillProps = { label?: string; title?: string; truncate?: boolean; disabled?: boolean; icon?: { category?: IconCategory; icon?: string; }; pillRef?: Ref; onRemove?: () => void; } & HTMLAttributes; /** * */ export const Pill: FC = (props) => { const { icon, disabled, label, title, truncate, className, pillRef, onClick, onRemove, } = props; const onPillRemove = useEventCallback( (e: MouseEvent | KeyboardEvent) => { e.preventDefault(); e.stopPropagation(); onRemove?.(); } ); const onKeyDown = useEventCallback((e: KeyboardEvent) => { if (e.keyCode === 8 || e.keyCode === 46) { // Bacspace / DEL onPillRemove(e); } }); const pillClassNames = classnames( 'slds-pill', { 'slds-pill_link': !disabled }, { 'slds-truncate': truncate }, className ); return ( {icon && icon.icon ? ( ) : undefined} {disabled ? ( {label} ) : ( {label} )}