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