1
- import React from 'react'
1
+ import React , { useMemo } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
4
- const renderItem = ( item , i ) => {
5
- const { _tag : Tag , _children, ...rest } = item
6
- const children = _children ? _children . map ( ( child , i ) => {
7
- return typeof child === 'object' ? renderItem ( child , i ) : child
8
- } ) : ''
9
- return < Tag key = { Tag + i } { ...rest } > { children } </ Tag >
10
- }
11
-
12
4
//component - CoreUI / CCreateElement
13
- const CCreateElement = props => {
5
+ const CCreateElement = ( { items, components = { } } ) => {
6
+ const renderItem = ( item , i ) => {
7
+ const { _tag, _children, ...rest } = item
8
+ const Tag = components [ _tag ] || _tag
9
+ const children = _children ? _children . map ( ( child , i ) => {
10
+ return typeof child === 'object' ? renderItem ( child , i ) : child
11
+ } ) : ''
12
+ return < Tag key = { Tag + i } { ...rest } > { children } </ Tag >
13
+ }
14
+
15
+ const generatedItems = useMemo ( ( ) => {
16
+ return items && items . map ( ( item , i ) => renderItem ( item , i ) )
17
+ } , [ JSON . stringify ( items ) ] )
18
+
14
19
return (
15
20
< React . Fragment >
16
- { props . items && props . items . map ( ( item , i ) => renderItem ( item , i ) ) }
21
+ { generatedItems }
17
22
</ React . Fragment >
18
23
)
19
24
}
20
25
21
26
CCreateElement . propTypes = {
22
- items : PropTypes . array . isRequired
27
+ items : PropTypes . array . isRequired ,
28
+ components : PropTypes . object
23
29
} ;
24
30
25
31
export default CCreateElement
0 commit comments