Procházet zdrojové kódy

Merge branch 'pr/11'

# Conflicts:
#	package-lock.json
Sendya před 5 roky
rodič
revize
5ecfaf8e5c

+ 50 - 0
src/components/CountDown/demo/simple.md

@@ -0,0 +1,50 @@
+---
+order: 0
+title:
+  zh-CN: 基本
+  en-US: Basic
+---
+
+## zh-CN
+
+简单的倒计时组件使用。
+
+## en-US
+
+The simplest usage.
+
+```html
+<template>
+  <div>
+    <CountDown
+      :target="new Date().getTime() + 120000"
+      :onEnd="handleCountEnd"
+      :format="newFormat"
+    />
+  </div>
+</template>
+
+<script>
+  import CountDown from "ant-design-vue-pro/CountDown";
+  export default {
+    methods: {
+      newFormat(time) {
+        const hours = 60 * 60 * 1000;
+        const minutes = 60 * 1000;
+
+        const h = Math.floor(time / hours);
+        const m = Math.floor((time - h * hours) / minutes);
+        const s = Math.floor((time - h * hours - m * minutes) / 1000);
+
+        return `${h} - ${m} - ${s}`;
+      },
+      handleCountEnd() {
+        console.log("count end");
+      }
+    },
+    components: {
+      CountDown
+    }
+  };
+</script>
+```

+ 13 - 0
src/components/CountDown/index.en-US.md

@@ -0,0 +1,13 @@
+---
+title: CountDown
+---
+
+Simple CountDown Component.
+
+## API
+
+| Property | Description                   | Type           | Default |
+| -------- | ----------------------------- | -------------- | ------- |
+| format   | Formatter of time             | Function(time) |         |
+| target   | Target time                   | Date           | -       |
+| onEnd    | Countdown to the end callback | Funtion        | -       |

+ 75 - 0
src/components/CountDown/index.vue

@@ -0,0 +1,75 @@
+<template>
+  <span>{{ realTime }}</span>
+</template>
+
+<script>
+export default {
+  props: {
+    target: null,
+    onEnd: Function,
+    format: Function
+  },
+  data() {
+    return {
+      lastTime: this.initTime(),
+      timer: null
+    };
+  },
+  mounted() {
+    this.tick();
+  },
+  computed: {
+    realTime() {
+      const format = this.format ? this.format : this.defaultFormat;
+      return format(this.lastTime);
+    }
+  },
+  methods: {
+    fixedZero(val) {
+      return val * 1 < 10 ? `0${val}` : val;
+    },
+    initTime() {
+      let lastTime = 0;
+      let targetTime = 0;
+      try {
+        if (Object.prototype.toString.call(this.target) === "[object Date]") {
+          targetTime = this.target.getTime();
+        } else {
+          targetTime = new Date(this.target).getTime();
+        }
+      } catch (e) {
+        throw new Error("invalid target prop", e);
+      }
+      lastTime = targetTime - new Date().getTime();
+      return lastTime < 0 ? 0 : lastTime;
+    },
+    tick() {
+      const interval = 1000;
+      this.timer = setTimeout(() => {
+        if (this.lastTime < interval) {
+          clearTimeout(this.timer);
+          this.lastTime = 0;
+          if (this.onEnd) {
+            this.onEnd();
+          }
+        } else {
+          this.lastTime -= interval;
+          this.tick();
+        }
+      }, interval);
+    },
+    defaultFormat(time) {
+      const hours = 60 * 60 * 1000;
+      const minutes = 60 * 1000;
+
+      const h = Math.floor(time / hours);
+      const m = Math.floor((time - h * hours) / minutes);
+      const s = Math.floor((time - h * hours - m * minutes) / 1000);
+
+      return `${this.fixedZero(h)}:${this.fixedZero(m)}:${this.fixedZero(s)}`;
+    }
+  }
+};
+</script>
+
+<style></style>

+ 14 - 0
src/components/CountDown/index.zh-CN.md

@@ -0,0 +1,14 @@
+---
+title: CountDown
+subtitle: 倒计时
+---
+
+倒计时组件。
+
+## API
+
+| 参数   | 说明           | 类型           | 默认值 |
+| ------ | -------------- | -------------- | ------ |
+| format | 时间格式化显示 | Function(time) |        |
+| target | 目标时间       | Date           | -      |
+| onEnd  | 倒计时结束回调 | Funtion        | -      |