ant-design-vue/components/vc-steps/Steps.jsx

158 lines
4.7 KiB
Vue
Raw Normal View History

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: {
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: {
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
}
// 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
const lastStepOffsetWidth = (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 (
this.lastStepOffsetWidth === lastStepOffsetWidth ||
Math.abs(this.lastStepOffsetWidth - lastStepOffsetWidth) <= 3
) {
return;
2018-03-07 10:50:11 +08:00
}
2019-01-12 11:33:27 +08:00
this.setState({ lastStepOffsetWidth });
});
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,
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;
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,
2019-01-09 21:49:33 +08:00
[`${prefixCls}-flex-not-supported`]: !flexSupported,
2019-01-12 11:33:27 +08:00
};
2018-03-07 10:50:11 +08:00
const stepsProps = {
class: classString,
ref: 'vcStepsRef',
2020-01-19 16:58:38 +08:00
on: getListeners(this),
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}`,
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';
2018-03-11 12:48:04 +08:00
}
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
};