Skip to content

Commit

Permalink
fix(DataGrid): track action columns correctly (#828)
Browse files Browse the repository at this point in the history
## 📝 Changes

- fix DataGrid using row count instead of column count to track where
columns were in the table
- Adds story and test

Bug is causing gaps in tables with 1 row:

<img width="978" alt="image"
src="https://github.com/EasyPost/easy-ui/assets/752942/e854726e-2d21-4826-b936-1cdd01c8ed8a">

## ✅ Checklist

- [x] Unit tests are written and passing
- [x] Changeset is added
  • Loading branch information
stephenjwatkins authored Dec 5, 2023
1 parent f72213e commit 474e931
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-turkeys-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@easypost/easy-ui": patch
---

fix(DataGrid): track action columns correctly
8 changes: 4 additions & 4 deletions easy-ui-react/src/DataGrid/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function Cell({ cell, state }: CellProps) {
const hasLeftShadow =
hasActionsAtEnd &&
table.isRightEdgeUnderScroll &&
cell.index === state.collection.size - 1;
cell.index === state.collection.columnCount - 1;

const className = classNames(
styles.Cell,
Expand All @@ -44,12 +44,12 @@ export function Cell({ cell, state }: CellProps) {
cell.index === 0 && styles.first,
hasActionsAtStart && cell.index === 0 && styles.firstWithActions,
hasActionsAtStart && cell.index === 1 && styles.secondWithActions,
cell.index === state.collection.size - 1 && styles.last,
cell.index === state.collection.columnCount - 1 && styles.last,
hasActionsAtEnd &&
cell.index === state.collection.size - 1 &&
cell.index === state.collection.columnCount - 1 &&
styles.lastWithActions,
hasActionsAtEnd &&
cell.index === state.collection.size - 2 &&
cell.index === state.collection.columnCount - 2 &&
styles.secondToLastWithActions,
);

Expand Down
8 changes: 4 additions & 4 deletions easy-ui-react/src/DataGrid/ColumnHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function ColumnHeader({ column, state }: ColumnHeaderProps) {
const hasLeftShadow =
hasActionsAtEnd &&
table.isRightEdgeUnderScroll &&
column.index === state.collection.size - 1;
column.index === state.collection.columnCount - 1;

const className = classNames(
styles.ColumnHeader,
Expand All @@ -50,12 +50,12 @@ export function ColumnHeader({ column, state }: ColumnHeaderProps) {
column.index === 0 && styles.first,
hasActionsAtStart && column.index === 0 && styles.firstWithActions,
hasActionsAtStart && column.index === 1 && styles.secondWithActions,
column.index === state.collection.size - 1 && styles.last,
column.index === state.collection.columnCount - 1 && styles.last,
hasActionsAtEnd &&
column.index === state.collection.size - 1 &&
column.index === state.collection.columnCount - 1 &&
styles.lastWithActions,
hasActionsAtEnd &&
column.index === state.collection.size - 2 &&
column.index === state.collection.columnCount - 2 &&
styles.secondToLastWithActions,
);

Expand Down
24 changes: 24 additions & 0 deletions easy-ui-react/src/DataGrid/DataGrid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ export const WithIcons: Story = {
},
};

export const WithRowExpansionAndKebabMenu: Story = {
render: Template.bind({}),
args: {
"aria-label": "Example data grid with row expansion and kebab menu",
rows,
renderExpandedRow: (rowKey: Key) => (
<PlaceholderBox width="100%" height="140px">
Space for row {rowKey} content
</PlaceholderBox>
),
rowActions: () => [
{
type: "menu",
renderMenuOverlay: () => (
<Menu.Overlay onAction={action("Menu item clicked!")}>
<Menu.Item>Action 1</Menu.Item>
<Menu.Item>Action 2</Menu.Item>
</Menu.Overlay>
),
},
],
},
};

function WithSortTemplate(args: Partial<DataGridProps>) {
// https://react-spectrum.adobe.com/react-stately/useAsyncList.html
const list = useAsyncList({
Expand Down
32 changes: 32 additions & 0 deletions easy-ui-react/src/DataGrid/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ describe("<DataGrid />", () => {
direction: "ascending",
});
});

it("should find row expansion and kebab menu cells", async () => {
render(
createDataGrid({
renderExpandedRow: (rowKey: Key) => <div>Row {rowKey} content</div>,
rowActions: () => [
{
type: "menu",
renderMenuOverlay: () => (
<Menu.Overlay>
<Menu.Item>Action 1</Menu.Item>
</Menu.Overlay>
),
},
],
}),
);

const row = getRow(1);
const cells = [...row.children];
const first = cells[0];
const last = cells[cells.length - 1];

expect(first).toHaveAttribute(
"class",
expect.stringContaining("firstWithActions"),
);
expect(last).toHaveAttribute(
"class",
expect.stringContaining("lastWithActions"),
);
});
});

const columns = [
Expand Down

0 comments on commit 474e931

Please sign in to comment.