Skip to content

Commit 2bcc163

Browse files
committed
fix: adjust Web Components to React 18
1 parent a914c4c commit 2bcc163

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

src/shared/components/DynamicPageComponent/DynamicPageComponent.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ export const DynamicPageComponent = ({
335335
className="tab-container"
336336
style={{ top: `${headerHeight}px` }}
337337
ref={tabContainerRef}
338+
collapsed
338339
onTabSelect={e => {
339340
if (customActionIfFormOpen) {
340341
customActionIfFormOpen(
@@ -343,9 +344,9 @@ export const DynamicPageComponent = ({
343344
isFormOpen,
344345
setIsFormOpen,
345346
);
347+
setSelectedSectionIdState(e.detail.tab.getAttribute('data-mode'));
346348
return;
347349
}
348-
349350
if (isFormOpen.formOpen) {
350351
e.preventDefault();
351352
}

src/web-components/DynamicPageWebComponent.jsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import createWebComponent from './createWebComponent';
33
import { DynamicPageComponent } from 'shared/components/DynamicPageComponent/DynamicPageComponent';
44
import { ThemeProvider } from '@ui5/webcomponents-react';
55
import customCSS from 'shared/components/DynamicPageComponent/DynamicPageComponent.scss?inline';
6-
import { styleData as pageCSS } from '@ui5/webcomponents-react/dist/components/DynamicPage/DynamicPage.module.css.js?inline';
7-
import { styleData as titleCSS } from '@ui5/webcomponents-react/dist/components/DynamicPageTitle/DynamicPageTitle.module.css.js?inline';
8-
import { styleData as headerCSS } from '@ui5/webcomponents-react/dist/components/DynamicPageHeader/DynamicPageHeader.module.css.js?inline';
9-
import objectPageCSS from '@ui5/webcomponents-react/dist/css/components/ObjectPage/ObjectPage.module.css?inline';
106
import { parseHtmlToJsx } from './htmlTojsx';
117

128
function DynamicPageWithRecoil(props) {
@@ -66,18 +62,5 @@ createWebComponent(
6662
'class-name',
6763
'custom-action-if-form-open',
6864
], // Observed attributes
69-
// Can be cleaned up after migration to UI5 V2
70-
`${pageCSS.content}\n${headerCSS.content}\n${titleCSS.content}\n${objectPageCSS}\n${customCSS}\n
71-
[data-component-name=ObjectPageTopHeader] [data-component-name=DynamicPageTitle] {
72-
grid-column: 2;
73-
padding-inline: 0;
74-
}
75-
[data-component-name=ObjectPageTopHeader] {
76-
padding-inline: 2rem;
77-
background-color: var(--sapObjectHeader_Background);
78-
}
79-
ui5-title[level=H3] {
80-
line-height: calc(var(--sapFontHeader3Size)* 1.15);
81-
}
82-
`,
65+
`${customCSS}\n`,
8366
);

src/web-components/createWebComponent.jsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ReactDOM from 'react-dom';
1+
import { createRoot } from 'react-dom/client';
22
import { parseHtmlToJsx } from './htmlTojsx';
33

44
function kebabToCamelCase(str) {
@@ -16,17 +16,32 @@ function createWebComponent(
1616
constructor() {
1717
super();
1818
this.reactRoot = null;
19+
this.reactRootInstance = null;
1920
this._props = {};
2021
this._slots = {};
2122
}
2223

2324
connectedCallback() {
25+
if (!this.reactRoot) {
26+
this.reactRoot = document.createElement('div');
27+
this.appendChild(this.reactRoot);
28+
this.reactRootInstance = createRoot(this.reactRoot); // Initialize root
29+
}
30+
2431
this.applyStyles();
2532
this.mountReactComponent();
2633
}
2734

2835
disconnectedCallback() {
29-
ReactDOM.unmountComponentAtNode(this.reactRoot);
36+
// Delay unmounting to avoid conflicts with React rendering
37+
if (this.reactRootInstance) {
38+
setTimeout(() => {
39+
if (this.reactRootInstance) {
40+
this.reactRootInstance.unmount();
41+
this.reactRootInstance = null; // Clean up instance
42+
}
43+
}, 0);
44+
}
3045
}
3146

3247
static get observedAttributes() {
@@ -76,11 +91,6 @@ function createWebComponent(
7691
}
7792

7893
mountReactComponent() {
79-
if (!this.reactRoot) {
80-
this.reactRoot = document.createElement('div');
81-
this.appendChild(this.reactRoot);
82-
}
83-
8494
// Generate props from attributes
8595
const propsFromAttributes = observedAttributes.reduce((acc, attr) => {
8696
const attrValue = this.getAttribute(attr);
@@ -122,10 +132,9 @@ function createWebComponent(
122132
}
123133
});
124134

125-
ReactDOM.render(
126-
<ReactComponent {...defaultProps} {...props} />,
127-
this.reactRoot,
128-
);
135+
if (this.reactRootInstance) {
136+
this.reactRootInstance.render(<ReactComponent {...props} />);
137+
}
129138
}
130139
}
131140

0 commit comments

Comments
 (0)