Merge branch 'derek/experimental' of https://github.com/DereC4/UT-Registration-Plus into DereC4-derek/experimental
This commit is contained in:
20
src/stories/components/ToggleSwitch.stories.tsx
Normal file
20
src/stories/components/ToggleSwitch.stories.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { Meta, StoryObj } from '@storybook/react';
|
||||
import SwitchButton from '@views/components/common/SwitchButton';
|
||||
|
||||
const meta = {
|
||||
title: 'Components/Common/SwitchButton',
|
||||
component: SwitchButton,
|
||||
tags: ['autodocs'],
|
||||
parameters: {
|
||||
layout: 'centered',
|
||||
},
|
||||
} satisfies Meta<typeof SwitchButton>;
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
export const Default: Story = {
|
||||
args: {
|
||||
isChecked: true,
|
||||
},
|
||||
};
|
||||
38
src/views/components/common/SwitchButton.tsx
Normal file
38
src/views/components/common/SwitchButton.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Switch } from '@headlessui/react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
type ToggleSwitchProps = {
|
||||
isChecked?: boolean;
|
||||
onChange?: (checked: boolean) => void;
|
||||
};
|
||||
|
||||
const SwitchButton: React.FC<ToggleSwitchProps> = ({ isChecked = true, onChange }) => {
|
||||
const [enabled, setEnabled] = useState(isChecked);
|
||||
|
||||
useEffect(() => {
|
||||
setEnabled(isChecked);
|
||||
}, [isChecked]);
|
||||
|
||||
const handleChange = (checked: boolean) => {
|
||||
setEnabled(checked);
|
||||
if (onChange) {
|
||||
onChange;
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Switch
|
||||
checked={enabled}
|
||||
onChange={handleChange}
|
||||
className={`${enabled ? 'bg-[#579D42]' : 'bg-gray-400'}
|
||||
relative inline-flex items-center h-8 w-13 rounded-full transition-colors ease-in-out duration-200`}
|
||||
>
|
||||
<span
|
||||
className={`${enabled ? 'translate-x-6' : 'translate-x-1'}
|
||||
inline-block w-6 h-6 transform bg-white rounded-full transition-transform ease-in-out duration-200`}
|
||||
/>
|
||||
</Switch>
|
||||
);
|
||||
};
|
||||
|
||||
export default SwitchButton;
|
||||
Reference in New Issue
Block a user