From 48c72206f6a9d870ba2ffd8fb94a7e274085ced1 Mon Sep 17 00:00:00 2001
From: mrayi <1078464063@qq.com>
Date: Tue, 16 Apr 2024 14:56:01 +0800
Subject: [PATCH] refactor(countdown): move to script setup (#3018)
---
src/config.json | 1 +
.../__VUE/countdown/countdown.taro.vue | 176 +++++++++++
src/packages/__VUE/countdown/countdown.vue | 176 +++++++++++
src/packages/__VUE/countdown/doc.en-US.md | 11 +
src/packages/__VUE/countdown/doc.md | 11 +
src/packages/__VUE/countdown/doc.taro.md | 11 +
src/packages/__VUE/countdown/index.taro.ts | 11 +
src/packages/__VUE/countdown/index.taro.vue | 284 ------------------
src/packages/__VUE/countdown/index.ts | 11 +
src/packages/__VUE/countdown/index.vue | 284 ------------------
src/packages/__VUE/countdown/util.ts | 70 +++++
11 files changed, 478 insertions(+), 568 deletions(-)
create mode 100644 src/packages/__VUE/countdown/countdown.taro.vue
create mode 100644 src/packages/__VUE/countdown/countdown.vue
create mode 100644 src/packages/__VUE/countdown/index.taro.ts
delete mode 100644 src/packages/__VUE/countdown/index.taro.vue
create mode 100644 src/packages/__VUE/countdown/index.ts
delete mode 100644 src/packages/__VUE/countdown/index.vue
diff --git a/src/config.json b/src/config.json
index 06c9ad53a1..2fe8e48770 100644
--- a/src/config.json
+++ b/src/config.json
@@ -703,6 +703,7 @@
{
"name": "Countdown",
"cName": "倒计时",
+ "setup": true,
"desc": "倒计时",
"author": "yangxiaolu"
},
diff --git a/src/packages/__VUE/countdown/countdown.taro.vue b/src/packages/__VUE/countdown/countdown.taro.vue
new file mode 100644
index 0000000000..4273802683
--- /dev/null
+++ b/src/packages/__VUE/countdown/countdown.taro.vue
@@ -0,0 +1,176 @@
+
+
+
+
+
+
+
+
+
diff --git a/src/packages/__VUE/countdown/countdown.vue b/src/packages/__VUE/countdown/countdown.vue
new file mode 100644
index 0000000000..4273802683
--- /dev/null
+++ b/src/packages/__VUE/countdown/countdown.vue
@@ -0,0 +1,176 @@
+
+
+
+
+
+
+
+
+
diff --git a/src/packages/__VUE/countdown/doc.en-US.md b/src/packages/__VUE/countdown/doc.en-US.md
index 97a4c27514..9504089575 100644
--- a/src/packages/__VUE/countdown/doc.en-US.md
+++ b/src/packages/__VUE/countdown/doc.en-US.md
@@ -90,6 +90,17 @@ Paused and restarted the countdown with the `paused` attribute
| pause | Count Down Pause |
| reset | Count Down Reset |
+### Types version
+
+The component exports the following type definitions:
+
+```js
+import type {
+ CountdownProps,
+ CountdownInstance
+} from '@nutui/nutui'
+```
+
## Theming
### CSS Variables
diff --git a/src/packages/__VUE/countdown/doc.md b/src/packages/__VUE/countdown/doc.md
index 918eb93de9..ddf0ea287f 100644
--- a/src/packages/__VUE/countdown/doc.md
+++ b/src/packages/__VUE/countdown/doc.md
@@ -92,6 +92,17 @@ app.use(Countdown)
| pause | 暂停倒计时 |
| reset | 重设倒计时,若 `auto-start`为 `true`,重设后会自动开始倒计时 |
+### 类型定义 version
+
+组件导出以下类型定义:
+
+```js
+import type {
+ CountdownProps,
+ CountdownInstance
+} from '@nutui/nutui'
+```
+
## 主题定制
### 样式变量
diff --git a/src/packages/__VUE/countdown/doc.taro.md b/src/packages/__VUE/countdown/doc.taro.md
index 22929d8390..31b311ca8f 100644
--- a/src/packages/__VUE/countdown/doc.taro.md
+++ b/src/packages/__VUE/countdown/doc.taro.md
@@ -92,6 +92,17 @@ app.use(Countdown)
| pause | 暂停倒计时 |
| reset | 重设倒计时,若 `auto-start`为 `true`,重设后会自动开始倒计时 |
+### 类型定义 version
+
+组件导出以下类型定义:
+
+```js
+import type {
+ CountdownProps,
+ CountdownInstance
+} from '@nutui/nutui-taro'
+```
+
## 主题定制
### 样式变量
diff --git a/src/packages/__VUE/countdown/index.taro.ts b/src/packages/__VUE/countdown/index.taro.ts
new file mode 100644
index 0000000000..948203c624
--- /dev/null
+++ b/src/packages/__VUE/countdown/index.taro.ts
@@ -0,0 +1,11 @@
+import Countdown from './countdown.taro.vue'
+import type { ComponentPublicInstance } from 'vue'
+import { withInstall } from '@/packages/utils'
+
+withInstall(Countdown)
+
+export type { CountdownProps } from './countdown.taro.vue'
+
+export type CountdownInstance = ComponentPublicInstance & InstanceType
+
+export { Countdown, Countdown as default }
diff --git a/src/packages/__VUE/countdown/index.taro.vue b/src/packages/__VUE/countdown/index.taro.vue
deleted file mode 100644
index eb29b02160..0000000000
--- a/src/packages/__VUE/countdown/index.taro.vue
+++ /dev/null
@@ -1,284 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/packages/__VUE/countdown/index.ts b/src/packages/__VUE/countdown/index.ts
new file mode 100644
index 0000000000..5fc5167282
--- /dev/null
+++ b/src/packages/__VUE/countdown/index.ts
@@ -0,0 +1,11 @@
+import Countdown from './countdown.vue'
+import type { ComponentPublicInstance } from 'vue'
+import { withInstall } from '@/packages/utils'
+
+withInstall(Countdown)
+
+export type { CountdownProps } from './countdown.vue'
+
+export type CountdownInstance = ComponentPublicInstance & InstanceType
+
+export { Countdown, Countdown as default }
diff --git a/src/packages/__VUE/countdown/index.vue b/src/packages/__VUE/countdown/index.vue
deleted file mode 100644
index eb29b02160..0000000000
--- a/src/packages/__VUE/countdown/index.vue
+++ /dev/null
@@ -1,284 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/packages/__VUE/countdown/util.ts b/src/packages/__VUE/countdown/util.ts
index 92e6049d6e..970cb1ade2 100644
--- a/src/packages/__VUE/countdown/util.ts
+++ b/src/packages/__VUE/countdown/util.ts
@@ -1,3 +1,5 @@
+import { padZero } from '@/packages/utils/util'
+
// 时间戳转换 或 获取当前时间的时间戳
export const getTimeStamp = (timeStr?: string | number) => {
if (!timeStr) return Date.now()
@@ -5,3 +7,71 @@ export const getTimeStamp = (timeStr?: string | number) => {
t = +t > 0 ? +t : t.toString().replace(/-/g, '/')
return new Date(t).getTime()
}
+
+export const parseFormat = (time: { d: number, h: number, m: number, s: number, ms: number }, format: string) => {
+ let { h, m, s, ms } = time
+ const { d } = time
+
+ if (format.includes('DD')) {
+ format = format.replace('DD', padZero(d))
+ } else {
+ h += Number(d) * 24
+ }
+
+ if (format.includes('HH')) {
+ format = format.replace('HH', padZero(h))
+ } else {
+ m += Number(h) * 60
+ }
+
+ if (format.includes('mm')) {
+ format = format.replace('mm', padZero(m))
+ } else {
+ s += Number(m) * 60
+ }
+
+ if (format.includes('ss')) {
+ format = format.replace('ss', padZero(s))
+ } else {
+ ms += Number(s) * 1000
+ }
+
+ if (format.includes('S')) {
+ const msC = padZero(ms, 3).toString()
+
+ if (format.includes('SSS')) {
+ format = format.replace('SSS', msC)
+ } else if (format.includes('SS')) {
+ format = format.replace('SS', msC.slice(0, 2))
+ } else if (format.includes('S')) {
+ format = format.replace('S', msC.slice(0, 1))
+ }
+ }
+ return format
+}
+
+export const formatRemainTime = (t: number, format: string, type?: string) => {
+ const ts = t
+ const rest = {
+ d: 0,
+ h: 0,
+ m: 0,
+ s: 0,
+ ms: 0
+ }
+
+ const SECOND = 1000
+ const MINUTE = 60 * SECOND
+ const HOUR = 60 * MINUTE
+ const DAY = 24 * HOUR
+
+ if (ts > 0) {
+ rest.d = ts >= SECOND ? Math.floor(ts / DAY) : 0
+ rest.h = Math.floor((ts % DAY) / HOUR)
+ rest.m = Math.floor((ts % HOUR) / MINUTE)
+ rest.s = Math.floor((ts % MINUTE) / SECOND)
+ rest.ms = Math.floor(ts % SECOND)
+ }
+
+ return type == 'custom' ? rest : parseFormat({ ...rest }, format)
+}