-
Notifications
You must be signed in to change notification settings - Fork 13
Feature: Component ButtonGroup #198
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
Draft
b1ink0
wants to merge
1
commit into
develop
Choose a base branch
from
feat/component-button-group
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
204 changes: 204 additions & 0 deletions
204
packages/frappe-ui-react/src/components/buttonGroup/buttonGroup.stories.tsx
This file contains hidden or 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,204 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { ArrowUpDown, ChevronDown, ListFilter, SmilePlus } from "lucide-react"; | ||
|
|
||
| import ButtonGroup from "./buttonGroup"; | ||
| import type { ButtonGroupProps } from "./types"; | ||
|
|
||
| export default { | ||
| title: "Components/ButtonGroup", | ||
| component: ButtonGroup, | ||
| tags: ["autodocs"], | ||
| argTypes: { | ||
| className: { | ||
| control: "text", | ||
| description: "Additional CSS classes for the button group container", | ||
| }, | ||
| buttons: { | ||
| control: "object", | ||
| description: "Array of button configurations", | ||
| }, | ||
| size: { | ||
| control: { type: "select", options: ["sm", "md", "lg", "xl", "2xl"] }, | ||
| description: "Size of the buttons in the group", | ||
| }, | ||
| variant: { | ||
| control: { | ||
| type: "select", | ||
| options: ["solid", "subtle", "outline", "ghost"], | ||
| }, | ||
| description: "Variant style of the buttons", | ||
| }, | ||
| theme: { | ||
| control: { type: "select", options: ["gray", "blue", "green", "red"] }, | ||
| description: "Theme color of the buttons", | ||
| }, | ||
| }, | ||
| parameters: { docs: { source: { type: "dynamic" } }, layout: "centered" }, | ||
| } as Meta<typeof ButtonGroup>; | ||
|
|
||
| type Story = StoryObj<ButtonGroupProps>; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| buttons: [ | ||
| { | ||
| id: "btn-1", | ||
| label: "Group by", | ||
| }, | ||
| { | ||
| id: "btn-2", | ||
| label: "Sort", | ||
| }, | ||
| { | ||
| id: "btn-3", | ||
| label: "Filters", | ||
| }, | ||
| ], | ||
| size: "sm", | ||
| variant: "subtle", | ||
| }, | ||
| render: (args) => ( | ||
| <div className="p-4"> | ||
| <ButtonGroup {...args} /> | ||
| </div> | ||
| ), | ||
| }; | ||
|
|
||
| export const IconSubtle: Story = { | ||
| args: { | ||
| buttons: [ | ||
| { | ||
| id: "btn-1", | ||
| icon: "phone", | ||
| }, | ||
| { | ||
| id: "btn-2", | ||
| icon: "mail", | ||
| }, | ||
| { | ||
| id: "btn-3", | ||
| icon: "external-link", | ||
| }, | ||
| ], | ||
| size: "sm", | ||
| variant: "subtle", | ||
| theme: "gray", | ||
| }, | ||
| render: (args) => ( | ||
| <div className="p-4"> | ||
| <ButtonGroup {...args} /> | ||
| </div> | ||
| ), | ||
| }; | ||
|
|
||
| export const IconMixedVariant: Story = { | ||
| args: { | ||
| buttons: [ | ||
| { | ||
| id: "btn-1", | ||
| icon: "corner-up-left", | ||
| }, | ||
| { | ||
| id: "btn-2", | ||
| icon: "map-pin", | ||
| }, | ||
| { | ||
| id: "btn-3", | ||
| variant: "subtle", | ||
| icon: () => <SmilePlus className="w-4 h-4" />, | ||
| }, | ||
| { | ||
| id: "btn-4", | ||
| icon: "more-horizontal", | ||
| }, | ||
| ], | ||
| size: "sm", | ||
| variant: "ghost", | ||
| theme: "gray", | ||
| }, | ||
| render: (args) => ( | ||
| <div className="p-4"> | ||
| <ButtonGroup {...args} /> | ||
| </div> | ||
| ), | ||
| }; | ||
|
|
||
| export const IconWithLabelSubtle: Story = { | ||
| args: { | ||
| buttons: [ | ||
| { | ||
| id: "btn-1", | ||
| label: "Save view", | ||
| iconLeft: "plus", | ||
| }, | ||
| { | ||
| id: "btn-2", | ||
| label: "Sort", | ||
| iconLeft: () => <ArrowUpDown className="w-4 h-4" />, | ||
| }, | ||
| { | ||
| id: "btn-3", | ||
| label: "Filter", | ||
| iconLeft: () => <ListFilter className="w-4 h-4" />, | ||
| iconRight: () => <ChevronDown className="w-4 h-4" />, | ||
| }, | ||
| { | ||
| id: "btn-4", | ||
| label: "Column", | ||
| iconLeft: "columns", | ||
| }, | ||
| { | ||
| id: "btn-5", | ||
| icon: "more-horizontal", | ||
| }, | ||
| ], | ||
| size: "md", | ||
| variant: "subtle", | ||
| theme: "gray", | ||
| }, | ||
| render: (args) => ( | ||
| <div className="p-4"> | ||
| <ButtonGroup {...args} /> | ||
| </div> | ||
| ), | ||
| }; | ||
|
|
||
| export const IconWithLabelOutline: Story = { | ||
| args: { | ||
| buttons: [ | ||
| { | ||
| id: "btn-1", | ||
| label: "Save view", | ||
| iconLeft: "plus", | ||
| }, | ||
| { | ||
| id: "btn-2", | ||
| label: "Sort", | ||
| iconLeft: () => <ArrowUpDown className="w-4 h-4" />, | ||
| }, | ||
| { | ||
| id: "btn-3", | ||
| label: "Filter", | ||
| iconLeft: () => <ListFilter className="w-4 h-4" />, | ||
| iconRight: () => <ChevronDown className="w-4 h-4" />, | ||
| }, | ||
| { | ||
| id: "btn-4", | ||
| label: "Column", | ||
| iconLeft: "columns", | ||
| }, | ||
| { | ||
| id: "btn-5", | ||
| icon: "more-horizontal", | ||
| }, | ||
| ], | ||
| size: "md", | ||
| variant: "outline", | ||
| theme: "gray", | ||
| }, | ||
| render: (args) => ( | ||
| <div className="p-4"> | ||
| <ButtonGroup {...args} /> | ||
| </div> | ||
| ), | ||
| }; |
34 changes: 34 additions & 0 deletions
34
packages/frappe-ui-react/src/components/buttonGroup/buttonGroup.tsx
This file contains hidden or 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,34 @@ | ||
| /** | ||
| * External dependencies. | ||
| */ | ||
| import clsx from "clsx"; | ||
|
|
||
| /** | ||
| * Internal dependencies. | ||
| */ | ||
| import Button from "../button/button"; | ||
| import type { ButtonGroupProps } from "./types"; | ||
|
|
||
| const ButtonGroup = ({ | ||
| buttons, | ||
| className, | ||
| size, | ||
| variant, | ||
| theme, | ||
| }: ButtonGroupProps) => { | ||
| return ( | ||
| <div className={clsx("flex gap-1 items-center", className)}> | ||
| {buttons.map((buttonProps, index) => ( | ||
| <Button | ||
| key={buttonProps.id ?? index} | ||
| size={size} | ||
| variant={variant} | ||
| theme={theme} | ||
| {...buttonProps} | ||
| /> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default ButtonGroup; |
This file contains hidden or 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,2 @@ | ||
| export { default as ButtonGroup } from "./buttonGroup"; | ||
| export * from "./types"; |
64 changes: 64 additions & 0 deletions
64
packages/frappe-ui-react/src/components/buttonGroup/tests/buttonGroup.tsx
This file contains hidden or 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,64 @@ | ||
| import { render, screen, fireEvent } from "@testing-library/react"; | ||
| import "@testing-library/jest-dom"; | ||
| import ButtonGroup from "../buttonGroup"; | ||
|
|
||
| describe("ButtonGroup Component", () => { | ||
| const defaultButtons = [ | ||
| { label: "Button 1", onClick: jest.fn() }, | ||
| { label: "Button 2", onClick: jest.fn() }, | ||
| ]; | ||
|
|
||
| const renderButtonGroup = (props = {}) => { | ||
| return render(<ButtonGroup buttons={defaultButtons} {...props} />); | ||
| }; | ||
|
|
||
| it("renders multiple buttons", () => { | ||
| renderButtonGroup(); | ||
| expect(screen.getByText("Button 1")).toBeInTheDocument(); | ||
| expect(screen.getByText("Button 2")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("applies shared props to all buttons", () => { | ||
| renderButtonGroup({ size: "sm", variant: "outline", theme: "red" }); | ||
|
|
||
| const button1 = screen.getByText("Button 1").closest("button"); | ||
| const button2 = screen.getByText("Button 2").closest("button"); | ||
|
|
||
| expect(button1).toHaveClass("h-7"); | ||
| expect(button1).toHaveClass("text-red-700"); | ||
| expect(button1).toHaveClass("border-outline-red-1"); | ||
|
|
||
| expect(button2).toHaveClass("h-7"); | ||
| expect(button2).toHaveClass("text-red-700"); | ||
| expect(button2).toHaveClass("border-outline-red-1"); | ||
| }); | ||
|
|
||
| it("applies custom global className", () => { | ||
| const { container } = renderButtonGroup({ | ||
| className: "custom-group-class", | ||
| }); | ||
| // ButtonGroup wraps buttons in a div | ||
| expect(container.firstChild).toHaveClass("custom-group-class"); | ||
| expect(container.firstChild).toHaveClass("flex"); | ||
| expect(container.firstChild).toHaveClass("gap-1"); | ||
| }); | ||
|
|
||
| it("handles click events on individual buttons", () => { | ||
| const handleClick1 = jest.fn(); | ||
| const handleClick2 = jest.fn(); | ||
|
|
||
| const buttons = [ | ||
| { label: "Action 1", onClick: handleClick1 }, | ||
| { label: "Action 2", onClick: handleClick2 }, | ||
| ]; | ||
|
|
||
| render(<ButtonGroup buttons={buttons} />); | ||
|
|
||
| fireEvent.click(screen.getByText("Action 1")); | ||
| expect(handleClick1).toHaveBeenCalledTimes(1); | ||
| expect(handleClick2).not.toHaveBeenCalled(); | ||
|
|
||
| fireEvent.click(screen.getByText("Action 2")); | ||
| expect(handleClick2).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
14 changes: 14 additions & 0 deletions
14
packages/frappe-ui-react/src/components/buttonGroup/types.ts
This file contains hidden or 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,14 @@ | ||
| import type { | ||
| ButtonProps, | ||
| ButtonSize, | ||
| ButtonTheme, | ||
| ButtonVariant, | ||
| } from "../button/types"; | ||
|
|
||
| export interface ButtonGroupProps { | ||
| buttons: (ButtonProps & { id?: string })[]; | ||
| className?: string; | ||
| size?: ButtonSize; | ||
| variant?: ButtonVariant; | ||
| theme?: ButtonTheme; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The component allows individual button props to override group-level props (size, variant, theme), as demonstrated in the IconMixedVariant story. However, there's no test verifying this override behavior. Consider adding a test case that verifies individual buttons can override group-level properties.