Carousel - 图1

  1. import React, { Component } from 'react';
  2. import {
  3. Carousel,
  4. CarouselItem,
  5. CarouselControl,
  6. CarouselIndicators,
  7. CarouselCaption
  8. } from 'reactstrap';
  9. const items = [
  10. {
  11. src: 'data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_15ba800aa1d%20text%20%7B%20fill%3A%23555%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_15ba800aa1d%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22285.921875%22%20y%3D%22218.3%22%3EFirst%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E',
  12. altText: 'Slide 1',
  13. caption: 'Slide 1'
  14. },
  15. {
  16. src: 'data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_15ba800aa20%20text%20%7B%20fill%3A%23444%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_15ba800aa20%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23666%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22247.3203125%22%20y%3D%22218.3%22%3ESecond%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E',
  17. altText: 'Slide 2',
  18. caption: 'Slide 2'
  19. },
  20. {
  21. src: 'data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_15ba800aa21%20text%20%7B%20fill%3A%23333%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_15ba800aa21%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23555%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22277%22%20y%3D%22218.3%22%3EThird%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E',
  22. altText: 'Slide 3',
  23. caption: 'Slide 3'
  24. }
  25. ];
  26. class Example extends Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = { activeIndex: 0 };
  30. this.next = this.next.bind(this);
  31. this.previous = this.previous.bind(this);
  32. this.goToIndex = this.goToIndex.bind(this);
  33. this.onExiting = this.onExiting.bind(this);
  34. this.onExited = this.onExited.bind(this);
  35. }
  36. onExiting() {
  37. this.animating = true;
  38. }
  39. onExited() {
  40. this.animating = false;
  41. }
  42. next() {
  43. if (this.animating) return;
  44. const nextIndex = this.state.activeIndex === items.length - 1 ? 0 : this.state.activeIndex + 1;
  45. this.setState({ activeIndex: nextIndex });
  46. }
  47. previous() {
  48. if (this.animating) return;
  49. const nextIndex = this.state.activeIndex === 0 ? items.length - 1 : this.state.activeIndex - 1;
  50. this.setState({ activeIndex: nextIndex });
  51. }
  52. goToIndex(newIndex) {
  53. if (this.animating) return;
  54. this.setState({ activeIndex: newIndex });
  55. }
  56. render() {
  57. const { activeIndex } = this.state;
  58. const slides = items.map((item) => {
  59. return (
  60. <CarouselItem
  61. onExiting={this.onExiting}
  62. onExited={this.onExited}
  63. key={item.src}
  64. >
  65. <img src={item.src} alt={item.altText} />
  66. <CarouselCaption captionText={item.caption} captionHeader={item.caption} />
  67. </CarouselItem>
  68. );
  69. });
  70. return (
  71. <Carousel
  72. activeIndex={activeIndex}
  73. next={this.next}
  74. previous={this.previous}
  75. >
  76. <CarouselIndicators items={items} activeIndex={activeIndex} onClickHandler={this.goToIndex} />
  77. {slides}
  78. <CarouselControl direction="prev" directionText="Previous" onClickHandler={this.previous} />
  79. <CarouselControl direction="next" directionText="Next" onClickHandler={this.next} />
  80. </Carousel>
  81. );
  82. }
  83. }
  84. export default Example;
  1. Carousel.propTypes = {
  2. // the current active slide of the carousel
  3. activeIndex: PropTypes.number,
  4. // a function which should advance the carousel to the next slide (via activeIndex)
  5. next: PropTypes.func.isRequired,
  6. // a function which should advance the carousel to the previous slide (via activeIndex)
  7. previous: PropTypes.func.isRequired,
  8. // controls if the left and right arrow keys should control the carousel
  9. keyboard: PropTypes.bool,
  10. /* If set to "hover", pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on
  11. * mouseleave. If set to false, hovering over the carousel won't pause it. (default: "hover")
  12. */
  13. pause: PropTypes.oneOf(['hover', false]),
  14. // Autoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load.
  15. // This is how bootstrap defines it... I would prefer a bool named autoplay or something...
  16. ride: PropTypes.oneOf(['carousel']),
  17. // the interval at which the carousel automatically cycles (default: 5000)
  18. interval: PropTypes.oneOfType([
  19. PropTypes.number,
  20. PropTypes.string,
  21. PropTypes.bool,
  22. ]),
  23. children: PropTypes.array,
  24. // called when the mouse enters the Carousel
  25. mouseEnter: PropTypes.func,
  26. // called when the mouse exits the Carousel
  27. mouseLeave: PropTypes.func,
  28. // controls whether the slide animation on the Carousel works or not
  29. slide: PropTypes.bool,
  30. cssModule: PropTypes.object,
  31. };

CarouselItem Properties

  1. CarouselItem.propTypes = {
  2. ...Transition.propTypes,
  3. tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  4. in: PropTypes.bool,
  5. cssModule: PropTypes.object,
  6. children: PropTypes.node,
  7. slide: PropTypes.bool,
  8. };

CarouselControl Properties

  1. CarouselControl.propTypes = {
  2. direction: PropTypes.oneOf(['prev', 'next']).isRequired,
  3. onClickHandler: PropTypes.func.isRequired,
  4. cssModule: PropTypes.object,
  5. directionText: PropTypes.string
  6. };

CarouselIndicators Properties

  1. CarouselIndicators.propTypes = {
  2. items: PropTypes.array.isRequired,
  3. activeIndex: PropTypes.number.isRequired,
  4. cssModule: PropTypes.object,
  5. onClickHandler: PropTypes.func.isRequired
  6. };

