Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

1.3 KB
Raw Blame
import { nanoid } from 'nanoid';
import classes from './Toggle.module.scss';
import FormField from '../FormField/FormField';

type ToggleProps = {
   label?: string;
   description?: string;
   customClasses?: string;
   inline?: boolean;
   hint?: string;
   error?: string;
   disabled?: boolean;
   fieldValue: boolean;
   onUpdate: (f: boolean) => void;
};

const Toggle = ({
   label,
   description,
   customClasses = '',
   fieldValue = false,
   inline = false,
   hint = '',
   error,
   disabled = false,
   onUpdate,
}: ToggleProps) => {
   const toggleID = nanoid();
   const updateField = (event: React.FormEvent<HTMLInputElement>) => {
      const inputVal = event.currentTarget.value === 'true';
      onUpdate(!inputVal);
   };

   return (
      <FormField type="toggle" label={label} hint={hint} error={error} inline={inline} classes={`${classes.toggleField} ${customClasses}`}>
         <div className={`${classes.toggleCheckbox} ${disabled ? classes.toggleDisabled : ''}`}>
            <input type="checkbox" id={toggleID} value={fieldValue.toString()} onChange={updateField} checked={fieldValue} disabled={disabled} />
            <label htmlFor={toggleID}>{label}</label>
            {description && <span className={classes.description}>{description}</span>}
         </div>
      </FormField>
   );
};

export default Toggle;