SingleNumber.js 3.5 KB

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