Created
November 14, 2022 12:11
-
-
Save IgorDePaula/d6c572271be9d40181251a1722c488d1 to your computer and use it in GitHub Desktop.
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
import React, {useEffect, useState} from "react"; | |
import {Switch} from '@headlessui/react' | |
export type SwitcherType = { | |
label?: string | |
state: boolean, | |
getState?: (param: boolean | undefined) => void | |
} | |
const Switcher: React.FunctionComponent<SwitcherType> = (props: SwitcherType) => { | |
const {label, state = false, getState = null} = props | |
const [enabled, setEnabled] = useState<boolean | undefined>(state) | |
useEffect(() => { | |
getState && getState(enabled) | |
}, [enabled]) | |
return ( | |
<Switch.Group> | |
<div className="flex items-center"> | |
{label && <Switch.Label className="mr-4">{label}</Switch.Label>} | |
<Switch | |
checked={enabled} | |
onChange={(event: any) => { | |
setEnabled(event) | |
}} | |
className={`${ | |
enabled ? 'bg-blue-600' : 'bg-gray-200' | |
} relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2`} | |
> | |
<span | |
className={`${ | |
enabled ? 'translate-x-6' : 'translate-x-1' | |
} inline-block h-4 w-4 transform rounded-full bg-white transition-transform`} | |
/> | |
</Switch> | |
</div> | |
</Switch.Group> | |
) | |
} | |
export default Switcher |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment