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

158 lines
4.6 KiB
Vue
Raw Normal View History

2018-03-19 10:16:27 +08:00
2018-03-07 10:50:11 +08:00
import PropTypes from '../_util/vue-types'
import BaseMixin from '../_util/BaseMixin'
import debounce from 'lodash/debounce'
import isFlexSupported from '../_util/isFlexSupported'
2018-03-09 18:52:31 +08:00
import {
filterEmpty,
getEvents,
getPropsData,
} from '../_util/props-util'
2018-03-11 12:48:04 +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(''),
progressDot: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.func,
2018-03-13 10:19:00 +08:00
]),
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
},
data () {
this.calcStepOffsetWidth = debounce(this.calcStepOffsetWidth, 150)
return {
flexSupported: true,
lastStepOffsetWidth: 0,
}
},
mounted () {
2018-03-11 12:48:04 +08:00
this.$nextTick(() => {
this.calcStepOffsetWidth()
if (!isFlexSupported()) {
this.setState({
flexSupported: false,
})
}
})
2018-03-07 10:50:11 +08:00
},
updated () {
2018-03-11 12:48:04 +08:00
this.$nextTick(() => {
this.calcStepOffsetWidth()
})
2018-03-07 10:50:11 +08:00
},
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
2018-03-09 18:52:31 +08:00
if (this.lastStepOffsetWidth === lastStepOffsetWidth ||
Math.abs(this.lastStepOffsetWidth - lastStepOffsetWidth) <= 3) {
2018-03-07 10:50:11 +08:00
return
}
this.setState({ lastStepOffsetWidth })
})
}
},
},
render () {
const {
prefixCls, direction,
2018-11-09 17:51:56 +08:00
labelPlacement, iconPrefix, status, size, current, $scopedSlots, initial,
icons,
2018-03-13 10:19:00 +08:00
} = this
let progressDot = this.progressDot
if (progressDot === undefined) {
progressDot = $scopedSlots.progressDot
}
2018-03-07 10:50:11 +08:00
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,
2019-01-09 21:49:33 +08:00
[`${prefixCls}-flex-not-supported`]: !flexSupported,
2018-03-07 10:50:11 +08:00
}
const stepsProps = {
class: classString,
ref: 'vcStepsRef',
on: this.$listeners,
}
return (
<div {...stepsProps}>
{
filteredChildren.map((child, index) => {
2018-03-09 18:52:31 +08:00
const childProps = getPropsData(child)
2018-11-14 20:43:20 +08:00
const stepNumber = initial + index
2018-03-07 10:50:11 +08:00
const stepProps = {
props: {
2018-11-09 17:51:56 +08:00
stepNumber: `${stepNumber + 1}`,
2018-03-07 10:50:11 +08:00
prefixCls,
iconPrefix,
2018-03-13 10:19:00 +08:00
progressDot: this.progressDot,
2018-11-09 17:51:56 +08:00
icons,
2018-03-11 12:48:04 +08:00
...childProps,
2018-03-07 10:50:11 +08:00
},
on: getEvents(child),
2018-03-13 10:19:00 +08:00
scopedSlots: $scopedSlots,
2018-03-07 10:50:11 +08:00
}
2018-03-11 12:48:04 +08:00
if (!flexSupported && direction !== 'vertical' && index !== lastIndex) {
stepProps.props.itemWidth = `${100 / lastIndex}%`
2018-05-18 22:04:10 +08:00
stepProps.props.adjustMarginRight = `${-Math.round(lastStepOffsetWidth / lastIndex + 1)}px`
2018-03-11 12:48:04 +08:00
}
// fix tail color
if (status === 'error' && index === current - 1) {
stepProps.class = `${prefixCls}-next-error`
}
if (!childProps.status) {
2018-11-09 17:51:56 +08:00
if (stepNumber === current) {
2018-03-11 12:48:04 +08:00
stepProps.props.status = status
2018-11-09 17:51:56 +08:00
} else if (stepNumber < current) {
2018-03-11 12:48:04 +08:00
stepProps.props.status = 'finish'
} else {
stepProps.props.status = 'wait'
}
}
return cloneElement(child, stepProps)
2018-03-07 10:50:11 +08:00
})
}
</div>
)
},
}
2018-03-19 10:16:27 +08:00