import type { TextField } 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 Textarea: React.FC<
  {
    errors: Partial<
      FieldErrorsImpl<{
        [x: string]: any;
      }>
    >;
    register: UseFormRegister<any & FieldValues>;
    rows?: number;
  } & TextField
> = ({ name, errors, label, register, required: requiredFromProps, rows = 3, width }) => {
  return (
    <Width width={width}>
      <div className="form-wrap">
        <textarea className="form-input" id={name} rows={rows} {...register(name, { required: requiredFromProps })} />
        <label className="form-label" htmlFor={name}>
          {label}
        </label>
        {requiredFromProps && errors[name] && <Error />}
      </div>
    </Width>
  );
};
