UNPKG

tav-ui

Version:
74 lines (71 loc) 2.5 kB
import { defineComponent, ref, computed, unref, watchEffect, watch, onMounted, openBlock, createElementBlock, normalizeStyle, toDisplayString } from 'vue'; import { useTransition, TransitionPresets } from '@vueuse/core'; import { isNumber } from '../../../utils/is2.mjs'; import { countToProps } from './types2.mjs'; import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs'; const _sfc_main = defineComponent({ name: "TaCountTo", props: countToProps, emits: ["onStarted", "onFinished"], setup(props, { emit }) { const source = ref(props.startVal); const disabled = ref(false); let outputValue = useTransition(source); const value = computed(() => formatNumber(unref(outputValue))); watchEffect(() => { source.value = props.startVal; }); watch([() => props.startVal, () => props.endVal], () => { if (props.autoplay) { start(); } }); onMounted(() => { props.autoplay && start(); }); function start() { run(); source.value = props.endVal; } function reset() { source.value = props.startVal; run(); } function run() { outputValue = useTransition(source, { disabled, duration: props.duration, onFinished: () => emit("onFinished"), onStarted: () => emit("onStarted"), ...props.useEasing ? { transition: TransitionPresets[props.transition] } : {} }); } function formatNumber(num) { if (!num && num !== 0) { return ""; } const { decimals, decimal, separator, suffix, prefix } = props; num = Number(num).toFixed(decimals); num += ""; const x = num.split("."); let x1 = x[0]; const x2 = x.length > 1 ? decimal + x[1] : ""; const rgx = /(\d+)(\d{3})/; if (separator && !isNumber(separator)) { while (rgx.test(x1)) { x1 = x1.replace(rgx, `$1${separator}$2`); } } return prefix + x1 + x2 + suffix; } return { value, start, reset }; } }); function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return openBlock(), createElementBlock("span", { style: normalizeStyle({ color: _ctx.color }) }, toDisplayString(_ctx.value), 5); } var CountTo = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/home/runner/work/tav-ui/tav-ui/packages/components/count-to/src/count-to.vue"]]); export { CountTo as default }; //# sourceMappingURL=count-to2.mjs.map