diff --git a/src/stories/components/ToggleSwitch.stories.tsx b/src/stories/components/ToggleSwitch.stories.tsx new file mode 100644 index 00000000..4df05a19 --- /dev/null +++ b/src/stories/components/ToggleSwitch.stories.tsx @@ -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; + +export default meta; +type Story = StoryObj; + +export const Default: Story = { + args: { + isChecked: true, + }, +}; diff --git a/src/views/components/common/SwitchButton.tsx b/src/views/components/common/SwitchButton.tsx new file mode 100644 index 00000000..ab8b4d16 --- /dev/null +++ b/src/views/components/common/SwitchButton.tsx @@ -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 ( + + + + ); +}; + +export default SwitchButton;