Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. class LongText extends Component {
  2. state = { showAll: false }
  3. showMore = () => this.setState({showAll: true});
  4. showLess = () => this.setState({showAll: false});
  5. render() {
  6. const {content, limit} = this.props;
  7. const {showAll} = this.state;
  8. if(content.length<=limit) {
  9. // there is nothing more to show
  10. return <div>{content}<div>
  11. }
  12. if(showAll) {
  13. // We show the extended text and a link to reduce it
  14. return <div>
  15. {content}
  16. <a onClick={this.showLess}>Read less</a>
  17. </div>
  18. }
  19. // In the final case, we show a text with ellipsis and a `Read more` button
  20. const toShow = content.substring(0,limit)+"...";
  21. return <div>
  22. {toShow}
  23. <span onClick={this.showMore}>Read more</a>
  24. </div>
  25. }
  26. }
  27.  
  28. var component = React.createClass({
  29. getInitialState: function() {
  30. return {
  31. expanded: false,
  32. myText: 'bla bla bla'
  33. };
  34. },
  35.  
  36. expandedText: function() {
  37. this.setState({
  38. expanded: true
  39. });
  40. },
  41.  
  42. getMoreTextDiv = function() {
  43. if (this.state.expanded) {
  44. return myText;
  45. } else {
  46. return myText.substr(0, 250);
  47. }
  48. }
  49.  
  50. render: function() {
  51. var expandedDiv = this.getMoreTextDiv();
  52. return (
  53. <div>
  54. <a onClick={ this.expandedText }>Read more</a>
  55. { expandedDiv }
  56. </div>
  57. );
  58. }
  59. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement