Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Label/Label.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Label = exports.LabelColor = exports.LabelStatus = 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_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Label/label"));
8 const label_group_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Label/label-group"));
9 const Button_1 = require("../Button");
10 const Tooltip_1 = require("../Tooltip");
11 const react_styles_1 = require("@patternfly/react-styles");
12 const helpers_1 = require("../../helpers");
13 const c_label__text_MaxWidth_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_label__text_MaxWidth'));
14 const rh_microns_close_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-close-icon'));
15 const rh_ui_check_circle_fill_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-ui-check-circle-fill-icon'));
16 const rh_ui_notification_fill_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-ui-notification-fill-icon'));
17 const rh_ui_error_fill_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-ui-error-fill-icon'));
18 const rh_ui_warning_fill_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-ui-warning-fill-icon'));
19 const rh_ui_information_fill_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-ui-information-fill-icon'));
20 /** Semantic status for labels (default icon and status styling). */
21 var LabelStatus;
22 (function (LabelStatus) {
23 LabelStatus["success"] = "success";
24 LabelStatus["warning"] = "warning";
25 LabelStatus["danger"] = "danger";
26 LabelStatus["info"] = "info";
27 LabelStatus["custom"] = "custom";
28 })(LabelStatus || (exports.LabelStatus = LabelStatus = {}));
29 /** Label palette color when not using the `status` prop. */
30 var LabelColor;
31 (function (LabelColor) {
32 LabelColor["blue"] = "blue";
33 LabelColor["teal"] = "teal";
34 LabelColor["green"] = "green";
35 LabelColor["orange"] = "orange";
36 LabelColor["purple"] = "purple";
37 LabelColor["red"] = "red";
38 LabelColor["orangered"] = "orangered";
39 LabelColor["grey"] = "grey";
40 LabelColor["yellow"] = "yellow";
41 })(LabelColor || (exports.LabelColor = LabelColor = {}));
42 const colorStyles = {
43 [LabelColor.blue]: label_1.default.modifiers.blue,
44 [LabelColor.teal]: label_1.default.modifiers.teal,
45 [LabelColor.green]: label_1.default.modifiers.green,
46 [LabelColor.orange]: label_1.default.modifiers.orange,
47 [LabelColor.purple]: label_1.default.modifiers.purple,
48 [LabelColor.red]: label_1.default.modifiers.red,
49 [LabelColor.orangered]: label_1.default.modifiers.orangered,
50 [LabelColor.yellow]: label_1.default.modifiers.yellow,
51 [LabelColor.grey]: ''
52 };
53 const statusIcons = {
54 [LabelStatus.success]: (0, jsx_runtime_1.jsx)(rh_ui_check_circle_fill_icon_1.default, {}),
55 [LabelStatus.warning]: (0, jsx_runtime_1.jsx)(rh_ui_warning_fill_icon_1.default, {}),
56 [LabelStatus.danger]: (0, jsx_runtime_1.jsx)(rh_ui_error_fill_icon_1.default, {}),
57 [LabelStatus.info]: (0, jsx_runtime_1.jsx)(rh_ui_information_fill_icon_1.default, {}),
58 [LabelStatus.custom]: (0, jsx_runtime_1.jsx)(rh_ui_notification_fill_icon_1.default, {})
59 };
60 const Label = (_a) => {
61 var { children, className = '', color = LabelColor.grey, variant = 'filled', status, isCompact = false, isDisabled = false, isEditable = false, isClickable: isClickableProp = false, editableProps, textMaxWidth, tooltipPosition, icon, onClose, onClick: onLabelClick, onEditCancel, onEditComplete, closeBtn, closeBtnAriaLabel, closeBtnProps, href, render } = _a, props = tslib_1.__rest(_a, ["children", "className", "color", "variant", "status", "isCompact", "isDisabled", "isEditable", "isClickable", "editableProps", "textMaxWidth", "tooltipPosition", "icon", "onClose", "onClick", "onEditCancel", "onEditComplete", "closeBtn", "closeBtnAriaLabel", "closeBtnProps", "href", "render"]);
62 const [isEditableActive, setIsEditableActive] = (0, react_1.useState)(false);
63 const [currValue, setCurrValue] = (0, react_1.useState)(children);
64 const editableButtonRef = (0, react_1.useRef)(undefined);
65 const editableInputRef = (0, react_1.useRef)(undefined);
66 const isOverflowLabel = variant === 'overflow';
67 const isAddLabel = variant === 'add';
68 const isClickable = isClickableProp || (onLabelClick && !isOverflowLabel && !isAddLabel) || href;
69 let _icon;
70 if (status) {
71 _icon = statusIcons[status];
72 }
73 if (icon) {
74 _icon = icon;
75 }
76 (0, react_1.useEffect)(() => {
77 const onDocMouseDown = (event) => {
78 if (isEditableActive &&
79 editableInputRef &&
80 editableInputRef.current &&
81 !editableInputRef.current.contains(event.target)) {
82 if (editableInputRef.current.value) {
83 onEditComplete && onEditComplete(event, editableInputRef.current.value);
84 }
85 setIsEditableActive(false);
86 }
87 };
88 const onKeyDown = (event) => {
89 var _a, _b;
90 const key = event.key;
91 if ((!isEditableActive &&
92 (!editableButtonRef ||
93 !editableButtonRef.current ||
94 !editableButtonRef.current.contains(event.target))) ||
95 (isEditableActive &&
96 (!editableInputRef || !editableInputRef.current || !editableInputRef.current.contains(event.target)))) {
97 return;
98 }
99 if (isEditableActive && (key === 'Enter' || key === 'Tab')) {
100 event.preventDefault();
101 event.stopImmediatePropagation();
102 if (editableInputRef.current.value) {
103 onEditComplete && onEditComplete(event, editableInputRef.current.value);
104 }
105 setIsEditableActive(false);
106 (_a = editableButtonRef === null || editableButtonRef === void 0 ? void 0 : editableButtonRef.current) === null || _a === void 0 ? void 0 : _a.focus();
107 }
108 if (isEditableActive && key === 'Escape') {
109 event.preventDefault();
110 event.stopImmediatePropagation();
111 // Reset div text to initial children prop - pre-edit
112 if (editableInputRef.current.value) {
113 editableInputRef.current.value = children;
114 onEditCancel && onEditCancel(event, children);
115 }
116 setIsEditableActive(false);
117 (_b = editableButtonRef === null || editableButtonRef === void 0 ? void 0 : editableButtonRef.current) === null || _b === void 0 ? void 0 : _b.focus();
118 }
119 if (!isEditableActive && key === 'Enter') {
120 event.preventDefault();
121 event.stopImmediatePropagation();
122 setIsEditableActive(true);
123 // Set cursor position to end of text
124 const el = event.target;
125 const range = document.createRange();
126 const sel = window.getSelection();
127 range.selectNodeContents(el);
128 range.collapse(false);
129 sel.removeAllRanges();
130 sel.addRange(range);
131 }
132 };
133 document.addEventListener('mousedown', onDocMouseDown);
134 document.addEventListener('keydown', onKeyDown);
135 return () => {
136 document.removeEventListener('mousedown', onDocMouseDown);
137 document.removeEventListener('keydown', onKeyDown);
138 };
139 }, [isEditableActive, editableInputRef, editableButtonRef, onEditComplete, onEditCancel, children]);
140 (0, react_1.useEffect)(() => {
141 if (onLabelClick && href) {
142 // eslint-disable-next-line no-console
143 console.warn('Link labels cannot have onClick passed, this results in invalid HTML. Please remove either the href or onClick prop.');
144 }
145 else if (onLabelClick && isEditable) {
146 // eslint-disable-next-line no-console
147 console.warn('Editable labels cannot have onClick passed, clicking starts the label edit process. Please remove either the isEditable or onClick prop.');
148 }
149 }, [onLabelClick, href, isEditable]);
150 const isClickableDisabled = (href || onLabelClick) && isDisabled;
151 const defaultCloseButton = ((0, jsx_runtime_1.jsx)(Button_1.Button, Object.assign({ type: "button", variant: "plain", hasNoPadding: true, onClick: onClose, "aria-label": closeBtnAriaLabel || `Close ${children}` }, (isClickableDisabled && { isDisabled: true }), closeBtnProps, { icon: (0, jsx_runtime_1.jsx)(rh_microns_close_icon_1.default, {}) })));
152 const closeButton = (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(label_1.default.labelActions), children: closeBtn || defaultCloseButton });
153 const textRef = (0, react_1.createRef)();
154 // ref to apply tooltip when rendered is used
155 const componentRef = (0, react_1.useRef)(undefined);
156 const [isTooltipVisible, setIsTooltipVisible] = (0, react_1.useState)(false);
157 (0, helpers_1.useIsomorphicLayoutEffect)(() => {
158 const currTextRef = isEditable ? editableButtonRef : textRef;
159 if (!isEditableActive) {
160 setIsTooltipVisible(currTextRef.current && currTextRef.current.offsetWidth < currTextRef.current.scrollWidth);
161 }
162 }, [isEditableActive]);
163 const content = ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [_icon && (0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(label_1.default.labelIcon), children: _icon }), (0, jsx_runtime_1.jsx)("span", Object.assign({ ref: textRef, className: (0, react_styles_1.css)(label_1.default.labelText) }, (textMaxWidth && {
164 style: {
165 [c_label__text_MaxWidth_1.default.name]: textMaxWidth
166 }
167 }), { children: children }))] }));
168 (0, react_1.useEffect)(() => {
169 if (isEditableActive && editableInputRef) {
170 editableInputRef.current && editableInputRef.current.focus();
171 }
172 }, [editableInputRef, isEditableActive]);
173 const updateVal = () => {
174 setCurrValue(editableInputRef.current.value);
175 };
176 let LabelComponentChildElement = 'span';
177 if (href) {
178 LabelComponentChildElement = 'a';
179 }
180 else if (isEditable || (onLabelClick && !isOverflowLabel && !isAddLabel)) {
181 LabelComponentChildElement = 'button';
182 }
183 const clickableLabelProps = {
184 type: 'button',
185 onClick: onLabelClick
186 };
187 const isButton = LabelComponentChildElement === 'button';
188 const labelComponentChildProps = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ className: (0, react_styles_1.css)(label_1.default.labelContent, isClickable && label_1.default.modifiers.clickable) }, (isTooltipVisible && { tabIndex: 0 })), (href && { href })), (href && isDisabled && { onClick: (event) => event.preventDefault() })), (isButton && clickableLabelProps)), (isEditable && Object.assign({ ref: editableButtonRef, onClick: (e) => {
189 setIsEditableActive(true);
190 e.stopPropagation();
191 } }, editableProps))), (isClickableDisabled && isButton && { disabled: true })), (isClickableDisabled && href && { tabIndex: -1, 'aria-disabled': true }));
192 let labelComponentChild = ((0, jsx_runtime_1.jsx)(LabelComponentChildElement, Object.assign({}, labelComponentChildProps, { children: content })));
193 if (render) {
194 labelComponentChild = ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [isTooltipVisible && (0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { triggerRef: componentRef, content: children, position: tooltipPosition }), render({
195 className: label_1.default.labelContent,
196 content,
197 componentRef
198 })] }));
199 }
200 else if (isTooltipVisible) {
201 labelComponentChild = ((0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { content: children, position: tooltipPosition, children: labelComponentChild }));
202 }
203 const LabelComponent = (isOverflowLabel || isAddLabel ? 'button' : 'span');
204 return ((0, jsx_runtime_1.jsxs)(LabelComponent, Object.assign({}, props, { className: (0, react_styles_1.css)(label_1.default.label, isClickableDisabled && label_1.default.modifiers.disabled, colorStyles[color], variant === 'filled' && label_1.default.modifiers.filled, variant === 'outline' && label_1.default.modifiers.outline, status && label_1.default.modifiers[status], isOverflowLabel && label_1.default.modifiers.overflow, isCompact && label_1.default.modifiers.compact, isEditable && label_group_1.default.modifiers.editable, isEditableActive && label_1.default.modifiers.editableActive, isClickable && label_1.default.modifiers.clickable, isAddLabel && label_1.default.modifiers.add, className), onClick: isOverflowLabel || isAddLabel ? onLabelClick : undefined }, (LabelComponent === 'button' && { type: 'button' }), { children: [!isEditableActive && labelComponentChild, !isEditableActive && onClose && closeButton, isEditableActive && ((0, jsx_runtime_1.jsx)("input", Object.assign({ className: (0, react_styles_1.css)(label_1.default.labelContent), type: "text", id: "editable-input", ref: editableInputRef, value: currValue, onChange: updateVal }, editableProps)))] })));
205 };
206 exports.Label = Label;
207 exports.Label.displayName = 'Label';
208 //# sourceMappingURL=Label.js.map