UNPKG

5.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ChipGroup = void 0;
4const tslib_1 = require("tslib");
5const React = tslib_1.__importStar(require("react"));
6const chip_group_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/ChipGroup/chip-group"));
7const react_styles_1 = require("@patternfly/react-styles");
8const Button_1 = require("../Button");
9const Chip_1 = require("../Chip");
10const Tooltip_1 = require("../Tooltip");
11const times_circle_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/times-circle-icon'));
12const helpers_1 = require("../../helpers");
13const GenerateId_1 = require("../../helpers/GenerateId/GenerateId");
14const helpers_2 = require("../../helpers");
15class ChipGroup extends React.Component {
16 constructor(props) {
17 super(props);
18 this.headingRef = React.createRef();
19 this.toggleCollapse = () => {
20 this.setState(prevState => ({
21 isOpen: !prevState.isOpen,
22 isTooltipVisible: Boolean(this.headingRef.current && this.headingRef.current.offsetWidth < this.headingRef.current.scrollWidth)
23 }));
24 };
25 this.state = {
26 isOpen: this.props.defaultIsOpen,
27 isTooltipVisible: false
28 };
29 }
30 componentDidMount() {
31 this.setState({
32 isTooltipVisible: Boolean(this.headingRef.current && this.headingRef.current.offsetWidth < this.headingRef.current.scrollWidth)
33 });
34 }
35 renderLabel(id) {
36 const { categoryName, tooltipPosition } = this.props;
37 const { isTooltipVisible } = this.state;
38 return isTooltipVisible ? (React.createElement(Tooltip_1.Tooltip, { position: tooltipPosition, content: categoryName },
39 React.createElement("span", { tabIndex: 0, ref: this.headingRef, className: react_styles_1.css(chip_group_1.default.chipGroupLabel) },
40 React.createElement("span", { id: id }, categoryName)))) : (React.createElement("span", { ref: this.headingRef, className: react_styles_1.css(chip_group_1.default.chipGroupLabel), id: id }, categoryName));
41 }
42 render() {
43 const _a = this.props, { categoryName, children, className, isClosable, closeBtnAriaLabel, 'aria-label': ariaLabel, onClick, onOverflowChipClick, numChips, expandedText, collapsedText, ouiaId,
44 /* eslint-disable @typescript-eslint/no-unused-vars */
45 defaultIsOpen, tooltipPosition } = _a,
46 /* eslint-enable @typescript-eslint/no-unused-vars */
47 rest = tslib_1.__rest(_a, ["categoryName", "children", "className", "isClosable", "closeBtnAriaLabel", 'aria-label', "onClick", "onOverflowChipClick", "numChips", "expandedText", "collapsedText", "ouiaId", "defaultIsOpen", "tooltipPosition"]);
48 const { isOpen } = this.state;
49 const numChildren = React.Children.count(children);
50 const collapsedTextResult = helpers_1.fillTemplate(collapsedText, {
51 remaining: React.Children.count(children) - numChips
52 });
53 const renderChipGroup = (id) => {
54 const chipArray = !isOpen
55 ? React.Children.toArray(children).slice(0, numChips)
56 : React.Children.toArray(children);
57 return (React.createElement("div", Object.assign({ className: react_styles_1.css(chip_group_1.default.chipGroup, className, categoryName && chip_group_1.default.modifiers.category), role: "group" }, (categoryName && { 'aria-labelledby': id }), (!categoryName && { 'aria-label': ariaLabel }), helpers_2.getOUIAProps(ChipGroup.displayName, ouiaId)),
58 React.createElement("div", { className: react_styles_1.css(chip_group_1.default.chipGroupMain) },
59 categoryName && this.renderLabel(id),
60 React.createElement("ul", Object.assign({ className: react_styles_1.css(chip_group_1.default.chipGroupList) }, (categoryName && { 'aria-labelledby': id }), (!categoryName && { 'aria-label': ariaLabel }), { role: "list" }, rest),
61 chipArray.map((child, i) => (React.createElement("li", { className: react_styles_1.css(chip_group_1.default.chipGroupListItem), key: i }, child))),
62 numChildren > numChips && (React.createElement("li", { className: react_styles_1.css(chip_group_1.default.chipGroupListItem) },
63 React.createElement(Chip_1.Chip, { isOverflowChip: true, onClick: event => {
64 this.toggleCollapse();
65 onOverflowChipClick(event);
66 }, component: "button" }, isOpen ? expandedText : collapsedTextResult))))),
67 isClosable && (React.createElement("div", { className: react_styles_1.css(chip_group_1.default.chipGroupClose) },
68 React.createElement(Button_1.Button, { variant: "plain", "aria-label": closeBtnAriaLabel, onClick: onClick, id: `remove_group_${id}`, "aria-labelledby": `remove_group_${id} ${id}`, ouiaId: ouiaId || closeBtnAriaLabel },
69 React.createElement(times_circle_icon_1.default, { "aria-hidden": "true" }))))));
70 };
71 return numChildren === 0 ? null : React.createElement(GenerateId_1.GenerateId, null, randomId => renderChipGroup(this.props.id || randomId));
72 }
73}
74exports.ChipGroup = ChipGroup;
75ChipGroup.displayName = 'ChipGroup';
76ChipGroup.defaultProps = {
77 expandedText: 'Show Less',
78 collapsedText: '${remaining} more',
79 categoryName: '',
80 defaultIsOpen: false,
81 numChips: 3,
82 isClosable: false,
83 // eslint-disable-next-line @typescript-eslint/no-unused-vars
84 onClick: (_e) => undefined,
85 onOverflowChipClick: (_e) => undefined,
86 closeBtnAriaLabel: 'Close chip group',
87 tooltipPosition: 'top',
88 'aria-label': 'Chip group category'
89};
90//# sourceMappingURL=ChipGroup.js.map
\No newline at end of file