react-rabbit-carousel
Version:
A simple image carousel component where you can control
154 lines (125 loc) • 6.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _cssModule = _interopRequireDefault(require("./css.module.css"));
var _Title = _interopRequireDefault(require("./Title"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var RabbitCarousel =
/*#__PURE__*/
function (_React$Component) {
_inherits(RabbitCarousel, _React$Component);
function RabbitCarousel(props) {
var _this;
_classCallCheck(this, RabbitCarousel);
_this = _possibleConstructorReturn(this, _getPrototypeOf(RabbitCarousel).call(this, props));
_this.state = {
increment: 1
};
_this.countImage = _this.props.imagen.length;
_this.seconds = _this.props.seconds ? parseInt(_this.props.seconds) * 1000 : 1000;
_this.handleClickNext = _this.handleClickNext.bind(_assertThisInitialized(_this));
_this.handleClickPrev = _this.handleClickPrev.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(RabbitCarousel, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
this.change = setInterval(function () {
return _this2.timeIncrement();
}, this.seconds);
}
}, {
key: "timeIncrement",
value: function timeIncrement() {
if (this.countImage === this.state.increment) {
this.setState({
increment: 1
});
} else {
this.setState({
increment: this.state.increment + 1
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
clearInterval(this.change);
}
}, {
key: "handleClickNext",
value: function handleClickNext() {
var _this3 = this;
var valor = this.state.increment + 1;
if (valor > this.countImage) {
valor = 1;
}
this.setState({
increment: valor
});
clearInterval(this.change);
this.change = setInterval(function () {
return _this3.timeIncrement();
}, this.seconds);
}
}, {
key: "handleClickPrev",
value: function handleClickPrev() {
var _this4 = this;
var valor = this.state.increment - 1;
if (valor === 0) {
valor = this.countImage;
}
this.setState({
increment: valor
});
clearInterval(this.change);
this.change = setInterval(function () {
return _this4.timeIncrement();
}, this.seconds);
}
}, {
key: "render",
value: function render() {
var _this5 = this;
return _react["default"].createElement("div", null, _react["default"].createElement("div", {
className: _cssModule["default"].container
}, _react["default"].createElement("img", {
className: _cssModule["default"].image,
src: this.props.imagen[this.state.increment - 1],
alt: "slider-" + this.state.increment
}), _react["default"].createElement("div", {
className: _cssModule["default"].textCenter
}, this.props.valueTitle[this.state.increment - 1].map(function (item, i) {
return _react["default"].createElement(_Title["default"], {
key: i,
type: _this5.props.title,
colorTitle: _this5.props.color,
value: item
});
})), _react["default"].createElement("div", {
className: _cssModule["default"].textLeft,
onClick: this.handleClickPrev
}, _react["default"].createElement("h4", null, "\u276E")), _react["default"].createElement("div", {
className: _cssModule["default"].textRight,
onClick: this.handleClickNext
}, _react["default"].createElement("h4", null, "\u276F"))));
}
}]);
return RabbitCarousel;
}(_react["default"].Component);
var _default = RabbitCarousel;
exports["default"] = _default;