UNPKG

3.52 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import { createVNode as _createVNode } from "vue";
3import { computed, defineComponent, onUnmounted, reactive, ref, watch } from 'vue';
4import classNames from '../_util/classNames';
5
6function UnitNumber(_ref) {
7 var prefixCls = _ref.prefixCls,
8 value = _ref.value,
9 current = _ref.current,
10 _ref$offset = _ref.offset,
11 offset = _ref$offset === void 0 ? 0 : _ref$offset;
12 var style;
13
14 if (offset) {
15 style = {
16 position: 'absolute',
17 top: "".concat(offset, "00%"),
18 left: 0
19 };
20 }
21
22 return _createVNode("p", {
23 "style": style,
24 "class": classNames("".concat(prefixCls, "-only-unit"), {
25 current: current
26 })
27 }, [value]);
28}
29
30function getOffset(start, end, unit) {
31 var index = start;
32 var offset = 0;
33
34 while ((index + 10) % 10 !== end) {
35 index += unit;
36 offset += unit;
37 }
38
39 return offset;
40}
41
42export default defineComponent({
43 name: 'SingleNumber',
44 props: {
45 prefixCls: String,
46 value: String,
47 count: Number
48 },
49 setup: function setup(props) {
50 var originValue = computed(function () {
51 return Number(props.value);
52 });
53 var originCount = computed(function () {
54 return Math.abs(props.count);
55 });
56 var state = reactive({
57 prevValue: originValue.value,
58 prevCount: originCount.value
59 }); // ============================= Events =============================
60
61 var onTransitionEnd = function onTransitionEnd() {
62 state.prevValue = originValue.value;
63 state.prevCount = originCount.value;
64 };
65
66 var timeout = ref(); // Fallback if transition event not support
67
68 watch(originValue, function () {
69 clearTimeout(timeout.value);
70 timeout.value = setTimeout(function () {
71 onTransitionEnd();
72 }, 1000);
73 }, {
74 flush: 'post'
75 });
76 onUnmounted(function () {
77 clearTimeout(timeout.value);
78 });
79 return function () {
80 var unitNodes;
81 var offsetStyle = {};
82 var value = originValue.value;
83
84 if (state.prevValue === value || Number.isNaN(value) || Number.isNaN(state.prevValue)) {
85 // Nothing to change
86 unitNodes = [UnitNumber(_extends(_extends({}, props), {
87 current: true
88 }))];
89 offsetStyle = {
90 transition: 'none'
91 };
92 } else {
93 unitNodes = []; // Fill basic number units
94
95 var end = value + 10;
96 var unitNumberList = [];
97
98 for (var index = value; index <= end; index += 1) {
99 unitNumberList.push(index);
100 } // Fill with number unit nodes
101
102
103 var prevIndex = unitNumberList.findIndex(function (n) {
104 return n % 10 === state.prevValue;
105 });
106 unitNodes = unitNumberList.map(function (n, index) {
107 var singleUnit = n % 10;
108 return UnitNumber(_extends(_extends({}, props), {
109 value: singleUnit,
110 offset: index - prevIndex,
111 current: index === prevIndex
112 }));
113 }); // Calculate container offset value
114
115 var unit = state.prevCount < originCount.value ? 1 : -1;
116 offsetStyle = {
117 transform: "translateY(".concat(-getOffset(state.prevValue, value, unit), "00%)")
118 };
119 }
120
121 return _createVNode("span", {
122 "class": "".concat(props.prefixCls, "-only"),
123 "style": offsetStyle,
124 "onTransitionend": function onTransitionend() {
125 return onTransitionEnd();
126 }
127 }, [unitNodes]);
128 };
129 }
130});
\No newline at end of file