diff --git a/.changeset/grumpy-cobras-roll.md b/.changeset/grumpy-cobras-roll.md new file mode 100644 index 0000000..ecf30f0 --- /dev/null +++ b/.changeset/grumpy-cobras-roll.md @@ -0,0 +1,5 @@ +--- +"@openfun/cunningham-react": patch +--- + +allow to set column size for data grid without header diff --git a/packages/react/src/components/DataGrid/index.spec.tsx b/packages/react/src/components/DataGrid/index.spec.tsx index f310d99..130fbcc 100644 --- a/packages/react/src/components/DataGrid/index.spec.tsx +++ b/packages/react/src/components/DataGrid/index.spec.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from "react"; import { faker } from "@faker-js/faker"; import { act, render, screen, waitFor } from "@testing-library/react"; -import { getAllByRole, getByRole } from "@testing-library/dom"; +import { getAllByRole, getByRole, queryAllByRole } from "@testing-library/dom"; import userEvent from "@testing-library/user-event"; import { usePagination } from ":/components/Pagination"; import { DataGrid, SortModel } from ":/components/DataGrid/index"; @@ -441,4 +441,52 @@ describe("", () => { expect(ths[0].style.width).toEqual("50px"); expect(ths[1].style.width).toEqual(""); }); + + it("should render column with specific width even without header", async () => { + const database = Array.from(Array(10)).map(() => ({ + id: faker.string.uuid(), + name: faker.person.fullName(), + email: faker.internet.email(), + })); + + const Component = () => { + return ( + + + + ); + }; + + render(); + + const table = screen.getByRole("table"); + const ths = queryAllByRole(table, "columnheader"); + + // No header should be displayed. + expect(ths.length).toBe(0); + + database.forEach((data) => { + // Each data should have a row. + const element = screen.getByTestId(data.id); + // Then cells containing "name" should have a width of 50px. + let td = getByRole(element, "cell", { name: data.name }); + expect(td.style.width).toEqual("50px"); + + td = getByRole(element, "cell", { name: data.email }); + expect(td.style.width).toEqual(""); + }); + }); }); diff --git a/packages/react/src/components/DataGrid/index.tsx b/packages/react/src/components/DataGrid/index.tsx index 65477f2..ddb2bac 100644 --- a/packages/react/src/components/DataGrid/index.tsx +++ b/packages/react/src/components/DataGrid/index.tsx @@ -282,6 +282,13 @@ export const DataGrid = ({ } else { highlight = !!columns[i].highlight; } + const style: CSSProperties = {}; + if (displayHeader === false) { + const column = columns[i]; + if (column && typeof column.size === "number") { + style.width = `${column.size}px`; + } + } return ( ({ "c__datagrid__row__cell--select": cell.column.id === "select", })} + style={style} > {flexRender( cell.column.columnDef.cell,