-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathShowMore.js
More file actions
79 lines (69 loc) · 1.82 KB
/
ShowMore.js
File metadata and controls
79 lines (69 loc) · 1.82 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Truncate from 'react-truncate';
class ShowMore extends Component {
static defaultProps = {
lines: 3,
more: 'Show more',
less: 'Show less',
anchorClass: '',
width: 0
}
static propTypes = {
children: PropTypes.node,
lines: PropTypes.number,
more: PropTypes.node,
less: PropTypes.node,
anchorClass: PropTypes.string,
width: PropTypes.number
}
state = {
expanded: false,
truncated: false
}
handleTruncate = truncated => {
if (truncated !== this.state.truncated) {
this.setState({
truncated
});
}
}
toggleLines = event => {
event.preventDefault();
this.setState({
expanded: !this.state.expanded
});
}
render() {
const {
children,
more,
less,
lines,
anchorClass,
width
} = this.props;
const {
expanded,
truncated
} = this.state;
return (
<div>
<Truncate
width={width}
lines={!expanded && lines}
ellipsis={(
<span>... <a href='#' className={anchorClass} onClick={this.toggleLines}>{more}</a></span>
)}
onTruncate={this.handleTruncate}
>
{children}
</Truncate>
{!truncated && expanded && (
<span> <a href='#' className={anchorClass} onClick={this.toggleLines}>{less}</a></span>
)}
</div>
);
}
}
export default ShowMore;