SlideShow.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import FlipMove from 'react-flip-move';
  4. class Slide extends React.Component {
  5. static propTypes = {
  6. index: PropTypes.number.isRequired,
  7. center: PropTypes.string.isRequired,
  8. zIndex: PropTypes.number
  9. };
  10. render() {
  11. const className = 'slide slide' + this.props.index + this.props.center;
  12. return (
  13. <div className={className} style={{ zIndex: this.props.zIndex }}>
  14. <h2>{this.props.slide}</h2>
  15. </div>
  16. );
  17. }
  18. }
  19. export default class SlideShow extends React.Component {
  20. state = {
  21. indices: [0, 1, 2, 3, 4],
  22. direction: null
  23. };
  24. renderSlides = () => {
  25. let nSlide = this.state.indices.length;
  26. return (
  27. <div className="slide-group">
  28. <FlipMove duration={600} easing="ease-in-out">
  29. {this.state.indices.map((slide, index) => {
  30. const center = index === (nSlide - 1) / 2 ? ' center' : '';
  31. let zIndex = null;
  32. if (
  33. this.state.direction === 'left' &&
  34. index === this.state.indices.length - 1
  35. ) {
  36. zIndex = -10;
  37. }
  38. if (this.state.direction === 'right' && index === 0) {
  39. zIndex = -10;
  40. }
  41. return (
  42. <Slide
  43. index={index}
  44. center={center}
  45. slide={slide}
  46. key={slide}
  47. zIndex={zIndex}
  48. />
  49. );
  50. })}
  51. </FlipMove>
  52. </div>
  53. );
  54. };
  55. handleLeftClick = () => {
  56. let shiftedState = this.state.indices.slice(1);
  57. let newState = [...shiftedState, this.state.indices[0]];
  58. this.setState({ indices: newState });
  59. this.setState({ direction: 'left' });
  60. };
  61. handleRightClick = () => {
  62. let shiftedState = this.state.indices.slice(0, -1);
  63. let newState = [
  64. this.state.indices[this.state.indices.length - 1],
  65. ...shiftedState
  66. ];
  67. this.setState({ indices: newState });
  68. this.setState({ direction: 'right' });
  69. };
  70. render() {
  71. return (
  72. <div className="slideshow">
  73. <button className="left" onClick={this.handleLeftClick}>
  74. &lt;
  75. </button>
  76. {this.renderSlides()}
  77. <button className="right" onClick={this.handleRightClick}>
  78. &gt;
  79. </button>
  80. </div>
  81. );
  82. }
  83. }