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.
149 lines
4.3 KiB
149 lines
4.3 KiB
7 years ago
|
|
||
7 years ago
|
import PropTypes from '../_util/vue-types'
|
||
|
import BaseMixin from '../_util/BaseMixin'
|
||
|
import debounce from 'lodash/debounce'
|
||
|
import isFlexSupported from '../_util/isFlexSupported'
|
||
7 years ago
|
import {
|
||
|
filterEmpty,
|
||
|
getEvents,
|
||
|
getPropsData,
|
||
|
} from '../_util/props-util'
|
||
7 years ago
|
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,
|
||
7 years ago
|
]),
|
||
7 years ago
|
current: PropTypes.number.def(0),
|
||
|
},
|
||
|
data () {
|
||
|
this.calcStepOffsetWidth = debounce(this.calcStepOffsetWidth, 150)
|
||
|
return {
|
||
|
flexSupported: true,
|
||
|
lastStepOffsetWidth: 0,
|
||
|
}
|
||
|
},
|
||
|
mounted () {
|
||
7 years ago
|
this.$nextTick(() => {
|
||
|
this.calcStepOffsetWidth()
|
||
|
if (!isFlexSupported()) {
|
||
|
this.setState({
|
||
|
flexSupported: false,
|
||
|
})
|
||
|
}
|
||
|
})
|
||
7 years ago
|
},
|
||
|
updated () {
|
||
7 years ago
|
this.$nextTick(() => {
|
||
|
this.calcStepOffsetWidth()
|
||
|
})
|
||
7 years ago
|
},
|
||
|
beforeDestroy () {
|
||
|
if (this.calcTimeout) {
|
||
|
clearTimeout(this.calcTimeout)
|
||
|
}
|
||
|
if (this.calcStepOffsetWidth && this.calcStepOffsetWidth.cancel) {
|
||
|
this.calcStepOffsetWidth.cancel()
|
||
|
}
|
||
|
},
|
||
|
methods: {
|
||
|
calcStepOffsetWidth () {
|
||
|
if (isFlexSupported()) {
|
||
|
return
|
||
|
}
|
||
|
// Just for IE9
|
||
|
const domNode = this.$refs.vcStepsRef
|
||
|
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 lastStepOffsetWidth = (domNode.lastChild.offsetWidth || 0) + 1
|
||
|
// Reduce shake bug
|
||
7 years ago
|
if (this.lastStepOffsetWidth === lastStepOffsetWidth ||
|
||
|
Math.abs(this.lastStepOffsetWidth - lastStepOffsetWidth) <= 3) {
|
||
7 years ago
|
return
|
||
|
}
|
||
|
this.setState({ lastStepOffsetWidth })
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
},
|
||
|
render () {
|
||
|
const {
|
||
|
prefixCls, direction,
|
||
7 years ago
|
labelPlacement, iconPrefix, status, size, current, $scopedSlots,
|
||
|
} = this
|
||
|
let progressDot = this.progressDot
|
||
|
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
|
||
|
const classString = {
|
||
|
[prefixCls]: true,
|
||
|
[`${prefixCls}-${direction}`]: true,
|
||
|
[`${prefixCls}-${size}`]: size,
|
||
|
[`${prefixCls}-label-${adjustedlabelPlacement}`]: direction === 'horizontal',
|
||
|
[`${prefixCls}-dot`]: !!progressDot,
|
||
|
}
|
||
|
const stepsProps = {
|
||
|
class: classString,
|
||
|
ref: 'vcStepsRef',
|
||
|
on: this.$listeners,
|
||
|
}
|
||
|
return (
|
||
|
<div {...stepsProps}>
|
||
|
{
|
||
|
filteredChildren.map((child, index) => {
|
||
7 years ago
|
const childProps = getPropsData(child)
|
||
7 years ago
|
const stepProps = {
|
||
|
props: {
|
||
|
stepNumber: `${index + 1}`,
|
||
|
prefixCls,
|
||
|
iconPrefix,
|
||
7 years ago
|
progressDot: this.progressDot,
|
||
7 years ago
|
...childProps,
|
||
7 years ago
|
},
|
||
|
on: getEvents(child),
|
||
7 years ago
|
scopedSlots: $scopedSlots,
|
||
7 years ago
|
}
|
||
7 years ago
|
if (!flexSupported && direction !== 'vertical' && index !== lastIndex) {
|
||
|
stepProps.props.itemWidth = `${100 / lastIndex}%`
|
||
|
stepProps.props.adjustMarginRight = -Math.round(lastStepOffsetWidth / lastIndex + 1)
|
||
|
}
|
||
|
// fix tail color
|
||
|
if (status === 'error' && index === current - 1) {
|
||
|
stepProps.class = `${prefixCls}-next-error`
|
||
|
}
|
||
|
if (!childProps.status) {
|
||
|
if (index === current) {
|
||
|
stepProps.props.status = status
|
||
|
} else if (index < current) {
|
||
|
stepProps.props.status = 'finish'
|
||
|
} else {
|
||
|
stepProps.props.status = 'wait'
|
||
|
}
|
||
|
}
|
||
|
return cloneElement(child, stepProps)
|
||
7 years ago
|
})
|
||
|
}
|
||
|
</div>
|
||
|
)
|
||
|
},
|
||
|
}
|
||
7 years ago
|
|