fix: ts error in components package (#4621)

#### What type of PR is this?

/kind cleanup
/area console

#### What this PR does / why we need it:

修复 `@halo-dev/components` 包编译时有 error 级异常的问题。

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/4544

#### Special notes for your reviewer:

观察此 PR 的 ci 中是否有异常输出即可。

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/4622/head^2
Ryan Wang 2023-09-18 16:28:20 +08:00 committed by GitHub
parent da2d56e7d3
commit dd55a0f490
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 18 deletions

View File

@ -8,7 +8,7 @@ import {
IconForbidLine,
IconInformation,
} from "../../icons/icons";
import { computed, ref } from "vue";
import { ref, type Component, markRaw, type Raw } from "vue";
import type { Type } from "@/components/dialog/interface";
import type { Type as ButtonType } from "@/components/button/interface";
@ -48,25 +48,24 @@ const emit = defineEmits<{
(event: "close"): void;
}>();
const icons = {
const icons: Record<Type, { icon: Raw<Component>; color: string }> = {
success: {
icon: IconCheckboxCircle,
icon: markRaw(IconCheckboxCircle),
color: "green",
},
info: {
icon: IconInformation,
icon: markRaw(IconInformation),
color: "blue",
},
warning: {
icon: IconErrorWarning,
icon: markRaw(IconErrorWarning),
color: "orange",
},
error: {
icon: IconForbidLine,
icon: markRaw(IconForbidLine),
color: "red",
},
};
const icon = computed(() => icons[props.type]);
const loading = ref(false);
@ -108,8 +107,8 @@ const handleClose = () => {
<div class="flex justify-between items-start py-2 mb-2">
<div class="flex flex-row items-center gap-3">
<component
:is="icon.icon"
:class="`text-${icon.color}-500`"
:is="icons[type].icon"
:class="`text-${icons[type].color}-500`"
class="w-6 h-6"
></component>
<div class="text-base text-gray-900 font-bold">{{ title }}</div>

View File

@ -1,6 +1,13 @@
<script lang="ts" setup>
import type { Type } from "./interface";
import { computed, onMounted, ref, watchEffect } from "vue";
import {
onMounted,
ref,
watchEffect,
type Raw,
type Component,
markRaw,
} from "vue";
import {
IconCheckboxCircle,
IconErrorWarning,
@ -36,27 +43,25 @@ const emit = defineEmits<{
(event: "close"): void;
}>();
const icons = {
const icons: Record<Type, { icon: Raw<Component>; color: string }> = {
success: {
icon: IconCheckboxCircle,
icon: markRaw(IconCheckboxCircle),
color: "text-green-500",
},
info: {
icon: IconInformation,
icon: markRaw(IconInformation),
color: "text-sky-500",
},
warning: {
icon: IconErrorWarning,
icon: markRaw(IconErrorWarning),
color: "text-orange-500",
},
error: {
icon: IconForbidLine,
icon: markRaw(IconForbidLine),
color: "text-red-500",
},
};
const icon = computed(() => icons[props.type]);
const createTimer = () => {
if (props.duration < 0) return;
timer.value = setTimeout(() => {
@ -115,7 +120,7 @@ defineExpose({ close });
>
<div class="toast-body">
<div class="toast-icon">
<component :is="icon.icon" :class="[icon.color]" />
<component :is="icons[type].icon" :class="icons[type].color" />
</div>
<div class="toast-content">
<div class="toast-description">