-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcomponents.js
More file actions
96 lines (84 loc) · 2.28 KB
/
components.js
File metadata and controls
96 lines (84 loc) · 2.28 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
import { isValidElement, Component } from 'react';
import Popover from 'react-tiny-popover';
import './Configuration.scss';
import PropTypes from 'prop-types';
const DEFAULT_CHILDREN_SIZE = 4;
const isFirstChild = (child) => typeof child === 'string' && child !== ' ';
const removeSpaces = (child) => (isFirstChild(child) ? child.trim() : child);
const addLink = (child, i, url) => {
return isFirstChild(child) ? (
<a href={url} key={i}>
{child}
</a>
) : (
child
);
};
const Card = ({ body }) => {
return (
<div className="markdown">
<pre className="inline">
<code>{body}</code>
</pre>
</div>
);
};
Card.propTypes = {
body: PropTypes.node,
};
export class Details extends Component {
static propTypes = {
url: PropTypes.string,
myChilds: PropTypes.arrayOf(PropTypes.node),
};
constructor(props) {
super(props);
this.state = {
open: false,
};
}
clickOutsideHandler = () => {
this.setState({ open: false });
};
toggleVisibility = () => {
this.setState({ open: !this.state.open });
};
render() {
const { myChilds, url } = this.props;
// Find the index of </default>
const closeDefaultTagIndex = myChilds.findIndex((child) => {
if (isValidElement(child)) {
return (
child.props.className.includes('tag') &&
child.props.children.length === DEFAULT_CHILDREN_SIZE
);
}
});
const content = myChilds.slice();
// Summary is the part of the snippet that would be shown in the code snippet,
// to get it we need to cut the <default></default> enclosing tags
const summary = content
.splice(2, closeDefaultTagIndex - 3)
.map(removeSpaces)
.map((child, i) => addLink(child, i, url));
content.splice(0, DEFAULT_CHILDREN_SIZE); // Remove <default></default> information
const { open } = this.state;
return (
<Popover
isOpen={open}
position={['right', 'top']}
padding={0}
onClickOutside={this.clickOutsideHandler}
containerClassName={'shadow-sm'}
content={<Card body={content} />}
>
<span
className="code-details-summary-span"
onClick={this.toggleVisibility}
>
{summary}
</span>
</Popover>
);
}
}