-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web: Add initial version of core/Field
A component to help laying out interactive information in pages.
- Loading branch information
Showing
5 changed files
with
274 additions
and
0 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,107 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// @ts-check | ||
|
||
import React from "react"; | ||
import { Icon } from "~/components/layout"; | ||
import { If } from "~/components/core"; | ||
|
||
/** | ||
* @typedef {import("~/components/layout/Icon").IconName} IconName | ||
* @typedef {import("~/components/layout/Icon").IconSize} IconSize | ||
*/ | ||
|
||
/** | ||
* @typedef {object} FieldProps | ||
* @property {React.ReactNode} label - The field label. | ||
* @property {React.ReactNode} [value] - The field value. | ||
* @property {React.ReactNode} [description] - A field description, useful for providing context to the user. | ||
* @property {IconName} [icon] - The name of the icon for the field. | ||
* @property {IconSize} [iconSize="s"] - The size for the field icon. | ||
* @property {string} [className] - ClassName | ||
* @property {() => {}} [onClick] - Callback | ||
* @property {React.ReactNode} [children] - A content to be rendered as field children | ||
* | ||
* @typedef {Omit<FieldProps, 'icon'>} FieldPropsWithoutIcon | ||
*/ | ||
|
||
/** | ||
* Component for laying out a page field | ||
* | ||
* @param {FieldProps} props | ||
*/ | ||
const Field = ({ | ||
label, | ||
value, | ||
description, | ||
icon, | ||
iconSize = "s", | ||
onClick, | ||
children, | ||
...props | ||
}) => { | ||
return ( | ||
<div {...props} data-type="agama/field"> | ||
<div> | ||
<button className="plain-control" onClick={onClick}> | ||
<If condition={icon?.length > 0} then={<Icon name={icon} size={iconSize} />} /> <b>{label}</b> | ||
</button> {value} | ||
</div> | ||
<div> | ||
{description} | ||
</div> | ||
<div> | ||
{ children } | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
/** | ||
* @param {Omit<FieldProps, 'icon'>} props | ||
*/ | ||
const SettingsField = ({ ...props }) => { | ||
return <Field {...props} icon="settings" />; | ||
}; | ||
|
||
/** | ||
* @param {Omit<FieldProps, 'icon'> & {isChecked: true}} props | ||
*/ | ||
const SwitchField = ({ isChecked, ...props }) => { | ||
const iconName = isChecked ? "toggle_on" : "toggle_off"; | ||
const className = isChecked ? "on" : "off"; | ||
|
||
return <Field {...props} icon={iconName} className={className} />; | ||
}; | ||
|
||
/** | ||
* @param {Omit<FieldProps, 'icon'> & {isExpanded: boolean}} props | ||
*/ | ||
const ExpandableField = ({ isExpanded, ...props }) => { | ||
const iconName = isExpanded ? "collapse_all" : "expand_all"; | ||
const className = isExpanded ? "expanded" : "collapsed"; | ||
|
||
return <Field {...props} icon={iconName} className={className} />; | ||
}; | ||
|
||
export default Field; | ||
export { ExpandableField, SettingsField, SwitchField }; |
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,112 @@ | ||
/* | ||
* 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 { Field, ExpandableField, SettingsField, SwitchField } from "~/components/core"; | ||
|
||
const onClick = jest.fn(); | ||
|
||
describe("Field", () => { | ||
it("renders a button with given icon and label", () => { | ||
const { container } = plainRender( | ||
<Field icon="edit" label="Theme" value="dark" onClick={onClick} /> | ||
); | ||
screen.getByRole("button", { name: "Theme" }); | ||
const icon = container.querySelector("button > svg"); | ||
expect(icon).toHaveAttribute("data-icon-name", "edit"); | ||
}); | ||
|
||
it("renders value, description, and given children", () => { | ||
plainRender( | ||
<Field | ||
icon="edit" | ||
label="Theme" | ||
value="dark" | ||
description="Choose your preferred color schema." | ||
onClick={onClick} | ||
> | ||
<p>This is a <b>preview</b></p>; | ||
</Field> | ||
); | ||
screen.getByText("dark"); | ||
screen.getByText("Choose your preferred color schema."); | ||
screen.getByText("This is a"); | ||
screen.getByText("preview"); | ||
}); | ||
|
||
it("triggers the onClick callback when users clicks the button", async () => { | ||
const { user } = plainRender( | ||
<Field label="Theme" value="dark" onClick={onClick} /> | ||
); | ||
const button = screen.getByRole("button"); | ||
await user.click(button); | ||
expect(onClick).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("SettingsField", () => { | ||
it("uses the 'settings' icon", () => { | ||
const { container } = plainRender( | ||
// Trying to set other icon, although typechecking should catch it. | ||
<SettingsField icon="edit" label="Theme" value="dark" onClick={onClick} /> | ||
); | ||
const icon = container.querySelector("button > svg"); | ||
expect(icon).toHaveAttribute("data-icon-name", "settings"); | ||
}); | ||
}); | ||
|
||
describe("SwitchField", () => { | ||
it("uses the 'toggle_on' icon when isChecked", () => { | ||
const { container } = plainRender( | ||
<SwitchField label="Zoom" value="active" isChecked /> | ||
); | ||
const icon = container.querySelector("button > svg"); | ||
expect(icon).toHaveAttribute("data-icon-name", "toggle_on"); | ||
}); | ||
|
||
it("uses the 'toggle_off' icon when not isChecked", () => { | ||
const { container } = plainRender( | ||
<SwitchField label="Zoom" value="not active" /> | ||
); | ||
const icon = container.querySelector("button > svg"); | ||
expect(icon).toHaveAttribute("data-icon-name", "toggle_off"); | ||
}); | ||
}); | ||
|
||
describe("ExpandableField", () => { | ||
it("uses the 'collapse_all' icon when isExpanded", () => { | ||
const { container } = plainRender( | ||
<ExpandableField label="More settings" isExpanded /> | ||
); | ||
const icon = container.querySelector("button > svg"); | ||
expect(icon).toHaveAttribute("data-icon-name", "collapse_all"); | ||
}); | ||
|
||
it("uses the 'expand_all' icon when not isExpanded", () => { | ||
const { container } = plainRender( | ||
<ExpandableField label="More settings" /> | ||
); | ||
const icon = container.querySelector("button > svg"); | ||
expect(icon).toHaveAttribute("data-icon-name", "expand_all"); | ||
}); | ||
}); |
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