Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

1.5 KB
Raw Blame
import { slug } from '@openpanel/shared';
import type { LucideIcon } from 'lucide-react';
import { forwardRef } from 'react';
import type { ControllerRenderProps } from 'react-hook-form';
import { Switch } from '../ui/switch';
import { cn } from '@/utils/cn';

type Props = {
  label: string;
  description: string;
  Icon: LucideIcon;
  children?: React.ReactNode;
  error?: string;
} & ControllerRenderProps;

export const CheckboxItem = forwardRef<HTMLButtonElement, Props>(
  (
    { label, description, Icon, children, onChange, value, disabled, error },
    ref
  ) => {
    const id = slug(label);
    return (
      <div>
        <label
          className={cn(
            'flex items-center gap-4 px-4 py-6 transition-colors hover:bg-def-200',
            disabled && 'cursor-not-allowed opacity-50'
          )}
          htmlFor={id}
        >
          {Icon && <div className="w-6 shrink-0">{<Icon />}</div>}
          <div className="flex-1">
            <div className="font-medium">{label}</div>
            <div className="text-muted-foreground">{description}</div>
            {error && <div className="text-red-600 text-sm">{error}</div>}
          </div>
          <div>
            <Switch
              checked={!!value}
              disabled={disabled}
              id={id}
              onCheckedChange={onChange}
              ref={ref}
            />
          </div>
        </label>
        {children}
      </div>
    );
  }
);

CheckboxItem.displayName = 'CheckboxItem';