-
Notifications
You must be signed in to change notification settings - Fork 7
✨(react) make optional DataGrid row selection #339
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@openfun/cunningham-react": minor | ||
| --- | ||
|
|
||
| make optional DataGrid row selection |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import { queryByRole, render, screen, waitFor } from "@testing-library/react"; | ||
| import React, { useState } from "react"; | ||
| import { faker } from "@faker-js/faker"; | ||
| import { getAllByRole, getByRole, within } from "@testing-library/dom"; | ||
|
|
@@ -267,6 +267,69 @@ describe("<SimpleDataGrid/>", () => { | |
| expect(lastRowSelection[rowsToSelect[1].id]).toBe(true); | ||
| }); | ||
| }); | ||
| it("should not render selection checkboxes for unselectable rows", async () => { | ||
| const rows = Array.from(Array(2)) | ||
| .map(() => ({ | ||
| id: faker.string.uuid(), | ||
| firstName: faker.person.firstName(), | ||
| lastName: faker.person.lastName(), | ||
| email: faker.internet.email(), | ||
| address: faker.location.streetAddress(), | ||
| })) | ||
| .sort((a, b) => a.firstName.localeCompare(b.firstName)); | ||
| rows[0].email = "[email protected]"; | ||
|
|
||
| const Wrapper = () => { | ||
| const [rowSelection, setRowSelection] = useState({}); | ||
|
|
||
| return ( | ||
| <CunninghamProvider> | ||
| <SimpleDataGrid | ||
| columns={[ | ||
| { | ||
| field: "firstName", | ||
| headerName: "First name", | ||
| }, | ||
| { | ||
| field: "lastName", | ||
| headerName: "Last name", | ||
| }, | ||
| { | ||
| field: "email", | ||
| headerName: "Email", | ||
| }, | ||
| { | ||
| field: "address", | ||
| headerName: "Address", | ||
| }, | ||
| ]} | ||
| rows={rows} | ||
| defaultSortModel={[ | ||
| { | ||
| field: "firstName", | ||
| sort: "asc", | ||
| }, | ||
| ]} | ||
| enableRowSelection={(row) => | ||
| row.original.email !== "[email protected]" | ||
| } | ||
| rowSelection={rowSelection} | ||
| onRowSelectionChange={setRowSelection} | ||
| /> | ||
| </CunninghamProvider> | ||
| ); | ||
| }; | ||
|
|
||
| render(<Wrapper />); | ||
|
|
||
| // Check first row. | ||
| let element = screen.getByTestId(rows[0].id); | ||
| expect(queryByRole(element, "checkbox")).not.toBeInTheDocument(); | ||
|
|
||
| // Check second row. | ||
| element = screen.getByTestId(rows[1].id); | ||
| expect(getByRole(element, "checkbox")).toBeInTheDocument(); | ||
| }); | ||
| it("should render a grid with working sortable columns", async () => { | ||
| const rows = Array.from(Array(23)) | ||
| .map(() => ({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,15 +52,20 @@ export const useHeadlessColumns = <T extends Row>({ | |||||
| id: HEADER_ID_SELECT, | ||||||
| size: 34, | ||||||
| header: () => null, | ||||||
| cell: ({ row }) => ( | ||||||
| <Checkbox | ||||||
| checked={row.getIsSelected()} | ||||||
| disabled={!row.getCanSelect} | ||||||
| indeterminate={row.getIsSomeSelected()} | ||||||
| onChange={row.getToggleSelectedHandler()} | ||||||
| aria-label={t("components.datagrid.row_selection_aria")} | ||||||
| /> | ||||||
| ), | ||||||
| cell: ({ row }) => { | ||||||
| if (!row.getCanSelect()) { | ||||||
| return null; | ||||||
| } | ||||||
| return ( | ||||||
| <Checkbox | ||||||
| checked={row.getIsSelected()} | ||||||
| disabled={!row.getCanSelect} | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your change highlights this logic is wrong
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right on this, but we need a way to hide checkboxes, not just grey them out. WDYT about adding a props on DataGrid named
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (ping) :)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woops! I did not our reply ... |
||||||
| indeterminate={row.getIsSomeSelected()} | ||||||
| onChange={row.getToggleSelectedHandler()} | ||||||
| aria-label={t("components.datagrid.row_selection_aria")} | ||||||
| /> | ||||||
| ); | ||||||
| }, | ||||||
| }), | ||||||
| ...headlessColumns, | ||||||
| ]; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.