| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.ExpandableSection = exports.ExpandableSectionVariant = 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 expandable_section_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/ExpandableSection/expandable-section")); |
| 8 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 9 |
const c_expandable_section_m_truncate__content_LineClamp_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_expandable_section_m_truncate__content_LineClamp')); |
| 10 |
const rh_microns_caret_down_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-down-icon')); |
| 11 |
const util_1 = require("../../helpers/util"); |
| 12 |
const resizeObserver_1 = require("../../helpers/resizeObserver"); |
| 13 |
const helpers_1 = require("../../helpers"); |
| 14 |
const Button_1 = require("../Button"); |
| 15 |
var ExpandableSectionVariant; |
| 16 |
(function (ExpandableSectionVariant) { |
| 17 |
ExpandableSectionVariant["default"] = "default"; |
| 18 |
ExpandableSectionVariant["truncate"] = "truncate"; |
| 19 |
})(ExpandableSectionVariant || (exports.ExpandableSectionVariant = ExpandableSectionVariant = {})); |
| 20 |
const directionClassMap = { |
| 21 |
up: expandable_section_1.default.modifiers.expandTop, |
| 22 |
down: expandable_section_1.default.modifiers.expandBottom |
| 23 |
}; |
| 24 |
const setLineClamp = (lines, element) => { |
| 25 |
if (!element || lines < 1) { |
| 26 |
return; |
| 27 |
} |
| 28 |
element.style.setProperty(c_expandable_section_m_truncate__content_LineClamp_1.default.name, lines.toString()); |
| 29 |
}; |
| 30 |
class ExpandableSection extends react_1.Component { |
| 31 |
constructor(props) { |
| 32 |
super(props); |
| 33 |
this.expandableContentRef = (0, react_1.createRef)(); |
| 34 |
this.observer = () => { }; |
| 35 |
this.checkToggleVisibility = () => { |
| 36 |
var _a; |
| 37 |
if ((_a = this.expandableContentRef) === null || _a === void 0 ? void 0 : _a.current) { |
| 38 |
const maxLines = this.props.truncateMaxLines || parseInt(c_expandable_section_m_truncate__content_LineClamp_1.default.value); |
| 39 |
const totalLines = this.expandableContentRef.current.scrollHeight / |
| 40 |
parseInt(getComputedStyle(this.expandableContentRef.current).lineHeight); |
| 41 |
this.setState({ |
| 42 |
hasToggle: totalLines > maxLines |
| 43 |
}); |
| 44 |
} |
| 45 |
}; |
| 46 |
this.resize = () => { |
| 47 |
if (this.expandableContentRef.current) { |
| 48 |
const { offsetWidth } = this.expandableContentRef.current; |
| 49 |
if (this.state.previousWidth !== offsetWidth) { |
| 50 |
this.setState({ previousWidth: offsetWidth }); |
| 51 |
this.checkToggleVisibility(); |
| 52 |
} |
| 53 |
} |
| 54 |
}; |
| 55 |
this.handleResize = (0, util_1.debounce)(this.resize, 250); |
| 56 |
this.state = { |
| 57 |
isExpanded: props.isExpanded, |
| 58 |
hasToggle: true, |
| 59 |
previousWidth: undefined |
| 60 |
}; |
| 61 |
} |
| 62 |
calculateToggleText(toggleText, toggleTextExpanded, toggleTextCollapsed, propOrStateIsExpanded) { |
| 63 |
if (propOrStateIsExpanded && toggleTextExpanded !== '') { |
| 64 |
return toggleTextExpanded; |
| 65 |
} |
| 66 |
if (!propOrStateIsExpanded && toggleTextCollapsed !== '') { |
| 67 |
return toggleTextCollapsed; |
| 68 |
} |
| 69 |
return toggleText; |
| 70 |
} |
| 71 |
componentDidMount() { |
| 72 |
if (this.props.variant === ExpandableSectionVariant.truncate) { |
| 73 |
const expandableContent = this.expandableContentRef.current; |
| 74 |
this.setState({ previousWidth: expandableContent.offsetWidth }); |
| 75 |
this.observer = (0, resizeObserver_1.getResizeObserver)(expandableContent, this.handleResize, false); |
| 76 |
if (this.props.truncateMaxLines) { |
| 77 |
setLineClamp(this.props.truncateMaxLines, expandableContent); |
| 78 |
} |
| 79 |
this.checkToggleVisibility(); |
| 80 |
} |
| 81 |
} |
| 82 |
componentDidUpdate(prevProps) { |
| 83 |
if (this.props.variant === ExpandableSectionVariant.truncate && |
| 84 |
(prevProps.truncateMaxLines !== this.props.truncateMaxLines || prevProps.children !== this.props.children)) { |
| 85 |
const expandableContent = this.expandableContentRef.current; |
| 86 |
setLineClamp(this.props.truncateMaxLines, expandableContent); |
| 87 |
this.checkToggleVisibility(); |
| 88 |
} |
| 89 |
} |
| 90 |
componentWillUnmount() { |
| 91 |
if (this.props.variant === ExpandableSectionVariant.truncate) { |
| 92 |
this.observer(); |
| 93 |
} |
| 94 |
} |
| 95 |
render() { |
| 96 |
const _a = this.props, { onToggle: onToggleProp, className, toggleText, toggleTextExpanded, toggleTextCollapsed, toggleContent, toggleAriaLabel, toggleAriaLabelledBy, toggleIcon = (0, jsx_runtime_1.jsx)(rh_microns_caret_down_icon_1.default, {}), hasToggleIcon = true, children, isExpanded, isDetached, displaySize, isWidthLimited, isIndented, contentId, toggleId, variant, |
| 97 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 98 |
truncateMaxLines, direction, toggleWrapper = 'div' } = _a, props = tslib_1.__rest(_a, ["onToggle", "className", "toggleText", "toggleTextExpanded", "toggleTextCollapsed", "toggleContent", "toggleAriaLabel", "toggleAriaLabelledBy", "toggleIcon", "hasToggleIcon", "children", "isExpanded", "isDetached", "displaySize", "isWidthLimited", "isIndented", "contentId", "toggleId", "variant", "truncateMaxLines", "direction", "toggleWrapper"]); |
| 99 |
if (isDetached && !toggleId) { |
| 100 |
/* eslint-disable no-console */ |
| 101 |
console.warn('ExpandableSection: The toggleId value must be passed in and must match the toggleId of the ExpandableSectionToggle.'); |
| 102 |
} |
| 103 |
let onToggle = onToggleProp; |
| 104 |
let propOrStateIsExpanded = isExpanded; |
| 105 |
// uncontrolled |
| 106 |
if (isExpanded === undefined) { |
| 107 |
propOrStateIsExpanded = this.state.isExpanded; |
| 108 |
onToggle = (event, isOpen) => { |
| 109 |
this.setState({ isExpanded: isOpen }, () => onToggleProp(event, this.state.isExpanded)); |
| 110 |
}; |
| 111 |
} |
| 112 |
const computedToggleText = this.calculateToggleText(toggleText, toggleTextExpanded, toggleTextCollapsed, propOrStateIsExpanded); |
| 113 |
const computedToggleContent = typeof toggleContent === 'function' ? toggleContent(propOrStateIsExpanded) : toggleContent; |
| 114 |
const ToggleWrapper = toggleWrapper; |
| 115 |
return ((0, jsx_runtime_1.jsx)(helpers_1.GenerateId, { prefix: "expandable-section-content-", children: (genContentId) => ((0, jsx_runtime_1.jsx)(helpers_1.GenerateId, { prefix: "expandable-section-toggle-", children: (genToggleId) => { |
| 116 |
const uniqueContentId = contentId || genContentId; |
| 117 |
const uniqueToggleId = toggleId || genToggleId; |
| 118 |
const expandableToggle = !isDetached && ((0, jsx_runtime_1.jsx)(ToggleWrapper, { className: `${expandable_section_1.default.expandableSection}__toggle`, children: (0, jsx_runtime_1.jsx)(Button_1.Button, Object.assign({ variant: "link" }, (variant === ExpandableSectionVariant.truncate && { isInline: true }), { "aria-expanded": propOrStateIsExpanded, "aria-controls": uniqueContentId, id: uniqueToggleId, onClick: (event) => onToggle(event, !propOrStateIsExpanded) }, (variant !== ExpandableSectionVariant.truncate && |
| 119 |
hasToggleIcon && { |
| 120 |
icon: (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(expandable_section_1.default.expandableSectionToggleIcon), children: toggleIcon }) |
| 121 |
}), { "aria-label": toggleAriaLabel, "aria-labelledby": toggleAriaLabelledBy, children: computedToggleContent || computedToggleText })) })); |
| 122 |
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, react_styles_1.css)(expandable_section_1.default.expandableSection, propOrStateIsExpanded && expandable_section_1.default.modifiers.expanded, displaySize === 'lg' && expandable_section_1.default.modifiers.displayLg, isWidthLimited && expandable_section_1.default.modifiers.limitWidth, isIndented && expandable_section_1.default.modifiers.indented, isDetached && direction && directionClassMap[direction], isDetached && direction && 'pf-m-detached', variant === ExpandableSectionVariant.truncate && expandable_section_1.default.modifiers.truncate, className) }, props, { children: [variant === ExpandableSectionVariant.default && expandableToggle, (0, jsx_runtime_1.jsx)("div", { ref: this.expandableContentRef, className: (0, react_styles_1.css)(expandable_section_1.default.expandableSectionContent), hidden: variant !== ExpandableSectionVariant.truncate && !propOrStateIsExpanded, id: uniqueContentId, "aria-labelledby": uniqueToggleId, role: "region", children: children }), variant === ExpandableSectionVariant.truncate && this.state.hasToggle && expandableToggle] }))); |
| 123 |
} })) })); |
| 124 |
} |
| 125 |
} |
| 126 |
exports.ExpandableSection = ExpandableSection; |
| 127 |
ExpandableSection.displayName = 'ExpandableSection'; |
| 128 |
ExpandableSection.defaultProps = { |
| 129 |
className: '', |
| 130 |
toggleText: '', |
| 131 |
toggleTextExpanded: '', |
| 132 |
toggleTextCollapsed: '', |
| 133 |
toggleAriaLabel: undefined, |
| 134 |
toggleAriaLabelledBy: undefined, |
| 135 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 136 |
onToggle: (event, isExpanded) => undefined, |
| 137 |
isDetached: false, |
| 138 |
displaySize: 'default', |
| 139 |
isWidthLimited: false, |
| 140 |
isIndented: false, |
| 141 |
variant: 'default' |
| 142 |
}; |
| 143 |
//# sourceMappingURL=ExpandableSection.js.map |