2023-03-31 09:06:43 -07:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import React, { useRef } from 'react';
|
|
|
|
|
|
|
|
interface IToggle extends Pick<React.InputHTMLAttributes<HTMLInputElement>, 'id' | 'checked' | 'onChange' | 'required'> {
|
|
|
|
|
|
|
|
}
|
2022-05-06 10:46:42 -07:00
|
|
|
|
2022-05-06 12:55:16 -07:00
|
|
|
/** A glorified checkbox. Wrapper around react-toggle. */
|
2023-03-31 09:06:43 -07:00
|
|
|
const Toggle: React.FC<IToggle> = ({ id, checked, onChange, required }) => {
|
|
|
|
const input = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
const handleClick: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
input.current?.focus();
|
|
|
|
input.current?.click();
|
|
|
|
};
|
|
|
|
|
2022-05-06 10:46:42 -07:00
|
|
|
return (
|
2023-03-31 09:06:43 -07:00
|
|
|
<button
|
|
|
|
className='w-9 rounded-full bg-gray-500 p-0.5'
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
|
|
|
<div className={clsx('h-4.5 w-4.5 rounded-full bg-white transition-transform', { 'translate-x-3': checked })} />
|
|
|
|
|
|
|
|
<input
|
|
|
|
id={id}
|
|
|
|
ref={input}
|
|
|
|
type='checkbox'
|
|
|
|
className='sr-only'
|
|
|
|
checked={checked}
|
|
|
|
onChange={onChange}
|
|
|
|
required={required}
|
|
|
|
/>
|
|
|
|
</button>
|
2022-05-06 10:46:42 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Toggle;
|