| 1 |
import { Children, Component, createRef, Fragment } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Label/label-group'; |
| 3 |
import labelStyles from '@patternfly/react-styles/css/components/Label/label'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import { Button } from '../Button'; |
| 6 |
import { Label } from './Label'; |
| 7 |
import { Tooltip, TooltipPosition } from '../Tooltip'; |
| 8 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 9 |
import { fillTemplate } from '../../helpers'; |
| 10 |
import { GenerateId } from '../../helpers/GenerateId/GenerateId'; |
| 11 |
|
| 12 |
export interface LabelGroupProps extends React.HTMLProps<HTMLUListElement> { |
| 13 |
/** Content rendered inside the label group. Should be <Label> elements. */ |
| 14 |
children?: React.ReactNode; |
| 15 |
/** Additional classes added to the label item */ |
| 16 |
className?: string; |
| 17 |
/** Flag for having the label group default to expanded */ |
| 18 |
defaultIsOpen?: boolean; |
| 19 |
/** Customizable "Show Less" text string */ |
| 20 |
expandedText?: string; |
| 21 |
/** Customizable template string. Use variable "${remaining}" for the overflow label count. */ |
| 22 |
collapsedText?: string; |
| 23 |
/** Category name text for the label group category. If this prop is supplied the label group with have a label and category styling applied */ |
| 24 |
categoryName?: string; |
| 25 |
/** Aria label for label group that does not have a category name */ |
| 26 |
'aria-label'?: string; |
| 27 |
/** Set number of labels to show before overflow */ |
| 28 |
numLabels?: number; |
| 29 |
/** Flag if label group can be closed */ |
| 30 |
isClosable?: boolean; |
| 31 |
/** Flag indicating the labels in the group are compact */ |
| 32 |
isCompact?: boolean; |
| 33 |
/** Aria label for close button */ |
| 34 |
closeBtnAriaLabel?: string; |
| 35 |
/** Function that is called when clicking on the label group close button */ |
| 36 |
onClick?: (event: React.MouseEvent) => void; |
| 37 |
/** Position of the tooltip which is displayed if the category name text is longer */ |
| 38 |
tooltipPosition?: |
| 39 |
| TooltipPosition |
| 40 |
| 'auto' |
| 41 |
| 'top' |
| 42 |
| 'bottom' |
| 43 |
| 'left' |
| 44 |
| 'right' |
| 45 |
| 'top-start' |
| 46 |
| 'top-end' |
| 47 |
| 'bottom-start' |
| 48 |
| 'bottom-end' |
| 49 |
| 'left-start' |
| 50 |
| 'left-end' |
| 51 |
| 'right-start' |
| 52 |
| 'right-end'; |
| 53 |
/** Flag to implement a vertical layout */ |
| 54 |
isVertical?: boolean; |
| 55 |
/** Flag indicating contained labels are editable. Allows spacing for a text input after the labels. */ |
| 56 |
isEditable?: boolean; |
| 57 |
/** Flag indicating the editable label group should be appended with a textarea. */ |
| 58 |
hasEditableTextArea?: boolean; |
| 59 |
/** Additional props passed to the editable textarea. */ |
| 60 |
editableTextAreaProps?: any; |
| 61 |
/** Control for adding new labels */ |
| 62 |
addLabelControl?: React.ReactNode; |
| 63 |
} |
| 64 |
|
| 65 |
interface LabelGroupState { |
| 66 |
isOpen: boolean; |
| 67 |
isTooltipVisible: boolean; |
| 68 |
} |
| 69 |
|
| 70 |
class LabelGroup extends Component<LabelGroupProps, LabelGroupState> { |
| 71 |
static displayName = 'LabelGroup'; |
| 72 |
constructor(props: LabelGroupProps) { |
| 73 |
super(props); |
| 74 |
this.state = { |
| 75 |
isOpen: this.props.defaultIsOpen, |
| 76 |
isTooltipVisible: false |
| 77 |
}; |
| 78 |
} |
| 79 |
private headingRef = createRef<HTMLSpanElement>(); |
| 80 |
|
| 81 |
static defaultProps: LabelGroupProps = { |
| 82 |
expandedText: 'Show Less', |
| 83 |
collapsedText: '${remaining} more', |
| 84 |
categoryName: '', |
| 85 |
defaultIsOpen: false, |
| 86 |
numLabels: 3, |
| 87 |
isClosable: false, |
| 88 |
isCompact: false, |
| 89 |
|
| 90 |
onClick: (_e: React.MouseEvent) => undefined as any, |
| 91 |
closeBtnAriaLabel: 'Close label group', |
| 92 |
tooltipPosition: 'top', |
| 93 |
'aria-label': 'Label group category', |
| 94 |
isVertical: false, |
| 95 |
isEditable: false, |
| 96 |
hasEditableTextArea: false |
| 97 |
}; |
| 98 |
|
| 99 |
componentDidMount() { |
| 100 |
this.setState({ |
| 101 |
isTooltipVisible: Boolean( |
| 102 |
this.headingRef.current && this.headingRef.current.offsetWidth < this.headingRef.current.scrollWidth |
| 103 |
) |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
toggleCollapse = () => { |
| 108 |
this.setState((prevState) => ({ |
| 109 |
isOpen: !prevState.isOpen, |
| 110 |
isTooltipVisible: Boolean( |
| 111 |
this.headingRef.current && this.headingRef.current.offsetWidth < this.headingRef.current.scrollWidth |
| 112 |
) |
| 113 |
})); |
| 114 |
}; |
| 115 |
|
| 116 |
renderLabel(id: string) { |
| 117 |
const { categoryName, tooltipPosition } = this.props; |
| 118 |
const { isTooltipVisible } = this.state; |
| 119 |
return isTooltipVisible ? ( |
| 120 |
<Tooltip position={tooltipPosition} content={categoryName}> |
| 121 |
<span tabIndex={0} ref={this.headingRef} className={css(styles.labelGroupLabel)}> |
| 122 |
<span aria-hidden="true" id={id}> |
| 123 |
{categoryName} |
| 124 |
</span> |
| 125 |
</span> |
| 126 |
</Tooltip> |
| 127 |
) : ( |
| 128 |
<span ref={this.headingRef} className={css(styles.labelGroupLabel)} aria-hidden="true" id={id}> |
| 129 |
{categoryName} |
| 130 |
</span> |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
render() { |
| 135 |
const { |
| 136 |
categoryName, |
| 137 |
children, |
| 138 |
className, |
| 139 |
isClosable, |
| 140 |
isCompact, |
| 141 |
closeBtnAriaLabel, |
| 142 |
'aria-label': ariaLabel, |
| 143 |
onClick, |
| 144 |
numLabels, |
| 145 |
expandedText, |
| 146 |
collapsedText, |
| 147 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 148 |
defaultIsOpen, |
| 149 |
tooltipPosition, |
| 150 |
isVertical, |
| 151 |
isEditable, |
| 152 |
hasEditableTextArea, |
| 153 |
editableTextAreaProps, |
| 154 |
addLabelControl, |
| 155 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 156 |
...rest |
| 157 |
} = this.props; |
| 158 |
const { isOpen } = this.state; |
| 159 |
const renderedChildren = Children.toArray(children); |
| 160 |
const numChildren = renderedChildren.length; |
| 161 |
const collapsedTextResult = fillTemplate(collapsedText as string, { |
| 162 |
remaining: numChildren - numLabels |
| 163 |
}); |
| 164 |
|
| 165 |
const renderLabelGroup = (id: string) => { |
| 166 |
const labelArray = !isOpen ? renderedChildren.slice(0, numLabels) : renderedChildren; |
| 167 |
|
| 168 |
const content = ( |
| 169 |
<Fragment> |
| 170 |
{categoryName && this.renderLabel(id)} |
| 171 |
<ul |
| 172 |
className={css(styles.labelGroupList)} |
| 173 |
{...(categoryName && { 'aria-labelledby': id })} |
| 174 |
{...(!categoryName && { 'aria-label': ariaLabel })} |
| 175 |
role="list" |
| 176 |
{...rest} |
| 177 |
> |
| 178 |
{labelArray.map((child, i) => ( |
| 179 |
<li className={css(styles.labelGroupListItem)} key={i}> |
| 180 |
{child} |
| 181 |
</li> |
| 182 |
))} |
| 183 |
{numChildren > numLabels && ( |
| 184 |
<li className={css(styles.labelGroupListItem)}> |
| 185 |
<Label |
| 186 |
variant="overflow" |
| 187 |
onClick={this.toggleCollapse} |
| 188 |
className={css(isCompact && labelStyles.modifiers.compact)} |
| 189 |
> |
| 190 |
{isOpen ? expandedText : collapsedTextResult} |
| 191 |
</Label> |
| 192 |
</li> |
| 193 |
)} |
| 194 |
{addLabelControl && <li className={css(styles.labelGroupListItem)}>{addLabelControl}</li>} |
| 195 |
{isEditable && hasEditableTextArea && ( |
| 196 |
<li className={css(styles.labelGroupListItem, styles.modifiers.textarea)}> |
| 197 |
<textarea className={css(styles.labelGroupTextarea)} rows={1} tabIndex={0} {...editableTextAreaProps} /> |
| 198 |
</li> |
| 199 |
)} |
| 200 |
</ul> |
| 201 |
</Fragment> |
| 202 |
); |
| 203 |
|
| 204 |
const close = ( |
| 205 |
<div className={css(styles.labelGroupClose)}> |
| 206 |
<Button |
| 207 |
variant="plain" |
| 208 |
size="sm" |
| 209 |
aria-label={closeBtnAriaLabel} |
| 210 |
onClick={onClick} |
| 211 |
id={`remove_group_${id}`} |
| 212 |
aria-labelledby={`remove_group_${id} ${id}`} |
| 213 |
icon={<RhMicronsCloseIcon />} |
| 214 |
/> |
| 215 |
</div> |
| 216 |
); |
| 217 |
|
| 218 |
return ( |
| 219 |
<div |
| 220 |
className={css( |
| 221 |
styles.labelGroup, |
| 222 |
className, |
| 223 |
categoryName && styles.modifiers.category, |
| 224 |
isVertical && styles.modifiers.vertical, |
| 225 |
isEditable && styles.modifiers.editable |
| 226 |
)} |
| 227 |
> |
| 228 |
{<div className={css(styles.labelGroupMain)}>{content}</div>} |
| 229 |
{isClosable && close} |
| 230 |
</div> |
| 231 |
); |
| 232 |
}; |
| 233 |
|
| 234 |
return numChildren === 0 && addLabelControl === undefined ? null : ( |
| 235 |
<GenerateId>{(randomId) => renderLabelGroup(this.props.id || randomId)}</GenerateId> |
| 236 |
); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
export { LabelGroup }; |