|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- "use strict";
-
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
-
- exports.__esModule = true;
- exports.default = void 0;
-
- var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
-
- var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
-
- var _react = _interopRequireDefault(require("react"));
-
- var _propTypes = _interopRequireDefault(require("prop-types"));
-
- var _classnames = _interopRequireDefault(require("classnames"));
-
- var _CarouselItem = _interopRequireDefault(require("./CarouselItem"));
-
- var _utils = require("./utils");
-
- var Carousel =
- /*#__PURE__*/
- function (_React$Component) {
- (0, _inheritsLoose2.default)(Carousel, _React$Component);
-
- function Carousel(props) {
- var _this;
-
- _this = _React$Component.call(this, props) || this;
- _this.handleKeyPress = _this.handleKeyPress.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
- _this.renderItems = _this.renderItems.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
- _this.hoverStart = _this.hoverStart.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
- _this.hoverEnd = _this.hoverEnd.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
- _this.state = {
- direction: 'right',
- indicatorClicked: false
- };
- return _this;
- }
-
- var _proto = Carousel.prototype;
-
- _proto.getChildContext = function getChildContext() {
- return {
- direction: this.state.direction
- };
- };
-
- _proto.componentDidMount = function componentDidMount() {
- // Set up the cycle
- if (this.props.ride === 'carousel') {
- this.setInterval();
- } // TODO: move this to the specific carousel like bootstrap. Currently it will trigger ALL carousels on the page.
-
-
- document.addEventListener('keyup', this.handleKeyPress);
- };
-
- _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
- this.setInterval(nextProps); // Calculate the direction to turn
-
- if (this.props.activeIndex + 1 === nextProps.activeIndex) {
- this.setState({
- direction: 'right'
- });
- } else if (this.props.activeIndex - 1 === nextProps.activeIndex) {
- this.setState({
- direction: 'left'
- });
- } else if (this.props.activeIndex > nextProps.activeIndex) {
- this.setState({
- direction: this.state.indicatorClicked ? 'left' : 'right'
- });
- } else if (this.props.activeIndex !== nextProps.activeIndex) {
- this.setState({
- direction: this.state.indicatorClicked ? 'right' : 'left'
- });
- }
-
- this.setState({
- indicatorClicked: false
- });
- };
-
- _proto.componentWillUnmount = function componentWillUnmount() {
- this.clearInterval();
- document.removeEventListener('keyup', this.handleKeyPress);
- };
-
- _proto.setInterval = function (_setInterval) {
- function setInterval() {
- return _setInterval.apply(this, arguments);
- }
-
- setInterval.toString = function () {
- return _setInterval.toString();
- };
-
- return setInterval;
- }(function (props) {
- if (props === void 0) {
- props = this.props;
- }
-
- // make sure not to have multiple intervals going...
- this.clearInterval();
-
- if (props.interval) {
- this.cycleInterval = setInterval(function () {
- props.next();
- }, parseInt(props.interval, 10));
- }
- });
-
- _proto.clearInterval = function (_clearInterval) {
- function clearInterval() {
- return _clearInterval.apply(this, arguments);
- }
-
- clearInterval.toString = function () {
- return _clearInterval.toString();
- };
-
- return clearInterval;
- }(function () {
- clearInterval(this.cycleInterval);
- });
-
- _proto.hoverStart = function hoverStart() {
- if (this.props.pause === 'hover') {
- this.clearInterval();
- }
-
- if (this.props.mouseEnter) {
- var _this$props;
-
- (_this$props = this.props).mouseEnter.apply(_this$props, arguments);
- }
- };
-
- _proto.hoverEnd = function hoverEnd() {
- if (this.props.pause === 'hover') {
- this.setInterval();
- }
-
- if (this.props.mouseLeave) {
- var _this$props2;
-
- (_this$props2 = this.props).mouseLeave.apply(_this$props2, arguments);
- }
- };
-
- _proto.handleKeyPress = function handleKeyPress(evt) {
- if (this.props.keyboard) {
- if (evt.keyCode === 37) {
- this.props.previous();
- } else if (evt.keyCode === 39) {
- this.props.next();
- }
- }
- };
-
- _proto.renderItems = function renderItems(carouselItems, className) {
- var _this2 = this;
-
- var slide = this.props.slide;
- return _react.default.createElement("div", {
- role: "listbox",
- className: className
- }, carouselItems.map(function (item, index) {
- var isIn = index === _this2.props.activeIndex;
- return _react.default.cloneElement(item, {
- in: isIn,
- slide: slide
- });
- }));
- };
-
- _proto.render = function render() {
- var _this3 = this;
-
- var _this$props3 = this.props,
- cssModule = _this$props3.cssModule,
- slide = _this$props3.slide,
- className = _this$props3.className;
- var outerClasses = (0, _utils.mapToCssModules)((0, _classnames.default)(className, 'carousel', slide && 'slide'), cssModule);
- var innerClasses = (0, _utils.mapToCssModules)((0, _classnames.default)('carousel-inner'), cssModule); // filter out booleans, null, or undefined
-
- var children = this.props.children.filter(function (child) {
- return child !== null && child !== undefined && typeof child !== 'boolean';
- });
- var slidesOnly = children.every(function (child) {
- return child.type === _CarouselItem.default;
- }); // Rendering only slides
-
- if (slidesOnly) {
- return _react.default.createElement("div", {
- className: outerClasses,
- onMouseEnter: this.hoverStart,
- onMouseLeave: this.hoverEnd
- }, this.renderItems(children, innerClasses));
- } // Rendering slides and controls
-
-
- if (children[0] instanceof Array) {
- var _carouselItems = children[0];
- var _controlLeft = children[1];
- var _controlRight = children[2];
- return _react.default.createElement("div", {
- className: outerClasses,
- onMouseEnter: this.hoverStart,
- onMouseLeave: this.hoverEnd
- }, this.renderItems(_carouselItems, innerClasses), _controlLeft, _controlRight);
- } // Rendering indicators, slides and controls
-
-
- var indicators = children[0];
-
- var wrappedOnClick = function wrappedOnClick(e) {
- if (typeof indicators.props.onClickHandler === 'function') {
- _this3.setState({
- indicatorClicked: true
- }, function () {
- return indicators.props.onClickHandler(e);
- });
- }
- };
-
- var wrappedIndicators = _react.default.cloneElement(indicators, {
- onClickHandler: wrappedOnClick
- });
-
- var carouselItems = children[1];
- var controlLeft = children[2];
- var controlRight = children[3];
- return _react.default.createElement("div", {
- className: outerClasses,
- onMouseEnter: this.hoverStart,
- onMouseLeave: this.hoverEnd
- }, wrappedIndicators, this.renderItems(carouselItems, innerClasses), controlLeft, controlRight);
- };
-
- return Carousel;
- }(_react.default.Component);
-
- Carousel.propTypes = {
- // the current active slide of the carousel
- activeIndex: _propTypes.default.number,
- // a function which should advance the carousel to the next slide (via activeIndex)
- next: _propTypes.default.func.isRequired,
- // a function which should advance the carousel to the previous slide (via activeIndex)
- previous: _propTypes.default.func.isRequired,
- // controls if the left and right arrow keys should control the carousel
- keyboard: _propTypes.default.bool,
-
- /* If set to "hover", pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on
- * mouseleave. If set to false, hovering over the carousel won't pause it. (default: "hover")
- */
- pause: _propTypes.default.oneOf(['hover', false]),
- // Autoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load.
- // This is how bootstrap defines it... I would prefer a bool named autoplay or something...
- ride: _propTypes.default.oneOf(['carousel']),
- // the interval at which the carousel automatically cycles (default: 5000)
- // eslint-disable-next-line react/no-unused-prop-types
- interval: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string, _propTypes.default.bool]),
- children: _propTypes.default.array,
- // called when the mouse enters the Carousel
- mouseEnter: _propTypes.default.func,
- // called when the mouse exits the Carousel
- mouseLeave: _propTypes.default.func,
- // controls whether the slide animation on the Carousel works or not
- slide: _propTypes.default.bool,
- cssModule: _propTypes.default.object,
- className: _propTypes.default.string
- };
- Carousel.defaultProps = {
- interval: 5000,
- pause: 'hover',
- keyboard: true,
- slide: true
- };
- Carousel.childContextTypes = {
- direction: _propTypes.default.string
- };
- var _default = Carousel;
- exports.default = _default;
|