UNPKG

7.11 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _reactDom = require('react-dom');
18
19var _reactDom2 = _interopRequireDefault(_reactDom);
20
21var _classnames = require('classnames');
22
23var _classnames2 = _interopRequireDefault(_classnames);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
26
27function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
28
29function _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; }
30
31function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35function _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; }
36
37function _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) : _defaults(subClass, superClass); }
38
39var propTypes = {
40 prefixCls: _propTypes2["default"].string,
41 iconPrefix: _propTypes2["default"].string,
42 labelPlacement: _propTypes2["default"].string,
43 children: _propTypes2["default"].any,
44 current: _propTypes2["default"].number,
45 status: _propTypes2["default"].oneOf(['wait', 'process', 'finish', 'error']),
46 direction: _propTypes2["default"].oneOf(['horizontal', 'vertical']),
47 size: _propTypes2["default"].oneOf(['default', 'small'])
48};
49
50var defaultProps = {
51 prefixCls: 'u-steps',
52 iconPrefix: 'u',
53 direction: 'horizontal',
54 labelPlacement: 'horizontal',
55 current: 0,
56 status: 'process',
57 size: 'default'
58};
59
60var Steps = function (_React$Component) {
61 _inherits(Steps, _React$Component);
62
63 function Steps(props) {
64 _classCallCheck(this, Steps);
65
66 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
67
68 _this.calcLastStepOffsetWidth = function () {
69 var domNode = _reactDom2["default"].findDOMNode(_this);
70 if (domNode.children.length > 0) {
71 if (_this.calcTimeout) {
72 clearTimeout(_this.calcTimeout);
73 }
74 _this.calcTimeout = setTimeout(function () {
75 // +1 for fit edge bug of digit width, like 35.4px
76 var lastStepOffsetWidth = (domNode.lastChild.offsetWidth || 0) + 1;
77 if (_this.state.lastStepOffsetWidth === lastStepOffsetWidth) {
78 return;
79 }
80 _this.setState({ lastStepOffsetWidth: lastStepOffsetWidth });
81 });
82 }
83 };
84
85 _this.state = {
86 lastStepOffsetWidth: 0
87 };
88 return _this;
89 }
90
91 Steps.prototype.componentDidMount = function componentDidMount() {
92 this.calcLastStepOffsetWidth();
93 };
94
95 Steps.prototype.componentDidUpdate = function componentDidUpdate() {
96 this.calcLastStepOffsetWidth();
97 };
98
99 Steps.prototype.componentWillUnmount = function componentWillUnmount() {
100 if (this.calcTimeout) {
101 clearTimeout(this.calcTimeout);
102 }
103 };
104
105 Steps.prototype.render = function render() {
106 var _classNames,
107 _this2 = this;
108
109 var props = this.props;
110
111 var prefixCls = props.prefixCls,
112 _props$style = props.style,
113 style = _props$style === undefined ? {} : _props$style,
114 className = props.className,
115 children = props.children,
116 direction = props.direction,
117 labelPlacement = props.labelPlacement,
118 iconPrefix = props.iconPrefix,
119 status = props.status,
120 size = props.size,
121 current = props.current,
122 restProps = _objectWithoutProperties(props, ['prefixCls', 'style', 'className', 'children', 'direction', 'labelPlacement', 'iconPrefix', 'status', 'size', 'current']);
123
124 var lastIndex = children.length - 1;
125 var reLayouted = this.state.lastStepOffsetWidth > 0;
126 var classString = (0, _classnames2["default"])((_classNames = {}, _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, prefixCls + '-' + size, size), _defineProperty(_classNames, prefixCls + '-' + direction, true), _defineProperty(_classNames, prefixCls + '-label-' + labelPlacement, direction === 'horizontal'), _defineProperty(_classNames, prefixCls + '-hidden', !reLayouted), _defineProperty(_classNames, className, className), _classNames));
127
128 return _react2["default"].createElement(
129 'div',
130 _extends({ className: classString, style: style }, restProps),
131 _react2["default"].Children.map(children, function (ele, idx) {
132 var tailWidth = direction === 'vertical' || idx === lastIndex || !reLayouted ? null : 100 / lastIndex + '%';
133 var adjustMarginRight = direction === 'vertical' || idx === lastIndex ? null : -Math.round(_this2.state.lastStepOffsetWidth / lastIndex + 1);
134 var np = {
135 stepNumber: (idx + 1).toString(),
136 stepLast: idx === lastIndex,
137 tailWidth: tailWidth,
138 adjustMarginRight: adjustMarginRight,
139 prefixCls: prefixCls,
140 iconPrefix: iconPrefix,
141 wrapperStyle: style
142 };
143
144 // fix tail color
145 if (status === 'error' && idx === current - 1) {
146 np.className = props.prefixCls + '-next-error';
147 }
148
149 if (!ele.props.status) {
150 if (idx === current) {
151 np.status = status;
152 } else if (idx < current) {
153 np.status = 'finish';
154 } else {
155 np.status = 'wait';
156 }
157 }
158 return _react2["default"].cloneElement(ele, np);
159 }, this)
160 );
161 };
162
163 return Steps;
164}(_react2["default"].Component);
165
166Steps.defaultProps = defaultProps;
167Steps.propTypes = propTypes;
168
169exports["default"] = Steps;
170module.exports = exports['default'];
\No newline at end of file