| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { Children, Component, createRef, Fragment } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Label/label-group.mjs'; |
| 5 |
import labelStyles from '@patternfly/react-styles/css/components/Label/label.mjs'; |
| 6 |
import { css } from '@patternfly/react-styles'; |
| 7 |
import { Button } from '../Button'; |
| 8 |
import { Label } from './Label'; |
| 9 |
import { Tooltip } from '../Tooltip'; |
| 10 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 11 |
import { fillTemplate } from '../../helpers'; |
| 12 |
import { GenerateId } from '../../helpers/GenerateId/GenerateId'; |
| 13 |
class LabelGroup extends Component { |
| 14 |
constructor(props) { |
| 15 |
super(props); |
| 16 |
this.headingRef = createRef(); |
| 17 |
this.toggleCollapse = () => { |
| 18 |
this.setState((prevState) => ({ |
| 19 |
isOpen: !prevState.isOpen, |
| 20 |
isTooltipVisible: Boolean(this.headingRef.current && this.headingRef.current.offsetWidth < this.headingRef.current.scrollWidth) |
| 21 |
})); |
| 22 |
}; |
| 23 |
this.state = { |
| 24 |
isOpen: this.props.defaultIsOpen, |
| 25 |
isTooltipVisible: false |
| 26 |
}; |
| 27 |
} |
| 28 |
componentDidMount() { |
| 29 |
this.setState({ |
| 30 |
isTooltipVisible: Boolean(this.headingRef.current && this.headingRef.current.offsetWidth < this.headingRef.current.scrollWidth) |
| 31 |
}); |
| 32 |
} |
| 33 |
renderLabel(id) { |
| 34 |
const { categoryName, tooltipPosition } = this.props; |
| 35 |
const { isTooltipVisible } = this.state; |
| 36 |
return isTooltipVisible ? (_jsx(Tooltip, { position: tooltipPosition, content: categoryName, children: _jsx("span", { tabIndex: 0, ref: this.headingRef, className: css(styles.labelGroupLabel), children: _jsx("span", { "aria-hidden": "true", id: id, children: categoryName }) }) })) : (_jsx("span", { ref: this.headingRef, className: css(styles.labelGroupLabel), "aria-hidden": "true", id: id, children: categoryName })); |
| 37 |
} |
| 38 |
render() { |
| 39 |
const _a = this.props, { categoryName, children, className, isClosable, isCompact, closeBtnAriaLabel, 'aria-label': ariaLabel, onClick, numLabels, expandedText, collapsedText, |
| 40 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 41 |
defaultIsOpen, tooltipPosition, isVertical, isEditable, hasEditableTextArea, editableTextAreaProps, addLabelControl } = _a, |
| 42 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 43 |
rest = __rest(_a, ["categoryName", "children", "className", "isClosable", "isCompact", "closeBtnAriaLabel", 'aria-label', "onClick", "numLabels", "expandedText", "collapsedText", "defaultIsOpen", "tooltipPosition", "isVertical", "isEditable", "hasEditableTextArea", "editableTextAreaProps", "addLabelControl"]); |
| 44 |
const { isOpen } = this.state; |
| 45 |
const renderedChildren = Children.toArray(children); |
| 46 |
const numChildren = renderedChildren.length; |
| 47 |
const collapsedTextResult = fillTemplate(collapsedText, { |
| 48 |
remaining: numChildren - numLabels |
| 49 |
}); |
| 50 |
const renderLabelGroup = (id) => { |
| 51 |
const labelArray = !isOpen ? renderedChildren.slice(0, numLabels) : renderedChildren; |
| 52 |
const content = (_jsxs(Fragment, { children: [categoryName && this.renderLabel(id), _jsxs("ul", Object.assign({ className: css(styles.labelGroupList) }, (categoryName && { 'aria-labelledby': id }), (!categoryName && { 'aria-label': ariaLabel }), { role: "list" }, rest, { children: [labelArray.map((child, i) => (_jsx("li", { className: css(styles.labelGroupListItem), children: child }, i))), numChildren > numLabels && (_jsx("li", { className: css(styles.labelGroupListItem), children: _jsx(Label, { variant: "overflow", onClick: this.toggleCollapse, className: css(isCompact && labelStyles.modifiers.compact), children: isOpen ? expandedText : collapsedTextResult }) })), addLabelControl && _jsx("li", { className: css(styles.labelGroupListItem), children: addLabelControl }), isEditable && hasEditableTextArea && (_jsx("li", { className: css(styles.labelGroupListItem, styles.modifiers.textarea), children: _jsx("textarea", Object.assign({ className: css(styles.labelGroupTextarea), rows: 1, tabIndex: 0 }, editableTextAreaProps)) }))] }))] })); |
| 53 |
const close = (_jsx("div", { className: css(styles.labelGroupClose), children: _jsx(Button, { variant: "plain", size: "sm", "aria-label": closeBtnAriaLabel, onClick: onClick, id: `remove_group_${id}`, "aria-labelledby": `remove_group_${id} ${id}`, icon: _jsx(RhMicronsCloseIcon, {}) }) })); |
| 54 |
return (_jsxs("div", { className: css(styles.labelGroup, className, categoryName && styles.modifiers.category, isVertical && styles.modifiers.vertical, isEditable && styles.modifiers.editable), children: [_jsx("div", { className: css(styles.labelGroupMain), children: content }), isClosable && close] })); |
| 55 |
}; |
| 56 |
return numChildren === 0 && addLabelControl === undefined ? null : (_jsx(GenerateId, { children: (randomId) => renderLabelGroup(this.props.id || randomId) })); |
| 57 |
} |
| 58 |
} |
| 59 |
LabelGroup.displayName = 'LabelGroup'; |
| 60 |
LabelGroup.defaultProps = { |
| 61 |
expandedText: 'Show Less', |
| 62 |
collapsedText: '${remaining} more', |
| 63 |
categoryName: '', |
| 64 |
defaultIsOpen: false, |
| 65 |
numLabels: 3, |
| 66 |
isClosable: false, |
| 67 |
isCompact: false, |
| 68 |
onClick: (_e) => undefined, |
| 69 |
closeBtnAriaLabel: 'Close label group', |
| 70 |
tooltipPosition: 'top', |
| 71 |
'aria-label': 'Label group category', |
| 72 |
isVertical: false, |
| 73 |
isEditable: false, |
| 74 |
hasEditableTextArea: false |
| 75 |
}; |
| 76 |
export { LabelGroup }; |
| 77 |
//# sourceMappingURL=LabelGroup.js.map |