import type * as React from "react";
import { useTranslation } from "react-i18next";
import { FormMessage } from "~/components/FormMessage";
import { Label } from "~/components/Label";
import { errorMessageId, infoMessageId } from "../utils";
import styles from "./FormFieldWrapper.module.css";
export function useTranslatedTexts({
label,
error,
bottomText,
}: {
label?: string;
error?: string;
bottomText?: string;
}) {
const { t } = useTranslation(["forms"]);
return {
translatedLabel: label?.includes(":") ? t(label as never) : label,
translatedError: error?.includes(":") ? t(error as never) : error,
translatedBottomText: bottomText?.includes(":")
? t(bottomText as never)
: bottomText,
};
}
export function FormFieldMessages({
name,
error,
bottomText,
}: {
name?: string;
error?: string;
bottomText?: string;
}) {
const { translatedError, translatedBottomText } = useTranslatedTexts({
error,
bottomText,
});
return (
<>
{translatedError ? (
{translatedError}
) : null}
{translatedBottomText ? (
{translatedBottomText}
) : null}
>
);
}
interface FormFieldWrapperProps {
id: string;
name?: string;
label?: string;
/** Extra element rendered next to the label, e.g. an `` explaining the field's syntax. */
labelPopover?: React.ReactNode;
required?: boolean;
error?: string;
bottomText?: string;
valueLimits?: { current: number; max: number };
children: React.ReactNode;
}
export function FormFieldWrapper({
id,
name,
label,
labelPopover,
required,
error,
bottomText,
valueLimits,
children,
}: FormFieldWrapperProps) {
const { translatedLabel } = useTranslatedTexts({ label });
const labelElement = translatedLabel ? (
) : null;
return (
{labelElement && labelPopover ? (
{labelElement}
{labelPopover}
) : (
labelElement
)}
{children}
);
}
/**
* Wrapper for the entity-search fields (user, team, tournament), which render
* their own label through the search component instead of taking one here.
*/
export function SearchFormFieldWrapper({
children,
}: {
children: React.ReactNode;
}) {
return (