Skip to content

Commit b00111c

Browse files
added memoization
1 parent 614e12c commit b00111c

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

lowcoder-comp-kanban/src/kanbanCompView.tsx

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext, useEffect, useRef, useState } from "react";
1+
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
22
import { KanbanImplComp } from "./KabanComp";
33
import {
44
EditorContext,
@@ -54,7 +54,7 @@ const getString = (assignee: string): string => {
5454
return (assignee.match(/\b(\w)/g) as string[]).join("").toUpperCase();
5555
};
5656

57-
const columnTemplate = (props: {
57+
const ColumnTemplate = React.memo((props: {
5858
data: { [key: string]: string },
5959
boardStyles: { [key: string]: string }
6060
}) => {
@@ -72,7 +72,7 @@ const columnTemplate = (props: {
7272
</div>
7373
</div>
7474
);
75-
};
75+
});
7676

7777
const cardRendered = (props: {
7878
args: CardRenderedEventArgs,
@@ -88,7 +88,7 @@ const cardRendered = (props: {
8888
addClass([cardElement], val);
8989
};
9090

91-
const cardTemplate = (props: {
91+
const CardTemplate = React.memo((props: {
9292
data: { [key: string]: string },
9393
cardIndex: number;
9494
childrenProps: any;
@@ -161,34 +161,37 @@ const cardTemplate = (props: {
161161
</div>
162162
</Wrapper>
163163
);
164-
};
164+
});
165165

166166
type Props = {
167167
comp: InstanceType<typeof KanbanImplComp>;
168168
};
169169

170-
export function KanbanCompView(props: Props) {
170+
export const KanbanCompView = React.memo((props: Props) => {
171171
const { comp } = props;
172-
const childrenProps = childrenToProps(comp.children);
172+
const childrenProps = useMemo(() =>
173+
childrenToProps(comp.children),
174+
[childrenToProps, comp.children],
175+
);
173176

174177
const [dataMap, setDataMap] = useState<Record<string, number>>({});
175178
const [isModalOpen, setIsModalOpen] = useState(false);
176179

177-
const updateDataMap = () => {
180+
const updateDataMap = useCallback(() => {
178181
const mapData: Record<string, number> = {};
179182
childrenProps.data?.forEach((item: any, index: number) => {
180183
mapData[item.Id] = index;
181184
})
182185
setDataMap(mapData);
183-
}
186+
}, [childrenProps.data, setDataMap]);
184187

185188
useEffect(() => {
186189
updateDataMap();
187-
}, []);
190+
}, [updateDataMap]);
188191

189192
useEffect(() => {
190193
updateDataMap();
191-
}, [JSON.stringify(childrenProps.data)]);
194+
}, [JSON.stringify(childrenProps.data), updateDataMap]);
192195

193196
console.log("🚀 ~ returnnewContainerCompBuilder ~ props:", props)
194197

@@ -293,6 +296,7 @@ export function KanbanCompView(props: Props) {
293296
onOk={handleOk}
294297
onCancel={handleCancel}
295298
okText="Update"
299+
maskClosable={false}
296300
>
297301
<Flex vertical gap={10}>
298302
<Typography.Title level={5}>Title</Typography.Title>
@@ -352,14 +356,16 @@ export function KanbanCompView(props: Props) {
352356
headerField: 'Title',
353357
template: (data: Record<string, string>) => {
354358
const cardIndex = dataMap[data.Id] || 0;
355-
return cardTemplate({
356-
data,
357-
cardIndex,
358-
childrenProps,
359-
cardHeaderStyles: childrenProps.cardHeaderStyles,
360-
cardContentStyles: childrenProps.cardContentStyles,
361-
tagStyles: childrenProps.tagStyles,
362-
});
359+
return (
360+
<CardTemplate
361+
data={data}
362+
cardIndex={cardIndex}
363+
childrenProps={childrenProps}
364+
cardHeaderStyles={childrenProps.cardHeaderStyles}
365+
cardContentStyles={childrenProps.cardContentStyles}
366+
tagStyles={childrenProps.tagStyles}
367+
/>
368+
);
363369
},
364370
selectionType: 'Multiple',
365371
}}
@@ -377,37 +383,33 @@ export function KanbanCompView(props: Props) {
377383
headerText="To Do"
378384
keyField="Open"
379385
allowToggle={true}
380-
template={(data: Record<string, string>) => columnTemplate({
381-
data,
382-
boardStyles: childrenProps.boardStyles,
383-
})}
386+
template={(data: Record<string, string>) => (
387+
<ColumnTemplate data={data} boardStyles={childrenProps.boardStyles} />
388+
)}
384389
/>
385390
<ColumnDirective
386391
headerText="In Progress"
387392
keyField="InProgress"
388393
allowToggle={true}
389-
template={(data: Record<string, string>) => columnTemplate({
390-
data,
391-
boardStyles: childrenProps.boardStyles,
392-
})}
394+
template={(data: Record<string, string>) => (
395+
<ColumnTemplate data={data} boardStyles={childrenProps.boardStyles} />
396+
)}
393397
/>
394398
<ColumnDirective
395399
headerText="In Review"
396400
keyField="Review"
397401
allowToggle={true}
398-
template={(data: Record<string, string>) => columnTemplate({
399-
data,
400-
boardStyles: childrenProps.boardStyles,
401-
})}
402+
template={(data: Record<string, string>) => (
403+
<ColumnTemplate data={data} boardStyles={childrenProps.boardStyles} />
404+
)}
402405
/>
403406
<ColumnDirective
404407
headerText="Done"
405408
keyField="Close"
406409
allowToggle={true}
407-
template={(data: Record<string, string>) => columnTemplate({
408-
data,
409-
boardStyles: childrenProps.boardStyles,
410-
})}
410+
template={(data: Record<string, string>) => (
411+
<ColumnTemplate data={data} boardStyles={childrenProps.boardStyles} />
412+
)}
411413
/>
412414
</ColumnsDirective>
413415
</KanbanComponent>
@@ -420,4 +422,4 @@ export function KanbanCompView(props: Props) {
420422
</div>
421423
</div>
422424
);
423-
}
425+
})

lowcoder-comp-kanban/src/kanbanPropertyView.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import {
33
Section,
44
} from "lowcoder-sdk";
55
import { KanbanImplComp } from "./KabanComp";
6+
import React from "react";
67

78
type Props = {
89
comp: InstanceType<typeof KanbanImplComp>;
910
};
1011

11-
export function KanbanPropertyView(props: Props) {
12+
export const KanbanPropertyView = React.memo((props: Props) => {
1213
const { comp } = props;
1314
const children = comp.children;
1415
return (
@@ -43,4 +44,4 @@ export function KanbanPropertyView(props: Props) {
4344

4445
</>
4546
);
46-
}
47+
});

0 commit comments

Comments
 (0)