Skip to content

PF4 Table attribute onExpand doesn't reflect a handler update #2357

@idevat

Description

@idevat

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

click1

  • 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)

click2

  • When I try the same scenario outside of table component it works fine:
    • I click on div element cluster-1 Nodes and then on div element cluster-2 Nodes (both elements are bellow the table).
    • Table is expanded as expected and also in the console every value is as expected.

alternative-clicks

Version of @patternfly/react-table is 2.5.11.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions