-
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.
Merge pull request #54 from getpingback/feat/split-button
Feat/split button
- Loading branch information
Showing
9 changed files
with
315 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './split-button'; |
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,54 @@ | ||
import * as React from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { SplitButton } from './split-button'; | ||
import { TrashCanIcon, PlusIcon } from '@stash-ui/light-icons'; | ||
import { HeartIcon } from '@stash-ui/regular-icons'; | ||
|
||
const meta = { | ||
title: 'Components/SplitButton', | ||
component: SplitButton, | ||
|
||
tags: ['autodocs'], | ||
|
||
argTypes: {} | ||
} satisfies Meta<typeof SplitButton>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Solid: Story = { | ||
args: { | ||
prefixIcon: <HeartIcon />, | ||
variant: 'solid', | ||
label: 'Button label', | ||
onPrefixClick: () => {}, | ||
menuItems: [ | ||
{ | ||
key: 'add', | ||
icon: <PlusIcon />, | ||
text: 'Add', | ||
onClick: () => console.log('add') | ||
}, | ||
{ | ||
key: 'delete', | ||
icon: <TrashCanIcon />, | ||
text: 'Delete', | ||
onClick: () => console.log('delete') | ||
} | ||
] | ||
} | ||
}; | ||
|
||
export const Outlined: Story = { | ||
args: { | ||
...Solid.args, | ||
variant: 'outlined' | ||
} | ||
}; | ||
|
||
export const Ghost: Story = { | ||
args: { | ||
...Solid.args, | ||
variant: 'ghost' | ||
} | ||
}; |
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,104 @@ | ||
import React from 'react'; | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { composeStories } from '@storybook/testing-react'; | ||
|
||
import * as stories from './split-button.stories'; | ||
|
||
const { Solid, Outlined, Ghost } = composeStories(stories); | ||
|
||
describe('SplitButton Component', () => { | ||
it('should render the SplitButton component', () => { | ||
render(<Solid />); | ||
const buttonContainer = screen.getByTestId('split-button'); | ||
const primaryButton = screen.getByTestId('split-button-primary'); | ||
const menuTrigger = screen.getByTestId('split-button-menu-trigger'); | ||
expect(buttonContainer).toBeInTheDocument(); | ||
expect(primaryButton).toBeInTheDocument(); | ||
expect(menuTrigger).toBeInTheDocument(); | ||
|
||
expect(primaryButton).toHaveStyle({ backgroundColor: 'var(--buttons-solid_default)' }); | ||
expect(menuTrigger).toHaveStyle({ backgroundColor: 'var(--buttons-solid_default)' }); | ||
}); | ||
|
||
it('should render the SplitButton menu items', async () => { | ||
render(<Solid />); | ||
const menuTrigger = screen.getByTestId('split-button-menu-trigger'); | ||
|
||
const user = userEvent.setup(); | ||
await user.click(menuTrigger); | ||
const menuContent = screen.getByTestId('split-button-menu-content'); | ||
const menuItems = screen.getAllByTestId(/split-button-menu-item/i); | ||
expect(menuContent).toBeInTheDocument(); | ||
expect(menuItems[0]).toHaveTextContent('Add'); | ||
expect(menuItems[1]).toHaveTextContent('Delete'); | ||
}); | ||
|
||
it('should call the onClick function when a menu item is clicked', async () => { | ||
const onClickMock = jest.fn(); | ||
render( | ||
<Solid | ||
{...{ | ||
menuItems: [ | ||
{ | ||
key: 'add', | ||
text: 'Add', | ||
onClick: () => onClickMock('add') | ||
}, | ||
{ | ||
key: 'delete', | ||
text: 'Delete', | ||
onClick: () => onClickMock('delete') | ||
} | ||
] | ||
}} | ||
/> | ||
); | ||
|
||
const user = userEvent.setup(); | ||
const menuTrigger = screen.getByTestId('split-button-menu-trigger'); | ||
await user.click(menuTrigger); | ||
const menuItems = screen.getAllByTestId(/split-button-menu-item/i); | ||
await user.click(menuItems[0]); | ||
|
||
expect(onClickMock).toHaveBeenCalledWith('add'); | ||
}); | ||
|
||
it('should call the onClick function when a primary button is clicked', async () => { | ||
const onClickMock = jest.fn(); | ||
render(<Solid {...{ onPrefixClick: () => onClickMock('primary action') }} />); | ||
const user = userEvent.setup(); | ||
const primaryButton = screen.getByTestId('split-button-primary'); | ||
await user.click(primaryButton); | ||
|
||
expect(onClickMock).toHaveBeenCalledWith('primary action'); | ||
}); | ||
|
||
it('should render the outlined SplitButton component', () => { | ||
render(<Outlined />); | ||
const buttonContainer = screen.getByTestId('split-button'); | ||
const primaryButton = screen.getByTestId('split-button-primary'); | ||
const menuTrigger = screen.getByTestId('split-button-menu-trigger'); | ||
|
||
expect(buttonContainer).toBeInTheDocument(); | ||
expect(primaryButton).toBeInTheDocument(); | ||
expect(menuTrigger).toBeInTheDocument(); | ||
|
||
expect(primaryButton).toHaveStyle({ backgroundColor: 'var(--buttons-outlined_default)' }); | ||
expect(menuTrigger).toHaveStyle({ backgroundColor: 'var(--buttons-outlined_default)' }); | ||
}); | ||
|
||
it('should render the ghost SplitButton component', () => { | ||
render(<Ghost />); | ||
const buttonContainer = screen.getByTestId('split-button'); | ||
const primaryButton = screen.getByTestId('split-button-primary'); | ||
const menuTrigger = screen.getByTestId('split-button-menu-trigger'); | ||
|
||
expect(buttonContainer).toBeInTheDocument(); | ||
expect(primaryButton).toBeInTheDocument(); | ||
expect(menuTrigger).toBeInTheDocument(); | ||
|
||
expect(primaryButton).toHaveStyle({ backgroundColor: 'var(--buttons-ghost_bg-color)' }); | ||
expect(menuTrigger).toHaveStyle({ backgroundColor: 'var(--buttons-ghost_bg-color)' }); | ||
}); | ||
}); |
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,104 @@ | ||
import React, { useState } from 'react'; | ||
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'; | ||
import { CaretDownIcon } from '@stash-ui/light-icons'; | ||
import { cva } from 'class-variance-authority'; | ||
import { DropdownItem } from '../dropdown'; | ||
|
||
const containerVariants = cva('flex items-center font-primary max-w-fit h-[32px] text-sm rounded-lg ', { | ||
variants: { | ||
type: { | ||
solid: 'bg-button-solid text-button-solid-foreground hover:shadow-solid transition-all duration-200 ease-in-out', | ||
outlined: | ||
'bg-button-outlined text-secondary-foreground border border-button-outlined-border hover:shadow-outlined transition-all duration-200 ease-in-out', | ||
ghost: | ||
'bg-button-ghost-gray text-button-ghost-foreground text-secondary-foreground hover:shadow-ghost transition-all duration-200 ease-in-out' | ||
} | ||
}, | ||
defaultVariants: { | ||
type: 'solid' | ||
} | ||
}); | ||
|
||
const leftButtonVariants = cva( | ||
'h-full w-full flex items-center gap-1 px-3 rounded-l-lg rounded-r-none transition-all duration-200 ease-in-out font-semibold', | ||
{ | ||
variants: { | ||
type: { | ||
solid: 'hover:bg-button-solid-hover', | ||
outlined: 'hover:bg-button-outlined-hover opacity-85 hover:opacity-100', | ||
ghost: 'hover:bg-button-ghost-hover opacity-85 hover:opacity-100' | ||
} | ||
} | ||
} | ||
); | ||
|
||
const menuTriggerVariants = cva( | ||
'h-full w-full min-w-[32px] max-w-[32px] border-l flex items-center justify-center rounded-r-lg rounded-l-none border-solid transition-all duration-200 ease-in-out', | ||
{ | ||
variants: { | ||
type: { | ||
solid: 'hover:bg-button-solid-hover border-[#282C2F1F]', | ||
outlined: 'hover:bg-button-outlined-hover opacity-85 hover:opacity-100 border-[#282C2F1F]', | ||
ghost: 'hover:bg-button-ghost-hover border-[#282C2F1F]' | ||
} | ||
} | ||
} | ||
); | ||
|
||
interface SplitButtonProps { | ||
prefixIcon: React.ReactNode; | ||
label: string; | ||
variant?: 'solid' | 'outlined' | 'ghost'; | ||
onPrefixClick: () => void; | ||
sufixIcon?: React.ReactNode; | ||
className?: string; | ||
menuItems: { | ||
key: string; | ||
icon: JSX.Element | undefined; | ||
text: string; | ||
onClick: () => void; | ||
}[]; | ||
} | ||
|
||
function SplitButton({ prefixIcon, label, variant, onPrefixClick, menuItems, sufixIcon, className }: SplitButtonProps) { | ||
const [isMenuActionsOpen, setIsMenuActionsOpen] = useState(false); | ||
|
||
return ( | ||
<DropdownMenu.Root open={isMenuActionsOpen} modal={false} onOpenChange={(open) => !open && setIsMenuActionsOpen(false)}> | ||
<DropdownMenu.Trigger asChild> | ||
<div className={containerVariants({ type: variant })} data-testid="split-button"> | ||
<button className={leftButtonVariants({ type: variant })} onClick={onPrefixClick} data-testid="split-button-primary"> | ||
{prefixIcon} | ||
{label} | ||
</button> | ||
<button | ||
onClick={() => setIsMenuActionsOpen(!isMenuActionsOpen)} | ||
className={menuTriggerVariants({ type: variant })} | ||
data-testid="split-button-menu-trigger" | ||
> | ||
{sufixIcon ? sufixIcon : <CaretDownIcon width={20} height={20} />} | ||
</button> | ||
</div> | ||
</DropdownMenu.Trigger> | ||
|
||
<DropdownMenu.Portal> | ||
<DropdownMenu.Content | ||
data-testid="split-button-menu-content" | ||
onClick={() => setIsMenuActionsOpen(false)} | ||
side="bottom" | ||
className={`rounded-lg w-[252px] flex-col z-50 min-w-fit overflow-hidden bg-background-accent shadow-modal py-2 ${className}`} | ||
sideOffset={4} | ||
align="center" | ||
> | ||
{menuItems.map((item, index) => ( | ||
<DropdownItem icon={item?.icon} key={index} onClick={item.onClick} data-testid="split-button-menu-item"> | ||
{item.text} | ||
</DropdownItem> | ||
))} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Portal> | ||
</DropdownMenu.Root> | ||
); | ||
} | ||
|
||
export { SplitButton }; |
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