2019-01-12 11:33:27 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import { getStyle } from '../_util/props-util';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2019-03-12 12:04:40 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2017-12-28 18:46:31 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getNumberArray(num) {
|
2017-12-28 18:46:31 +08:00
|
|
|
return num
|
2019-01-12 11:33:27 +08:00
|
|
|
? num
|
|
|
|
.toString()
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
2020-03-07 19:45:13 +08:00
|
|
|
.map(i => {
|
|
|
|
const current = Number(i);
|
|
|
|
return isNaN(current) ? i : current;
|
|
|
|
})
|
2019-01-12 11:33:27 +08:00
|
|
|
: [];
|
2017-12-28 18:46:31 +08:00
|
|
|
}
|
2018-03-09 17:50:33 +08:00
|
|
|
|
|
|
|
const ScrollNumberProps = {
|
2019-03-12 12:04:40 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2019-01-03 20:46:53 +08:00
|
|
|
count: PropTypes.any,
|
2018-03-09 17:50:33 +08:00
|
|
|
component: PropTypes.string,
|
2018-09-05 21:28:54 +08:00
|
|
|
title: PropTypes.oneOfType([PropTypes.number, PropTypes.string, null]),
|
2019-01-03 20:46:53 +08:00
|
|
|
displayComponent: PropTypes.any,
|
|
|
|
className: PropTypes.object,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-09 17:50:33 +08:00
|
|
|
|
2017-11-16 18:29:02 +08:00
|
|
|
export default {
|
2018-01-12 16:10:41 +08:00
|
|
|
mixins: [BaseMixin],
|
2018-03-09 17:50:33 +08:00
|
|
|
props: ScrollNumberProps,
|
2019-03-12 12:04:40 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-12 12:04:40 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
2017-11-16 18:29:02 +08:00
|
|
|
return {
|
2017-12-28 18:46:31 +08:00
|
|
|
animateStarted: true,
|
2018-03-09 17:50:33 +08:00
|
|
|
sCount: this.count,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-11-16 18:29:02 +08:00
|
|
|
},
|
2017-12-28 18:46:31 +08:00
|
|
|
watch: {
|
2020-03-07 19:45:13 +08:00
|
|
|
count() {
|
|
|
|
this.lastCount = this.sCount;
|
|
|
|
this.setState({
|
|
|
|
animateStarted: true,
|
|
|
|
});
|
2017-12-28 18:46:31 +08:00
|
|
|
},
|
2017-11-16 18:29:02 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
updated() {
|
|
|
|
const { animateStarted, count } = this;
|
|
|
|
if (animateStarted) {
|
2020-03-15 14:02:16 +08:00
|
|
|
this.clearTimeout();
|
|
|
|
// Let browser has time to reset the scroller before actually
|
|
|
|
// performing the transition.
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
animateStarted: false,
|
|
|
|
sCount: count,
|
|
|
|
},
|
|
|
|
this.onAnimated,
|
|
|
|
);
|
|
|
|
});
|
2020-03-07 19:45:13 +08:00
|
|
|
}
|
|
|
|
},
|
2020-06-11 16:13:09 +08:00
|
|
|
beforeUnmount() {
|
2020-03-15 14:02:16 +08:00
|
|
|
this.clearTimeout();
|
|
|
|
},
|
2017-11-16 18:29:02 +08:00
|
|
|
methods: {
|
2020-03-15 14:02:16 +08:00
|
|
|
clearTimeout() {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = undefined;
|
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
getPositionByNum(num, i) {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { sCount } = this;
|
|
|
|
const currentCount = Math.abs(Number(sCount));
|
|
|
|
const lastCount = Math.abs(Number(this.lastCount));
|
|
|
|
const currentDigit = Math.abs(getNumberArray(sCount)[i]);
|
|
|
|
const lastDigit = Math.abs(getNumberArray(this.lastCount)[i]);
|
|
|
|
|
2018-03-09 17:50:33 +08:00
|
|
|
if (this.animateStarted) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 10 + num;
|
2017-12-28 18:46:31 +08:00
|
|
|
}
|
|
|
|
// 同方向则在同一侧切换数字
|
2020-03-07 19:45:13 +08:00
|
|
|
if (currentCount > lastCount) {
|
2017-12-28 18:46:31 +08:00
|
|
|
if (currentDigit >= lastDigit) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 10 + num;
|
2017-12-28 18:46:31 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return 20 + num;
|
2017-12-28 18:46:31 +08:00
|
|
|
}
|
|
|
|
if (currentDigit <= lastDigit) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 10 + num;
|
2017-12-28 18:46:31 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return num;
|
2017-12-28 18:46:31 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
onAnimated() {
|
|
|
|
this.$emit('animated');
|
|
|
|
},
|
|
|
|
|
|
|
|
renderNumberList(position, className) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const childrenToReturn = [];
|
2018-03-09 17:50:33 +08:00
|
|
|
for (let i = 0; i < 30; i++) {
|
2019-01-12 11:33:27 +08:00
|
|
|
childrenToReturn.push(
|
2020-03-07 19:45:13 +08:00
|
|
|
<p
|
|
|
|
key={i.toString()}
|
|
|
|
class={classNames(className, {
|
|
|
|
current: position === i,
|
|
|
|
})}
|
|
|
|
>
|
2019-01-12 11:33:27 +08:00
|
|
|
{i % 10}
|
|
|
|
</p>,
|
|
|
|
);
|
2018-03-09 17:50:33 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return childrenToReturn;
|
2017-12-28 18:46:31 +08:00
|
|
|
},
|
2019-03-12 12:04:40 +08:00
|
|
|
renderCurrentNumber(prefixCls, num, i) {
|
2020-03-07 19:45:13 +08:00
|
|
|
if (typeof num === 'number') {
|
|
|
|
const position = this.getPositionByNum(num, i);
|
|
|
|
const removeTransition =
|
|
|
|
this.animateStarted || getNumberArray(this.lastCount)[i] === undefined;
|
|
|
|
const style = {
|
|
|
|
transition: removeTransition ? 'none' : undefined,
|
|
|
|
msTransform: `translateY(${-position * 100}%)`,
|
|
|
|
WebkitTransform: `translateY(${-position * 100}%)`,
|
|
|
|
transform: `translateY(${-position * 100}%)`,
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<span class={`${prefixCls}-only`} style={style} key={i}>
|
|
|
|
{this.renderNumberList(position, `${prefixCls}-only-unit`)}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2017-12-28 18:46:31 +08:00
|
|
|
return (
|
2020-03-07 19:45:13 +08:00
|
|
|
<span key="symbol" class={`${prefixCls}-symbol`}>
|
|
|
|
{num}
|
2017-12-28 18:46:31 +08:00
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2017-12-28 18:46:31 +08:00
|
|
|
},
|
2018-03-09 17:50:33 +08:00
|
|
|
|
2019-03-12 12:04:40 +08:00
|
|
|
renderNumberElement(prefixCls) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { sCount } = this;
|
2019-03-12 12:04:40 +08:00
|
|
|
if (sCount && Number(sCount) % 1 === 0) {
|
|
|
|
return getNumberArray(sCount)
|
2019-05-28 11:37:38 +08:00
|
|
|
.map((num, i) => this.renderCurrentNumber(prefixCls, num, i))
|
|
|
|
.reverse();
|
2019-03-12 12:04:40 +08:00
|
|
|
}
|
|
|
|
return sCount;
|
2017-12-28 18:46:31 +08:00
|
|
|
},
|
2017-11-16 18:29:02 +08:00
|
|
|
},
|
2018-03-09 17:50:33 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2019-03-12 12:04:40 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
title,
|
|
|
|
component: Tag = 'sup',
|
|
|
|
displayComponent,
|
|
|
|
className,
|
|
|
|
} = this;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-12 12:04:40 +08:00
|
|
|
const prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
|
2019-01-03 20:46:53 +08:00
|
|
|
if (displayComponent) {
|
|
|
|
return cloneElement(displayComponent, {
|
|
|
|
class: `${prefixCls}-custom-component`,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2019-01-03 20:46:53 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const style = getStyle(this, true);
|
2018-03-09 17:50:33 +08:00
|
|
|
// fix https://fb.me/react-unknown-prop
|
2019-01-12 11:33:27 +08:00
|
|
|
const restProps = omit(this.$props, ['count', 'component', 'prefixCls', 'displayComponent']);
|
2018-03-09 17:50:33 +08:00
|
|
|
const newProps = {
|
|
|
|
props: {
|
|
|
|
...restProps,
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
attrs: {
|
2018-03-09 17:50:33 +08:00
|
|
|
title,
|
|
|
|
},
|
|
|
|
style,
|
2019-01-03 20:46:53 +08:00
|
|
|
class: classNames(prefixCls, className),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-09 17:50:33 +08:00
|
|
|
// allow specify the border
|
|
|
|
// mock border-color by box-shadow for compatible with old usage:
|
|
|
|
// <Badge count={4} style={{ backgroundColor: '#fff', color: '#999', borderColor: '#d9d9d9' }} />
|
|
|
|
if (style && style.borderColor) {
|
2019-01-12 11:33:27 +08:00
|
|
|
newProps.style.boxShadow = `0 0 0 1px ${style.borderColor} inset`;
|
2018-03-09 17:50:33 +08:00
|
|
|
}
|
2019-01-03 20:46:53 +08:00
|
|
|
|
2019-03-12 12:04:40 +08:00
|
|
|
return <Tag {...newProps}>{this.renderNumberElement(prefixCls)}</Tag>;
|
2017-11-16 18:29:02 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|