Skip to content

Commit b1a5118

Browse files
refactor(VariantManagement): support SSR (#4272)
Part of #4091
1 parent a5d9aa8 commit b1a5118

File tree

9 files changed

+258
-317
lines changed

9 files changed

+258
-317
lines changed

packages/main/src/components/VariantManagement/CodeGen.tsx

Lines changed: 0 additions & 79 deletions
This file was deleted.

packages/main/src/components/VariantManagement/ManageViewsDialog.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import {
1717
VIEW,
1818
SEARCH
1919
} from '../../i18n/i18n-defaults';
20+
import { useCanRenderPortal } from '../../internal/ssr';
2021
import { Icon, Input } from '../../webComponents';
2122
import { Bar } from '../../webComponents/Bar';
2223
import { Button } from '../../webComponents/Button';
23-
import { Dialog, DialogDomRef } from '../../webComponents/Dialog';
24+
import { Dialog } from '../../webComponents/Dialog';
2425
import { Table } from '../../webComponents/Table';
2526
import { TableColumn } from '../../webComponents/TableColumn';
2627
import { FlexBox } from '../FlexBox';
@@ -136,14 +137,6 @@ export const ManageViewsDialog = (props: ManageViewsDialogPropTypes) => {
136137
<TableColumn key="delete-variant-item" />
137138
</>
138139
);
139-
const manageViewsRef = useRef<DialogDomRef>(null);
140-
141-
useEffect(() => {
142-
manageViewsRef.current.show();
143-
return () => {
144-
manageViewsRef.current?.close();
145-
};
146-
}, []);
147140

148141
const [childrenProps, setChildrenProps] = useState(
149142
Children.map(children, (child) => {
@@ -217,11 +210,16 @@ export const ManageViewsDialog = (props: ManageViewsDialogPropTypes) => {
217210
);
218211
};
219212

213+
const canRenderPortal = useCanRenderPortal();
214+
if (!canRenderPortal) {
215+
return null;
216+
}
217+
220218
return createPortal(
221219
<Dialog
220+
open
222221
className={classes.manageViewsDialog}
223222
data-component-name="VariantManagementManageViewsDialog"
224-
ref={manageViewsRef}
225223
onAfterClose={onAfterClose}
226224
headerText={manageViewsText}
227225
header={
@@ -277,6 +275,6 @@ export const ManageViewsDialog = (props: ManageViewsDialogPropTypes) => {
277275
})}
278276
</Table>
279277
</Dialog>,
280-
portalContainer
278+
portalContainer ?? document.body
281279
);
282280
};

packages/main/src/components/VariantManagement/ManageViewsTableRows.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import favoriteIcon from '@ui5/webcomponents-icons/dist/favorite.js';
33
import unfavoriteIcon from '@ui5/webcomponents-icons/dist/unfavorite.js';
44
import { ThemingParameters, useI18nBundle } from '@ui5/webcomponents-react-base';
55
import React, { useReducer, useRef, useState } from 'react';
6-
import { ButtonDesign } from '../../enums/ButtonDesign';
7-
import { ValueState } from '../../enums/ValueState';
6+
import { ButtonDesign, ValueState } from '../../enums';
87
import {
98
APPLY_AUTOMATICALLY,
109
DELETE_VIEW,
@@ -19,13 +18,7 @@ import {
1918
VIEW
2019
} from '../../i18n/i18n-defaults';
2120
import { trimAndRemoveSpaces } from '../../internal/utils';
22-
import { Button } from '../../webComponents/Button';
23-
import { CheckBox } from '../../webComponents/CheckBox';
24-
import { Icon } from '../../webComponents/Icon';
25-
import { Input } from '../../webComponents/Input';
26-
import { RadioButton } from '../../webComponents/RadioButton';
27-
import { TableCell } from '../../webComponents/TableCell';
28-
import { TableRow } from '../../webComponents/TableRow';
21+
import { Button, CheckBox, Icon, Input, RadioButton, TableCell, TableRow } from '../../webComponents';
2922
import { Text } from '../Text';
3023
import { VariantItemPropTypes } from './VariantItem';
3124

packages/main/src/components/VariantManagement/SaveViewDialog.tsx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { useI18nBundle, useIsomorphicId } from '@ui5/webcomponents-react-base';
22
import { clsx } from 'clsx';
3-
import React, { useEffect, useRef, useState } from 'react';
3+
import React, { useRef, useState } from 'react';
44
import { createPortal } from 'react-dom';
55
import { createUseStyles } from 'react-jss';
6-
import { BarDesign } from '../../enums';
7-
import { ButtonDesign } from '../../enums/ButtonDesign';
8-
import { FlexBoxAlignItems } from '../../enums/FlexBoxAlignItems';
9-
import { FlexBoxDirection } from '../../enums/FlexBoxDirection';
6+
import { BarDesign, ButtonDesign, FlexBoxAlignItems, FlexBoxDirection } from '../../enums';
107
import {
118
APPLY_AUTOMATICALLY,
129
CANCEL,
@@ -19,14 +16,20 @@ import {
1916
VIEW
2017
} from '../../i18n/i18n-defaults';
2118
import { Ui5CustomEvent } from '../../interfaces/Ui5CustomEvent';
19+
import { useCanRenderPortal } from '../../internal/ssr';
2220
import { trimAndRemoveSpaces } from '../../internal/utils';
2321
import { SelectedVariant } from '../../internal/VariantManagementContext';
24-
import { Bar } from '../../webComponents/Bar';
25-
import { Button, ButtonDomRef } from '../../webComponents/Button';
26-
import { CheckBox } from '../../webComponents/CheckBox';
27-
import { Dialog, DialogDomRef } from '../../webComponents/Dialog';
28-
import { Input, InputPropTypes } from '../../webComponents/Input';
29-
import { Label } from '../../webComponents/Label';
22+
import {
23+
Bar,
24+
Button,
25+
ButtonDomRef,
26+
CheckBox,
27+
Dialog,
28+
DialogDomRef,
29+
Input,
30+
InputPropTypes,
31+
Label
32+
} from '../../webComponents';
3033
import { FlexBox } from '../FlexBox';
3134

3235
const useStyles = createUseStyles(
@@ -137,15 +140,14 @@ export const SaveViewDialog = (props: SaveViewDialogPropTypes) => {
137140
setApplyAutomatically(e.target.checked);
138141
};
139142

140-
useEffect(() => {
141-
saveViewDialogRef.current.show();
142-
return () => {
143-
saveViewDialogRef.current?.close();
144-
};
145-
}, []);
143+
const canRenderPortal = useCanRenderPortal();
144+
if (!canRenderPortal) {
145+
return null;
146+
}
146147

147148
return createPortal(
148149
<Dialog
150+
open
149151
className={classes.dialog}
150152
ref={saveViewDialogRef}
151153
headerText={headingText}
@@ -198,6 +200,6 @@ export const SaveViewDialog = (props: SaveViewDialogPropTypes) => {
198200
</FlexBox>
199201
</FlexBox>
200202
</Dialog>,
201-
portalContainer
203+
portalContainer ?? document.body
202204
);
203205
};

packages/main/src/components/VariantManagement/TestComp.tsx

Lines changed: 0 additions & 65 deletions
This file was deleted.

packages/main/src/components/VariantManagement/VariantManagement.cy.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { useState } from 'react';
22
import { TitleLevel } from '../../enums';
3-
import { VariantManagementWithCustomValidation } from './CodeGen';
43
import { VariantItem } from './VariantItem';
4+
import { WithCustomValidation as WithCustomValidationStory } from './VariantManagement.stories';
55
import { VariantManagement, VariantManagementPropTypes } from './index';
66
import { cypressPassThroughTestsFactory } from '@/cypress/support/utils';
77

8+
const WithCustomValidation = WithCustomValidationStory.render;
9+
810
const TwoVariantItems = [
911
<VariantItem key="0">VariantItem 1</VariantItem>,
1012
<VariantItem selected key="1">
@@ -96,7 +98,7 @@ describe('VariantManagement', () => {
9698

9799
it('saveViewInputProps & manageViewsInputProps', () => {
98100
// manageViewsInputProps
99-
cy.mount(<VariantManagementWithCustomValidation />);
101+
cy.mount(<WithCustomValidation />);
100102
cy.contains('Max 12 chars').click();
101103
cy.findByText('Manage').click();
102104
cy.get('[ui5-dialog]').should('be.visible');
@@ -113,7 +115,8 @@ describe('VariantManagement', () => {
113115
cy.contains('Max 12 charB').should('be.visible');
114116

115117
//saveViewInputProps
116-
cy.mount(<VariantManagementWithCustomValidation selectedByIndex={0} />);
118+
// @ts-expect-error: not a real prop, just for testing
119+
cy.mount(<WithCustomValidation selectedByIndex={0} />);
117120
cy.contains('Only alphanumeric chars in Save View input').click();
118121
cy.findByText('Save As').click();
119122
cy.get('[ui5-dialog]').should('be.visible');

0 commit comments

Comments
 (0)