Skip to content

Commit 9d6182c

Browse files
authored
refactor(kalert): ts improvement for kalert (#2671)
* refactor(kalert): ts improvement for kalert * chore(kalert): improve types * chore(kalert): props destructure
1 parent 7166ff7 commit 9d6182c

File tree

2 files changed

+64
-33
lines changed

2 files changed

+64
-33
lines changed

src/components/KAlert/KAlert.vue

+14-33
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,35 @@
3737
class="alert-dismiss-icon"
3838
:size="KUI_ICON_SIZE_40"
3939
tabindex="0"
40-
@click="$emit('dismiss')"
40+
@click="emit('dismiss')"
4141
@keydown.space.prevent
42-
@keyup.enter="$emit('dismiss')"
43-
@keyup.space="$emit('dismiss')"
42+
@keyup.enter="emit('dismiss')"
43+
@keyup.space="emit('dismiss')"
4444
/>
4545
</div>
4646
</template>
4747

4848
<script setup lang="ts">
4949
import { computed } from 'vue'
50-
import type { PropType } from 'vue'
51-
import type { AlertAppearance } from '@/types'
5250
import { AlertAppearances } from '@/types'
51+
import type { AlertEmits, AlertProps , AlertSlots } from '@/types'
5352
import { InfoIcon, CheckCircleIcon, WarningIcon, DangerIcon, CloseIcon } from '@kong/icons'
5453
import { KUI_ICON_SIZE_40 } from '@kong/design-tokens'
5554
5655
type AlertIcon = typeof InfoIcon // all icons are the same type so we can use any of them
5756
58-
const props = defineProps({
59-
title: {
60-
type: String,
61-
default: '',
62-
},
63-
message: {
64-
type: String,
65-
default: '',
66-
},
67-
appearance: {
68-
type: String as PropType<AlertAppearance>,
69-
default: 'info',
70-
validator: (value: AlertAppearance): boolean => {
71-
return Object.values(AlertAppearances).includes(value)
72-
},
73-
},
74-
showIcon: {
75-
type: Boolean,
76-
default: false,
77-
},
78-
dismissible: {
79-
type: Boolean,
80-
default: false,
81-
},
82-
})
83-
84-
defineEmits(['dismiss'])
57+
const {
58+
title = '',
59+
message = '',
60+
appearance = AlertAppearances.info,
61+
showIcon,
62+
dismissible,
63+
} = defineProps<AlertProps>()
64+
const emit = defineEmits<AlertEmits>()
65+
defineSlots<AlertSlots>()
8566
8667
const getAlertIcon = computed((): AlertIcon => {
87-
switch (props.appearance) {
68+
switch (appearance) {
8869
case AlertAppearances.info:
8970
return InfoIcon
9071
case AlertAppearances.success:

src/types/alert.ts

+50
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,53 @@ export const AlertAppearances: AlertAppearanceRecord = {
77
danger: 'danger',
88
warning: 'warning',
99
}
10+
11+
export interface AlertProps {
12+
/**
13+
* The title of the alert
14+
*/
15+
title?: string
16+
17+
/**
18+
* The message of the alert
19+
*/
20+
message?: string
21+
22+
/**
23+
* Base styling of the alert
24+
* One of ['info', 'success', 'danger', 'warning']
25+
* @default 'info'
26+
*/
27+
appearance?: AlertAppearance
28+
29+
/**
30+
* When true, shows an icon
31+
* @default false
32+
*/
33+
showIcon?: boolean
34+
35+
/**
36+
* When true, shows a close button
37+
* @default false
38+
*/
39+
dismissible?: boolean
40+
}
41+
42+
export interface AlertEmits {
43+
/**
44+
* Emitted when dismiss button is clicked.
45+
*/
46+
dismiss: []
47+
}
48+
49+
export interface AlertSlots {
50+
/**
51+
* Slot for alert message content.
52+
*/
53+
default?(): any
54+
55+
/**
56+
* Slot for custom icon to the left of the alert message.
57+
*/
58+
icon?(): any
59+
}

0 commit comments

Comments
 (0)