22<template >
33 <component
44 :is =" buttonTag"
5+ ref =" buttonRef"
56 :class =" buttonStyle"
67 :disabled =" disabled"
78 class =" ued-button"
89 >
9- <ued-icon v-if =" icon" >
10- <component :is =" icon" />
10+ <ued-icon v-if =" icon || $slots.icon" :class =" loadingStyle" >
11+ <component :is =" icon" v-if =" icon" />
12+ <slot v-else name =" icon" />
1113 </ued-icon >
12- <slot />
14+ <span v-if =" $slots.default" >
15+ <slot />
16+ </span >
1317 </component >
1418</template >
1519
1620<script lang="ts" setup>
1721import { UedIcon } from ' ../icon'
18- import { computed , ComponentCustomProps } from ' vue'
22+ import { Loading } from ' ../icon/icon'
23+ import { computed , ComponentCustomProps , useSlots , ref , inject } from ' vue'
1924import ' ./styles/index.scss'
2025
26+ const buttonType = inject (' type' , undefined )
27+ const buttonSize = inject (' size' , undefined )
28+
2129defineOptions ({ name: ' UedButton' })
2230
2331type ButtonProps = {
@@ -32,25 +40,59 @@ type ButtonProps = {
3240 size? : string
3341 tag? : string
3442 icon? : ComponentCustomProps
43+ loading? : boolean
44+ loadingIcon? : ComponentCustomProps
3545}
3646
3747const buttonProps = defineProps <ButtonProps >()
3848
49+ const $slots = useSlots ()
50+
51+ const buttonRef = ref ()
52+
3953const buttonStyle = computed (() => {
4054 return {
41- [` ued-button--${buttonProps .type } ` ]: buttonProps .type ,
42- [` ued-button--${buttonProps .size } ` ]: buttonProps .size ,
55+ [` ued-button--${buttonType ?? buttonProps .type } ` ]:
56+ buttonType ?? buttonProps .type ,
57+ [` ued-button--${buttonSize ?? buttonProps .size } ` ]:
58+ buttonSize ?? buttonProps .size ,
4359 ' is-plain' : buttonProps .plain ,
4460 ' is-round' : buttonProps .round ,
4561 ' is-circle' : buttonProps .circle ,
46- ' is-disabled' : buttonProps .disabled ,
62+ ' is-disabled' : buttonProps .disabled || buttonProps . loading ,
4763 ' is-link' : buttonProps .link ,
4864 ' is-text' : buttonProps .text ,
4965 ' is-has-bg' : buttonProps .bg ,
66+ ' is-loading' : buttonProps .loading ,
67+ }
68+ })
69+
70+ const loadingStyle = computed (() => {
71+ return {
72+ ' is-loading' : buttonProps .loading ,
5073 }
5174})
5275
5376const buttonTag = computed (() => {
5477 return buttonProps .tag ?? ' button'
5578})
79+
80+ const icon = computed (() => {
81+ return buttonProps .loading
82+ ? $slots .icon
83+ ? undefined
84+ : buttonProps .loadingIcon ?? Loading
85+ : buttonProps .icon
86+ })
87+
88+ defineExpose ({
89+ /** @description button html element */
90+ ref: buttonRef ,
91+ /** @description button type */
92+ type: buttonProps .type ,
93+ /** @description button disabled */
94+ disabled: buttonProps .disabled ,
95+ /** @description button size */
96+ size: buttonProps .size ,
97+ })
5698 </script >
0 commit comments