2026-01-23 17:23:52 +03:00
|
|
|
// Field Template - wraps each form field with label and description
|
2026-01-24 18:42:59 +03:00
|
|
|
import type { FieldTemplateProps, StrictRJSFSchema } from "@rjsf/utils";
|
2026-01-23 17:23:52 +03:00
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-01-23 18:58:40 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-01-24 18:42:59 +03:00
|
|
|
import { isNullableUnionSchema } from "../fields/nullableUtils";
|
2026-01-30 17:52:11 +03:00
|
|
|
import { getTranslatedLabel } from "@/utils/i18n";
|
2026-01-23 18:58:40 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build the i18n translation key path for nested fields using the field path
|
2026-01-30 17:52:11 +03:00
|
|
|
* provided by RJSF. This avoids ambiguity with underscores in field names and
|
|
|
|
|
* skips dynamic filter labels for per-object filter fields.
|
2026-01-23 18:58:40 +03:00
|
|
|
*/
|
|
|
|
|
function buildTranslationPath(path: Array<string | number>): string {
|
2026-01-30 17:52:11 +03:00
|
|
|
const segments = path.filter(
|
|
|
|
|
(segment): segment is string => typeof segment === "string",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const filtersIndex = segments.indexOf("filters");
|
|
|
|
|
if (filtersIndex !== -1 && segments.length > filtersIndex + 2) {
|
|
|
|
|
const normalized = [
|
|
|
|
|
...segments.slice(0, filtersIndex + 1),
|
|
|
|
|
...segments.slice(filtersIndex + 2),
|
|
|
|
|
];
|
|
|
|
|
return normalized.join(".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return segments.join(".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFilterObjectLabel(pathSegments: string[]): string | undefined {
|
|
|
|
|
const filtersIndex = pathSegments.indexOf("filters");
|
|
|
|
|
if (filtersIndex === -1 || pathSegments.length <= filtersIndex + 1) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
const objectLabel = pathSegments[filtersIndex + 1];
|
|
|
|
|
return typeof objectLabel === "string" && objectLabel.length > 0
|
|
|
|
|
? objectLabel
|
|
|
|
|
: undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function humanizeKey(value: string): string {
|
|
|
|
|
return value
|
|
|
|
|
.split("_")
|
|
|
|
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
|
|
|
.join(" ");
|
2026-01-23 18:58:40 +03:00
|
|
|
}
|
2026-01-23 17:23:52 +03:00
|
|
|
|
|
|
|
|
export function FieldTemplate(props: FieldTemplateProps) {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
children,
|
|
|
|
|
errors,
|
|
|
|
|
help,
|
|
|
|
|
description,
|
|
|
|
|
hidden,
|
|
|
|
|
required,
|
|
|
|
|
displayLabel,
|
|
|
|
|
schema,
|
|
|
|
|
uiSchema,
|
2026-01-23 18:58:40 +03:00
|
|
|
registry,
|
|
|
|
|
fieldPathId,
|
2026-01-23 17:23:52 +03:00
|
|
|
} = props;
|
|
|
|
|
|
2026-01-23 18:58:40 +03:00
|
|
|
// Get i18n namespace from form context (passed through registry)
|
|
|
|
|
const formContext = registry?.formContext as
|
|
|
|
|
| Record<string, unknown>
|
|
|
|
|
| undefined;
|
|
|
|
|
const i18nNamespace = formContext?.i18nNamespace as string | undefined;
|
2026-01-30 17:52:11 +03:00
|
|
|
const { t, i18n } = useTranslation([
|
|
|
|
|
i18nNamespace || "common",
|
|
|
|
|
"views/settings",
|
|
|
|
|
]);
|
2026-01-23 18:58:40 +03:00
|
|
|
|
2026-01-23 17:23:52 +03:00
|
|
|
if (hidden) {
|
|
|
|
|
return <div className="hidden">{children}</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get UI options
|
|
|
|
|
const uiOptions = uiSchema?.["ui:options"] || {};
|
|
|
|
|
|
2026-01-28 18:49:32 +03:00
|
|
|
// Determine field characteristics
|
|
|
|
|
const isAdvanced = uiOptions.advanced === true;
|
2026-01-29 23:58:13 +03:00
|
|
|
const isBoolean =
|
|
|
|
|
schema.type === "boolean" ||
|
|
|
|
|
(Array.isArray(schema.type) && schema.type.includes("boolean"));
|
2026-01-28 18:49:32 +03:00
|
|
|
const isObjectField = schema.type === "object";
|
2026-01-24 18:42:59 +03:00
|
|
|
const isNullableUnion = isNullableUnionSchema(schema as StrictRJSFSchema);
|
2026-01-24 20:06:08 +03:00
|
|
|
const suppressMultiSchema =
|
|
|
|
|
(uiSchema?.["ui:options"] as Record<string, unknown> | undefined)
|
|
|
|
|
?.suppressMultiSchema === true;
|
|
|
|
|
|
|
|
|
|
// Only suppress labels/descriptions if this is a multi-schema field (anyOf/oneOf) with suppressMultiSchema flag
|
|
|
|
|
// This prevents duplicate labels while still showing the inner field's label
|
|
|
|
|
const isMultiSchemaWrapper =
|
|
|
|
|
(schema.anyOf || schema.oneOf) && (suppressMultiSchema || isNullableUnion);
|
2026-01-24 18:42:59 +03:00
|
|
|
|
2026-01-23 18:58:40 +03:00
|
|
|
// Get translation path for this field
|
2026-01-30 17:52:11 +03:00
|
|
|
const pathSegments = fieldPathId.path.filter(
|
|
|
|
|
(segment): segment is string => typeof segment === "string",
|
|
|
|
|
);
|
|
|
|
|
const translationPath = buildTranslationPath(pathSegments);
|
|
|
|
|
const filterObjectLabel = getFilterObjectLabel(pathSegments);
|
|
|
|
|
const translatedFilterObjectLabel = filterObjectLabel
|
|
|
|
|
? getTranslatedLabel(filterObjectLabel, "object")
|
|
|
|
|
: undefined;
|
2026-01-23 18:58:40 +03:00
|
|
|
|
|
|
|
|
// Use schema title/description as primary source (from JSON Schema)
|
|
|
|
|
const schemaTitle = (schema as Record<string, unknown>).title as
|
|
|
|
|
| string
|
|
|
|
|
| undefined;
|
|
|
|
|
const schemaDescription = (schema as Record<string, unknown>).description as
|
|
|
|
|
| string
|
|
|
|
|
| undefined;
|
|
|
|
|
|
|
|
|
|
// Try to get translated label, falling back to schema title, then RJSF label
|
|
|
|
|
let finalLabel = label;
|
|
|
|
|
if (i18nNamespace && translationPath) {
|
|
|
|
|
const translationKey = `${translationPath}.label`;
|
2026-01-30 17:52:11 +03:00
|
|
|
if (i18n.exists(translationKey, { ns: i18nNamespace })) {
|
|
|
|
|
finalLabel = t(translationKey, { ns: i18nNamespace });
|
2026-01-23 18:58:40 +03:00
|
|
|
} else if (schemaTitle) {
|
|
|
|
|
finalLabel = schemaTitle;
|
2026-01-30 17:52:11 +03:00
|
|
|
} else if (translatedFilterObjectLabel) {
|
|
|
|
|
const filtersIndex = pathSegments.indexOf("filters");
|
|
|
|
|
const isFilterObjectField =
|
|
|
|
|
filtersIndex > -1 && pathSegments.length === filtersIndex + 2;
|
|
|
|
|
|
|
|
|
|
if (isFilterObjectField) {
|
|
|
|
|
finalLabel = translatedFilterObjectLabel;
|
|
|
|
|
} else {
|
|
|
|
|
// Try to get translated field label, fall back to humanized
|
|
|
|
|
const fieldName = pathSegments[pathSegments.length - 1] || "";
|
|
|
|
|
let fieldLabel = schemaTitle;
|
|
|
|
|
if (!fieldLabel) {
|
|
|
|
|
const fieldTranslationKey = `${fieldName}.label`;
|
|
|
|
|
if (
|
|
|
|
|
i18nNamespace &&
|
|
|
|
|
i18n.exists(fieldTranslationKey, { ns: i18nNamespace })
|
|
|
|
|
) {
|
|
|
|
|
fieldLabel = t(fieldTranslationKey, { ns: i18nNamespace });
|
|
|
|
|
} else {
|
|
|
|
|
fieldLabel = humanizeKey(fieldName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fieldLabel) {
|
|
|
|
|
finalLabel = t("configForm.filters.objectFieldLabel", {
|
|
|
|
|
ns: "views/settings",
|
|
|
|
|
field: fieldLabel,
|
|
|
|
|
label: translatedFilterObjectLabel,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-23 18:58:40 +03:00
|
|
|
}
|
|
|
|
|
} else if (schemaTitle) {
|
|
|
|
|
finalLabel = schemaTitle;
|
2026-01-30 17:52:11 +03:00
|
|
|
} else if (translatedFilterObjectLabel) {
|
|
|
|
|
const filtersIndex = pathSegments.indexOf("filters");
|
|
|
|
|
const isFilterObjectField =
|
|
|
|
|
filtersIndex > -1 && pathSegments.length === filtersIndex + 2;
|
|
|
|
|
if (isFilterObjectField) {
|
|
|
|
|
finalLabel = translatedFilterObjectLabel;
|
|
|
|
|
} else {
|
|
|
|
|
// Try to get translated field label, fall back to humanized
|
|
|
|
|
const fieldName = pathSegments[pathSegments.length - 1] || "";
|
|
|
|
|
let fieldLabel = schemaTitle;
|
|
|
|
|
if (!fieldLabel) {
|
|
|
|
|
const fieldTranslationKey = `${fieldName}.label`;
|
|
|
|
|
if (
|
|
|
|
|
i18nNamespace &&
|
|
|
|
|
i18n.exists(fieldTranslationKey, { ns: i18nNamespace })
|
|
|
|
|
) {
|
|
|
|
|
fieldLabel = t(fieldTranslationKey, { ns: i18nNamespace });
|
|
|
|
|
} else {
|
|
|
|
|
fieldLabel = humanizeKey(fieldName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fieldLabel) {
|
|
|
|
|
finalLabel = t("configForm.filters.objectFieldLabel", {
|
|
|
|
|
ns: "views/settings",
|
|
|
|
|
field: fieldLabel,
|
|
|
|
|
label: translatedFilterObjectLabel,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-23 18:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to get translated description, falling back to schema description
|
|
|
|
|
let finalDescription = description || "";
|
|
|
|
|
if (i18nNamespace && translationPath) {
|
2026-01-30 17:52:11 +03:00
|
|
|
const descriptionKey = `${translationPath}.description`;
|
|
|
|
|
if (i18n.exists(descriptionKey, { ns: i18nNamespace })) {
|
|
|
|
|
finalDescription = t(descriptionKey, { ns: i18nNamespace });
|
2026-01-23 18:58:40 +03:00
|
|
|
} else if (schemaDescription) {
|
|
|
|
|
finalDescription = schemaDescription;
|
|
|
|
|
}
|
|
|
|
|
} else if (schemaDescription) {
|
|
|
|
|
finalDescription = schemaDescription;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 17:23:52 +03:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2026-01-28 20:00:41 +03:00
|
|
|
"space-y-1",
|
2026-01-23 17:23:52 +03:00
|
|
|
isAdvanced && "border-l-2 border-muted pl-4",
|
|
|
|
|
isBoolean && "flex items-center justify-between gap-4",
|
|
|
|
|
)}
|
2026-01-24 18:42:59 +03:00
|
|
|
data-field-id={translationPath}
|
2026-01-23 17:23:52 +03:00
|
|
|
>
|
2026-01-30 17:52:11 +03:00
|
|
|
{displayLabel &&
|
|
|
|
|
finalLabel &&
|
|
|
|
|
!isBoolean &&
|
|
|
|
|
!isMultiSchemaWrapper &&
|
|
|
|
|
!isObjectField && (
|
|
|
|
|
<Label
|
|
|
|
|
htmlFor={id}
|
|
|
|
|
className={cn(
|
|
|
|
|
"text-sm font-medium",
|
|
|
|
|
errors && errors.props?.errors?.length > 0 && "text-destructive",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{finalLabel}
|
|
|
|
|
{required && <span className="ml-1 text-destructive">*</span>}
|
|
|
|
|
</Label>
|
|
|
|
|
)}
|
2026-01-23 17:23:52 +03:00
|
|
|
|
|
|
|
|
{isBoolean ? (
|
|
|
|
|
<div className="flex w-full items-center justify-between gap-4">
|
|
|
|
|
<div className="space-y-0.5">
|
2026-01-23 18:58:40 +03:00
|
|
|
{displayLabel && finalLabel && (
|
2026-01-23 17:23:52 +03:00
|
|
|
<Label htmlFor={id} className="text-sm font-medium">
|
2026-01-23 18:58:40 +03:00
|
|
|
{finalLabel}
|
2026-01-23 17:23:52 +03:00
|
|
|
{required && <span className="ml-1 text-destructive">*</span>}
|
|
|
|
|
</Label>
|
|
|
|
|
)}
|
2026-01-24 20:06:08 +03:00
|
|
|
{finalDescription && !isMultiSchemaWrapper && (
|
2026-01-29 23:58:13 +03:00
|
|
|
<p className="text-xs text-muted-foreground">
|
2026-01-24 20:06:08 +03:00
|
|
|
{finalDescription}
|
2026-01-23 18:58:40 +03:00
|
|
|
</p>
|
2026-01-23 17:23:52 +03:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
2026-01-28 20:00:41 +03:00
|
|
|
{children}
|
2026-01-28 18:49:32 +03:00
|
|
|
{finalDescription && !isMultiSchemaWrapper && !isObjectField && (
|
2026-01-25 20:20:28 +03:00
|
|
|
<p className="text-xs text-muted-foreground">{finalDescription}</p>
|
2026-01-23 17:23:52 +03:00
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{errors}
|
|
|
|
|
{help}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|