Skip to content

Commit d7b99d2

Browse files
committed
feat(CIcon): allow icon change
1 parent 80cfe9d commit d7b99d2

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/CIcon.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, defineComponent, h, inject, PropType } from 'vue'
1+
import { computed, defineComponent, h, inject, PropType, ref, watch } from 'vue'
22

33
const CIcon = defineComponent({
44
name: 'CIcon',
@@ -83,7 +83,14 @@ const CIcon = defineComponent({
8383
},
8484
setup(props, { attrs }) {
8585
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+
)
8794

8895
const toCamelCase = (str: string) => {
8996
return str
@@ -94,20 +101,26 @@ const CIcon = defineComponent({
94101
}
95102

96103
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+
: '',
98109
)
99110

100111
const titleCode = props.title ? `<title>${props.title}</title>` : 'undefined'
101112

102113
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',
108119
)
109120

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+
)
111124

112125
const scale = Array.isArray(code.value) && code.value.length > 1 ? code.value[0] : '64 64'
113126

@@ -139,7 +152,7 @@ const CIcon = defineComponent({
139152
xmlns: 'http://www.w3.org/2000/svg',
140153
class: classNames,
141154
viewBox: viewBox,
142-
innerHTML: `${titleCode}${iconCode}`,
155+
innerHTML: `${titleCode}${iconCode.value}`,
143156
role: 'img',
144157
})
145158
},

0 commit comments

Comments
 (0)