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

158 lines
4.6 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
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';
2018-03-07 02:50:11 +00: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 03:33:27 +00:00
progressDot: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
2018-11-09 09:51:56 +00:00
initial: PropTypes.number.def(0),
2018-03-07 02:50:11 +00:00
current: PropTypes.number.def(0),
2018-11-09 09:51:56 +00:00
icons: PropTypes.shape({
finish: PropTypes.any,
error: PropTypes.any,
}).loose,
2018-03-07 02:50:11 +00:00
},
2019-01-12 03:33:27 +00:00
data() {
this.calcStepOffsetWidth = debounce(this.calcStepOffsetWidth, 150);
2018-03-07 02:50:11 +00:00
return {
flexSupported: true,
lastStepOffsetWidth: 0,
2019-01-12 03:33:27 +00:00
};
2018-03-07 02:50:11 +00:00
},
2019-01-12 03:33:27 +00:00
mounted() {
2018-03-11 04:48:04 +00:00
this.$nextTick(() => {
2019-01-12 03:33:27 +00:00
this.calcStepOffsetWidth();
2018-03-11 04:48:04 +00:00
if (!isFlexSupported()) {
this.setState({
flexSupported: false,
2019-01-12 03:33:27 +00:00
});
2018-03-11 04:48:04 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-03-07 02:50:11 +00:00
},
2019-01-12 03:33:27 +00:00
updated() {
2018-03-11 04:48:04 +00:00
this.$nextTick(() => {
2019-01-12 03:33:27 +00:00
this.calcStepOffsetWidth();
});
2018-03-07 02:50:11 +00:00
},
2019-01-12 03:33:27 +00:00
beforeDestroy() {
2018-03-07 02:50:11 +00:00
if (this.calcTimeout) {
2019-01-12 03:33:27 +00:00
clearTimeout(this.calcTimeout);
2018-03-07 02:50:11 +00:00
}
if (this.calcStepOffsetWidth && this.calcStepOffsetWidth.cancel) {
2019-01-12 03:33:27 +00:00
this.calcStepOffsetWidth.cancel();
2018-03-07 02:50:11 +00:00
}
},
methods: {
2019-01-12 03:33:27 +00:00
calcStepOffsetWidth() {
2018-03-07 02:50:11 +00:00
if (isFlexSupported()) {
2019-01-12 03:33:27 +00:00
return;
2018-03-07 02:50:11 +00:00
}
// Just for IE9
2019-01-12 03:33:27 +00:00
const domNode = this.$refs.vcStepsRef;
2018-03-07 02:50:11 +00:00
if (domNode.children.length > 0) {
if (this.calcTimeout) {
2019-01-12 03:33:27 +00:00
clearTimeout(this.calcTimeout);
2018-03-07 02:50:11 +00:00
}
this.calcTimeout = setTimeout(() => {
2019-01-12 03:33:27 +00:00
// +1 for fit edge bug of digit width, like 35.4px
const lastStepOffsetWidth = (domNode.lastChild.offsetWidth || 0) + 1;
2018-03-07 02:50:11 +00:00
// Reduce shake bug
2019-01-12 03:33:27 +00:00
if (
this.lastStepOffsetWidth === lastStepOffsetWidth ||
Math.abs(this.lastStepOffsetWidth - lastStepOffsetWidth) <= 3
) {
return;
2018-03-07 02:50:11 +00:00
}
2019-01-12 03:33:27 +00:00
this.setState({ lastStepOffsetWidth });
});
2018-03-07 02:50:11 +00:00
}
},
},
2019-01-12 03:33:27 +00:00
render() {
2018-03-07 02:50:11 +00:00
const {
2019-01-12 03:33:27 +00:00
prefixCls,
direction,
labelPlacement,
iconPrefix,
status,
size,
current,
$scopedSlots,
initial,
2018-11-09 09:51:56 +00:00
icons,
2019-01-12 03:33:27 +00:00
} = this;
let progressDot = this.progressDot;
2018-03-13 02:19:00 +00:00
if (progressDot === undefined) {
2019-01-12 03:33:27 +00:00
progressDot = $scopedSlots.progressDot;
2018-03-13 02:19:00 +00:00
}
2019-01-12 03:33:27 +00:00
const { lastStepOffsetWidth, flexSupported } = this;
const filteredChildren = filterEmpty(this.$slots.default);
const lastIndex = filteredChildren.length - 1;
const adjustedlabelPlacement = progressDot ? 'vertical' : labelPlacement;
2018-03-07 02:50:11 +00:00
const classString = {
[prefixCls]: true,
[`${prefixCls}-${direction}`]: true,
[`${prefixCls}-${size}`]: size,
[`${prefixCls}-label-${adjustedlabelPlacement}`]: direction === 'horizontal',
[`${prefixCls}-dot`]: !!progressDot,
2019-01-09 13:49:33 +00:00
[`${prefixCls}-flex-not-supported`]: !flexSupported,
2019-01-12 03:33:27 +00:00
};
2018-03-07 02:50:11 +00:00
const stepsProps = {
class: classString,
ref: 'vcStepsRef',
on: this.$listeners,
2019-01-12 03:33:27 +00:00
};
2018-03-07 02:50:11 +00:00
return (
<div {...stepsProps}>
2019-01-12 03:33:27 +00: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 04:48:04 +00:00
}
2019-01-12 03:33:27 +00:00
}
return cloneElement(child, stepProps);
})}
2018-03-07 02:50:11 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-03-07 02:50:11 +00:00
},
2019-01-12 03:33:27 +00:00
};