Skip to content

Commit

Permalink
feat(count-down): support title and toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Dec 6, 2024
1 parent d829d77 commit 88bb8d9
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 15 deletions.
51 changes: 51 additions & 0 deletions docs/components/count-down.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,64 @@ This document is mainly used to describe some features and usage of the ShadcnCo

:::

## Title

::: raw

<CodeRunner title="Title">
<ShadcnCountDown :time="new Date(Date.now() + 2 * 24 * 60 * 60 * 1000)" title="Count Down" />
</CodeRunner>

:::

::: details Show code

```vue
<template>
<ShadcnCountDown :time="new Date(Date.now() + 2 * 24 * 60 * 60 * 1000)" title="Count Down" />
</template>
```

:::

## Toolbar

::: raw

<CodeRunner title="Toolbar">
<ShadcnCountDown title="Count Down" toolbar :time="new Date(Date.now() + 2 * 24 * 60 * 60 * 1000)"/>
</CodeRunner>

:::

::: details Show code

```vue
<template>
<ShadcnCountDown title="Count Down" toolbar :time="new Date(Date.now() + 2 * 24 * 60 * 60 * 1000)"/>
</template>
```

:::

## Count Down Props

<ApiTable title="Props"
:headers="['Attribute', 'Description', 'Type', 'Default Value']"
:columns="[
['time', 'The time of the count down', 'date', ''],
['simple', 'Whether to display the simple version', 'boolean', 'false'],
['title', 'The title of the count down, only valid when <code>simple</code> is false', 'string', ''],
['toolbar', 'Whether to display the toolbar', 'boolean', 'false'],
]">
</ApiTable>

## Count Down Slots

<ApiTable title="Slots"
:headers="['Slot', 'Description']"
:columns="[
['title', 'Count down content'],
]">
</ApiTable>

Expand Down
4 changes: 3 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<template>
<div class="p-32">
<ShadcnCountDown :time="new Date(Date.now() + 2 * 24 * 60 * 60 * 1000)" />
<ShadcnCountDown title="Activity events"
toolbar
:time="new Date(Date.now() + 2 * 24 * 60 * 60 * 1000)"/>
</div>
</template>

Expand Down
5 changes: 4 additions & 1 deletion src/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export default {
day: 'Day',
hour: 'Hour',
minute: 'Minute',
second: 'Second'
second: 'Second',
pause: 'Pause',
resume: 'Resume',
reset: 'Reset'
}
}
}
5 changes: 4 additions & 1 deletion src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export default {
day: '天',
hour: '时',
minute: '分',
second: '秒'
second: '秒',
pause: '暂停',
resume: '继续',
reset: '重置'
}
}
}
76 changes: 64 additions & 12 deletions src/ui/count-down/ShadcnCountDown.vue
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
<template>
<ShadcnCard :border="false">
<div v-if="simple">
<div class="text-2xl font-bold">
{{ `${ timeLeft.days } : ${ timeLeft.hours } : ${ timeLeft.minutes } : ${ timeLeft.seconds }` }}
</div>
<div v-if="simple">
<div class="text-2xl font-bold">
{{ `${ padNumber(timeLeft.days) } : ${ padNumber(timeLeft.hours) } : ${ padNumber(timeLeft.minutes) } : ${ padNumber(timeLeft.seconds) }` }}
</div>
</div>

<ShadcnCard v-else :border="false">
<template #title>
<slot name="title">
<div class="mb-2">
{{ title }}
</div>
</slot>
</template>

<div v-else class="grid grid-cols-4 gap-4 text-center">
<template v-if="toolbar" #extra>
<div class="space-x-2">
<ShadcnButton :type="isPaused ? 'primary' : 'warning'" @click="togglePause">
{{ isPaused ? t('countDown.text.resume') : t('countDown.text.pause') }}
</ShadcnButton>

<ShadcnButton type="default" @click="onReset">
{{ t('countDown.text.reset') }}
</ShadcnButton>
</div>
</template>

<div class="grid grid-cols-4 gap-4 text-center">
<!-- Days -->
<div class="flex flex-col">
<div class="text-4xl font-bold bg-slate-100 rounded-lg p-4">
{{ timeLeft.days }}
{{ padNumber(timeLeft.days) }}
</div>
<span class="text-sm mt-2">{{ t('countDown.text.day') }}</span>
</div>

<!-- Hours -->
<div class="flex flex-col">
<div class="text-4xl font-bold bg-slate-100 rounded-lg p-4">
{{ timeLeft.hours }}
{{ padNumber(timeLeft.hours) }}
</div>
<span class="text-sm mt-2">{{ t('countDown.text.hour') }}</span>
</div>

<!-- Minutes -->
<div class="flex flex-col">
<div class="text-4xl font-bold bg-slate-100 rounded-lg p-4">
{{ timeLeft.minutes }}
{{ padNumber(timeLeft.minutes) }}
</div>
<span class="text-sm mt-2">{{ t('countDown.text.minute') }}</span>
</div>

<!-- Seconds -->
<div class="flex flex-col">
<div class="text-4xl font-bold bg-slate-100 rounded-lg p-4">
{{ timeLeft.seconds }}
{{ padNumber(timeLeft.seconds) }}
</div>
<span class="text-sm mt-2">{{ t('countDown.text.second') }}</span>
</div>
Expand All @@ -43,14 +63,15 @@
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { onMounted, onUnmounted, ref, watch } from 'vue'
import { t } from '@/utils/locale'
import ShadcnCard from '@/ui/card'
import { CountDownEmits, CountDownProps } from '@/ui/count-down/types'
const emit = defineEmits<CountDownEmits>()
const props = withDefaults(defineProps<CountDownProps>(), {
simple: false
simple: false,
toolbar: false
})
const timeLeft = ref({
Expand All @@ -61,6 +82,7 @@ const timeLeft = ref({
})
let timer: NodeJS.Timeout | null = null
const isPaused = ref(false)
const calculateTimeLeft = () => {
const now = new Date().getTime()
Expand All @@ -84,6 +106,36 @@ const calculateTimeLeft = () => {
}
}
// Pause/Resume
// 暂停/继续
const togglePause = () => {
isPaused.value = !isPaused.value
if (!isPaused.value) {
calculateTimeLeft()
timer = setInterval(calculateTimeLeft, 1000)
}
else {
clearInterval(timer!)
}
}
// Reset
// 重置
const onReset = () => {
isPaused.value = false
calculateTimeLeft()
}
// Number pad
// 数字补零
const padNumber = (num) => {
return String(num).padStart(2, '0')
}
watch(() => props.time, () => {
onReset()
})
onMounted(() => {
calculateTimeLeft()
timer = setInterval(calculateTimeLeft, 1000)
Expand Down
2 changes: 2 additions & 0 deletions src/ui/count-down/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export interface CountDownProps
{
time: Date
simple?: boolean
title?: string
toolbar?: boolean
}

export type CountDownEmits = {
Expand Down

0 comments on commit 88bb8d9

Please sign in to comment.