UNPKG

19.1 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
10
11var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
12
13var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
14
15var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
16
17var _inherits2 = require('babel-runtime/helpers/inherits');
18
19var _inherits3 = _interopRequireDefault(_inherits2);
20
21var _class, _temp;
22
23var _reactDom = require('react-dom');
24
25var _reactDom2 = _interopRequireDefault(_reactDom);
26
27var _react = require('react');
28
29var _react2 = _interopRequireDefault(_react);
30
31var _propTypes = require('prop-types');
32
33var _propTypes2 = _interopRequireDefault(_propTypes);
34
35var _classnames = require('classnames');
36
37var _classnames2 = _interopRequireDefault(_classnames);
38
39var _resizeObserverPolyfill = require('resize-observer-polyfill');
40
41var _resizeObserverPolyfill2 = _interopRequireDefault(_resizeObserverPolyfill);
42
43var _icon = require('../../icon');
44
45var _icon2 = _interopRequireDefault(_icon);
46
47var _progress = require('../../progress');
48
49var _progress2 = _interopRequireDefault(_progress);
50
51var _configProvider = require('../../config-provider');
52
53var _configProvider2 = _interopRequireDefault(_configProvider);
54
55var _util = require('../../util');
56
57function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
58
59/** Step.Item */
60var StepItem = (_temp = _class = function (_Component) {
61 (0, _inherits3.default)(StepItem, _Component);
62
63 function StepItem(props) {
64 (0, _classCallCheck3.default)(this, StepItem);
65
66 var _this = (0, _possibleConstructorReturn3.default)(this, _Component.call(this, props));
67
68 _this.onClick = function () {
69 var _this$props = _this.props,
70 index = _this$props.index,
71 disabled = _this$props.disabled,
72 readOnly = _this$props.readOnly,
73 animation = _this$props.animation;
74
75 if (disabled || readOnly) {
76 return false;
77 }
78
79 if (animation && _this.stepNode) {
80 _util.dom.hasClass(_this.stepNode, 'clicked') ? _util.dom.removeClass(_this.stepNode, 'clicked') : _util.dom.addClass(_this.stepNode, 'clicked');
81 }
82 _this.props.onClick(index);
83 };
84
85 _this.removeClickedCls = _this.removeClickedCls.bind(_this);
86 _this._refHandlerCreator = _this._refHandlerCreator.bind(_this);
87 _this.resize = _this.resize.bind(_this);
88 _this.ro = new _resizeObserverPolyfill2.default(function () {
89 var _this$props2 = _this.props,
90 shape = _this$props2.shape,
91 direction = _this$props2.direction,
92 onResize = _this$props2.onResize;
93
94 if (!_this.body || shape === 'arrow') {
95 return;
96 }
97 if (direction === 'vertical' || direction === 'ver') {
98 _this.resize();
99 } else {
100 onResize && onResize();
101 }
102 });
103 return _this;
104 }
105
106 StepItem.prototype.componentDidMount = function componentDidMount() {
107 var _props = this.props,
108 shape = _props.shape,
109 direction = _props.direction,
110 labelPlacement = _props.labelPlacement,
111 index = _props.index,
112 total = _props.total,
113 stretch = _props.stretch;
114
115 this.body && this.ro.observe(_reactDom2.default.findDOMNode(this.body));
116 if (shape === 'arrow') {
117 return;
118 }
119 if (direction === 'vertical' || direction === 'ver') {
120 this.resize();
121 this.forceUpdate(); // 解决Step嵌套的情况下,嵌套节点宽度为0的问题
122 this.eventHandler = _util.events.on(window, 'resize', this.resize); // 调整垂直Step
123 } else if ((direction === 'horizontal' || direction === 'hoz') && (labelPlacement === 'horizontal' || labelPlacement === 'hoz') && index !== total - 1) {
124 // 调整横向Content
125 this.adjustTail();
126 }
127 if (stretch && (direction === 'horizontal' || direction === 'hoz')) {
128 this.adjustItemWidth();
129 }
130 };
131
132 StepItem.prototype.componentDidUpdate = function componentDidUpdate() {
133 var _this2 = this;
134
135 var _props2 = this.props,
136 shape = _props2.shape,
137 direction = _props2.direction,
138 labelPlacement = _props2.labelPlacement,
139 index = _props2.index,
140 total = _props2.total,
141 rtl = _props2.rtl,
142 stretch = _props2.stretch;
143
144 if (shape === 'arrow') {
145 return;
146 }
147 var resetTailStyle = function resetTailStyle() {
148 _util.dom.setStyle(_this2.tail, {
149 width: '',
150 top: '',
151 height: ''
152 });
153 };
154
155 if (direction === 'vertical' || direction === 'ver') {
156 this.resize();
157 } else if (direction === 'horizontal' || direction === 'hoz') {
158 var _dom$setStyle;
159
160 var pos = rtl ? 'right' : 'left';
161 _util.dom.setStyle(this.body, (_dom$setStyle = {
162 width: ''
163 }, _dom$setStyle[pos] = '', _dom$setStyle));
164 if (shape === 'circle' && (labelPlacement === 'horizontal' || labelPlacement === 'hoz') && index !== total - 1) {
165 // 调整横向Content
166 this.adjustTail();
167 } else {
168 resetTailStyle();
169 }
170 if (stretch) {
171 this.adjustItemWidth();
172 }
173 } else if (index !== total - 1) {
174 resetTailStyle();
175 }
176 };
177
178 StepItem.prototype.componentWillUnmount = function componentWillUnmount() {
179 this.eventHandler && this.eventHandler.off();
180 };
181
182 StepItem.prototype.adjustItemWidth = function adjustItemWidth() {
183 var _props3 = this.props,
184 index = _props3.index,
185 total = _props3.total,
186 labelPlacement = _props3.labelPlacement;
187
188 var lastNodeWidth = labelPlacement === 'horizontal' || labelPlacement === 'hoz' ? this.container.offsetWidth + this.body.offsetWidth : this.title.offsetWidth;
189 var width = total - 1 !== index ? 'calc((100% - ' + lastNodeWidth + 'px)/' + (total - 1) + ')' : 'auto';
190 _util.dom.setStyle(this.step, {
191 width: width
192 });
193 };
194
195 StepItem.prototype.adjustTail = function adjustTail() {
196 var width = this.container.offsetWidth + this.title.offsetWidth;
197 _util.dom.setStyle(this.tail, {
198 width: 'calc(100% - ' + width + 'px)',
199 top: _util.dom.getStyle(this.container, 'height') / 2 + 'px'
200 });
201 };
202
203 StepItem.prototype.resize = function resize() {
204 var direction = this.props.direction;
205
206 if (direction === 'vertical' || direction === 'ver') {
207 var stepWidth = _util.dom.getStyle(this.step, 'width');
208 var stepHozWhitespace = _util.dom.getNodeHozWhitespace(this.step.parentNode);
209 var stepBodyHozWhitespace = _util.dom.getNodeHozWhitespace(this.body);
210 var rtl = this.props.rtl;
211
212 rtl ? this.body.style.right = stepWidth + 'px' : this.body.style.left = stepWidth + 'px';
213 _util.dom.setStyle(this.body, {
214 width: _util.dom.getStyle(this.step.parentNode.parentNode, 'width') - stepWidth - stepHozWhitespace - stepBodyHozWhitespace
215 });
216 _util.dom.setStyle(this.tail, 'height', _util.dom.getStyle(this.body, 'height') - _util.dom.getStyle(this.container, 'height'));
217 }
218 };
219
220 StepItem.prototype._getNode = function _getNode() {
221 var _props4 = this.props,
222 prefix = _props4.prefix,
223 index = _props4.index,
224 status = _props4.status,
225 icon = _props4.icon,
226 shape = _props4.shape,
227 percent = _props4.percent,
228 itemRender = _props4.itemRender;
229
230 var nodeElement = icon;
231 if (shape === 'dot') {
232 nodeElement = icon ? _react2.default.createElement(_icon2.default, { type: icon }) : _react2.default.createElement(
233 'div',
234 { className: prefix + 'step-item-node-dot' },
235 ' '
236 );
237 } else if (shape === 'circle' && percent) {
238 nodeElement = _react2.default.createElement(_progress2.default, { shape: 'circle', percent: percent, className: prefix + 'step-item-progress' });
239 } else if (shape === 'circle' && !!itemRender && typeof itemRender === 'function') {
240 nodeElement = null; // 如果是需要自定义节点,则不处理,返回空
241 } else {
242 nodeElement = _react2.default.createElement(
243 'div',
244 { className: prefix + 'step-item-node-circle' },
245 icon ? _react2.default.createElement(_icon2.default, { type: icon }) : this._itemRender(index, status)
246 );
247 }
248
249 return nodeElement;
250 };
251
252 StepItem.prototype.getNode = function getNode(args) {
253 var _props5 = this.props,
254 prefix = _props5.prefix,
255 itemRender = _props5.itemRender,
256 index = _props5.index,
257 status = _props5.status,
258 title = _props5.title,
259 content = _props5.content,
260 shape = _props5.shape;
261 var others = args.others,
262 stepCls = args.stepCls,
263 overlayCls = args.overlayCls;
264
265 var nodeElement = this._getNode();
266 var containerStyle = shape === 'dot' && { fontSize: 'initial' } || {};
267 var finalNodeElement = _react2.default.createElement(
268 'div',
269 {
270 className: prefix + 'step-item-container',
271 style: containerStyle,
272 ref: this._refHandlerCreator('container')
273 },
274 _react2.default.createElement(
275 'div',
276 { className: prefix + 'step-item-node-placeholder', onClick: this.onClick },
277 _react2.default.createElement(
278 'div',
279 {
280 className: prefix + 'step-item-node',
281 ref: this._refHandlerCreator('stepNode'),
282 onTransitionEnd: this.removeClickedCls
283 },
284 nodeElement
285 )
286 )
287 );
288
289 if (!nodeElement) {
290 // 需要自定义子节点
291 finalNodeElement = _react2.default.createElement(
292 'div',
293 {
294 className: prefix + 'step-item-container',
295 style: containerStyle,
296 ref: this._refHandlerCreator('container')
297 },
298 _react2.default.createElement(
299 'div',
300 { className: prefix + 'step-item-node-placeholder', onClick: this.onClick },
301 itemRender(index, status, title, content)
302 )
303 );
304 }
305 if (shape !== 'arrow') {
306 delete others.tabIndex;
307 delete others['aria-current'];
308 }
309
310 return _react2.default.createElement(
311 'li',
312 (0, _extends3.default)({}, others, { style: this.getStyle(), className: stepCls, ref: this._refHandlerCreator('step') }),
313 finalNodeElement,
314 _react2.default.createElement(
315 'div',
316 {
317 className: prefix + 'step-item-body',
318 ref: this._refHandlerCreator('body'),
319 tabIndex: this.props.tabIndex,
320 'aria-current': this.props['aria-current']
321 },
322 _react2.default.createElement(
323 'div',
324 { className: prefix + 'step-item-title', ref: this._refHandlerCreator('title') },
325 title
326 ),
327 _react2.default.createElement(
328 'div',
329 { className: prefix + 'step-item-content' },
330 content
331 )
332 ),
333 _react2.default.createElement(
334 'div',
335 { className: prefix + 'step-item-tail', ref: this._refHandlerCreator('tail') },
336 _react2.default.createElement(
337 'div',
338 { className: prefix + 'step-item-tail-underlay' },
339 _react2.default.createElement('div', { className: prefix + 'step-item-tail-overlay', style: overlayCls })
340 )
341 )
342 );
343 };
344
345 StepItem.prototype.getStyle = function getStyle() {
346 var _props6 = this.props,
347 parentWidth = _props6.parentWidth,
348 parentHeight = _props6.parentHeight,
349 direction = _props6.direction,
350 total = _props6.total,
351 index = _props6.index,
352 shape = _props6.shape;
353
354 var width = 'auto';
355
356 if (Number(parentWidth) && Number(parentHeight)) {
357 if (!_util.support.flex && shape === 'arrow') {
358 width = Math.floor(parentWidth / total - parentHeight / 2 - parentHeight / 8);
359 }
360 }
361 if (shape !== 'arrow' && (direction === 'horizontal' || direction === 'hoz')) {
362 width = total - 1 !== index ? Math.floor(100 / total) + '%' : 'auto';
363 }
364 return {
365 width: width
366 };
367 };
368
369 StepItem.prototype.removeClickedCls = function removeClickedCls() {
370 var animation = this.props.animation;
371
372 if (animation && this.stepNode && _util.dom.hasClass(this.stepNode, 'clicked')) {
373 _util.dom.removeClass(this.stepNode, 'clicked');
374 }
375 };
376
377 // 节点的渲染方法
378
379
380 StepItem.prototype._itemRender = function _itemRender(index, status) {
381 var itemRender = this.props.itemRender;
382
383 if (itemRender) {
384 return itemRender(index, status);
385 }
386 return status === 'finish' ? _react2.default.createElement(_icon2.default, { type: 'select' }) : index + 1;
387 };
388
389 StepItem.prototype._refHandlerCreator = function _refHandlerCreator(refName) {
390 var self = this;
391 return function (ref) {
392 self[refName] = ref;
393 };
394 };
395
396 StepItem.prototype.render = function render() {
397 var _classNames;
398
399 // eslint-disable-next-line
400 var _props7 = this.props,
401 prefix = _props7.prefix,
402 locale = _props7.locale,
403 className = _props7.className,
404 status = _props7.status,
405 title = _props7.title,
406 icon = _props7.icon,
407 index = _props7.index,
408 total = _props7.total,
409 shape = _props7.shape,
410 content = _props7.content,
411 direction = _props7.direction,
412 disabled = _props7.disabled,
413 onClick = _props7.onClick,
414 readOnly = _props7.readOnly,
415 animation = _props7.animation,
416 parentHeight = _props7.parentHeight,
417 itemRender = _props7.itemRender,
418 parentWidth = _props7.parentWidth,
419 labelPlacement = _props7.labelPlacement,
420 rtl = _props7.rtl,
421 onResize = _props7.onResize;
422
423
424 var others = _util.obj.pickOthers(StepItem.propTypes, this.props);
425
426 var stepCls = (0, _classnames2.default)((_classNames = {}, _classNames[prefix + 'step-item'] = true, _classNames[prefix + 'step-item-' + status] = status, _classNames[prefix + 'step-item-first'] = index === 0, _classNames[prefix + 'step-item-last'] = index === total - 1, _classNames[prefix + 'step-item-disabled'] = disabled, _classNames[prefix + 'step-item-read-only'] = readOnly, _classNames[className] = className, _classNames));
427
428 var overlayCls = status === 'finish' ? { width: '100%' } : null;
429 var arrowElement = _react2.default.createElement(
430 'li',
431 (0, _extends3.default)({}, others, { style: this.getStyle(), className: stepCls, onClick: this.onClick }),
432 _react2.default.createElement(
433 'div',
434 { className: prefix + 'step-item-container' },
435 _react2.default.createElement(
436 'div',
437 { className: prefix + 'step-item-title' },
438 title
439 )
440 )
441 );
442 var otherElement = this.getNode({ others: others, stepCls: stepCls, overlayCls: overlayCls });
443
444 return shape === 'arrow' ? arrowElement : otherElement;
445 };
446
447 return StepItem;
448}(_react.Component), _class.propTypes = (0, _extends3.default)({}, _configProvider2.default.propTypes, {
449 prefix: _propTypes2.default.string,
450 rtl: _propTypes2.default.bool,
451 /**
452 * 步骤的状态,如不传,会根据外层的 Step 的 current 属性生成,可选值为 `wait`, `process`, `finish`
453 */
454 status: _propTypes2.default.oneOf(['wait', 'process', 'finish']),
455 /**
456 * 标题
457 */
458 title: _propTypes2.default.node,
459 direction: _propTypes2.default.oneOf(['hoz', 'ver']),
460 labelPlacement: _propTypes2.default.oneOf(['hoz', 'ver']),
461 shape: _propTypes2.default.oneOf(['circle', 'arrow', 'dot']),
462 /**
463 * 图标
464 */
465 icon: _propTypes2.default.string,
466 /**
467 * 内容填充, shape为 arrow 时无效
468 */
469 content: _propTypes2.default.node,
470 /**
471 * StepItem 的自定义渲染, 会覆盖父节点设置的itemRender
472 * @param {Number} index 节点索引
473 * @param {String} status 节点状态
474 * @returns {Node} 节点的渲染结果
475 */
476 itemRender: _propTypes2.default.func,
477 /**
478 * 百分比
479 */
480 percent: _propTypes2.default.number,
481 index: _propTypes2.default.number,
482 total: _propTypes2.default.number,
483 animation: _propTypes2.default.bool, // 是否开启动效,由父级传入
484 /**
485 * 是否禁用
486 */
487 disabled: _propTypes2.default.bool,
488 parentWidth: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
489 parentHeight: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
490 /**
491 * 点击步骤时的回调
492 * @param {Number} index 节点索引
493 */
494 onClick: _propTypes2.default.func,
495 /**
496 * 自定义样式
497 */
498 className: _propTypes2.default.string,
499 readOnly: _propTypes2.default.bool,
500 onResize: _propTypes2.default.func,
501 stretch: _propTypes2.default.bool
502}), _class.defaultProps = {
503 shape: 'circle',
504 index: 0,
505 total: 1,
506 onClick: function onClick() {},
507 stretch: false
508}, _temp);
509StepItem.displayName = 'StepItem';
510exports.default = _configProvider2.default.config(StepItem);
511module.exports = exports['default'];
\No newline at end of file