Skip to content

Commit 0ef3216

Browse files
memoize uiCompBuilder
1 parent cd948af commit 0ef3216

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

client/packages/lowcoder/src/comps/generators/uiCompBuilder.tsx

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BoolCodeControl, StringControl } from "comps/controls/codeControl";
2-
import React, { ReactNode, useContext, useEffect, useRef, useState } from "react";
2+
import React, { ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
33
import { ExternalEditorContext } from "util/context/ExternalEditorContext";
44
import { Comp, CompParams, MultiBaseComp } from "lowcoder-core";
55
import {
@@ -28,7 +28,7 @@ import { BoolControl } from "../controls/boolControl";
2828
import { valueComp, withDefault } from "./simpleGenerators";
2929
import { getPromiseAfterDispatch } from "@lowcoder-ee/util/promiseUtils";
3030
import { EditorContext } from "../editorState";
31-
import { values } from "lodash";
31+
import { isEqual, values } from "lodash";
3232
import { UICompType, uiCompRegistry } from "../uiCompRegistry";
3333
import { getNpmPackageMeta } from "../utils/remote";
3434
import { compPluginsList } from "constants/compPluginConstants";
@@ -289,44 +289,34 @@ const UIView = React.memo((props: {
289289
childrenProps['disabled'] = disabled || parentDisabled;
290290
}
291291

292-
//ADDED BY FRED
293-
if (childrenProps.events) {
294-
const events = childrenProps.events as {value?: any[]};
295-
if (!events.value || events.value.length === 0) {
296-
events.value = [];
297-
}
298-
}
299-
//END ADD BY FRED
300-
301292
useMergeCompStyles(
302293
childrenJsonProps as Record<string, any>,
303294
comp.dispatch
304295
);
305296

306-
// render condition for modal and drawer as we are not getting compType here
307-
if (comp.children.hasOwnProperty('showMask') && comp.children.hasOwnProperty('maskClosable')) {
308-
return (
309-
<HidableView hidden={childrenProps.hidden as boolean}>
310-
{props.viewFn(
311-
childrenProps,
312-
comp.dispatch
313-
)}
314-
</HidableView>
315-
);
316-
}
297+
const defaultChildren = useMemo(() => comp.children, [comp.children]);
298+
const isNotContainer = useMemo(() => Boolean(defaultChildren.style), [defaultChildren.style]);
299+
const restrictPaddingOnRotation = useMemo(() => Boolean(defaultChildren.restrictPaddingOnRotation), [defaultChildren.restrictPaddingOnRotation]);
300+
const rotationVal = useMemo(() => {
301+
if (isNotContainer) {
302+
return defaultChildren.style.children?.rotation?.valueAndMsg.value
303+
}
304+
return null;
305+
}, [isNotContainer, defaultChildren.style.children?.rotation?.valueAndMsg.value]);
306+
const boxShadowVal = useMemo(() => {
307+
if (isNotContainer) {
308+
return defaultChildren.style?.children?.boxShadow?.valueAndMsg?.value;
309+
}
310+
return null;
311+
}, [isNotContainer, defaultChildren.style?.children?.boxShadow?.valueAndMsg?.value]);
312+
const restrictPaddingOnRotationVal = useMemo(() => {
313+
if (isNotContainer) {
314+
return defaultChildren?.restrictPaddingOnRotation?.valueAndMsg?.value
315+
}
316+
return null;
317+
}, [isNotContainer, defaultChildren?.restrictPaddingOnRotation?.valueAndMsg?.value]);
317318

318-
let defaultChildren = comp.children;
319-
const isNotContainer = defaultChildren.hasOwnProperty('style');
320-
const restrictPaddingOnRotation = defaultChildren.hasOwnProperty('restrictPaddingOnRotation');
321-
let rotationVal:any = null
322-
let boxShadowVal:any = null;
323-
let restrictPaddingOnRotationVal:any=null;
324-
if (isNotContainer) {
325-
rotationVal = defaultChildren.style.children?.rotation?.valueAndMsg.value;
326-
boxShadowVal = defaultChildren.style?.children?.boxShadow?.valueAndMsg?.value;
327-
restrictPaddingOnRotationVal = defaultChildren?.restrictPaddingOnRotation?.valueAndMsg?.value;
328-
}
329-
const getPadding = () => {
319+
const getPadding = useCallback(() => {
330320
if (
331321
(rotationVal === null ||
332322
rotationVal === undefined ||
@@ -386,9 +376,22 @@ const UIView = React.memo((props: {
386376
} else {
387377
return '0px'; // Default value if neither rotation nor box-shadow is applied
388378
}
389-
};
379+
}, [
380+
rotationVal,
381+
boxShadowVal,
382+
restrictPaddingOnRotationVal,
383+
restrictPaddingOnRotation,
384+
]);
390385

391-
return (
386+
// render condition for modal and drawer as we are not getting compType here
387+
388+
const uiCompRender = useMemo(() => props.viewFn(childrenProps, comp.dispatch), [
389+
childrenProps,
390+
comp.dispatch
391+
]);
392+
393+
const uiCompRenderWithWrapper = useMemo(() => {
394+
return (
392395
<div
393396
ref={props.innerRef}
394397
className={childrenProps.className as string}
@@ -400,9 +403,27 @@ const UIView = React.memo((props: {
400403
padding: getPadding()
401404
}}
402405
>
403-
<HidableView hidden={childrenProps.hidden as boolean}>
404-
{props.viewFn(childrenProps, comp.dispatch)}
405-
</HidableView>
406+
{uiCompRender}
406407
</div>
407-
);
408+
)}, [
409+
uiCompRender,
410+
props.innerRef,
411+
childrenProps.className,
412+
childrenProps.dataTestId,
413+
getPadding,
414+
]);
415+
416+
const renderView = useMemo(() => {
417+
if (
418+
comp.children.hasOwnProperty('showMask')
419+
&& comp.children.hasOwnProperty('maskClosable')
420+
) {
421+
return uiCompRender;
422+
}
423+
return uiCompRenderWithWrapper;
424+
}, [comp.children]);
425+
426+
return renderView;
427+
}, (prevProps, nextProps) => {
428+
return isEqual(prevProps, nextProps);
408429
});

0 commit comments

Comments
 (0)