-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): JOB-78452 Select DataList row (#1529)
* test change for checkbox stuff * Undo moving generateDataListItems to a hook * Initial Checkbox code * Added Tests and ability to unselect checkbox * Make checkbox always visible on small breakpoint * Fix styling on smaller screens * Address storybook comments * Address type related comments * Added opacity animation * Allow focus on checkbox * Adjust animation speed * Address DataListItemInternal comments * Update types for DataList Select * Add padding to checkboxes on smaller screens * Small changes addressing review comments * Set props to readonly Revert renaming * Add whitespace * Remove selection example temporarily
- Loading branch information
1 parent
0ec05cc
commit 3b503b6
Showing
11 changed files
with
237 additions
and
4 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
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
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
44 changes: 44 additions & 0 deletions
44
packages/components/src/DataList/components/DataListLayoutInternal/DataListItemInternal.tsx
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,44 @@ | ||
import classNames from "classnames"; | ||
import React from "react"; | ||
import { Checkbox } from "../../../Checkbox"; | ||
import { useDataListContext } from "../../context/DataListContext"; | ||
import styles from "../../DataList.css"; | ||
import { DataListObject } from "../../DataList.types"; | ||
|
||
interface ListItemInternalProps<T extends DataListObject> { | ||
readonly children: JSX.Element; | ||
readonly item: T; | ||
} | ||
|
||
export function DataListItemInternal<T extends DataListObject>({ | ||
children, | ||
item, | ||
}: ListItemInternalProps<T>) { | ||
const { selected, onSelect } = useDataListContext(); | ||
|
||
if (selected !== undefined && onSelect) { | ||
return ( | ||
<div | ||
className={classNames(styles.selectable, { | ||
[styles.selected]: selected?.length, | ||
})} | ||
> | ||
<Checkbox | ||
checked={selected?.includes(item.id)} | ||
onChange={handleChange} | ||
/> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
return children; | ||
|
||
function handleChange() { | ||
if (selected?.includes(item.id)) { | ||
onSelect?.(selected?.filter(id => id !== item.id)); | ||
} else if (selected) { | ||
onSelect?.([...selected, item.id]); | ||
} | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
packages/components/src/DataList/components/DataListLayoutInternal/DataListItems.test.tsx
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,140 @@ | ||
import { cleanup, render, screen, waitFor } from "@testing-library/react"; | ||
import React from "react"; | ||
import omit from "lodash/omit"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { DataListItems } from "./DataListItems"; | ||
import { defaultValues } from "../../context/DataListContext"; | ||
import * as dataListContext from "../../context/DataListContext/DataListContext"; | ||
import { DataListLayout } from "../DataListLayout"; | ||
import { DataListItemType } from "../../DataList.types"; | ||
|
||
const spy = jest.spyOn(dataListContext, "useDataListContext"); | ||
const mockData = [ | ||
{ id: 1, label: "Luke Skywalker" }, | ||
{ id: 2, label: "Anakin Skywalker" }, | ||
]; | ||
const mockHeader = { label: "Name" }; | ||
const contextValueWithRenderableChildren = { | ||
...defaultValues, | ||
data: mockData, | ||
onSelect: jest.fn(), | ||
selected: [], | ||
headers: mockHeader, | ||
children: ( | ||
<DataListLayout key="layout1"> | ||
{(item: DataListItemType<typeof mockData>) => ( | ||
<div> | ||
<div>{item.label}</div> | ||
</div> | ||
)} | ||
</DataListLayout> | ||
), | ||
}; | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
spy.mockReset(); | ||
}); | ||
|
||
describe("DataListItems", () => { | ||
describe("selectable", () => { | ||
it("should render list items with checkboxes when onSelect and selected provided", () => { | ||
spy.mockReturnValue(contextValueWithRenderableChildren); | ||
|
||
renderItems(); | ||
|
||
expect(screen.getAllByRole("checkbox")).toHaveLength(2); | ||
}); | ||
|
||
it("should not render list items with checkboxes when selected is not provided", async () => { | ||
spy.mockReturnValue(omit(contextValueWithRenderableChildren, "selected")); | ||
|
||
renderItems(); | ||
await waitFor(() => { | ||
expect(screen.queryAllByRole("checkbox")).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
it("should not render list items with checkboxes when onSelect is not provided", async () => { | ||
spy.mockReturnValue(omit(contextValueWithRenderableChildren, "onSelect")); | ||
|
||
renderItems(); | ||
await waitFor(() => { | ||
expect(screen.queryAllByRole("checkbox")).toHaveLength(0); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("onSelect", () => { | ||
it("should call onSelect when a single checkbox is selected", async () => { | ||
const onSelectMock = jest.fn(); | ||
spy.mockReturnValue({ | ||
...contextValueWithRenderableChildren, | ||
onSelect: onSelectMock, | ||
}); | ||
renderItems(); | ||
|
||
const checkbox = screen.getAllByRole("checkbox")[0]; | ||
|
||
await userEvent.click(checkbox); | ||
|
||
expect(onSelectMock).toHaveBeenCalledTimes(1); | ||
expect(onSelectMock).toHaveBeenCalledWith([mockData[0].id]); | ||
}); | ||
|
||
it("should call onSelect with multiple checkboxes selected", async () => { | ||
const onSelectMock = jest.fn(); | ||
spy.mockReturnValue({ | ||
...contextValueWithRenderableChildren, | ||
selected: [mockData[0].id], | ||
onSelect: onSelectMock, | ||
}); | ||
renderItems(); | ||
|
||
const checkbox = screen.getAllByRole("checkbox")[1]; | ||
|
||
userEvent.click(checkbox); | ||
await waitFor(() => { | ||
expect(onSelectMock).toHaveBeenCalledTimes(1); | ||
expect(onSelectMock).toHaveBeenCalledWith([ | ||
mockData[0].id, | ||
mockData[1].id, | ||
]); | ||
}); | ||
}); | ||
|
||
it("should call onSelect when a signle checkbox is un-selected", async () => { | ||
const onSelectMock = jest.fn(); | ||
spy.mockReturnValue({ | ||
...contextValueWithRenderableChildren, | ||
onSelect: onSelectMock, | ||
selected: [mockData[0].id, mockData[1].id], | ||
}); | ||
renderItems(); | ||
const checkbox = screen.getAllByRole("checkbox")[0]; | ||
|
||
await userEvent.click(checkbox); | ||
|
||
expect(onSelectMock).toHaveBeenCalledTimes(1); | ||
expect(onSelectMock).toHaveBeenCalledWith([mockData[1].id]); | ||
}); | ||
}); | ||
}); | ||
|
||
function renderItems() { | ||
render( | ||
<DataListItems | ||
layouts={[ | ||
<DataListLayout key={1}> | ||
{(item: DataListItemType<typeof mockData>) => ( | ||
<div> | ||
<div>{item.label}</div> | ||
</div> | ||
)} | ||
</DataListLayout>, | ||
]} | ||
mediaMatches={{ xs: true, sm: true, md: true, lg: true, xl: true }} | ||
data={mockData} | ||
/>, | ||
); | ||
} |
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