You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/vc-steps/Steps.jsx

158 lines
4.6 KiB

import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import debounce from 'lodash/debounce';
import isFlexSupported from '../_util/isFlexSupported';
import { filterEmpty, getEvents, getPropsData } from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
7 years ago
export default {
name: 'Steps',
mixins: [BaseMixin],
props: {
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(''),
progressDot: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
initial: PropTypes.number.def(0),
7 years ago
current: PropTypes.number.def(0),
icons: PropTypes.shape({
finish: PropTypes.any,
error: PropTypes.any,
}).loose,
7 years ago
},
data() {
this.calcStepOffsetWidth = debounce(this.calcStepOffsetWidth, 150);
7 years ago
return {
flexSupported: true,
lastStepOffsetWidth: 0,
};
7 years ago
},
mounted() {
7 years ago
this.$nextTick(() => {
this.calcStepOffsetWidth();
7 years ago
if (!isFlexSupported()) {
this.setState({
flexSupported: false,
});
7 years ago
}
});
7 years ago
},
updated() {
7 years ago
this.$nextTick(() => {
this.calcStepOffsetWidth();
});
7 years ago
},
beforeDestroy() {
7 years ago
if (this.calcTimeout) {
clearTimeout(this.calcTimeout);
7 years ago
}
if (this.calcStepOffsetWidth && this.calcStepOffsetWidth.cancel) {
this.calcStepOffsetWidth.cancel();
7 years ago
}
},
methods: {
calcStepOffsetWidth() {
7 years ago
if (isFlexSupported()) {
return;
7 years ago
}
// Just for IE9
const domNode = this.$refs.vcStepsRef;
7 years ago
if (domNode.children.length > 0) {
if (this.calcTimeout) {
clearTimeout(this.calcTimeout);
7 years ago
}
this.calcTimeout = setTimeout(() => {
// +1 for fit edge bug of digit width, like 35.4px
const lastStepOffsetWidth = (domNode.lastChild.offsetWidth || 0) + 1;
7 years ago
// Reduce shake bug
if (
this.lastStepOffsetWidth === lastStepOffsetWidth ||
Math.abs(this.lastStepOffsetWidth - lastStepOffsetWidth) <= 3
) {
return;
7 years ago
}
this.setState({ lastStepOffsetWidth });
});
7 years ago
}
},
},
render() {
7 years ago
const {
prefixCls,
direction,
labelPlacement,
iconPrefix,
status,
size,
current,
$scopedSlots,
initial,
icons,
} = this;
let progressDot = this.progressDot;
7 years ago
if (progressDot === undefined) {
progressDot = $scopedSlots.progressDot;
7 years ago
}
const { lastStepOffsetWidth, flexSupported } = this;
const filteredChildren = filterEmpty(this.$slots.default);
const lastIndex = filteredChildren.length - 1;
const adjustedlabelPlacement = progressDot ? 'vertical' : labelPlacement;
7 years ago
const classString = {
[prefixCls]: true,
[`${prefixCls}-${direction}`]: true,
[`${prefixCls}-${size}`]: size,
[`${prefixCls}-label-${adjustedlabelPlacement}`]: direction === 'horizontal',
[`${prefixCls}-dot`]: !!progressDot,
[`${prefixCls}-flex-not-supported`]: !flexSupported,
};
7 years ago
const stepsProps = {
class: classString,
ref: 'vcStepsRef',
on: this.$listeners,
};
7 years ago
return (
<div {...stepsProps}>
{filteredChildren.map((child, index) => {
const childProps = getPropsData(child);
const stepNumber = initial + index;
const stepProps = {
props: {
stepNumber: `${stepNumber + 1}`,
prefixCls,
iconPrefix,
progressDot: this.progressDot,
icons,
...childProps,
},
on: getEvents(child),
scopedSlots: $scopedSlots,
};
if (!flexSupported && direction !== 'vertical' && index !== lastIndex) {
stepProps.props.itemWidth = `${100 / lastIndex}%`;
stepProps.props.adjustMarginRight = `${-Math.round(
lastStepOffsetWidth / lastIndex + 1,
)}px`;
}
// 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';
7 years ago
}
}
return cloneElement(child, stepProps);
})}
7 years ago
</div>
);
7 years ago
},
};