Skip to content
العربية
Forms

FormField

Binds a react-hook-form field to Field: label, hint, the field's error message and the ids. Import it from @percentsa/ui/react-hook-form. react-hook-form is an optional peer: @percentsa/ui alone does not need it. Controls with onValueChange (PhoneInput, Combobox, Autocomplete, DatePicker) take field.onChange in render. max shows a character counter of the value.

import { FormField } from "@percentsa/ui/react-hook-form";

Examples

Optional
0 of 200 characters
Code
export function FormFieldExample() {
  const t = useT();
  const { control, handleSubmit } = useForm({ defaultValues: { name: "", bank: "", note: "" } });
  return (
    <form noValidate onSubmit={handleSubmit(() => {})} className="grid w-full max-w-md gap-8">
      <FormField
        control={control}
        name="name"
        label={t("Name", "الاسم")}
        required
        rules={{ required: t("Enter your name.", "أدخل اسمك.") }}
        render={({ field }) => <Input {...field} autoComplete="name" />}
      />
      <FormField
        control={control}
        name="bank"
        label={t("Bank", "البنك")}
        rules={{ required: t("Choose a bank.", "اختر بنكًا.") }}
        render={({ field }) => (
          <Combobox value={field.value} onValueChange={field.onChange} onBlur={field.onBlur} name={field.name} options={BANKS.map(([value, en, ar]) => ({ value, label: t(en, ar) }))} />
        )}
      />
      <FormField control={control} name="note" label={t("Note", "ملاحظة")} optional max={200} render={({ field }) => <Textarea {...field} maxLength={200} />} />
      <Button type="submit" className="justify-self-start">{t("Save", "حفظ")}</Button>
    </form>
  );
}