'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

require('./CreateAccordion.scss');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var CreateAccordion = function (_Component) {
	(0, _inherits3.default)(CreateAccordion, _Component);

	function CreateAccordion(props, context) {
		(0, _classCallCheck3.default)(this, CreateAccordion);

		var _this = (0, _possibleConstructorReturn3.default)(this, (CreateAccordion.__proto__ || Object.getPrototypeOf(CreateAccordion)).call(this, props, context));

		_this.state = {
			show: !1
		};
		_this.handleSlide = _this.handleSlide.bind(_this);
		_this.setDefault = _this.setDefault.bind(_this);
		return _this;
	}

	(0, _createClass3.default)(CreateAccordion, [{
		key: 'handleSlide',
		value: function handleSlide(event) {
			var _this2 = this;

			event.preventDefault();
			event.stopPropagation();
			var refName = this.props.refName;

			this.setState({
				show: !this.state.show
			}, function () {
				try {
					var $this = _this2.refs.slide.refs[refName];
					var height = 0;
					for (var i = 0; i < $this.childNodes.length; i++) {
						height += $this.childNodes[i].offsetHeight;
					}
					$this.style.height = _this2.state.show ? height + 'px' : '0px';
				} catch (e) {
					console.log(e);
				}
			});
		}
	}, {
		key: 'setDefault',
		value: function setDefault() {
			var _this3 = this;

			this.setState({
				show: !1
			}, function () {
				var refName = _this3.props.refName;

				var $this = _this3.refs.slide.refs[refName];
				$this.style.height = '0px';
			});
		}
	}, {
		key: 'render',
		value: function render() {
			var show = this.state.show;

			return (0, _react.cloneElement)(_react.Children.only(this.props.children), {
				__decorator: 'success',
				accordion: {
					show: show,
					eventHandler: this.handleSlide,
					icon: 'iconfont ' + (show ? "icon-up" : "icon-down"),
					content: 'rc-accordion ' + (show ? "__active" : ""),
					setDefault: this.setDefault
				},
				ref: "slide"
			});
		}
	}]);
	return CreateAccordion;
}(_react.Component);

CreateAccordion.propTypes = {
	refName: _propTypes2.default.string.isRequired
};
exports.default = CreateAccordion;