import type { CheckboxField } from "@payloadcms/plugin-form-builder/types";
import type { FieldErrorsImpl, FieldValues, UseFormRegister } from "react-hook-form";

import React from "react";

import { Error } from "../Error";
import { Width } from "../Width";

export const Checkbox: React.FC<
  {
    errors: Partial<
      FieldErrorsImpl<{
        [x: string]: any;
      }>
    >;
    getValues: any;
    register: UseFormRegister<any & FieldValues>;
    setValue: any;
  } & CheckboxField
> = ({ name, errors, label, register, required: requiredFromProps, width }) => {
  return (
    <Width width={width}>
      <div className="form-wrap pr-4">
        <div className="flex">
          <input className="form-input mr-2 p-2" type="checkbox" {...register(name, { required: requiredFromProps })} />
          <label className="form-label" htmlFor={name}>
            {label}
          </label>
        </div>
        {requiredFromProps && errors[name] && <Error />}
      </div>
    </Width>
  );
};
