1
- import React from 'react' ;
2
- import PropTypes from 'prop-types' ;
3
- import classNames from 'classnames' ;
4
- import { mapToCssModules , colog } from './Shared/helper' ;
5
- import style from './CIcon.module.css' ;
1
+ import React , { useMemo , useState } from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import classNames from 'classnames'
4
+ import { mapToCssModules , colog } from './Shared/helper'
6
5
7
- //component - CoreUI / CIconRaw
6
+ const toCamelCase = ( str ) => {
7
+ return str . replace ( / ( [ - _ ] [ a - z 0 - 9 ] ) / ig, ( $1 ) => {
8
+ return $1 . toUpperCase ( )
9
+ } ) . replaceAll ( '-' , '' )
10
+ }
8
11
9
- const CIconRaw = props => {
12
+ //component - CoreUI / CIconRaw
13
+ const CIconRaw = props => {
10
14
11
15
const {
12
16
className,
17
+ cssModule,
13
18
//
14
19
name,
15
20
content,
@@ -19,120 +24,102 @@ const CIconRaw = props=>{
19
24
title,
20
25
use,
21
26
...attributes
22
- } = props ;
23
-
24
- // methods
27
+ } = props
25
28
26
- const toCamelCase = ( str ) => {
27
- return str . replace ( / ( [ - _ ] [ a - z 0 - 9 ] ) / ig, ( $1 ) => {
28
- return $1 . toUpperCase ( ) . replace ( '-' , '' )
29
- } )
30
- } ;
29
+ const [ change , setChange ] = useState ( 0 )
31
30
32
- //vars
31
+ useMemo ( ( ) => setChange ( change + 1 ) , [ name , JSON . stringify [ content ] ] )
33
32
34
- const iconName = ( ( ) => {
33
+ const iconName = useMemo ( ( ) => {
35
34
const iconNameIsKebabCase = name && name . includes ( '-' ) ;
36
35
return iconNameIsKebabCase ? toCamelCase ( name ) : name
37
- } ) ( ) ;
38
- const titleCode = ( ( ) => {
39
- return title ? `<title>${ title } </title>` : ''
40
- } ) ( ) ;
41
- const code = ( ( ) => {
36
+ } , [ change ] )
37
+
38
+ const titleCode = title ? `<title>${ title } </title>` : ''
39
+
40
+ const code = useMemo ( ( ) => {
42
41
if ( content ) {
43
42
return content
44
- } else if ( React . icons ) {
45
- return React . icons [ iconName ] ?
46
- React . icons [ iconName ] :
47
- React . icons [ 'cil' + iconName ] ?
48
- React . icons [ 'cil' + iconName ] :
49
- colog ( 'Not existing icon: ' + iconName )
43
+ } else if ( name && React . icons ) {
44
+ return React . icons [ iconName ] ? React . icons [ iconName ] :
45
+ colog ( 'Not existing icon: ' + iconName + ' in React.icons object' )
50
46
}
51
- return content ;
52
- } ) ( ) ;
53
- const iconCode = ( ( ) => {
47
+ } , [ change ] )
48
+
49
+ const iconCode = useMemo ( ( ) => {
54
50
return Array . isArray ( code ) ? code [ 1 ] || code [ 0 ] : code
55
- } ) ( ) ;
51
+ } , [ change ] )
52
+
56
53
const scale = ( ( ) => {
57
54
return Array . isArray ( code ) && code . length > 1 ? code [ 0 ] : '64 64'
58
- } ) ( ) ;
55
+ } ) ( )
56
+
59
57
const viewBox = ( ( ) => {
60
58
return attributes . viewBox || `0 0 ${ scale } `
61
- } ) ( ) ;
59
+ } ) ( )
60
+
62
61
const computedSize = ( ( ) => {
63
62
const addCustom = ! size && ( attributes . width || attributes . height ) ;
64
63
return size === 'custom' || addCustom ? 'custom-size' : size
65
- } ) ( ) ;
66
- //console.log(iconName, computedSize)
67
- /*
68
- computedClasses () {
69
- const size = this.computedSize
70
- return this.customClasses || ['c-icon', { [`c-icon-${size}`]: size }]
71
- }
72
- */
64
+ } ) ( )
73
65
74
66
//render
75
-
76
- const class1 = mapToCssModules ( classNames (
77
- className ,
67
+ const computedClasses = mapToCssModules ( classNames (
78
68
'c-icon' ,
79
- computedSize ? `c-icon-${ computedSize } ` : ''
80
- ) ) ;
81
-
82
- const class2 = mapToCssModules ( classNames (
83
- 'c-icon' ,
84
- computedSize ? `c-icon-${ computedSize } ` : ''
85
- ) , style ) ;
86
-
87
- const classes = customClasses || ( class1 + ' ' + class2 ) ;
69
+ computedSize && `c-icon-${ computedSize } ` ,
70
+ className
71
+ ) , cssModule )
88
72
89
- //console.log(code, viewBox, classes, titleCode+' - '+iconCode);
90
- //v-html={titleCode+iconCode}
73
+ const classes = customClasses || computedClasses
91
74
92
75
return (
93
76
< React . Fragment >
94
- { ! src && ! use ?
95
- < svg
96
- { ...attributes }
97
- xmlns = "http://www.w3.org/2000/svg"
98
- viewBox = { viewBox }
99
- className = { classes }
100
- role = "img"
101
- dangerouslySetInnerHTML = { { __html : titleCode + iconCode } }
102
- > </ svg > : src ?
103
- < img
104
- { ...attributes }
105
- src = { src }
106
- role = "img"
107
- /> : use ?
108
- < svg
109
- { ...attributes }
110
- xmlns = "http://www.w3.org/2000/svg"
111
- className = { classes }
112
- role = "img"
113
- >
114
- < use href = { use } > </ use >
115
- </ svg > : '' }
77
+ { ! src && ! use &&
78
+ < svg
79
+ { ...attributes }
80
+ xmlns = "http://www.w3.org/2000/svg"
81
+ viewBox = { viewBox }
82
+ className = { classes }
83
+ role = "img"
84
+ dangerouslySetInnerHTML = { { __html : titleCode + iconCode } }
85
+ />
86
+ }
87
+ { src && ! use &&
88
+ < img
89
+ { ...attributes }
90
+ className = { className }
91
+ src = { src }
92
+ role = "img"
93
+ />
94
+ }
95
+ { ! src && use &&
96
+ < svg
97
+ { ...attributes }
98
+ xmlns = "http://www.w3.org/2000/svg"
99
+ className = { classes }
100
+ role = "img"
101
+ >
102
+ < use href = { use } > </ use >
103
+ </ svg >
104
+ }
116
105
</ React . Fragment >
117
- ) ;
118
-
119
- } ;
106
+ )
107
+ }
120
108
121
109
CIconRaw . propTypes = {
122
- tag : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . string ] ) ,
123
110
className : PropTypes . string ,
111
+ cssModule : PropTypes . object ,
124
112
//
125
113
name : PropTypes . string ,
126
114
content : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . array ] ) ,
127
- size : PropTypes . oneOf ( [ 'custom' , 'custom-size' , 'sm' , 'lg' , 'xl' ,
128
- '2xl' , '3xl' , '4xl' , '5xl' , '6xl' , '7xl' , '8xl' , '9xl' ] ) ,
115
+ size : PropTypes . oneOf ( [
116
+ 'custom' , 'custom-size' , 'sm' , 'lg' , 'xl' ,
117
+ '2xl' , '3xl' , '4xl' , '5xl' , '6xl' , '7xl' , '8xl' , '9xl'
118
+ ] ) ,
129
119
customClasses : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . array , PropTypes . object ] ) ,
130
120
src : PropTypes . string ,
131
121
title : PropTypes . string ,
132
122
use : PropTypes . string
133
123
} ;
134
124
135
- CIconRaw . defaultProps = {
136
- } ;
137
-
138
- export default CIconRaw ;
125
+ export default CIconRaw
0 commit comments