@sitecore-feaas/clientside 0.3.7 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -109,10 +109,19 @@ export default class FEAASExternal extends FEAASElement<FEAASExternalProps, Reac
109
109
  return Promise.resolve({ React, ReactDOM })
110
110
  }
111
111
 
112
+ update() {
113
+ const { dataExternalId: id } = this.props
114
+ const Component = FEAASExternal.registered[id]?.component
115
+ if (!Component && this.childNodes.length > 0) return
116
+ return super.update()
117
+ }
118
+
112
119
  render(props: FEAASExternalProps, { React }: ReactModules): ReactJSX {
113
120
  const { dataExternalId: id } = props
114
121
  const Component = FEAASExternal.registered[id]?.component
115
- if (!Component) console.warn('FEAASExternal: The component you requested is not registered')
122
+ if (!Component) {
123
+ console.warn('FEAASExternal: The component you requested is not registered')
124
+ }
116
125
  return <>{Component && <Component {...props} />}</>
117
126
  }
118
127
 
package/src/ui/react.tsx CHANGED
@@ -40,12 +40,16 @@ export function FEAASReactComponent({ data, ...props }: FEAASReactComponentProps
40
40
  let children: null | React.ReactNode
41
41
 
42
42
  if ('template' in props && props.template) {
43
- children = parseHTMLContent(props.template, data || {}).map((node) =>
44
- HTMLNodeToReact(node, (node) => {
45
- if (node.localName == 'feaas-external' && getComponent(node.getAttribute('data-external-id'))) {
46
- return [FEAASReactExternal, {}]
47
- }
48
- })
43
+ children = parseHTMLContent(props.template, data || {}).map((node, index) =>
44
+ HTMLNodeToReact(
45
+ node,
46
+ (node) => {
47
+ if (node.localName == 'feaas-external' && getComponent(node.getAttribute('data-external-id'))) {
48
+ return [FEAASReactExternal, {}]
49
+ }
50
+ },
51
+ index
52
+ )
49
53
  )
50
54
  }
51
55
 
@@ -63,7 +67,7 @@ export function FEAASReactComponent({ data, ...props }: FEAASReactComponentProps
63
67
  )}
64
68
  <feaas-component
65
69
  {...props}
66
- suppressHydrationWarning={true}
70
+ suppressHydrationWarning
67
71
  template={null}
68
72
  data={typeof data == 'string' ? data : data ? JSON.stringify(data) : null}
69
73
  >
@@ -75,9 +79,10 @@ export function FEAASReactComponent({ data, ...props }: FEAASReactComponentProps
75
79
 
76
80
  export function FEAASReactExternal(props: { 'data-external-id': string }) {
77
81
  const Component = getComponent(props['data-external-id'])?.component
82
+ // execute component as a function to supress hydration errors inside
78
83
  return (
79
- <feaas-external {...props}>
80
- <Component {...props} />
84
+ <feaas-external {...props} suppressHydrationWarning>
85
+ {typeof Component == 'function' ? Component({ ...props }) : <Component {...props} />}
81
86
  </feaas-external>
82
87
  )
83
88
  }
@@ -85,12 +90,13 @@ export function FEAASReactExternal(props: { 'data-external-id': string }) {
85
90
  /** Turn HTMLNode into React tree. It accepts callback to customize the behavior */
86
91
  export function HTMLNodeToReact(
87
92
  node: HTMLNode,
88
- callback?: (node: HTMLNode) => [tagName: any, attributes: Record<string, any>]
93
+ callback?: (node: HTMLNode) => [tagName: any, attributes: Record<string, any>],
94
+ key?: any
89
95
  ): React.ReactNode {
90
96
  if (node.nodeType === 3) {
91
97
  return node.textContent
92
98
  }
93
- const children = Array.from(node.childNodes).map((child) => HTMLNodeToReact(child, callback))
99
+ const children = Array.from(node.childNodes).map((child, index) => HTMLNodeToReact(child, callback, index))
94
100
 
95
101
  const attributes: { [key: string]: string } = {}
96
102
  for (let i = 0; i < node.attributes.length; i++) {
@@ -100,7 +106,7 @@ export function HTMLNodeToReact(
100
106
 
101
107
  const reactNode = callback?.(node)
102
108
  const reactName = reactNode?.[0] || node.nodeName.toLowerCase()
103
- const reactAttributes = Object.assign(attributes, reactName?.[1] || {})
109
+ const reactAttributes = Object.assign({ key, suppressHydrationWarning: true }, attributes, reactNode?.[1] || {})
104
110
  return React.createElement(reactName, reactAttributes, ['img', 'br', 'input'].includes(reactName) ? null : children)
105
111
  }
106
112