import * as React from 'react'; import { useState } from 'react'; import styles from '@patternfly/react-styles/css/components/Label/label'; import labelGrpStyles from '@patternfly/react-styles/css/components/Label/label-group'; import { Button } from '../Button'; import { Tooltip, TooltipPosition } from '../Tooltip'; import { css } from '@patternfly/react-styles'; import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon'; import { useIsomorphicLayoutEffect } from '../../helpers'; export interface LabelProps extends React.HTMLProps { /** Content rendered inside the label. */ children?: React.ReactNode; /** Additional classes added to the label. */ className?: string; /** Color of the label. */ color?: 'blue' | 'cyan' | 'green' | 'orange' | 'purple' | 'red' | 'grey' | 'gold'; /** Variant of the label. */ variant?: 'outline' | 'filled'; /** Flag indicating the label is compact. */ isCompact?: boolean; /** @beta Flag indicating the label is editable. */ isEditable?: boolean; /** @beta Additional props passed to the editable label text div. Optionally passing onInput and onBlur callbacks will allow finer custom text input control. */ editableProps?: any; /** @beta Callback when an editable label completes an edit. */ onEditComplete?: (event: MouseEvent | KeyboardEvent, newText: string) => void; /** @beta Callback when an editable label cancels an edit. */ onEditCancel?: (event: KeyboardEvent, previousText: string) => void; /** The max width of the label before it is truncated. Can be any valid CSS unit, such as '100%', '100px', or '16ch'. */ textMaxWidth?: string; /** Position of the tooltip which is displayed if text is truncated */ tooltipPosition?: | TooltipPosition | 'auto' | 'top' | 'bottom' | 'left' | 'right' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'left-start' | 'left-end' | 'right-start' | 'right-end'; /** Icon added to the left of the label text. */ icon?: React.ReactNode; /** Close click callback for removable labels. If present, label will have a close button. */ onClose?: (event: React.MouseEvent) => void; /** Node for custom close button. */ closeBtn?: React.ReactNode; /** Aria label for close button */ closeBtnAriaLabel?: string; /** Additional properties for the default close button. */ closeBtnProps?: any; /** Href for a label that is a link. If present, the label will change to an anchor element. */ href?: string; /** Flag indicating if the label is an overflow label */ isOverflowLabel?: boolean; /** Forwards the label content and className to rendered function. Use this prop for react router support.*/ render?: ({ className, content, componentRef }: { className: string; content: React.ReactNode; componentRef: any; }) => React.ReactNode; } const colorStyles = { blue: styles.modifiers.blue, cyan: styles.modifiers.cyan, green: styles.modifiers.green, orange: styles.modifiers.orange, purple: styles.modifiers.purple, red: styles.modifiers.red, gold: styles.modifiers.gold, grey: '' }; export const Label: React.FunctionComponent = ({ children, className = '', color = 'grey', variant = 'filled', isCompact = false, isEditable = false, editableProps, textMaxWidth, tooltipPosition, icon, onClose, onEditCancel, onEditComplete, closeBtn, closeBtnAriaLabel, closeBtnProps, href, isOverflowLabel, render, ...props }: LabelProps) => { const [isEditableActive, setIsEditableActive] = useState(false); const [currValue, setCurrValue] = useState(children); const editableButtonRef = React.useRef(); const editableInputRef = React.useRef(); React.useEffect(() => { document.addEventListener('mousedown', onDocMouseDown); document.addEventListener('keydown', onKeyDown); return () => { document.removeEventListener('mousedown', onDocMouseDown); document.removeEventListener('keydown', onKeyDown); }; }); const onDocMouseDown = (event: MouseEvent) => { if ( isEditableActive && editableInputRef && editableInputRef.current && !editableInputRef.current.contains(event.target as Node) ) { if (editableInputRef.current.value) { onEditComplete && onEditComplete(event, editableInputRef.current.value); } setIsEditableActive(false); } }; const onKeyDown = (event: KeyboardEvent) => { const key = event.key; if ( (!isEditableActive && (!editableButtonRef || !editableButtonRef.current || !editableButtonRef.current.contains(event.target as Node))) || (isEditableActive && (!editableInputRef || !editableInputRef.current || !editableInputRef.current.contains(event.target as Node))) ) { return; } if (isEditableActive && (key === 'Enter' || key === 'Tab')) { event.preventDefault(); event.stopImmediatePropagation(); if (editableInputRef.current.value) { onEditComplete && onEditComplete(event, editableInputRef.current.value); } setIsEditableActive(false); editableButtonRef?.current?.focus(); } if (isEditableActive && key === 'Escape') { event.preventDefault(); event.stopImmediatePropagation(); // Reset div text to initial children prop - pre-edit if (editableInputRef.current.value) { editableInputRef.current.value = children as string; onEditCancel && onEditCancel(event, children as string); } setIsEditableActive(false); editableButtonRef?.current?.focus(); } if (!isEditableActive && key === 'Enter') { event.preventDefault(); event.stopImmediatePropagation(); setIsEditableActive(true); // Set cursor position to end of text const el = event.target as HTMLElement; const range = document.createRange(); const sel = window.getSelection(); range.selectNodeContents(el); range.collapse(false); sel.removeAllRanges(); sel.addRange(range); } }; const LabelComponent = (isOverflowLabel ? 'button' : 'span') as any; const defaultButton = ( ); const button = {closeBtn || defaultButton}; const textRef = React.createRef(); // ref to apply tooltip when rendered is used const componentRef = React.useRef(); const [isTooltipVisible, setIsTooltipVisible] = React.useState(false); useIsomorphicLayoutEffect(() => { const currTextRef = isEditable ? editableButtonRef : textRef; if (!isEditableActive) { setIsTooltipVisible(currTextRef.current && currTextRef.current.offsetWidth < currTextRef.current.scrollWidth); } }, [isEditableActive]); const content = ( {icon && {icon}} {children} ); React.useEffect(() => { if (isEditableActive && editableInputRef) { editableInputRef.current && editableInputRef.current.focus(); } }, [editableInputRef, isEditableActive]); const updateVal = () => { setCurrValue(editableInputRef.current.value); }; let LabelComponentChildElement = 'span'; if (href) { LabelComponentChildElement = 'a'; } else if (isEditable) { LabelComponentChildElement = 'button'; } const labelComponentChildProps = { className: css(styles.labelContent), ...(isTooltipVisible && { tabIndex: 0 }), ...(href && { href }), ...(isEditable && { ref: editableButtonRef, onClick: (e: React.MouseEvent) => { setIsEditableActive(true); e.stopPropagation(); }, ...editableProps }) }; let labelComponentChild = ( {content} ); if (render) { labelComponentChild = ( {isTooltipVisible && } {render({ className: styles.labelContent, content, componentRef })} ); } else if (isTooltipVisible) { labelComponentChild = ( {labelComponentChild} ); } return ( {!isEditableActive && labelComponentChild} {!isEditableActive && onClose && button} {isEditableActive && ( )} ); }; Label.displayName = 'Label';