-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathSteps.jsx
More file actions
166 lines (157 loc) · 4.66 KB
/
Steps.jsx
File metadata and controls
166 lines (157 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* eslint react/no-did-mount-set-state: 0, react/prop-types: 0 */
import React, { cloneElement, Children, Component } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import debounce from 'lodash/debounce';
import { isFlexSupported } from './utils';
export default class Steps extends Component {
static defaultProps = {
type: 'default',
prefixCls: 'rc-steps',
iconPrefix: 'rc',
direction: 'horizontal',
labelPlacement: 'horizontal',
initial: 0,
current: 0,
status: 'process',
size: '',
progressDot: false,
};
constructor(props) {
super(props);
this.state = {
flexSupported: true,
lastStepOffsetWidth: 0,
};
this.calcStepOffsetWidth = debounce(this.calcStepOffsetWidth, 150);
}
componentDidMount() {
this.calcStepOffsetWidth();
if (!isFlexSupported()) {
this.setState({
flexSupported: false,
});
}
}
componentDidUpdate() {
this.calcStepOffsetWidth();
}
componentWillUnmount() {
if (this.calcTimeout) {
clearTimeout(this.calcTimeout);
}
if (this.calcStepOffsetWidth && this.calcStepOffsetWidth.cancel) {
this.calcStepOffsetWidth.cancel();
}
}
onStepClick = next => {
const { onChange, current } = this.props;
if (onChange && current !== next) {
onChange(next);
}
};
calcStepOffsetWidth = () => {
if (isFlexSupported()) {
return;
}
const { lastStepOffsetWidth } = this.state;
// Just for IE9
const domNode = findDOMNode(this);
if (domNode.children.length > 0) {
if (this.calcTimeout) {
clearTimeout(this.calcTimeout);
}
this.calcTimeout = setTimeout(() => {
// +1 for fit edge bug of digit width, like 35.4px
const offsetWidth = (domNode.lastChild.offsetWidth || 0) + 1;
// Reduce shake bug
if (
lastStepOffsetWidth === offsetWidth ||
Math.abs(lastStepOffsetWidth - offsetWidth) <= 3
) {
return;
}
this.setState({ lastStepOffsetWidth: offsetWidth });
});
}
};
render() {
const {
prefixCls,
style = {},
className,
children,
direction,
type,
labelPlacement,
iconPrefix,
status,
size,
current,
progressDot,
initial,
icons,
onChange,
showNumber,
...restProps
} = this.props;
const isNav = type === 'navigation';
const { lastStepOffsetWidth, flexSupported } = this.state;
const filteredChildren = React.Children.toArray(children).filter(c => !!c);
const lastIndex = filteredChildren.length - 1;
const adjustedlabelPlacement = progressDot ? 'vertical' : labelPlacement;
const classString = classNames(prefixCls, `${prefixCls}-${direction}`, className, {
[`${prefixCls}-${size}`]: size,
[`${prefixCls}-label-${adjustedlabelPlacement}`]: direction === 'horizontal',
[`${prefixCls}-dot`]: !!progressDot,
[`${prefixCls}-navigation`]: isNav,
[`${prefixCls}-flex-not-supported`]: !flexSupported,
});
return (
<div className={classString} style={style} {...restProps}>
{Children.map(filteredChildren, (child, index) => {
if (!child) {
return null;
}
const stepNumber = initial + index;
const childProps = {
stepNumber: `${stepNumber + 1}`,
stepIndex: stepNumber,
prefixCls,
iconPrefix,
wrapperStyle: style,
progressDot,
icons,
showNumber,
onStepClick: onChange && this.onStepClick,
...child.props,
};
if (!flexSupported && direction !== 'vertical') {
if (isNav) {
childProps.itemWidth = `${100 / (lastIndex + 1)}%`;
childProps.adjustMarginRight = 0;
} else if (index !== lastIndex) {
childProps.itemWidth = `${100 / lastIndex}%`;
childProps.adjustMarginRight = -Math.round(lastStepOffsetWidth / lastIndex + 1);
}
}
// fix tail color
if (status === 'error' && index === current - 1) {
childProps.className = `${prefixCls}-next-error`;
}
if (!child.props.status) {
if (stepNumber === current) {
childProps.status = status;
} else if (stepNumber < current) {
childProps.status = 'finish';
} else {
childProps.status = 'wait';
}
}
childProps.active = stepNumber === current;
return cloneElement(child, childProps);
})}
</div>
);
}
}