Skip to content

Commit

Permalink
fix(flat-table-row): update sticky column functionality to support wr…
Browse files Browse the repository at this point in the history
…apping row headers

Updates functionality to use `child.type.displayName` instead of `child.type`. This will allow
wrapping of the `FlatTableRowHeader` component as the wrapper component can set its `displayName` to
`FlatTableRowHeader.displayName` to ensure the required columns are sticky.

fix #5208
  • Loading branch information
edleeks87 committed Jun 17, 2022
1 parent 80526ac commit d15cb0d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {
useContext,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
Expand Down Expand Up @@ -48,9 +49,15 @@ const FlatTableRow = React.forwardRef(
const firstColumnExpandable = expandableArea === "firstColumn";
const [stickyCellWidths, setStickyCellWidths] = useState([]);
const [leftPositions, setLeftPositions] = useState([]);
const childrenArray = React.Children.toArray(children);
const rowHeaderIndex = childrenArray.findIndex(
(child) => child.type === FlatTableRowHeader
const childrenArray = useMemo(() => React.Children.toArray(children), [
children,
]);
const rowHeaderIndex = useMemo(
() =>
childrenArray.findIndex(
(child) => child.type.displayName === FlatTableRowHeader.displayName
),
[childrenArray]
);
const themeContext = useContext(FlatTableThemeContext);

Expand Down
28 changes: 28 additions & 0 deletions src/components/flat-table/flat-table-row/flat-table-row.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,34 @@ describe("FlatTableRow", () => {
);
});
});

describe("wrapping FlatTableRowHeader", () => {
it("calculates the rowHeaderIndex as expected", () => {
// eslint-disable-next-line react/prop-types
const FlatTableRowHeaderWrapper = ({ children }) => (
<FlatTableRowHeader>{children}</FlatTableRowHeader>
);
FlatTableRowHeaderWrapper.displayName = FlatTableRowHeader.displayName;
const rowHeaderIndex = mount(
<table>
<thead>
<FlatTableRow>
<FlatTableCell>Foo</FlatTableCell>
<FlatTableCell>Foo</FlatTableCell>
<FlatTableCell>Foo</FlatTableCell>
<FlatTableRowHeaderWrapper>Foo</FlatTableRowHeaderWrapper>
<FlatTableCell>Foo</FlatTableCell>
<FlatTableCell>Foo</FlatTableCell>
</FlatTableRow>
</thead>
</table>
)
.find(StyledFlatTableRow)
.prop("rowHeaderIndex");

expect(rowHeaderIndex).toEqual(3);
});
});
});

function renderFlatTableRow(props = {}, renderer = mount) {
Expand Down
81 changes: 81 additions & 0 deletions src/components/flat-table/flat-table.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,87 @@ of the button control.
</Story>
</Canvas>

## Wrapping FlatTableRowHeaders

The `FlatTableRow` relies on specific child composition to identify sticky columns. If you need to wrap the `FlatTableRowHeader` you will need to set the `displayName` of the wrapper to `FlatTableRowHeader.displayName`.

<Canvas>
<Story
name="wrapping row headers"
parameters={{ chromatic: { disable: true } }}
>
{() => {
const FlatTableRowHeaderWrapper = ({ children, ...rest }) => (
<FlatTableRowHeader {...rest}>
{children}
</FlatTableRowHeader>
);
FlatTableRowHeaderWrapper.displayName = FlatTableRowHeader.displayName
return (
<FlatTable width="310px" overflowX="auto">
<FlatTableHead>
<FlatTableRow>
<FlatTableHeader>Name</FlatTableHeader>
<FlatTableRowHeaderWrapper>Location</FlatTableRowHeaderWrapper>
<FlatTableHeader>Relationship Status</FlatTableHeader>
<FlatTableHeader>Dependents</FlatTableHeader>
</FlatTableRow>
</FlatTableHead>
<FlatTableBody>
<FlatTableRow>
<FlatTableCell>John Doe</FlatTableCell>
<FlatTableRowHeaderWrapper>London</FlatTableRowHeaderWrapper>
<FlatTableCell>Single</FlatTableCell>
<FlatTableCell>
<ActionPopover>
<ActionPopoverItem onClick={() => {}} icon="edit">
Edit
</ActionPopoverItem>
</ActionPopover>
</FlatTableCell>
</FlatTableRow>
<FlatTableRow>
<FlatTableCell>Jane Doe</FlatTableCell>
<FlatTableRowHeaderWrapper>York</FlatTableRowHeaderWrapper>
<FlatTableCell>Married</FlatTableCell>
<FlatTableCell>
<ActionPopover>
<ActionPopoverItem onClick={() => {}} icon="edit">
Edit
</ActionPopoverItem>
</ActionPopover>
</FlatTableCell>
</FlatTableRow>
<FlatTableRow>
<FlatTableCell>John Smith</FlatTableCell>
<FlatTableRowHeaderWrapper>Edinburgh</FlatTableRowHeaderWrapper>
<FlatTableCell>Single</FlatTableCell>
<FlatTableCell>
<ActionPopover>
<ActionPopoverItem onClick={() => {}} icon="edit">
Edit
</ActionPopoverItem>
</ActionPopover>
</FlatTableCell>
</FlatTableRow>
<FlatTableRow>
<FlatTableCell>Jane Smith</FlatTableCell>
<FlatTableRowHeaderWrapper>Newcastle</FlatTableRowHeaderWrapper>
<FlatTableCell>Married</FlatTableCell>
<FlatTableCell>
<ActionPopover>
<ActionPopoverItem onClick={() => {}} icon="edit">
Edit
</ActionPopoverItem>
</ActionPopover>
</FlatTableCell>
</FlatTableRow>
</FlatTableBody>
</FlatTable>
)}}
</Story>
</Canvas>

## Props

### FlatTable
Expand Down

0 comments on commit d15cb0d

Please sign in to comment.