-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(react) make optional DataGrid row selection
When using the selection feature on another project we realized that we needed to make some row non selectable at all by hiding the checkbox completely.
- Loading branch information
Showing
5 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@openfun/cunningham-react": minor | ||
--- | ||
|
||
make optional DataGrid row selection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => ({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters