|
1 | 1 | <script lang="ts" setup>
|
2 |
| -import { computed, ref } from 'vue' |
3 |
| -import { alert } from '@heroui/theme' |
4 |
| -import { defaultIconPath, successIconPath, warningIconPath } from './constant' |
| 2 | +import { computed } from "vue"; |
| 3 | +import { alert } from "@heroui/theme"; |
| 4 | +import { defaultIconPath, successIconPath, warningIconPath } from "./constant"; |
5 | 5 |
|
6 | 6 | export interface AlertProps {
|
7 |
| - /** |
8 |
| - * 警告消息的标题 |
9 |
| - */ |
10 |
| - title?: string |
11 |
| - /** |
12 |
| - * 警告消息的描述 |
13 |
| - */ |
14 |
| - description?: string |
15 |
| - /** |
16 |
| - * 警告是否可见。 |
17 |
| - * @default false |
18 |
| - */ |
19 |
| - isVisible?: boolean |
20 |
| - /** |
21 |
| - * 警告默认是否可见。 |
22 |
| - * @default false |
23 |
| - */ |
24 |
| - isDefaultVisible?: boolean |
25 |
| - /** |
26 |
| - * 是否允许用户关闭警告 |
27 |
| - */ |
28 |
| - isClosable?: boolean |
| 7 | + /** |
| 8 | + * 警告消息的标题 |
| 9 | + */ |
| 10 | + title?: string; |
| 11 | + /** |
| 12 | + * 警告消息的描述 |
| 13 | + */ |
| 14 | + description?: string; |
| 15 | + /** |
| 16 | + * 警告是否可见。 |
| 17 | + * @default false |
| 18 | + */ |
| 19 | + isVisible?: boolean; |
| 20 | + /** |
| 21 | + * 警告默认是否可见。 |
| 22 | + * @default false |
| 23 | + */ |
| 24 | + isDefaultVisible?: boolean; |
| 25 | + /** |
| 26 | + * 是否允许用户关闭警告 |
| 27 | + */ |
| 28 | + isClosable?: boolean; |
29 | 29 | }
|
30 | 30 |
|
31 | 31 | export interface AlertVariantProps {
|
32 |
| - variant?: 'solid' | 'flat' | 'faded' | 'bordered' | undefined |
33 |
| - color?: |
34 |
| - | 'default' |
35 |
| - | 'primary' |
36 |
| - | 'secondary' |
37 |
| - | 'success' |
38 |
| - | 'warning' |
39 |
| - | 'danger' |
40 |
| - | undefined |
41 |
| - radius?: 'sm' | 'none' | 'md' | 'lg' | 'full' | undefined |
42 |
| - hideIcon?: boolean | undefined |
43 |
| - hideIconWrapper?: boolean | undefined |
44 |
| - hasContent?: boolean | undefined |
| 32 | + variant?: "solid" | "flat" | "faded" | "bordered" | undefined; |
| 33 | + color?: |
| 34 | + | "default" |
| 35 | + | "primary" |
| 36 | + | "secondary" |
| 37 | + | "success" |
| 38 | + | "warning" |
| 39 | + | "danger" |
| 40 | + | undefined; |
| 41 | + radius?: "sm" | "none" | "md" | "lg" | "full" | undefined; |
| 42 | + hideIcon?: boolean | undefined; |
| 43 | + hideIconWrapper?: boolean | undefined; |
| 44 | + hasContent?: boolean | undefined; |
45 | 45 | }
|
46 | 46 |
|
47 |
| -const props = withDefaults(defineProps<AlertProps & AlertVariantProps>(), { |
48 |
| -
|
49 |
| -}) |
| 47 | +const props = withDefaults(defineProps<AlertProps & AlertVariantProps>(), {}); |
50 | 48 |
|
51 | 49 | const emits = defineEmits<{
|
52 |
| - /** |
53 |
| - * 关闭按钮被点击时调用的函数 |
54 |
| - */ |
55 |
| - (e: 'close'): void |
56 |
| - /** |
57 |
| - * 警告可见状态变化时的事件处理程序。 |
58 |
| - * @param isVisible 布尔值,表示是否可见 |
59 |
| - * @returns void |
60 |
| - */ |
61 |
| - (e: 'visibleChange', isVisible: boolean): void |
62 |
| -}>() |
| 50 | + /** |
| 51 | + * 关闭按钮被点击时调用的函数 |
| 52 | + */ |
| 53 | + (e: "close"): void; |
| 54 | + /** |
| 55 | + * 警告可见状态变化时的事件处理程序。 |
| 56 | + * @param isVisible 布尔值,表示是否可见 |
| 57 | + * @returns void |
| 58 | + */ |
| 59 | + (e: "visibleChange", isVisible: boolean): void; |
| 60 | +}>(); |
63 | 61 |
|
64 | 62 | const { base, mainWrapper, title, description, alertIcon, iconWrapper } = alert(
|
65 |
| - { |
66 |
| - color: props.color ?? 'default', |
67 |
| - variant: props.variant ?? 'flat', |
68 |
| - hideIconWrapper: props.hideIconWrapper, |
69 |
| - }, |
70 |
| -) |
| 63 | + { |
| 64 | + color: props.color ?? "default", |
| 65 | + variant: props.variant ?? "flat", |
| 66 | + hideIconWrapper: props.hideIconWrapper, |
| 67 | + hasContent: !!props.description, |
| 68 | + }, |
| 69 | +); |
71 | 70 |
|
72 | 71 | const icon = computed(() => {
|
73 |
| - const color = props.color |
74 |
| -
|
75 |
| - if (color === 'default' || color === 'secondary' || color === 'primary') { |
76 |
| - return defaultIconPath |
77 |
| - } |
78 |
| - else if (color === 'success') { |
79 |
| - return successIconPath |
80 |
| - } |
81 |
| - else if (color === 'warning' || color === 'danger') { |
82 |
| - return warningIconPath |
83 |
| - } |
84 |
| - else { |
85 |
| - return defaultIconPath |
86 |
| - } |
87 |
| -}) |
| 72 | + const color = props.color; |
| 73 | +
|
| 74 | + if (color === "default" || color === "secondary" || color === "primary") { |
| 75 | + return defaultIconPath; |
| 76 | + } else if (color === "success") { |
| 77 | + return successIconPath; |
| 78 | + } else if (color === "warning" || color === "danger") { |
| 79 | + return warningIconPath; |
| 80 | + } else { |
| 81 | + return defaultIconPath; |
| 82 | + } |
| 83 | +}); |
88 | 84 |
|
89 | 85 | function onClose() {
|
90 |
| - emits('close') |
| 86 | + emits("close"); |
91 | 87 | }
|
92 | 88 | </script>
|
93 | 89 |
|
94 | 90 | <template>
|
95 |
| - <div :class="base()"> |
96 |
| - <slot name="startContent" /> |
97 |
| - |
98 |
| - <div v-if="!props.hideIcon" :class="iconWrapper()"> |
99 |
| - <slot name="icon"> |
100 |
| - <svg |
101 |
| - fill="none" |
102 |
| - height="24" |
103 |
| - viewBox="0 0 24 24" |
104 |
| - width="24" |
105 |
| - xmlns="http://www.w3.org/2000/svg" |
106 |
| - :class="alertIcon()" |
107 |
| - > |
108 |
| - <path :d="icon" /> |
109 |
| - </svg> |
110 |
| - </slot> |
111 |
| - </div> |
112 |
| - |
113 |
| - <div :class="mainWrapper()"> |
114 |
| - <div v-if="props.title" :class="title()"> |
115 |
| - {{ props.title }} |
116 |
| - </div> |
117 |
| - <slot v-else /> |
118 |
| - |
119 |
| - <div v-if="props.description" :class="description()"> |
120 |
| - {{ props.description }} |
121 |
| - </div> |
122 |
| - <slot v-else name="description" /> |
| 91 | + <div :class="base()"> |
| 92 | + <slot name="startContent" /> |
| 93 | + |
| 94 | + <div v-if="!props.hideIcon" :class="iconWrapper()"> |
| 95 | + <slot name="icon"> |
| 96 | + <svg |
| 97 | + fill="none" |
| 98 | + height="24" |
| 99 | + viewBox="0 0 24 24" |
| 100 | + width="24" |
| 101 | + xmlns="http://www.w3.org/2000/svg" |
| 102 | + :class="alertIcon()" |
| 103 | + > |
| 104 | + <path :d="icon" /> |
| 105 | + </svg> |
| 106 | + </slot> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div :class="mainWrapper()"> |
| 110 | + <div v-if="props.title" :class="title()"> |
| 111 | + {{ props.title }} |
| 112 | + </div> |
| 113 | + <slot v-else /> |
| 114 | + |
| 115 | + <div v-if="props.description" :class="description()"> |
| 116 | + {{ props.description }} |
| 117 | + </div> |
| 118 | + <slot v-else name="description" /> |
| 119 | + </div> |
| 120 | + |
| 121 | + <slot name="endContent" /> |
| 122 | + |
| 123 | + <!-- Improve: use the `Button` component instead --> |
| 124 | + <button v-if="props.isClosable" @click="onClose">close</button> |
123 | 125 | </div>
|
124 |
| - |
125 |
| - <slot name="endContent" /> |
126 |
| - |
127 |
| - <!-- Improve: use the `Button` component instead --> |
128 |
| - <button v-if="props.isClosable" @click="onClose"> |
129 |
| - close |
130 |
| - </button> |
131 |
| - </div> |
132 | 126 | </template>
|
133 | 127 |
|
134 | 128 | <style scoped></style>
|
0 commit comments