forked from 5monkeys/djedi-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToplist.js
More file actions
60 lines (52 loc) · 1.4 KB
/
Toplist.js
File metadata and controls
60 lines (52 loc) · 1.4 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
import { Node, djedi } from "djedi-react";
import PropTypes from "prop-types";
import React from "react";
export default function Toplist() {
return (
<Node
uri="Toplist/toplist"
// `edit={false}` makes `state.content` a string instead of a <span>.
edit={false}
// Use a custom render function to parse the node content into a list, so
// it can be passed to the `<ReversibleList>` component.
render={state => {
if (state.type !== "success") {
return djedi.options.defaultRender(state);
}
const items = state.content
.split("\n")
.map(line => line.trim())
.filter(line => line !== "");
return items.length === 0 ? null : <ReversibleList items={items} />;
}}
>{`
Python
JavaScript
CSS
HTML
`}</Node>
);
}
class ReversibleList extends React.Component {
static propTypes = {
items: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};
state = {
reversed: false,
};
toggleReversed = () => {
this.setState(state => ({ reversed: !state.reversed }));
};
render() {
const items = this.state.reversed
? this.props.items.slice().reverse()
: this.props.items;
return (
<ol onClick={this.toggleReversed}>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ol>
);
}
}