UNPKG

zent

Version:

一套前端设计语言和基于React的实现

125 lines (96 loc) 5.17 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = undefined; 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 _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _classnames = require('zent-utils/classnames'); var _classnames2 = _interopRequireDefault(_classnames); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 NumberSteps = function (_Component) { _inherits(NumberSteps, _Component); function NumberSteps(props) { _classCallCheck(this, NumberSteps); var _this = _possibleConstructorReturn(this, (NumberSteps.__proto__ || Object.getPrototypeOf(NumberSteps)).call(this, props)); _this.culcLastStepOffsetWidth = function () { var domNode = _reactDom2['default'].findDOMNode(_this); if (domNode.children.length > 0) { // +1 for fit edge bug of digit width, like 35.4px var lastStepOffsetWidth = domNode.lastChild.offsetWidth + 1; if (isNaN(lastStepOffsetWidth) || _this.state.lastStepOffsetWidth === lastStepOffsetWidth) { return; } _this.setState({ lastStepOffsetWidth: lastStepOffsetWidth }); } }; _this.state = { lastStepOffsetWidth: 0 }; return _this; } _createClass(NumberSteps, [{ key: 'componentDidMount', value: function componentDidMount() { this.culcLastStepOffsetWidth(); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.culcLastStepOffsetWidth(); } }, { key: 'render', value: function render() { var _classNames, _this2 = this; var props = this.props; var className = props.className, prefix = props.prefix, children = props.children, current = props.current, status = props.status; var lastIndex = children.length - 1; var reLayouted = this.state.lastStepOffsetWidth > 0; var classString = (0, _classnames2['default'])((_classNames = {}, _defineProperty(_classNames, prefix + '-steps', true), _defineProperty(_classNames, '' + className, true), _classNames)); return _react2['default'].createElement( 'div', { className: classString }, _react2['default'].Children.map(children, function (item, index) { var tailWidth = index === lastIndex || !reLayouted ? null : 100 / lastIndex + '%'; var adjustMarginRight = index === lastIndex ? null : -(_this2.state.lastStepOffsetWidth / lastIndex + 1); var np = { stepNumber: (index + 1).toString(), stepLast: index === lastIndex, isCurrentStep: index === current - 1, isLastFinishStep: status === 'error' && index === current - 2, tailWidth: tailWidth, adjustMarginRight: adjustMarginRight, prefix: prefix, status: 'wait' }; if (!item.props.status) { if (index === current - 1) { np.status = status; } else if (index < current - 1) { np.status = 'finish'; } else { np.status = 'wait'; } } return _react2['default'].cloneElement(item, np); }, this) ); } }]); return NumberSteps; }(_react.Component); exports['default'] = NumberSteps; module.exports = exports['default'];