checkbox.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use client";
  2. import { useState } from "react";
  3. import type { ComponentRenderProps } from "./types";
  4. import { baseClass, getCustomClass } from "./utils";
  5. export function Checkbox({ element }: ComponentRenderProps) {
  6. const { props } = element;
  7. const customClass = getCustomClass(props);
  8. const [checked, setChecked] = useState(!!props.checked);
  9. return (
  10. <label
  11. className={`flex items-center gap-2 text-xs cursor-pointer ${baseClass} ${customClass}`}
  12. onClick={() => setChecked((prev) => !prev)}
  13. >
  14. <div
  15. className={`w-3.5 h-3.5 border border-border rounded-sm flex items-center justify-center transition-colors ${checked ? "bg-foreground" : "bg-background"}`}
  16. >
  17. {checked && (
  18. <svg
  19. className="w-2.5 h-2.5 text-background"
  20. fill="none"
  21. stroke="currentColor"
  22. viewBox="0 0 24 24"
  23. >
  24. <path
  25. strokeLinecap="round"
  26. strokeLinejoin="round"
  27. strokeWidth={3}
  28. d="M5 13l4 4L19 7"
  29. />
  30. </svg>
  31. )}
  32. </div>
  33. {props.label as string}
  34. </label>
  35. );
  36. }