Skip to content

Commit c49c957

Browse files
fix infinite re-rendering issue
1 parent 5808be1 commit c49c957

File tree

5 files changed

+55
-42
lines changed

5 files changed

+55
-42
lines changed

lowcoder-comp-kanban/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'bootstrap/dist/css/bootstrap.css';
88
// Put any other imports below so that CSS from your
99
// components takes precedence over default styles.
1010
function CompDevApp() {
11-
console.log("compMap", lowcoder.comps)
1211
return (
1312
<CompIDE
1413
compMap={compMap}

lowcoder-comp-kanban/src/KabanComp.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
withViewFn,
88
withPropertyViewFn,
99
withMethodExposing,
10+
CompAction,
1011
} from 'lowcoder-sdk';
1112
import { trans } from "./i18n/comps";
1213
import { KanbanInitComp } from './kanbanTypes';
@@ -16,6 +17,7 @@ import * as datasource from './datasource.json';
1617

1718
type IContainer = typeof IContainer;
1819
type NameGenerator = typeof NameGenerator;
20+
type CompAction = typeof CompAction;
1921

2022
export class KanbanImplComp extends KanbanInitComp implements IContainer {
2123
private getSlotContainer() {
@@ -44,6 +46,20 @@ export class KanbanImplComp extends KanbanInitComp implements IContainer {
4446
autoHeight(): boolean {
4547
return this.children.autoHeight.getView();
4648
}
49+
50+
override reduce(action: CompAction): this {
51+
let comp = super.reduce(action);
52+
const params = comp.children.cardView.children.cardView.getCachedParams('0');
53+
if (!Boolean(params)) {
54+
comp = comp.setChild(
55+
"cardView",
56+
comp.children.cardView.reduce(
57+
comp.children.cardView.setSelectionAction('0', params)
58+
)
59+
);
60+
}
61+
return comp;
62+
}
4763
}
4864

4965
const KanbanRenderComp = withViewFn(KanbanImplComp, (comp: KanbanImplComp) => <KanbanCompView comp={comp} />);

lowcoder-comp-kanban/src/cardViewControl.tsx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
NameGenerator,
1818
JSONValue,
1919
} from "lowcoder-sdk";
20+
import React from "react";
2021

2122
type ContainerBaseProps = typeof ContainerBaseProps;
2223
type ConstructorToView<T> = typeof ConstructorToView;
@@ -26,11 +27,11 @@ type JSONValue = typeof JSONValue;
2627

2728
const ContextSlotControl = withSelectedMultiContext(SlotControl);
2829

29-
const ContainerView = (props: ContainerBaseProps) => {
30-
return <InnerGrid {...props} emptyRows={30} autoHeight />;
31-
};
30+
const ContainerView = React.memo((props: ContainerBaseProps) => {
31+
return <InnerGrid {...props} emptyRows={15} autoHeight />;
32+
});
3233

33-
function CardView(props: { containerProps: ConstructorToView<typeof SimpleContainerComp> }) {
34+
const CardView = React.memo((props: { containerProps: ConstructorToView<typeof SimpleContainerComp> }) => {
3435
const { containerProps } = props;
3536
// const background = useContext(BackgroundColorContext);
3637
return (
@@ -43,11 +44,10 @@ function CardView(props: { containerProps: ConstructorToView<typeof SimpleContai
4344
// bgColor={background}
4445
items={gridItemCompToGridItems(containerProps.items)}
4546
hintPlaceholder=""
46-
onMouseDown
4747
// containerPadding={[2, 2]}
4848
/>
4949
);
50-
}
50+
});
5151

5252
let CardViewControlTmp = (function () {
5353
// const label = trans("table.expandable");
@@ -56,8 +56,8 @@ let CardViewControlTmp = (function () {
5656
// expandable: BoolControl,
5757
cardView: ContextSlotControl,
5858
},
59-
() => ({ expandableConfig: {}, expandModalView: null })
60-
)
59+
() => ({ cardViewConfig: {}, cardModalView: null })
60+
)
6161
.setControlItemData({ filterText: '' })
6262
.setPropertyViewFn((children, dispatch) => {
6363
return (
@@ -75,26 +75,21 @@ let CardViewControlTmp = (function () {
7575

7676
export class CardViewControl extends CardViewControlTmp {
7777
getView() {
78-
// if (!this.children.expandable.getView()) {
79-
// return { expandableConfig: {}, expandModalView: null };
80-
// }
8178
const selectedContainer = this.children.cardView.getSelectedComp();
8279
return {
83-
cardViewConfig: {
84-
cardTemplate: (data: Record<string, string>, index: number) => {
85-
const slotControl = this.children.cardView.getView()(
86-
{
87-
currentRow: data,
88-
currentIndex: index,
89-
// currentOriginalIndex: tryToNumber(record[OB_ROW_ORI_INDEX]),
90-
},
91-
String(index)
92-
);
93-
const containerProps = slotControl.children.container.getView();
94-
return <CardView key={index} containerProps={containerProps} />;
95-
},
80+
cardTemplate: (data: Record<string, string>, index: number) => {
81+
const slotControl = this.children.cardView.getView()(
82+
{
83+
currentRow: data,
84+
currentIndex: index,
85+
currentOriginalIndex: index,
86+
},
87+
String(index)
88+
);
89+
const containerProps = slotControl.children.container.getView();
90+
return <CardView key={index} containerProps={containerProps} />;
9691
},
97-
expandModalView: selectedContainer.getView(),
92+
cardModalView: selectedContainer.getView(),
9893
};
9994
}
10095

lowcoder-comp-kanban/src/datasource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"name": "Nancy Davloio"
4646
},
4747
{
48-
"id": "1",
48+
"id": "2",
4949
"name": "Andrew Fuller"
5050
}
5151
]

lowcoder-comp-kanban/src/kanbanCompView.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,16 @@ const CardTemplate = React.memo((props: {
9696
tagStyles: Record<string, string>;
9797
}) => {
9898
const editorState = useContext(EditorContext);
99-
if (editorState) {
100-
return props.cardView.cardViewConfig.cardTemplate(
99+
100+
const template = useMemo(() => {
101+
return props.cardView.cardTemplate(
101102
props.data,
102103
props.cardIndex,
103-
);
104+
)
105+
}, [JSON.stringify(props.data), props.cardIndex]);
106+
107+
if (editorState) {
108+
return template;
104109
}
105110

106111
return (
@@ -167,12 +172,10 @@ type Props = {
167172

168173
export const KanbanCompView = React.memo((props: Props) => {
169174
const { comp } = props;
170-
171175
// const childrenProps = useMemo(() =>
172176
// childrenToProps(comp.children),
173177
// [childrenToProps, comp.children],
174178
// );
175-
176179
const childrenProps = childrenToProps(comp.children);
177180

178181
const [dataMap, setDataMap] = useState<Record<string, number>>({});
@@ -182,7 +185,7 @@ export const KanbanCompView = React.memo((props: Props) => {
182185
const updateDataMap = useCallback(() => {
183186
const mapData: Record<string, number> = {};
184187
childrenProps.data?.forEach((item: any, index: number) => {
185-
mapData[item.Id] = index;
188+
mapData[item.id] = index;
186189
})
187190
setDataMap(mapData);
188191
}, [ setDataMap]);
@@ -195,13 +198,13 @@ export const KanbanCompView = React.memo((props: Props) => {
195198
updateDataMap();
196199
}, [JSON.stringify(childrenProps.data), updateDataMap]);
197200

198-
let data: Object[] = useMemo(() => extend(
201+
const kanbanData: Object[] = useMemo(() => extend(
199202
[],
200203
childrenProps.data as { [key: string]: Object },
201204
undefined,
202205
true
203206
) as Object[]
204-
, [childrenProps.data]
207+
, [JSON.stringify(childrenProps.data)]
205208
);
206209

207210
const showModal = useCallback(() => {
@@ -233,7 +236,7 @@ export const KanbanCompView = React.memo((props: Props) => {
233236
}
234237
});
235238
return assignees;
236-
}, [childrenProps.assigneeOptions]);
239+
}, [JSON.stringify(childrenProps.assigneeOptions)]);
237240

238241
const statusOptions = useMemo(() => {
239242
let uniqueObjectsArray: any = [];
@@ -251,7 +254,7 @@ export const KanbanCompView = React.memo((props: Props) => {
251254
}
252255
});
253256
return uniqueObjectsArray;
254-
}, [childrenProps.statusOptions]);
257+
}, [JSON.stringify(childrenProps.statusOptions)]);
255258

256259
const handleDataChange = (kanbanData: Array<Record<string,any>>) => {
257260
comp.children?.data.children.manual.children.manual.dispatch(
@@ -271,7 +274,7 @@ export const KanbanCompView = React.memo((props: Props) => {
271274
}: {
272275
changedRecords : Array<Record<string,any>>
273276
}) => {
274-
const updatedData = [ ...data ] as Array<Record<string,any>>;
277+
const updatedData = [ ...kanbanData ] as Array<Record<string,any>>;
275278
changedRecords.forEach((record) => {
276279
const { id } = record;
277280
const index = updatedData.findIndex((item: any) => item.id === id);
@@ -284,7 +287,7 @@ export const KanbanCompView = React.memo((props: Props) => {
284287

285288
const handleOk = (dialogData: Record<string, string>) => {
286289
const { id } = dialogData;
287-
const updatedData = [ ...data ];
290+
const updatedData = [ ...kanbanData ];
288291
const index = updatedData.findIndex((item: any) => item.id === id);
289292
if (index > -1) {
290293
updatedData[index] = dialogData;
@@ -303,7 +306,7 @@ export const KanbanCompView = React.memo((props: Props) => {
303306
const cardIndex = dataMap[data.id] || 0;
304307
return (
305308
<CardTemplate
306-
data={data}
309+
data={{...data}}
307310
cardIndex={cardIndex}
308311
cardView={childrenProps.cardView}
309312
cardHeaderStyles={childrenProps.cardHeaderStyles}
@@ -343,7 +346,7 @@ export const KanbanCompView = React.memo((props: Props) => {
343346
id="kanban"
344347
cssClass="kanban-overview"
345348
keyField="status"
346-
dataSource={data}
349+
dataSource={[...kanbanData]}
347350
cardDoubleClick={OnCardDoubleClick}
348351
cardClick={(args: CardClickEventArgs) => args.event?.stopPropagation()}
349352
swimlaneSettings={{keyField: 'assignee'}}
@@ -373,7 +376,7 @@ export const KanbanCompView = React.memo((props: Props) => {
373376
</LayoutContainer>
374377
</ScrollBar>
375378
<SlotConfigContext.Provider value={{ modalWidth: 600 }}>
376-
{childrenProps.cardView.expandModalView}
379+
{childrenProps.cardView.cardModalView}
377380
</SlotConfigContext.Provider>
378381
</div>
379382
</div>

0 commit comments

Comments
 (0)