Skip to content

Commit

Permalink
added SwitchButtonGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeystylle committed Jun 30, 2024
1 parent 224863f commit ac5147b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@
}

@layer utilities {
.customBoxShadow {
box-shadow: 2px 4px 8px hsl(0deg 0% 0% / 0.25);
}
}
66 changes: 66 additions & 0 deletions components/SwitchButtonGroup/SwitchButtonGroup.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { Meta, StoryObj } from '@storybook/react';

import SwitchButtonGroup from './SwitchButtonGroup';
import { useState } from 'react';

const meta: Meta<typeof SwitchButtonGroup> = {
title: 'Components/SwitchButtonGroup',
component: SwitchButtonGroup,
tags: ['autodocs'],
parameters: {
layout: 'centered',
},
};

export default meta;
type Story = StoryObj<typeof SwitchButtonGroup>;

export const TwoSwitch: Story = {
parameters: {},
args: {
options: ['Monthly', 'Yearly'],
activeOption: 'Monthly',
},
render: args => {
//state for option on the ToggleButton
const [selectedOption, setSelectedOption] = useState(args.activeOption);

//handler for toggle button select
const handleOptionSelect = (option: string) => {
setSelectedOption(option);
};

return (
<SwitchButtonGroup
options={args.options}
onOptionSelect={handleOptionSelect}
activeOption={selectedOption}
/>
);
},
};

export const ThreeSwitch: Story = {
parameters: {},
args: {
options: ['Monthly', 'Yearly', 'Daily'],
activeOption: 'Yearly',
},
render: args => {
//state for option on the ToggleButton
const [selectedOption, setSelectedOption] = useState(args.activeOption);

//handler for toggle button select
const handleOptionSelect = (option: string) => {
setSelectedOption(option);
};

return (
<SwitchButtonGroup
options={args.options}
onOptionSelect={handleOptionSelect}
activeOption={selectedOption}
/>
);
},
};
49 changes: 49 additions & 0 deletions components/SwitchButtonGroup/SwitchButtonGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { cn } from '@/lib/utils';
import React from 'react';

interface SwitchButtonGroupProps {
/**
* options to display as toggle buttons
*/
options: string[];
/**
* currently active option
*/
activeOption: string;
/**
* handler for selecting option
*/
onOptionSelect: (selectedOption: string) => void;
}

const SwitchButtonGroup = (props: SwitchButtonGroupProps) => {
const { options, activeOption, onOptionSelect } = props;

const switchContainerClass = cn(
'flex items-center justify-center ',
'rounded-xl w-fit p-1 bg-gray-300 ',
'drop-shadow'
);

return (
<div className={switchContainerClass}>
{options.map(option => (
<button
className={cn(
'flex items-center justify-center ',
'text-sm font-semibold p-2 w-fit rounded-lg ',
{
'bg-blue-500 text-white customBoxShadow': option === activeOption,
}
)}
key={option}
onClick={() => onOptionSelect(option)}
>
{option}
</button>
))}
</div>
);
};

export default SwitchButtonGroup;
1 change: 1 addition & 0 deletions components/SwitchButtonGroup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './SwitchButtonGroup';

0 comments on commit ac5147b

Please sign in to comment.