| 1 |
import { Component, Fragment } from 'react'; |
| 2 |
import { TooltipPosition } from '../../../components/Tooltip'; |
| 3 |
import { Label, LabelProps } from '../../../components/Label'; |
| 4 |
import { getOUIAProps, OUIAProps } from '../../../helpers'; |
| 5 |
import { SSRSafeIds } from '../../../helpers/SSRSafeIds/SSRSafeIds'; |
| 6 |
|
| 7 |
export interface ChipProps extends LabelProps, OUIAProps { |
| 8 |
/** Badge to add to the chip. The badge will be rendered after the chip text. */ |
| 9 |
badge?: React.ReactNode; |
| 10 |
/** Content rendered inside the chip text */ |
| 11 |
children?: React.ReactNode; |
| 12 |
/** Aria Label for close button */ |
| 13 |
closeBtnAriaLabel?: string; |
| 14 |
/** Additional classes added to the chip item */ |
| 15 |
className?: string; |
| 16 |
/** Flag indicating if the chip is an overflow chip */ |
| 17 |
isOverflowChip?: boolean; |
| 18 |
/** Flag indicating if chip is read only */ |
| 19 |
isReadOnly?: boolean; |
| 20 |
/** Function that is called when clicking on the chip close button */ |
| 21 |
onClick?: (event: React.MouseEvent) => void; |
| 22 |
/** @deprecated Component that will be used for chip. It is recommended that <button> or <li> are used when the chip is an overflow chip. */ |
| 23 |
component?: React.ReactNode; |
| 24 |
/** Position of the tooltip which is displayed if text is longer */ |
| 25 |
tooltipPosition?: |
| 26 |
| TooltipPosition |
| 27 |
| 'auto' |
| 28 |
| 'top' |
| 29 |
| 'bottom' |
| 30 |
| 'left' |
| 31 |
| 'right' |
| 32 |
| 'top-start' |
| 33 |
| 'top-end' |
| 34 |
| 'bottom-start' |
| 35 |
| 'bottom-end' |
| 36 |
| 'left-start' |
| 37 |
| 'left-end' |
| 38 |
| 'right-start' |
| 39 |
| 'right-end'; |
| 40 |
|
| 41 |
/** Css property expressed in percentage or any css unit that overrides the default value of the max-width of the chip's text */ |
| 42 |
textMaxWidth?: string; |
| 43 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 44 |
ouiaId?: number | string; |
| 45 |
} |
| 46 |
|
| 47 |
class Chip extends Component<ChipProps> { |
| 48 |
static displayName = 'Chip'; |
| 49 |
constructor(props: ChipProps) { |
| 50 |
super(props); |
| 51 |
} |
| 52 |
|
| 53 |
static defaultProps: ChipProps = { |
| 54 |
className: '', |
| 55 |
isOverflowChip: false, |
| 56 |
isReadOnly: false, |
| 57 |
tooltipPosition: 'top' as 'auto' | 'top' | 'bottom' | 'left' | 'right', |
| 58 |
|
| 59 |
onClick: (_e: React.MouseEvent) => undefined as any, |
| 60 |
textMaxWidth: '16ch' |
| 61 |
}; |
| 62 |
|
| 63 |
render() { |
| 64 |
const { |
| 65 |
isOverflowChip, |
| 66 |
children, |
| 67 |
badge, |
| 68 |
onClick, |
| 69 |
className, |
| 70 |
textMaxWidth, |
| 71 |
isReadOnly, |
| 72 |
closeBtnAriaLabel, |
| 73 |
tooltipPosition, |
| 74 |
ouiaId, |
| 75 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 76 |
component, |
| 77 |
...props |
| 78 |
} = this.props; |
| 79 |
|
| 80 |
return ( |
| 81 |
<SSRSafeIds prefix="pf-" ouiaComponentType={Chip.displayName}> |
| 82 |
{(_, generatedOuiaId) => ( |
| 83 |
<Label |
| 84 |
variant="outline" |
| 85 |
className={className} |
| 86 |
textMaxWidth={textMaxWidth} |
| 87 |
tooltipPosition={tooltipPosition} |
| 88 |
{...(!isReadOnly && |
| 89 |
!isOverflowChip && { |
| 90 |
onClose: onClick, |
| 91 |
closeBtnAriaLabel: closeBtnAriaLabel ? closeBtnAriaLabel : `Close ${children}` |
| 92 |
})} |
| 93 |
{...(isOverflowChip && { onClick, variant: 'overflow' })} |
| 94 |
{...getOUIAProps( |
| 95 |
isOverflowChip ? 'OverflowChip' : Chip.displayName, |
| 96 |
ouiaId !== undefined ? ouiaId : generatedOuiaId |
| 97 |
)} |
| 98 |
{...props} |
| 99 |
> |
| 100 |
{children} |
| 101 |
{badge && <Fragment> {badge}</Fragment>} |
| 102 |
</Label> |
| 103 |
)} |
| 104 |
</SSRSafeIds> |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
export { Chip }; |