| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { Component, Fragment, createRef } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/ClipboardCopy/clipboard-copy.mjs'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { TooltipPosition } from '../Tooltip'; |
| 7 |
import { TextInput } from '../TextInput'; |
| 8 |
import { Truncate } from '../Truncate'; |
| 9 |
import { GenerateId } from '../../helpers/GenerateId/GenerateId'; |
| 10 |
import { ClipboardCopyButton } from './ClipboardCopyButton'; |
| 11 |
import { ClipboardCopyToggle } from './ClipboardCopyToggle'; |
| 12 |
import { ClipboardCopyExpanded } from './ClipboardCopyExpanded'; |
| 13 |
import { getOUIAProps } from '../../helpers'; |
| 14 |
export const clipboardCopyFunc = (_event, text) => { |
| 15 |
try { |
| 16 |
navigator.clipboard.writeText(text.toString()); |
| 17 |
} |
| 18 |
catch (error) { |
| 19 |
// eslint-disable-next-line no-console |
| 20 |
console.warn("Clipboard API not found, this copy function will not work. This is likely because you're using an", "unsupported browser or you're not using HTTPS. \n\nIf you're a developer building an application which needs", "to support copying to the clipboard without the clipboard API, you'll have to create your own copy", 'function and pass it to the ClipboardCopy component as the onCopy prop. For more information see', 'https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard'); |
| 21 |
// eslint-disable-next-line no-console |
| 22 |
console.error(error); |
| 23 |
} |
| 24 |
}; |
| 25 |
export var ClipboardCopyVariant; |
| 26 |
(function (ClipboardCopyVariant) { |
| 27 |
ClipboardCopyVariant["inline"] = "inline"; |
| 28 |
ClipboardCopyVariant["expansion"] = "expansion"; |
| 29 |
ClipboardCopyVariant["inlineCompact"] = "inline-compact"; |
| 30 |
})(ClipboardCopyVariant || (ClipboardCopyVariant = {})); |
| 31 |
class ClipboardCopy extends Component { |
| 32 |
constructor(props) { |
| 33 |
super(props); |
| 34 |
this.timer = null; |
| 35 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 36 |
this.componentDidUpdate = (prevProps, prevState) => { |
| 37 |
if (prevProps.children !== this.props.children) { |
| 38 |
const newText = Array.isArray(this.props.children) |
| 39 |
? this.props.children.join(' ') |
| 40 |
: this.props.children; |
| 41 |
this.setState({ text: newText, textWhenExpanded: newText }); |
| 42 |
} |
| 43 |
}; |
| 44 |
this.componentWillUnmount = () => { |
| 45 |
if (this.timer) { |
| 46 |
window.clearTimeout(this.timer); |
| 47 |
} |
| 48 |
}; |
| 49 |
this.expandContent = (_event) => { |
| 50 |
this.setState((prevState) => ({ |
| 51 |
expanded: !prevState.expanded |
| 52 |
})); |
| 53 |
}; |
| 54 |
this.updateText = (event, text) => { |
| 55 |
this.setState({ text }); |
| 56 |
this.props.onChange(event, text); |
| 57 |
}; |
| 58 |
this.updateTextWhenExpanded = (event, text) => { |
| 59 |
this.setState({ textWhenExpanded: text }); |
| 60 |
this.props.onChange(event, text); |
| 61 |
}; |
| 62 |
this.render = () => { |
| 63 |
const _a = this.props, { |
| 64 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 65 |
isExpanded, onChange, // Don't pass to <div> |
| 66 |
onInputFocus, // Don't pass to <div> |
| 67 |
onInputBlur, // Don't pass to <div> |
| 68 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 69 |
isReadOnly, isCode, isBlock, exitDelay, maxWidth, entryDelay, onCopy, hoverTip, clickTip, textAriaLabel, toggleAriaLabel, inputId, inputName, variant, position, className, additionalActions, truncation, ouiaId, ouiaSafe, copyAriaLabel } = _a, divProps = __rest(_a, ["isExpanded", "onChange", "onInputFocus", "onInputBlur", "isReadOnly", "isCode", "isBlock", "exitDelay", "maxWidth", "entryDelay", "onCopy", "hoverTip", "clickTip", "textAriaLabel", "toggleAriaLabel", "inputId", "inputName", "variant", "position", "className", "additionalActions", "truncation", "ouiaId", "ouiaSafe", "copyAriaLabel"]); |
| 70 |
const textIdPrefix = 'text-input-'; |
| 71 |
const toggleIdPrefix = 'toggle-'; |
| 72 |
const contentIdPrefix = 'content-'; |
| 73 |
const copyableText = this.state.text; |
| 74 |
const shouldTruncate = variant === ClipboardCopyVariant.inlineCompact && truncation; |
| 75 |
const inlineCompactContent = shouldTruncate ? (_jsx(Truncate, Object.assign({ tooltipProps: { triggerRef: this.clipboardRef }, content: copyableText }, (typeof truncation === 'object' && truncation)))) : (copyableText); |
| 76 |
return (_jsxs("div", Object.assign({ className: css(styles.clipboardCopy, variant === ClipboardCopyVariant.inlineCompact && styles.modifiers.inline, isBlock && styles.modifiers.block, this.state.expanded && styles.modifiers.expanded, shouldTruncate && styles.modifiers.truncate, className), ref: this.clipboardRef }, (shouldTruncate && { tabIndex: 0 }), divProps, getOUIAProps(ClipboardCopy.displayName, ouiaId, ouiaSafe), { children: [variant === ClipboardCopyVariant.inlineCompact && (_jsx(GenerateId, { prefix: "", children: (id) => (_jsxs(Fragment, { children: [!isCode && (_jsx("span", { className: css(styles.clipboardCopyText), id: `${textIdPrefix}${id}`, children: inlineCompactContent })), isCode && (_jsx("code", { className: css(styles.clipboardCopyText, styles.modifiers.code), id: `${textIdPrefix}${id}`, children: inlineCompactContent })), _jsxs("span", { className: css(styles.clipboardCopyActions), children: [_jsx("span", { className: css(styles.clipboardCopyActionsItem), children: _jsx(ClipboardCopyButton, { variant: "plain", exitDelay: exitDelay, entryDelay: entryDelay, maxWidth: maxWidth, position: position, id: `copy-button-${id}`, "aria-label": copyAriaLabel !== null && copyAriaLabel !== void 0 ? copyAriaLabel : hoverTip, onClick: (event) => { |
| 77 |
onCopy(event, copyableText); |
| 78 |
this.setState({ copied: true }); |
| 79 |
}, onTooltipHidden: () => this.setState({ copied: false }), hasNoPadding: true, children: this.state.copied ? clickTip : hoverTip }) }), additionalActions && additionalActions] })] })) })), variant !== ClipboardCopyVariant.inlineCompact && (_jsx(GenerateId, { prefix: "", children: (id) => (_jsxs(Fragment, { children: [_jsxs("div", { className: css(styles.clipboardCopyGroup), children: [variant === ClipboardCopyVariant.expansion && (_jsx(ClipboardCopyToggle, { isExpanded: this.state.expanded, onClick: (_event) => { |
| 80 |
this.expandContent(_event); |
| 81 |
if (this.state.expanded) { |
| 82 |
this.setState({ text: this.state.textWhenExpanded }); |
| 83 |
} |
| 84 |
else { |
| 85 |
this.setState({ textWhenExpanded: copyableText }); |
| 86 |
} |
| 87 |
}, id: `${toggleIdPrefix}${id}`, contentId: `${contentIdPrefix}${id}`, "aria-label": toggleAriaLabel })), _jsx(TextInput, Object.assign({ readOnlyVariant: isReadOnly || this.state.expanded ? 'default' : undefined, onChange: this.updateText, value: this.state.expanded ? this.state.textWhenExpanded : copyableText, id: inputId !== null && inputId !== void 0 ? inputId : `text-input-${id}`, name: inputName, "aria-label": textAriaLabel, onFocus: onInputFocus, onBlur: onInputBlur }, (isCode && { dir: 'ltr' }))), _jsx(ClipboardCopyButton, { exitDelay: exitDelay, entryDelay: entryDelay, maxWidth: maxWidth, position: position, id: `copy-button-${id}`, "aria-label": copyAriaLabel !== null && copyAriaLabel !== void 0 ? copyAriaLabel : hoverTip, onClick: (event) => { |
| 88 |
onCopy(event, this.state.expanded ? this.state.textWhenExpanded : copyableText); |
| 89 |
this.setState({ copied: true }); |
| 90 |
}, onTooltipHidden: () => this.setState({ copied: false }), children: this.state.copied ? clickTip : hoverTip })] }), this.state.expanded && (_jsx(ClipboardCopyExpanded, { isReadOnly: isReadOnly, isCode: isCode, id: `content-${id}`, onChange: this.updateTextWhenExpanded, children: copyableText }))] })) }))] }))); |
| 91 |
}; |
| 92 |
const text = Array.isArray(this.props.children) ? this.props.children.join(' ') : this.props.children; |
| 93 |
this.state = { |
| 94 |
text, |
| 95 |
expanded: this.props.isExpanded, |
| 96 |
copied: false, |
| 97 |
textWhenExpanded: text |
| 98 |
}; |
| 99 |
this.clipboardRef = createRef(); |
| 100 |
} |
| 101 |
} |
| 102 |
ClipboardCopy.displayName = 'ClipboardCopy'; |
| 103 |
ClipboardCopy.defaultProps = { |
| 104 |
hoverTip: 'Copy to clipboard', |
| 105 |
clickTip: 'Successfully copied to clipboard!', |
| 106 |
isReadOnly: false, |
| 107 |
isExpanded: false, |
| 108 |
isCode: false, |
| 109 |
variant: 'inline', |
| 110 |
position: TooltipPosition.top, |
| 111 |
maxWidth: '150px', |
| 112 |
exitDelay: 1500, |
| 113 |
entryDelay: 300, |
| 114 |
onCopy: clipboardCopyFunc, |
| 115 |
onChange: () => undefined, |
| 116 |
textAriaLabel: 'Copyable input', |
| 117 |
toggleAriaLabel: 'Show content', |
| 118 |
additionalActions: null, |
| 119 |
truncation: false, |
| 120 |
ouiaSafe: true |
| 121 |
}; |
| 122 |
export { ClipboardCopy }; |
| 123 |
//# sourceMappingURL=ClipboardCopy.js.map |