forked from agama-project/agama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] Adds a core/OptionPicker component
Which helps rendering a set of options with a tile look&feel. Not using PatternFly/Tile instead because it looks like it's going to be deprecated [1] and the PatternFly/Card#isSelectable component is a bit complex for this first use case. [1] patternfly/patternfly-react#8542
- Loading branch information
Showing
5 changed files
with
195 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) [2024] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
import React from "react"; | ||
|
||
/** | ||
* Wrapper for OptionsPicker options | ||
* @component | ||
* | ||
* @param {object} props | ||
* @param {string} [props.title] - Text to be used as option title | ||
* @param {string} [props.body] - Text to be used as option body | ||
* @param {boolean} [props.isSelected=false] - Whether the option should be set as select of not | ||
* @param {object} [props.props] - Other props sent to div#option node | ||
*/ | ||
const Option = ({ title, body, isSelected = false, ...props }) => { | ||
return ( | ||
<div | ||
{...props} | ||
role="option" | ||
aria-selected={isSelected} | ||
> | ||
<div><b>{title}</b></div> | ||
<div>{body}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
/** | ||
* Helper component to build rich options picker | ||
* @component | ||
* | ||
* @param {object} props | ||
* @param {string} [props.ariaLabel] - Text to be used as accessible label | ||
* @param {Array<Option>} props.children - A collection of Option | ||
* @param {object} [props.props] - Other props sent to div#listbox node | ||
*/ | ||
const OptionsPicker = ({ "aria-label": ariaLabel, children, ...props }) => { | ||
return ( | ||
<div | ||
{...props} | ||
role="listbox" | ||
data-type="agama/options-picker" | ||
aria-label={ariaLabel} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
OptionsPicker.Option = Option; | ||
|
||
export default OptionsPicker; |
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,75 @@ | ||
/* | ||
* Copyright (c) [2024] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
import React from "react"; | ||
import { screen } from "@testing-library/react"; | ||
import { plainRender } from "~/test-utils"; | ||
import { OptionsPicker } from "~/components/core"; | ||
|
||
describe("OptionsPicker", () => { | ||
it("renders a node with listbox role", () => { | ||
plainRender(<OptionsPicker />); | ||
screen.getByRole("listbox"); | ||
}); | ||
}); | ||
|
||
describe("OptionsPicker.Option", () => { | ||
it("renders a node with option role", () => { | ||
plainRender(<OptionsPicker.Option />); | ||
screen.getByRole("option"); | ||
}); | ||
|
||
it("renders given title", () => { | ||
plainRender(<OptionsPicker.Option title="Custom" />); | ||
screen.getByRole("option", { name: "Custom" }); | ||
}); | ||
|
||
it("renders given body", () => { | ||
plainRender(<OptionsPicker.Option body="More freedom for user" />); | ||
screen.getByRole("option", { name: "More freedom for user" }); | ||
}); | ||
|
||
it("triggers given onClick callback when user clicks on it", async () => { | ||
const onClick = jest.fn(); | ||
const { user } = plainRender(<OptionsPicker.Option title="Custom" onClick={onClick} />); | ||
const option = screen.getByRole("option", { name: "Custom" }); | ||
await user.click(option); | ||
expect(onClick).toHaveBeenCalled(); | ||
}); | ||
|
||
it("sets as selected if isSelected is given", () => { | ||
plainRender(<OptionsPicker.Option isSelected />); | ||
const option = screen.getByRole("option"); | ||
expect(option).toHaveAttribute("aria-selected", "true"); | ||
}); | ||
|
||
it("sets as not selected if isSelected is not given", () => { | ||
plainRender(<OptionsPicker.Option />); | ||
const option = screen.getByRole("option"); | ||
expect(option).toHaveAttribute("aria-selected", "false"); | ||
}); | ||
|
||
it("sets as not selected if isSelected=false", () => { | ||
plainRender(<OptionsPicker.Option isSelected={false} />); | ||
const option = screen.getByRole("option"); | ||
expect(option).toHaveAttribute("aria-selected", "false"); | ||
}); | ||
}); |
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