Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamic table MAASENG-2987 #125

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,8 @@
"react-dropzone": "14.2.3",
"react-router-dom": "^6.0.0",
"vanilla-framework": "^4.6.0"
},
"dependencies": {
"@tanstack/react-table": "^8.15.3"
ndv99 marked this conversation as resolved.
Show resolved Hide resolved
}
}
10 changes: 10 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const BREAKPOINTS = {
ndv99 marked this conversation as resolved.
Show resolved Hide resolved
// Mobile (portrait)
xSmall: 460,
// Mobile or tablet
small: 620,
// Desktop
large: 1036,
// Large desktop
xLarge: 1681,
};
53 changes: 53 additions & 0 deletions src/lib/components/DynamicTable/DynamicTable.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@import "vanilla-framework";

.p-table-dynamic {
margin-bottom: 0;

thead,
tbody {
display: block;
overflow: hidden auto;
}

thead {
scrollbar-gutter: stable;
}

thead tr,
tbody tr {
display: table;
table-layout: fixed;
width: 100%;
}

tbody {
height: auto;
min-height: auto;
}

&:not([aria-busy="true"]) tbody:not([aria-busy="true"]) {
tr:hover,
tr:focus-within {
@include vf-transition($property: #{background}, $duration: fast);

background-color: $colors--light-theme--background-hover;
}
}

thead th:last-child,
tbody td:last-child {
text-align: right;
}

thead th button {
padding-top: 0;
margin-bottom: 0;
}
}

.p-table-dynamic.p-table-dynamic--with-select {
thead th:first-child,
tbody td:first-child {
width: 3rem;
}
}
115 changes: 115 additions & 0 deletions src/lib/components/DynamicTable/DynamicTable.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Meta } from "@storybook/react";
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";

import { DynamicTable } from "@/lib/components/DynamicTable/DynamicTable";

type Device = {
fqdn: string;
ipAddress: string;
zone: string;
owner: string;
};

type DeviceColumnDef = ColumnDef<Device, Device[keyof Device]>;

const columns: DeviceColumnDef[] = [
{
id: "fqdn",
accessorKey: "fqdn",
header: () => <div>FQDN</div>,
},
{
id: "ipAddress",
accessorKey: "ipAddress",
header: () => <div>IP address</div>,
},
{
id: "zone",
accessorKey: "zone",
header: () => <div>Zone</div>,
},
{
id: "owner",
accessorKey: "owner",
header: () => <div>Owner</div>,
},
];

const data = Array.from({ length: 50 }, (_, index) => ({
fqdn: `machine-${index}`,
ipAddress: `192.168.1.${index}`,
zone: `zone-${index}`,
owner: `owner-${index}`,
}));

const TableChildren = () => {
const table = useReactTable<Device>({
data,
columns,
getCoreRowModel: getCoreRowModel(),
});

return (
<>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<th
className={`${header.column.id}`}
colSpan={header.colSpan}
key={header.id}
>
{flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</th>
);
})}
</tr>
))}
</thead>
<DynamicTable.Body>
{table.getRowModel().rows.map((row) => {
return (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => {
return (
<td className={`${cell.column.id}`} key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
);
})}
</tr>
);
})}
</DynamicTable.Body>
</>
);
};

const meta: Meta<typeof DynamicTable> = {
title: "components/DynamicTable",
component: DynamicTable,
tags: ["autodocs"],
parameters: {
status: {
type: "candidate",
ndv99 marked this conversation as resolved.
Show resolved Hide resolved
},
},
};

export default meta;

export const Example = {
ndv99 marked this conversation as resolved.
Show resolved Hide resolved
args: {
children: <TableChildren />,
},
};
73 changes: 73 additions & 0 deletions src/lib/components/DynamicTable/DynamicTable.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { act, fireEvent, render, screen } from "@testing-library/react";
import { vi } from "vitest";

import { DynamicTable } from "./DynamicTable";

import { BREAKPOINTS } from "@/constants";

const offset = 100;

beforeAll(() => {
// simulate top offset as JSDOM doesn't support getBoundingClientRect
// - equivalent of another element of height 100px being displayed above the table
vi.spyOn(
window.HTMLElement.prototype,
"getBoundingClientRect",
).mockReturnValue({
bottom: 0,
height: 0,
left: 0,
right: 0,
top: offset,
width: 0,
} as DOMRect);
});

it("sets a fixed table body height based on top offset on large screens", async () => {
vi.spyOn(window, "innerWidth", "get").mockReturnValue(BREAKPOINTS.xSmall);

await act(async () => {
fireEvent(window, new Event("resize"));
});

const { container } = render(
<DynamicTable>
<DynamicTable.Body className="test-class">
<tr>
<td>Test content</td>
</tr>
</DynamicTable.Body>
</DynamicTable>,
);

const tbody = container.querySelector("tbody");

await act(async () => {
fireEvent(window, new Event("resize"));
});

// does not alter the height on small screens
expect(tbody).toHaveStyle("height: undefined");

vi.spyOn(window, "innerWidth", "get").mockReturnValue(BREAKPOINTS.large);

await act(async () => {
fireEvent(window, new Event("resize"));
});

await vi.waitFor(() =>
expect(tbody).toHaveStyle(`height: calc(100vh - ${offset + 1}px)`),
);
});

it("displays loading state", () => {
const { container } = render(
<DynamicTable>
<DynamicTable.Loading />
</DynamicTable>,
);

expect(screen.getByText("Loading...")).toBeInTheDocument();
expect(container.querySelector("tbody")).toHaveAttribute("aria-busy", "true");
expect(screen.getAllByRole("row", { hidden: true })).toHaveLength(10);
});
Loading
Loading