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