-
Notifications
You must be signed in to change notification settings - Fork 377
Closed
Milestone
Description
This is a simplified use case:
import React from "react";
import {
Table,
TableHeader,
TableBody,
compoundExpand,
} from "@patternfly/react-table";
let counter = 0;
const expandChangeHandler = (expands, handlerCounter) => {
console.log("create handler", handlerCounter, expands);
return (e, rowIndex, colIndex, isOpen) => {
console.log("--- CLICK ---\nusing handler", handlerCounter, expands);
const newExpands = expands.map(
(row, i) => {
if (Math.trunc(rowIndex / 2) !== i) {
return row;
}
return !isOpen;
},
);
console.log("returning from handler", handlerCounter, newExpands);
return newExpands;
};
};
const ClusterListView = ({ clusterList }) => {
const [expands, setExpands] = React.useState([false, false]);
const rows = clusterList.reduce((allRows, cluster, i) => [
...allRows,
{
isOpen: expands[i],
cells: [
{ title: cluster.name },
{ title: cluster.nodeList.length, props: { isOpen: expands[i] } },
],
},
{
parent: i * 2,
compoundParent: 1,
cells: [{ title: "Nodes", props: { colSpan: 2 } }],
},
], []);
const columns = [
"Clusters",
{ title: "Nodes", cellTransforms: [compoundExpand] },
];
console.log("component state", expands);
const expandChange = expandChangeHandler(expands, counter++);
return (
<React.Fragment>
<Table
aria-label="Cluster list"
rows={rows}
cells={columns}
onExpand={(...args) => setExpands(expandChange(...args))}
>
<TableHeader />
<TableBody />
</Table>
<hr />
<div onClick={e => setExpands(expandChange(e, 0, 1, expands[0]))}>
{`${clusterList[0].name} Nodes`}
</div>
<div onClick={e => setExpands(expandChange(e, 2, 1, expands[1]))}>
{`${clusterList[1].name} Nodes`}
</div>
</React.Fragment>
);
};
export default () => {
const clusterList = [
{ name: "cluster-1", nodeList: ["one", "two"] },
{ name: "cluster-2", nodeList: ["one", "two", "three"] },
];
return <ClusterListView clusterList={clusterList} />;
};- First click on cluster-1 nodes (i.e. click on number 2 in the cell) - everything works fine:
- handler variables are: handlerCounter=0 and expands=[false, false] (console:
using handler) - the handler returns [true, false] as expected (console:
returning from handler) - component is re-rendered, expands state is [true, false] (console:
component state) - the handler seems to be regenerated with: handlerCounter=1 and expands=[true, false] (console:
create handler) - an area with nodes of cluster-1 is expanded
- handler variables are: handlerCounter=0 and expands=[false, false] (console:
- Then click on cluster-2 nodes (i.e. click on number 3 in the cell) - the result is not as expected.
- handler variables are not as expected (console:
using handler):- expected: handlerCounter=1 and expands=[true, false]
- but was: handlerCounter=0 and expands=[false, false]
- so handler returns [false, true] (console:
returning from handler) - and the are with nodes of cluster-1 is collapsed (it shouldn't be)
- handler variables are not as expected (console:
- When I try the same scenario outside of table component it works fine:
- I click on
divelementcluster-1 Nodesand then ondivelementcluster-2 Nodes(both elements are bellow the table). - Table is expanded as expected and also in the console every value is as expected.
- I click on
Version of @patternfly/react-table is 2.5.11.
Metadata
Metadata
Assignees
Labels
No labels


