Skip to content

Commit d0a7eb1

Browse files
fix: your name translation (#14863)
* your name translation fixed * do translation outside Components as that is needed by all components. Allows the code to stay DRY --------- Co-authored-by: Hariom <hariombalhara@gmail.com>
1 parent 9a473d5 commit d0a7eb1

File tree

6 files changed

+68
-30
lines changed

6 files changed

+68
-30
lines changed

packages/features/form-builder/FormBuilder.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import {
3030
import { fieldTypesConfigMap } from "./fieldTypes";
3131
import { fieldsThatSupportLabelAsSafeHtml } from "./fieldsThatSupportLabelAsSafeHtml";
3232
import type { fieldsSchema } from "./schema";
33-
import { getVariantsConfig } from "./utils";
3433
import { getFieldIdentifier } from "./utils/getFieldIdentifier";
34+
import { getConfig as getVariantsConfig } from "./utils/variantsConfig";
3535

3636
type RhfForm = {
3737
fields: z.infer<typeof fieldsSchema>;

packages/features/form-builder/FormBuilderField.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Components, isValidValueProp } from "./Components";
1111
import { fieldTypesConfigMap } from "./fieldTypes";
1212
import { fieldsThatSupportLabelAsSafeHtml } from "./fieldsThatSupportLabelAsSafeHtml";
1313
import type { fieldsSchema } from "./schema";
14-
import { getVariantsConfig } from "./utils";
14+
import { getTranslatedConfig as getTranslatedVariantsConfig } from "./utils/variantsConfig";
1515

1616
type RhfForm = {
1717
fields: z.infer<typeof fieldsSchema>;
@@ -211,6 +211,7 @@ export const ComponentForField = ({
211211
} & ValueProps) => {
212212
const fieldType = field.type || "text";
213213
const componentConfig = Components[fieldType];
214+
const { t } = useLocale();
214215

215216
const isValueOfPropsType = (val: unknown, propsType: typeof componentConfig.propsType) => {
216217
const isValid = isValidValueProp[propsType](val);
@@ -333,8 +334,8 @@ export const ComponentForField = ({
333334
}
334335

335336
if (componentConfig.propsType === "variants") {
336-
const variantsConfig = getVariantsConfig(field);
337-
if (!variantsConfig) {
337+
const translatedVariantsConfig = getTranslatedVariantsConfig(field, t);
338+
if (!translatedVariantsConfig) {
338339
return null;
339340
}
340341

@@ -346,7 +347,7 @@ export const ComponentForField = ({
346347
variant={field.variant}
347348
value={value as { value: string; optionValue: string }}
348349
setValue={setValue as (arg: Record<string, string> | string) => void}
349-
variants={variantsConfig.variants}
350+
variants={translatedVariantsConfig.variants}
350351
/>
351352
);
352353
}

packages/features/form-builder/fieldTypes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const configMap: Record<FieldType, Omit<z.infer<typeof fieldTypeConfigSchema>, "
6666
{
6767
name: "fullName",
6868
type: "text",
69-
label: "Your Name",
69+
label: "your_name",
7070
required: true,
7171
},
7272
],

packages/features/form-builder/schema.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { z } from "zod";
33
import { getValidRhfFieldName } from "@calcom/lib/getValidRhfFieldName";
44

55
import { fieldTypesConfigMap } from "./fieldTypes";
6-
import { getVariantsConfig, preprocessNameFieldDataWithVariant } from "./utils";
6+
import { preprocessNameFieldDataWithVariant } from "./utils";
7+
import { getConfig as getVariantsConfig } from "./utils/variantsConfig";
78

89
const nonEmptyString = () => z.string().refine((value: string) => value.trim().length > 0);
910

packages/features/form-builder/utils.ts

-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import type z from "zod";
2-
3-
import { fieldTypesConfigMap } from "./fieldTypes";
4-
import type { fieldSchema } from "./schema";
5-
61
export const preprocessNameFieldDataWithVariant = (
72
variantName: "fullName" | "firstAndLastName",
83
value: string | Record<"firstName" | "lastName", string> | undefined
@@ -53,21 +48,3 @@ function getFirstAndLastName(value: string | Record<"firstName" | "lastName", st
5348
}
5449
return newValue;
5550
}
56-
57-
/**
58-
* Get's the field's variantsConfig and if not available, then it will get the default variantsConfig from the fieldTypesConfigMap
59-
*/
60-
export const getVariantsConfig = (field: Pick<z.infer<typeof fieldSchema>, "variantsConfig" | "type">) => {
61-
const fieldVariantsConfig = field.variantsConfig;
62-
const fieldTypeConfig = fieldTypesConfigMap[field.type as keyof typeof fieldTypesConfigMap];
63-
64-
if (!fieldTypeConfig) throw new Error(`Invalid field.type ${field.type}}`);
65-
66-
const defaultVariantsConfig = fieldTypeConfig?.variantsConfig?.defaultValue;
67-
const variantsConfig = fieldVariantsConfig || defaultVariantsConfig;
68-
69-
if (fieldTypeConfig.propsType === "variants" && !variantsConfig) {
70-
throw new Error(`propsType variants must have variantsConfig`);
71-
}
72-
return variantsConfig;
73-
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type z from "zod";
2+
3+
import type { useLocale } from "@calcom/lib/hooks/useLocale";
4+
5+
import { fieldTypesConfigMap } from "../fieldTypes";
6+
import type { fieldSchema } from "../schema";
7+
8+
type ConfigVariants = NonNullable<ReturnType<typeof getConfig>>["variants"];
9+
type Field = z.infer<typeof fieldSchema>;
10+
type Translate = ReturnType<typeof useLocale>["t"];
11+
12+
function getTranslatedConfigVariants(configVariants: ConfigVariants, translate: Translate) {
13+
return Object.entries(configVariants).reduce((variantsConfigVariants, [variantName, variant]) => {
14+
const translatedFields = variant.fields.map((field) => {
15+
const label = field.label ?? "";
16+
const placeholder = field.placeholder ?? "";
17+
return {
18+
...field,
19+
label: translate(label),
20+
placeholder: translate(placeholder),
21+
};
22+
});
23+
variantsConfigVariants[variantName] = {
24+
...variant,
25+
fields: translatedFields,
26+
};
27+
28+
return variantsConfigVariants;
29+
}, {} as typeof configVariants);
30+
}
31+
32+
/**
33+
* Get's the field's variantsConfig and if not available, then it will get the default variantsConfig from the fieldTypesConfigMap
34+
*/
35+
export const getConfig = (field: Pick<Field, "variantsConfig" | "type">) => {
36+
const fieldVariantsConfig = field.variantsConfig;
37+
const fieldTypeConfig = fieldTypesConfigMap[field.type as keyof typeof fieldTypesConfigMap];
38+
39+
if (!fieldTypeConfig) throw new Error(`Invalid field.type ${field.type}}`);
40+
41+
const defaultVariantsConfig = fieldTypeConfig?.variantsConfig?.defaultValue;
42+
const variantsConfig = fieldVariantsConfig || defaultVariantsConfig;
43+
44+
if (fieldTypeConfig.propsType === "variants" && !variantsConfig) {
45+
throw new Error(`propsType variants must have variantsConfig`);
46+
}
47+
return variantsConfig;
48+
};
49+
50+
export const getTranslatedConfig = (field: Pick<Field, "variantsConfig" | "type">, translate: Translate) => {
51+
const variantsConfig = getConfig(field);
52+
if (!variantsConfig) return variantsConfig;
53+
const newVariantsConfigVariants = getTranslatedConfigVariants(variantsConfig.variants, translate);
54+
55+
return {
56+
...variantsConfig,
57+
variants: newVariantsConfigVariants,
58+
};
59+
};

0 commit comments

Comments
 (0)