CarouselCaption Properties

  1. CarouselCaption.propTypes = {
  2. captionHeader: PropTypes.string,
  3. captionText: PropTypes.string.isRequired,
  4. cssModule: PropTypes.object
  5. };

For the most basic use-case an uncontrolled component can provide the functionality wanted without the need to manage/control the state of the component. UncontrolledCarousel does not require previous, next nor activeIndex props to work. Anything provided to a normal Carousel can also be provided to UncontrolledCarousel, overriding the control UncontrolledCarousel provides. Additionally, you can hide the controls by passing false to the controls prop and you can disable the indicators by passing false to the indicators prop; both are visible by default. Autoplay (ride="carousel") is enabled by default, you can disable it by passing false to the autoPlay prop.

Carousel - 图2

  1. import React from 'react';
  2. import { UncontrolledCarousel } from 'reactstrap';
  3. const items = [
  4. {
  5. src: 'data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_15ba800aa1d%20text%20%7B%20fill%3A%23555%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_15ba800aa1d%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22285.921875%22%20y%3D%22218.3%22%3EFirst%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E',
  6. altText: 'Slide 1',
  7. caption: 'Slide 1',
  8. header: 'Slide 1 Header'
  9. },
  10. {
  11. src: 'data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_15ba800aa20%20text%20%7B%20fill%3A%23444%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_15ba800aa20%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23666%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22247.3203125%22%20y%3D%22218.3%22%3ESecond%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E',
  12. altText: 'Slide 2',
  13. caption: 'Slide 2',
  14. header: 'Slide 2 Header'
  15. },
  16. {
  17. src: 'data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_15ba800aa21%20text%20%7B%20fill%3A%23333%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_15ba800aa21%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23555%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22277%22%20y%3D%22218.3%22%3EThird%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E',
  18. altText: 'Slide 3',
  19. caption: 'Slide 3',
  20. header: 'Slide 3 Header'
  21. }
  22. ];
  23. const Example = () => <UncontrolledCarousel items={items} />;
  24. export default Example;

Same as Carousel (except children) can be overridden plus the following

  1. UncontrolledCarousel.propTypes = {
  2. items: PropTypes.array.isRequired,
  3. indicators: PropTypes.bool, // default: true
  4. controls: PropTypes.bool, // default: true
  5. autoPlay: PropTypes.bool, // default: true
  6. };

Carousel - 图3

  1. import React, { Component } from 'react';
  2. import {
  3. Carousel,
  4. CarouselItem,
  5. CarouselControl,
  6. CarouselIndicators,
  7. CarouselCaption
  8. } from 'reactstrap';
  9. const items = [
  10. {
  11. id: 1,
  12. altText: 'Slide 1',
  13. caption: 'Slide 1'
  14. },
  15. {
  16. id: 2,
  17. altText: 'Slide 2',
  18. caption: 'Slide 2'
  19. },
  20. {
  21. id: 3,
  22. altText: 'Slide 3',
  23. caption: 'Slide 3'
  24. }
  25. ];
  26. class Example extends Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = { activeIndex: 0 };
  30. this.next = this.next.bind(this);
  31. this.previous = this.previous.bind(this);
  32. this.goToIndex = this.goToIndex.bind(this);
  33. this.onExiting = this.onExiting.bind(this);
  34. this.onExited = this.onExited.bind(this);
  35. }
  36. onExiting() {
  37. this.animating = true;
  38. }
  39. onExited() {
  40. this.animating = false;
  41. }
  42. next() {
  43. if (this.animating) return;
  44. const nextIndex = this.state.activeIndex === items.length - 1 ? 0 : this.state.activeIndex + 1;
  45. this.setState({ activeIndex: nextIndex });
  46. }
  47. previous() {
  48. if (this.animating) return;
  49. const nextIndex = this.state.activeIndex === 0 ? items.length - 1 : this.state.activeIndex - 1;
  50. this.setState({ activeIndex: nextIndex });
  51. }
  52. goToIndex(newIndex) {
  53. if (this.animating) return;
  54. this.setState({ activeIndex: newIndex });
  55. }
  56. render() {
  57. const { activeIndex } = this.state;
  58. const slides = items.map((item) => {
  59. return (
  60. <CarouselItem
  61. className="custom-tag"
  62. tag="div"
  63. key={item.id}
  64. onExiting={this.onExiting}
  65. onExited={this.onExited}
  66. >
  67. <CarouselCaption className="text-danger" captionText={item.caption} captionHeader={item.caption} />
  68. </CarouselItem>
  69. );
  70. });
  71. return (
  72. <div>
  73. <style>
  74. {
  75. `.custom-tag {
  76. max-width: 100%;
  77. height: 500px;
  78. background: black;
  79. }`
  80. }
  81. </style>
  82. <Carousel
  83. activeIndex={activeIndex}
  84. next={this.next}
  85. previous={this.previous}
  86. >
  87. <CarouselIndicators items={items} activeIndex={activeIndex} onClickHandler={this.goToIndex} />
  88. {slides}
  89. <CarouselControl direction="prev" directionText="Previous" onClickHandler={this.previous} />
  90. <CarouselControl direction="next" directionText="Next" onClickHandler={this.next} />
  91. </Carousel>
  92. </div>
  93. );
  94. }
  95. }
  96. export default Example;