| 1 |
import { Component } from 'react'; |
| 2 |
import { LabelGroup, LabelGroupProps } from '../../../components/Label'; |
| 3 |
import { TooltipPosition } from '../../../components/Tooltip'; |
| 4 |
import { getOUIAProps, OUIAProps } from '../../../helpers'; |
| 5 |
|
| 6 |
export interface ChipGroupProps extends Omit<LabelGroupProps, 'ref'>, OUIAProps { |
| 7 |
/** Content rendered inside the chip group. Should be <Chip> elements. */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** Additional classes added to the chip item */ |
| 10 |
className?: string; |
| 11 |
/** Flag for having the chip group default to expanded */ |
| 12 |
defaultIsOpen?: boolean; |
| 13 |
/** Customizable "Show Less" text string */ |
| 14 |
expandedText?: string; |
| 15 |
/** Customizable template string. Use variable "${remaining}" for the overflow chip count. */ |
| 16 |
collapsedText?: string; |
| 17 |
/** Category name text for the chip group category. If this prop is supplied the chip group with have a label and category styling applied */ |
| 18 |
categoryName?: string; |
| 19 |
/** Aria label for chip group that does not have a category name */ |
| 20 |
'aria-label'?: string; |
| 21 |
/** Set number of chips to show before overflow */ |
| 22 |
numChips?: number; |
| 23 |
/** Flag if chip group can be closed*/ |
| 24 |
isClosable?: boolean; |
| 25 |
/** Aria label for close button */ |
| 26 |
closeBtnAriaLabel?: string; |
| 27 |
/** Function that is called when clicking on the chip group close button or on an overflow chip */ |
| 28 |
onClick?: (event: React.MouseEvent) => void; |
| 29 |
/** @deprecated Function that is called when clicking on the overflow (expand/collapse) chip button */ |
| 30 |
onOverflowChipClick?: (event: React.MouseEvent) => void; |
| 31 |
/** Position of the tooltip which is displayed if the category name text is longer */ |
| 32 |
tooltipPosition?: |
| 33 |
| TooltipPosition |
| 34 |
| 'auto' |
| 35 |
| 'top' |
| 36 |
| 'bottom' |
| 37 |
| 'left' |
| 38 |
| 'right' |
| 39 |
| 'top-start' |
| 40 |
| 'top-end' |
| 41 |
| 'bottom-start' |
| 42 |
| 'bottom-end' |
| 43 |
| 'left-start' |
| 44 |
| 'left-end' |
| 45 |
| 'right-start' |
| 46 |
| 'right-end'; |
| 47 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 48 |
ouiaId?: number | string; |
| 49 |
} |
| 50 |
|
| 51 |
class ChipGroup extends Component<ChipGroupProps> { |
| 52 |
static displayName = 'ChipGroup'; |
| 53 |
constructor(props: ChipGroupProps) { |
| 54 |
super(props); |
| 55 |
} |
| 56 |
|
| 57 |
static defaultProps: ChipGroupProps = { |
| 58 |
expandedText: 'Show Less', |
| 59 |
collapsedText: '${remaining} more', |
| 60 |
categoryName: '', |
| 61 |
defaultIsOpen: false, |
| 62 |
numChips: 3, |
| 63 |
isClosable: false, |
| 64 |
closeBtnAriaLabel: 'Close chip group', |
| 65 |
tooltipPosition: 'top', |
| 66 |
'aria-label': 'Chip group category' |
| 67 |
}; |
| 68 |
|
| 69 |
render() { |
| 70 |
const { |
| 71 |
categoryName, |
| 72 |
children, |
| 73 |
className, |
| 74 |
isClosable, |
| 75 |
closeBtnAriaLabel, |
| 76 |
'aria-label': ariaLabel, |
| 77 |
onClick, |
| 78 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 79 |
onOverflowChipClick, |
| 80 |
numChips, |
| 81 |
expandedText, |
| 82 |
collapsedText, |
| 83 |
ouiaId, |
| 84 |
defaultIsOpen, |
| 85 |
tooltipPosition, |
| 86 |
...props |
| 87 |
} = this.props; |
| 88 |
|
| 89 |
return ( |
| 90 |
<LabelGroup |
| 91 |
className={className} |
| 92 |
categoryName={categoryName} |
| 93 |
numLabels={numChips} |
| 94 |
isClosable={isClosable} |
| 95 |
closeBtnAriaLabel={closeBtnAriaLabel} |
| 96 |
aria-label={ariaLabel} |
| 97 |
onClick={onClick} |
| 98 |
expandedText={expandedText} |
| 99 |
collapsedText={collapsedText} |
| 100 |
defaultIsOpen={defaultIsOpen} |
| 101 |
tooltipPosition={tooltipPosition} |
| 102 |
{...getOUIAProps(ChipGroup.displayName, ouiaId)} |
| 103 |
{...props} |
| 104 |
> |
| 105 |
{children} |
| 106 |
</LabelGroup> |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
export { ChipGroup }; |