Compare commits
21 Commits
fix/vite-h
...
DereC4-der
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f2a29f268 | ||
|
|
d5c3d32170 | ||
| 9356357545 | |||
| 0d63c66356 | |||
| 703a0fa596 | |||
| 7acc91f4ee | |||
| 5d9980cac3 | |||
| 57832b0dd4 | |||
| b9358803dd | |||
| 4397dfb1c5 | |||
| 0e70bbd52f | |||
| 79f2ffa4cc | |||
| c330e3123a | |||
| 38d09e4aae | |||
| 98a7cc618b | |||
| 74cc6e6da1 | |||
| c68b017462 | |||
| aeb19cff54 | |||
| 5a73c8ee6c | |||
| 35b0d01c51 | |||
| b84cf7c15d |
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,
|
||||
},
|
||||
};
|
||||
47
src/views/components/common/SwitchButton.tsx
Normal file
47
src/views/components/common/SwitchButton.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Switch } from '@headlessui/react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
type ToggleSwitchProps = {
|
||||
isChecked?: boolean;
|
||||
onChange?: (checked: boolean) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* A custom switch button component.
|
||||
*
|
||||
* @component
|
||||
* @param {Object} props - The component props.
|
||||
* @param {boolean} [props.isChecked=true] - The initial checked state of the switch button.
|
||||
* @param {Function} props.onChange - The callback function to be called when the switch button is toggled.
|
||||
* @returns {JSX.Element} The rendered SwitchButton component.
|
||||
*/
|
||||
const SwitchButton = ({ isChecked = true, onChange }: ToggleSwitchProps): JSX.Element => {
|
||||
const [enabled, setEnabled] = useState(isChecked);
|
||||
|
||||
useEffect(() => {
|
||||
setEnabled(isChecked);
|
||||
}, [isChecked]);
|
||||
|
||||
const handleChange = (checked: boolean) => {
|
||||
setEnabled(checked);
|
||||
if (onChange) {
|
||||
onChange(checked);
|
||||
}
|
||||
};
|
||||
|
||||
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