-
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 #49 from getpingback/feat/switchButton
Feat/switch button
- Loading branch information
Showing
6 changed files
with
173 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './switch'; |
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,53 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { Switch } from './switch'; | ||
|
||
const meta: Meta<typeof Switch> = { | ||
title: 'Components/Switch', | ||
component: Switch, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
checked: { | ||
description: 'Controls the checked state of the switch', | ||
control: 'boolean', | ||
value: false | ||
}, | ||
disabled: { | ||
description: 'Determines if the switch is disabled', | ||
control: 'boolean', | ||
value: false | ||
}, | ||
highlight: { | ||
description: 'Applies a highlight style to the switch', | ||
control: 'boolean', | ||
value: false | ||
}, | ||
onChange: { | ||
description: 'Callback function triggered when the switch state changes', | ||
control: false | ||
} | ||
} | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Switch>; | ||
|
||
export const Default: Story = { | ||
args: {} | ||
}; | ||
|
||
export const Checked: Story = { | ||
args: { | ||
checked: true | ||
} | ||
}; | ||
|
||
export const Highlight: Story = { | ||
args: { highlight: true, checked: true } | ||
}; | ||
|
||
export const Disabled: Story = { | ||
args: { | ||
checked: false, | ||
disabled: true | ||
} | ||
}; |
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,42 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import { Switch } from './switch'; | ||
|
||
describe('Switch Component', () => { | ||
it('should render correctly', () => { | ||
render(<Switch />); | ||
expect(screen.getByRole('checkbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call onChange when clicked', () => { | ||
const handleChange = jest.fn(); | ||
render(<Switch onChange={handleChange} />); | ||
|
||
fireEvent.click(screen.getByRole('checkbox')); | ||
expect(handleChange).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should display correct checked state', () => { | ||
render(<Switch checked />); | ||
expect(screen.getByRole('checkbox')).toBeChecked(); | ||
}); | ||
|
||
it('should be disabled when disabled prop is true', () => { | ||
render(<Switch disabled />); | ||
expect(screen.getByRole('checkbox')).toBeDisabled(); | ||
}); | ||
|
||
it('should not call onChange when clicked if disabled', () => { | ||
const handleChange = jest.fn(); | ||
render(<Switch onChange={handleChange} disabled />); | ||
|
||
fireEvent.click(screen.getByRole('checkbox')); | ||
expect(handleChange).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should have purple background when highlight prop is true', () => { | ||
render(<Switch highlight />); | ||
const switchElement = screen.getByRole('checkbox'); | ||
expect(switchElement).toHaveStyle({ backgroundColor: 'var(--purple-500)' }); | ||
}); | ||
}); |
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,41 @@ | ||
import React from 'react'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
interface SwitchProps { | ||
checked?: boolean; | ||
disabled?: boolean; | ||
highlight?: boolean; | ||
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void; | ||
} | ||
|
||
function Switch({ checked, disabled, highlight, onChange }: SwitchProps) { | ||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
if (!disabled && onChange) { | ||
onChange(event); | ||
} | ||
}; | ||
|
||
return ( | ||
<label className="inline-flex items-center cursor-pointer"> | ||
<input type="checkbox" className="sr-only peer" checked={checked} disabled={disabled} onChange={handleChange} /> | ||
<div | ||
className={cn( | ||
'relative w-9 h-5 bg-switch-bg hover:bg-switch-bg-hover transition-all shadow-switch rounded-full peer', | ||
'peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full', | ||
"after:content-[''] after:bg-switch-thumb after:absolute after:top-[2px] after:start-[2px]", | ||
'after:rounded-full after:h-4 after:w-4 after:transition-all', | ||
'after:shadow-[0px_0px_4px_0px_#00000052,0px_1px_1px_0px_#00000014]', | ||
'peer-focus:after:ring-8 peer-focus:after:ring-[#0000000A]', | ||
highlight | ||
? 'peer-checked:bg-switch-highlight peer-checked:hover:bg-switch-highlight-hover peer-checked:peer-focus:after:ring-switch-highlight-ring' | ||
: 'peer-checked:bg-switch-checked peer-checked:hover:bg-switch-checked-hover peer-checked:peer-focus:after:ring-switch-checked-ring', | ||
'peer-disabled:bg-gray-300/0 peer-disabled:cursor-not-allowed', | ||
'peer-disabled:shadow-[inset_0_0_0_1px_rgb(228,228,231)]', | ||
'peer-disabled:after:bg-switch-thumb-disabled peer-disabled:after:shadow-none' | ||
)} | ||
/> | ||
</label> | ||
); | ||
} | ||
|
||
export { Switch }; |
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