1 | import { __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import styles from '@patternfly/react-styles/css/components/ClipboardCopy/clipboard-copy';
|
4 | import { css } from '@patternfly/react-styles';
|
5 | import { PopoverPosition } from '../Popover';
|
6 | import { TextInput } from '../TextInput';
|
7 | import { GenerateId } from '../../helpers/GenerateId/GenerateId';
|
8 | import { ClipboardCopyButton } from './ClipboardCopyButton';
|
9 | import { ClipboardCopyToggle } from './ClipboardCopyToggle';
|
10 | import { ClipboardCopyExpanded } from './ClipboardCopyExpanded';
|
11 | export const clipboardCopyFunc = (event, text) => {
|
12 | const clipboard = event.currentTarget.parentElement;
|
13 | const el = document.createElement('textarea');
|
14 | el.value = text.toString();
|
15 | clipboard.appendChild(el);
|
16 | el.select();
|
17 | document.execCommand('copy');
|
18 | clipboard.removeChild(el);
|
19 | };
|
20 | export var ClipboardCopyVariant;
|
21 | (function (ClipboardCopyVariant) {
|
22 | ClipboardCopyVariant["inline"] = "inline";
|
23 | ClipboardCopyVariant["expansion"] = "expansion";
|
24 | ClipboardCopyVariant["inlineCompact"] = "inline-compact";
|
25 | })(ClipboardCopyVariant || (ClipboardCopyVariant = {}));
|
26 | export class ClipboardCopy extends React.Component {
|
27 | constructor(props) {
|
28 | super(props);
|
29 | this.timer = null;
|
30 |
|
31 | this.componentDidUpdate = (prevProps, prevState) => {
|
32 | if (prevProps.children !== this.props.children) {
|
33 | this.updateText(this.props.children);
|
34 | }
|
35 | };
|
36 | this.componentWillUnmount = () => {
|
37 | if (this.timer) {
|
38 | window.clearTimeout(this.timer);
|
39 | }
|
40 | };
|
41 |
|
42 | this.expandContent = (_event) => {
|
43 | this.setState(prevState => ({
|
44 | expanded: !prevState.expanded
|
45 | }));
|
46 | };
|
47 | this.updateText = (text) => {
|
48 | this.setState({ text });
|
49 | this.props.onChange(text);
|
50 | };
|
51 | this.render = () => {
|
52 | const _a = this.props, {
|
53 |
|
54 | isExpanded, onChange,
|
55 |
|
56 | isReadOnly, isCode, isBlock, exitDelay, maxWidth, entryDelay, switchDelay, onCopy, hoverTip, clickTip, textAriaLabel, toggleAriaLabel, variant, position, className, additionalActions } = _a, divProps = __rest(_a, ["isExpanded", "onChange", "isReadOnly", "isCode", "isBlock", "exitDelay", "maxWidth", "entryDelay", "switchDelay", "onCopy", "hoverTip", "clickTip", "textAriaLabel", "toggleAriaLabel", "variant", "position", "className", "additionalActions"]);
|
57 | const textIdPrefix = 'text-input-';
|
58 | const toggleIdPrefix = 'toggle-';
|
59 | const contentIdPrefix = 'content-';
|
60 | return (React.createElement("div", Object.assign({ className: css(styles.clipboardCopy, variant === 'inline-compact' && styles.modifiers.inline, isBlock && styles.modifiers.block, this.state.expanded && styles.modifiers.expanded, className) }, divProps),
|
61 | variant === 'inline-compact' && (React.createElement(GenerateId, { prefix: "" }, id => (React.createElement(React.Fragment, null,
|
62 | !isCode && (React.createElement("span", { className: css(styles.clipboardCopyText), id: `${textIdPrefix}${id}` }, this.state.text)),
|
63 | isCode && (React.createElement("code", { className: css(styles.clipboardCopyText, styles.modifiers.code), id: `${textIdPrefix}${id}` }, this.state.text)),
|
64 | React.createElement("span", { className: css(styles.clipboardCopyActions) },
|
65 | React.createElement("span", { className: css(styles.clipboardCopyActionsItem) },
|
66 | React.createElement(ClipboardCopyButton, { variant: "plain", exitDelay: exitDelay, entryDelay: entryDelay, maxWidth: maxWidth, position: position, id: `copy-button-${id}`, textId: `text-input-${id}`, "aria-label": hoverTip, onClick: (event) => {
|
67 | if (this.timer) {
|
68 | window.clearTimeout(this.timer);
|
69 | this.setState({ copied: false });
|
70 | }
|
71 | onCopy(event, this.state.text);
|
72 | this.setState({ copied: true }, () => {
|
73 | this.timer = window.setTimeout(() => {
|
74 | this.setState({ copied: false });
|
75 | this.timer = null;
|
76 | }, switchDelay);
|
77 | });
|
78 | } }, this.state.copied ? clickTip : hoverTip)),
|
79 | additionalActions && additionalActions))))),
|
80 | variant !== 'inline-compact' && (React.createElement(GenerateId, { prefix: "" }, id => (React.createElement(React.Fragment, null,
|
81 | React.createElement("div", { className: css(styles.clipboardCopyGroup) },
|
82 | variant === 'expansion' && (React.createElement(ClipboardCopyToggle, { isExpanded: this.state.expanded, onClick: this.expandContent, id: `${toggleIdPrefix}${id}`, textId: `${textIdPrefix}${id}`, contentId: `${contentIdPrefix}${id}`, "aria-label": toggleAriaLabel })),
|
83 | React.createElement(TextInput, { isReadOnly: isReadOnly || this.state.expanded, onChange: this.updateText, value: this.state.text, id: `text-input-${id}`, "aria-label": textAriaLabel }),
|
84 | React.createElement(ClipboardCopyButton, { exitDelay: exitDelay, entryDelay: entryDelay, maxWidth: maxWidth, position: position, id: `copy-button-${id}`, textId: `text-input-${id}`, "aria-label": hoverTip, onClick: (event) => {
|
85 | if (this.timer) {
|
86 | window.clearTimeout(this.timer);
|
87 | this.setState({ copied: false });
|
88 | }
|
89 | onCopy(event, this.state.text);
|
90 | this.setState({ copied: true }, () => {
|
91 | this.timer = window.setTimeout(() => {
|
92 | this.setState({ copied: false });
|
93 | this.timer = null;
|
94 | }, switchDelay);
|
95 | });
|
96 | } }, this.state.copied ? clickTip : hoverTip)),
|
97 | this.state.expanded && (React.createElement(ClipboardCopyExpanded, { isReadOnly: isReadOnly, isCode: isCode, id: `content-${id}`, onChange: this.updateText }, this.state.text))))))));
|
98 | };
|
99 | this.state = {
|
100 | text: Array.isArray(this.props.children)
|
101 | ? this.props.children.join('')
|
102 | : this.props.children,
|
103 | expanded: this.props.isExpanded,
|
104 | copied: false
|
105 | };
|
106 | }
|
107 | }
|
108 | ClipboardCopy.displayName = 'ClipboardCopy';
|
109 | ClipboardCopy.defaultProps = {
|
110 | hoverTip: 'Copy to clipboard',
|
111 | clickTip: 'Successfully copied to clipboard!',
|
112 | isReadOnly: false,
|
113 | isExpanded: false,
|
114 | isCode: false,
|
115 | variant: 'inline',
|
116 | position: PopoverPosition.top,
|
117 | maxWidth: '150px',
|
118 | exitDelay: 1600,
|
119 | entryDelay: 300,
|
120 | switchDelay: 2000,
|
121 | onCopy: clipboardCopyFunc,
|
122 | onChange: () => undefined,
|
123 | textAriaLabel: 'Copyable input',
|
124 | toggleAriaLabel: 'Show content',
|
125 | additionalActions: null
|
126 | };
|
127 |
|
\ | No newline at end of file |