sendou.ink/app/components/form/TextFormField.tsx
Kalle 198010e3e7
Some checks are pending
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
New date picker + VoD form rework (#2055)
* Initial

* Progress

* Form fields fun

* Pov

* Progress

* Some errors

* Progress

* Progress

* Progress

* Progress

* Comment

* Progress

* Remove comment

* Fix editing

* Redundant check
2025-01-26 12:56:19 +02:00

52 lines
1.1 KiB
TypeScript

import * as React from "react";
import {
type FieldPath,
type FieldValues,
get,
useFormContext,
} from "react-hook-form";
import { FormMessage } from "~/components/FormMessage";
import { Label } from "~/components/Label";
import { type FormFieldSize, formFieldSizeToClassName } from "./form-utils";
export function TextFormField<T extends FieldValues>({
label,
name,
bottomText,
placeholder,
required,
size = "small",
}: {
label: string;
name: FieldPath<T>;
bottomText?: string;
placeholder?: string;
required?: boolean;
size?: FormFieldSize;
}) {
const methods = useFormContext();
const id = React.useId();
const error = get(methods.formState.errors, name);
return (
<div>
<Label htmlFor={id} required={required}>
{label}
</Label>
<input
id={id}
placeholder={placeholder}
{...methods.register(name)}
className={formFieldSizeToClassName(size)}
/>
{error && (
<FormMessage type="error">{error.message as string}</FormMessage>
)}
{bottomText && !error ? (
<FormMessage type="info">{bottomText}</FormMessage>
) : null}
</div>
);
}