2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import debounce from 'lodash/debounce';
|
|
|
|
import isFlexSupported from '../_util/isFlexSupported';
|
2020-01-19 16:58:38 +08:00
|
|
|
import { filterEmpty, getEvents, getPropsData, getListeners } from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
import { cloneElement } from '../_util/vnode';
|
2018-03-07 10:50:11 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Steps',
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: {
|
2020-03-07 19:45:13 +08:00
|
|
|
type: PropTypes.string.def('default'),
|
2018-03-07 10:50:11 +08:00
|
|
|
prefixCls: PropTypes.string.def('rc-steps'),
|
|
|
|
iconPrefix: PropTypes.string.def('rc'),
|
|
|
|
direction: PropTypes.string.def('horizontal'),
|
|
|
|
labelPlacement: PropTypes.string.def('horizontal'),
|
|
|
|
status: PropTypes.string.def('process'),
|
|
|
|
size: PropTypes.string.def(''),
|
2019-01-12 11:33:27 +08:00
|
|
|
progressDot: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
|
2018-11-09 17:51:56 +08:00
|
|
|
initial: PropTypes.number.def(0),
|
2018-03-07 10:50:11 +08:00
|
|
|
current: PropTypes.number.def(0),
|
2018-11-09 17:51:56 +08:00
|
|
|
icons: PropTypes.shape({
|
|
|
|
finish: PropTypes.any,
|
|
|
|
error: PropTypes.any,
|
|
|
|
}).loose,
|
2018-03-07 10:50:11 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
this.calcStepOffsetWidth = debounce(this.calcStepOffsetWidth, 150);
|
2018-03-07 10:50:11 +08:00
|
|
|
return {
|
|
|
|
flexSupported: true,
|
|
|
|
lastStepOffsetWidth: 0,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-07 10:50:11 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
2018-03-11 12:48:04 +08:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.calcStepOffsetWidth();
|
2018-03-11 12:48:04 +08:00
|
|
|
if (!isFlexSupported()) {
|
|
|
|
this.setState({
|
|
|
|
flexSupported: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-11 12:48:04 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-07 10:50:11 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2018-03-11 12:48:04 +08:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.calcStepOffsetWidth();
|
|
|
|
});
|
2018-03-07 10:50:11 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
2018-03-07 10:50:11 +08:00
|
|
|
if (this.calcTimeout) {
|
2019-01-12 11:33:27 +08:00
|
|
|
clearTimeout(this.calcTimeout);
|
2018-03-07 10:50:11 +08:00
|
|
|
}
|
|
|
|
if (this.calcStepOffsetWidth && this.calcStepOffsetWidth.cancel) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.calcStepOffsetWidth.cancel();
|
2018-03-07 10:50:11 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2020-03-07 19:45:13 +08:00
|
|
|
onStepClick(next) {
|
|
|
|
const { current } = this.$props;
|
|
|
|
if (current !== next) {
|
|
|
|
this.$emit('change', next);
|
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
calcStepOffsetWidth() {
|
2018-03-07 10:50:11 +08:00
|
|
|
if (isFlexSupported()) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-07 10:50:11 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
const { lastStepOffsetWidth } = this.$data;
|
2018-03-07 10:50:11 +08:00
|
|
|
// Just for IE9
|
2019-01-12 11:33:27 +08:00
|
|
|
const domNode = this.$refs.vcStepsRef;
|
2018-03-07 10:50:11 +08:00
|
|
|
if (domNode.children.length > 0) {
|
|
|
|
if (this.calcTimeout) {
|
2019-01-12 11:33:27 +08:00
|
|
|
clearTimeout(this.calcTimeout);
|
2018-03-07 10:50:11 +08:00
|
|
|
}
|
|
|
|
this.calcTimeout = setTimeout(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
// +1 for fit edge bug of digit width, like 35.4px
|
2020-03-07 19:45:13 +08:00
|
|
|
const offsetWidth = (domNode.lastChild.offsetWidth || 0) + 1;
|
2018-03-07 10:50:11 +08:00
|
|
|
// Reduce shake bug
|
2019-01-12 11:33:27 +08:00
|
|
|
if (
|
2020-03-07 19:45:13 +08:00
|
|
|
lastStepOffsetWidth === offsetWidth ||
|
|
|
|
Math.abs(lastStepOffsetWidth - offsetWidth) <= 3
|
2019-01-12 11:33:27 +08:00
|
|
|
) {
|
|
|
|
return;
|
2018-03-07 10:50:11 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
this.setState({ lastStepOffsetWidth: offsetWidth });
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-07 10:50:11 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2018-03-07 10:50:11 +08:00
|
|
|
const {
|
2019-01-12 11:33:27 +08:00
|
|
|
prefixCls,
|
|
|
|
direction,
|
2020-03-07 19:45:13 +08:00
|
|
|
type,
|
2019-01-12 11:33:27 +08:00
|
|
|
labelPlacement,
|
|
|
|
iconPrefix,
|
|
|
|
status,
|
|
|
|
size,
|
|
|
|
current,
|
|
|
|
$scopedSlots,
|
|
|
|
initial,
|
2018-11-09 17:51:56 +08:00
|
|
|
icons,
|
2019-01-12 11:33:27 +08:00
|
|
|
} = this;
|
2020-03-07 19:45:13 +08:00
|
|
|
const isNav = type === 'navigation';
|
2019-01-12 11:33:27 +08:00
|
|
|
let progressDot = this.progressDot;
|
2018-03-13 10:19:00 +08:00
|
|
|
if (progressDot === undefined) {
|
2019-01-12 11:33:27 +08:00
|
|
|
progressDot = $scopedSlots.progressDot;
|
2018-03-13 10:19:00 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const { lastStepOffsetWidth, flexSupported } = this;
|
|
|
|
const filteredChildren = filterEmpty(this.$slots.default);
|
|
|
|
const lastIndex = filteredChildren.length - 1;
|
|
|
|
const adjustedlabelPlacement = progressDot ? 'vertical' : labelPlacement;
|
2018-03-07 10:50:11 +08:00
|
|
|
const classString = {
|
|
|
|
[prefixCls]: true,
|
|
|
|
[`${prefixCls}-${direction}`]: true,
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
|
|
|
[`${prefixCls}-label-${adjustedlabelPlacement}`]: direction === 'horizontal',
|
|
|
|
[`${prefixCls}-dot`]: !!progressDot,
|
2020-03-07 19:45:13 +08:00
|
|
|
[`${prefixCls}-navigation`]: isNav,
|
2019-01-09 21:49:33 +08:00
|
|
|
[`${prefixCls}-flex-not-supported`]: !flexSupported,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
const listeners = getListeners(this);
|
2018-03-07 10:50:11 +08:00
|
|
|
const stepsProps = {
|
|
|
|
class: classString,
|
|
|
|
ref: 'vcStepsRef',
|
2020-03-07 19:45:13 +08:00
|
|
|
on: listeners,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-07 10:50:11 +08:00
|
|
|
return (
|
|
|
|
<div {...stepsProps}>
|
2019-01-12 11:33:27 +08:00
|
|
|
{filteredChildren.map((child, index) => {
|
|
|
|
const childProps = getPropsData(child);
|
|
|
|
const stepNumber = initial + index;
|
|
|
|
const stepProps = {
|
|
|
|
props: {
|
|
|
|
stepNumber: `${stepNumber + 1}`,
|
2020-03-07 19:45:13 +08:00
|
|
|
stepIndex: stepNumber,
|
2019-01-12 11:33:27 +08:00
|
|
|
prefixCls,
|
|
|
|
iconPrefix,
|
|
|
|
progressDot: this.progressDot,
|
|
|
|
icons,
|
|
|
|
...childProps,
|
|
|
|
},
|
|
|
|
on: getEvents(child),
|
|
|
|
scopedSlots: $scopedSlots,
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
if (listeners.change) {
|
|
|
|
stepProps.on.stepClick = this.onStepClick;
|
|
|
|
}
|
|
|
|
if (!flexSupported && direction !== 'vertical') {
|
|
|
|
if (isNav) {
|
|
|
|
stepProps.props.itemWidth = `${100 / (lastIndex + 1)}%`;
|
|
|
|
stepProps.props.adjustMarginRight = 0;
|
|
|
|
} else if (index !== lastIndex) {
|
|
|
|
stepProps.props.itemWidth = `${100 / lastIndex}%`;
|
|
|
|
stepProps.props.adjustMarginRight = `${-Math.round(
|
|
|
|
lastStepOffsetWidth / lastIndex + 1,
|
|
|
|
)}px`;
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
}
|
|
|
|
// fix tail color
|
|
|
|
if (status === 'error' && index === current - 1) {
|
|
|
|
stepProps.class = `${prefixCls}-next-error`;
|
|
|
|
}
|
|
|
|
if (!childProps.status) {
|
|
|
|
if (stepNumber === current) {
|
|
|
|
stepProps.props.status = status;
|
|
|
|
} else if (stepNumber < current) {
|
|
|
|
stepProps.props.status = 'finish';
|
|
|
|
} else {
|
|
|
|
stepProps.props.status = 'wait';
|
2018-03-11 12:48:04 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
stepProps.props.active = stepNumber === current;
|
2019-01-12 11:33:27 +08:00
|
|
|
return cloneElement(child, stepProps);
|
|
|
|
})}
|
2018-03-07 10:50:11 +08:00
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-07 10:50:11 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|