UNPKG

fk-react-ui-components

Version:

Step 1 : Create a file in [ Seeds / Plants / Trees ] <br> Step 2 : It should export an Object with component name and story Component [Refer other components] <br> Step 3 : Story Component should return a react component <br> Step 3 : Created file should

124 lines (98 loc) 5.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _styles = require('./styles'); var _CarouselContent = require('./CarouselContent'); var _CarouselContent2 = _interopRequireDefault(_CarouselContent); var _NavButton = require('./NavButton'); var _NavButton2 = _interopRequireDefault(_NavButton); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var ImageCarousel = function (_React$Component) { _inherits(ImageCarousel, _React$Component); function ImageCarousel() { _classCallCheck(this, ImageCarousel); var _this = _possibleConstructorReturn(this, (ImageCarousel.__proto__ || Object.getPrototypeOf(ImageCarousel)).call(this)); _this.state = { left: 0, slideWidth: 0, imageCount: 0, currentIndex: 1 }; _this.initializeSlider = _this.initializeSlider.bind(_this); _this.moveCarousel = _this.moveCarousel.bind(_this); return _this; } _createClass(ImageCarousel, [{ key: 'initializeSlider', value: function initializeSlider(width) { this.setState(function (prevState) { return { slideWidth: width, imageCount: prevState.imageCount + 1 }; }); } }, { key: 'moveCarousel', value: function moveCarousel(direction) { if (direction === 'next') { if (this.state.currentIndex < this.state.imageCount) { this.setState({ left: this.state.left - this.state.slideWidth, currentIndex: this.state.currentIndex + 1 }); } } else if (direction === 'prev') { if (this.state.currentIndex > 1) { this.setState({ left: this.state.left + this.state.slideWidth, currentIndex: this.state.currentIndex - 1 }); } } } }, { key: 'render', value: function render() { var _this2 = this; var items = _react2.default.Children.map(this.props.children, function (child) { if (child.type === _CarouselContent2.default) { return _react2.default.cloneElement(child, { initializeSlider: _this2.initializeSlider, left: _this2.state.left }); } else if (child.type === _NavButton2.default) { return _react2.default.cloneElement(child, { moveCarousel: _this2.moveCarousel }); } }); return _react2.default.createElement( _styles.Section, { width: this.props.width, padding: this.props.padding, margin: this.props.margin }, items ); } }]); return ImageCarousel; }(_react2.default.Component); exports.default = ImageCarousel; ImageCarousel.propTypes = { children: _propTypes2.default.arrayOf(_propTypes2.default.node).isRequired, width: _propTypes2.default.string.isRequired, padding: _propTypes2.default.string.isRequired, margin: _propTypes2.default.string.isRequired };