-
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathModuleItem.jsx
More file actions
110 lines (93 loc) · 2.72 KB
/
ModuleItem.jsx
File metadata and controls
110 lines (93 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import cls from "classnames";
import escapeRegExp from "escape-string-regexp";
import { filesize } from "filesize";
import { escape } from "html-escaper";
import PropTypes from "prop-types";
import PureComponent from "../lib/PureComponent.jsx";
import * as styles from "./ModuleItem.css";
import { ModuleType, SizeType } from "./types.js";
export default class ModuleItem extends PureComponent {
static propTypes = {
module: ModuleType.isRequired,
showSize: SizeType.isRequired,
highlightedText: PropTypes.instanceOf(RegExp),
isVisible: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,
};
state = {
visible: true,
};
render({ module, showSize }) {
const invisible = !this.state.visible;
const classes = cls(styles.container, styles[this.itemType], {
[styles.invisible]: invisible,
});
return (
<div
className={classes}
title={invisible ? this.invisibleHint : null}
onClick={this.handleClick}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
>
<span dangerouslySetInnerHTML={{ __html: this.titleHtml }} />
{showSize && (
<>
{" ("}
<strong>{filesize(module[showSize])}</strong>
{")"}
</>
)}
</div>
);
}
get itemType() {
const { module } = this.props;
if (!module.path) return "chunk";
return module.groups ? "folder" : "module";
}
get titleHtml() {
let html;
const { module } = this.props;
const title = module.path || module.label;
const term = this.props.highlightedText;
if (term) {
const regexp =
term instanceof RegExp
? new RegExp(term.source, "igu")
: new RegExp(`(?:${escapeRegExp(term)})+`, "iu");
let match;
let lastMatch;
do {
lastMatch = match;
match = regexp.exec(title);
} while (match);
if (lastMatch) {
html = `${escape(
title.slice(0, lastMatch.index),
)}<strong>${escape(lastMatch[0])}</strong>${escape(
title.slice(lastMatch.index + lastMatch[0].length),
)}`;
}
}
if (!html) {
html = escape(title);
}
return html;
}
get invisibleHint() {
const itemType =
this.itemType.charAt(0).toUpperCase() + this.itemType.slice(1);
return `${itemType} is not rendered in the treemap because it's too small.`;
}
get isVisible() {
const { isVisible } = this.props;
return isVisible ? isVisible(this.props.module) : true;
}
handleClick = () => this.props.onClick(this.props.module);
handleMouseEnter = () => {
if (this.props.isVisible) {
this.setState({ visible: this.isVisible });
}
};
}