• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mac-s-g / react-json-view / #1279

09 Mar 2021 03:27AM CUT coverage: 83.298%. Remained the same
#1279

push

web-flow
Merge pull request #342 from mac-s-g/bump-patch

bump patch version

284 of 349 branches covered (81.38%)

Branch coverage included in aggregate %.

504 of 597 relevant lines covered (84.42%)

45.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.63
/src/js/components/CopyToClipboard.js
1
import React from 'react';
2

3
import { toType } from './../helpers/util';
4

5
//clibboard icon
6
import { Clippy } from './icons';
7

8
//theme
9
import Theme from './../themes/getStyle';
10

11
export default class extends React.PureComponent {
12
    constructor(props) {
13
        super(props);
46✔
14
        this.state = {
46✔
15
            copied: false
16
        };
17
    }
18

19
    copiedTimer = null;
46✔
20

21
    componentWillUnmount() {
22
        if (this.copiedTimer) {
1!
23
            clearTimeout(this.copiedTimer);
×
24
            this.copiedTimer = null;
×
25
        }
26
    }
27

28
    handleCopy = () => {
46✔
29
        const container = document.createElement('textarea');
1✔
30
        const { clickCallback, src, namespace } = this.props;
1✔
31

32
        container.innerHTML = JSON.stringify(
1✔
33
            this.clipboardValue(src),
34
            null,
35
            '  '
36
        );
37

38
        document.body.appendChild(container);
1✔
39
        container.select();
1✔
40
        document.execCommand('copy');
1✔
41

42
        document.body.removeChild(container);
1✔
43

44
        this.copiedTimer = setTimeout(() => {
1✔
45
            this.setState({
1✔
46
                copied: false
47
            });
48
        }, 5500);
49

50
        this.setState({ copied: true }, () => {
1✔
51
            if (typeof clickCallback !== 'function') {
1!
52
                return;
×
53
            }
54

55
            clickCallback({
1✔
56
                src: src,
57
                namespace: namespace,
58
                name: namespace[namespace.length - 1]
59
            });
60
        });
61
    };
62

63
    getClippyIcon = () => {
46✔
64
        const { theme } = this.props;
49✔
65

66
        if (this.state.copied) {
49✔
67
            return (
1✔
68
                <span>
69
                    <Clippy class="copy-icon" {...Theme(theme, 'copy-icon')} />
70
                    <span {...Theme(theme, 'copy-icon-copied')}>✔</span>
71
                </span>
72
            );
73
        }
74

75
        return <Clippy class="copy-icon" {...Theme(theme, 'copy-icon')} />;
48✔
76
    };
77

78
    clipboardValue = value => {
46✔
79
        const type = toType(value);
1✔
80
        switch (type) {
1!
81
            case 'function':
82
            case 'regexp':
83
                return value.toString();
×
84
            default:
85
                return value;
1✔
86
        }
87
    };
88

89
    render() {
90
        const { src, theme, hidden, rowHovered } = this.props;
49✔
91
        let style = Theme(theme, 'copy-to-clipboard').style;
49✔
92
        let display = 'inline';
49✔
93

94
        if (hidden) {
49✔
95
            display = 'none';
1✔
96
        }
97

98
        return (
49✔
99
            <span
100
                className="copy-to-clipboard-container"
101
                title="Copy to clipboard"
102
                style={{
103
                    verticalAlign: 'top',
104
                    display: rowHovered ? 'inline-block' : 'none'
49!
105
                }}
106
            >
107
                <span
108
                    style={{
109
                        ...style,
110
                        display: display
111
                    }}
112
                    onClick={this.handleCopy}
113
                >
114
                    {this.getClippyIcon()}
115
                </span>
116
            </span>
117
        );
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc