1
- import { computed , defineComponent , h , inject , PropType } from 'vue'
1
+ import { computed , defineComponent , h , inject , PropType , ref , watch } from 'vue'
2
2
3
3
const CIcon = defineComponent ( {
4
4
name : 'CIcon' ,
@@ -83,7 +83,14 @@ const CIcon = defineComponent({
83
83
} ,
84
84
setup ( props , { attrs } ) {
85
85
const icons : any = inject ( 'icons' )
86
- const _icon = props . icon || props . content || props . name
86
+ const _icon = ref ( props . icon || props . content || props . name )
87
+
88
+ watch (
89
+ ( ) => props . icon ,
90
+ ( ) => {
91
+ _icon . value = props . icon
92
+ } ,
93
+ )
87
94
88
95
const toCamelCase = ( str : string ) => {
89
96
return str
@@ -94,20 +101,26 @@ const CIcon = defineComponent({
94
101
}
95
102
96
103
const iconName = computed ( ( ) =>
97
- _icon && typeof _icon === 'string' ? ( _icon . includes ( '-' ) ? toCamelCase ( _icon ) : _icon ) : '' ,
104
+ _icon . value && typeof _icon . value === 'string'
105
+ ? _icon . value . includes ( '-' )
106
+ ? toCamelCase ( _icon . value )
107
+ : _icon . value
108
+ : '' ,
98
109
)
99
110
100
111
const titleCode = props . title ? `<title>${ props . title } </title>` : 'undefined'
101
112
102
113
const code = computed ( ( ) =>
103
- Array . isArray ( _icon )
104
- ? _icon
105
- : typeof _icon === 'string' && iconName . value && icons [ iconName . value ]
106
- ? icons [ iconName . value ]
107
- : 'undefined' ,
114
+ Array . isArray ( _icon . value )
115
+ ? _icon . value
116
+ : typeof _icon . value === 'string' && iconName . value && icons [ iconName . value ]
117
+ ? icons [ iconName . value ]
118
+ : 'undefined' ,
108
119
)
109
120
110
- const iconCode = Array . isArray ( code . value ) ? code . value [ 1 ] || code . value [ 0 ] : code . value
121
+ const iconCode = computed ( ( ) =>
122
+ Array . isArray ( code . value ) ? code . value [ 1 ] || code . value [ 0 ] : code . value ,
123
+ )
111
124
112
125
const scale = Array . isArray ( code . value ) && code . value . length > 1 ? code . value [ 0 ] : '64 64'
113
126
@@ -139,7 +152,7 @@ const CIcon = defineComponent({
139
152
xmlns : 'http://www.w3.org/2000/svg' ,
140
153
class : classNames ,
141
154
viewBox : viewBox ,
142
- innerHTML : `${ titleCode } ${ iconCode } ` ,
155
+ innerHTML : `${ titleCode } ${ iconCode . value } ` ,
143
156
role : 'img' ,
144
157
} )
145
158
} ,
0 commit comments