Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/ExpandableSection/ExpandableSection.js

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