feat: adds line break support in open text question textarea (#6456)

This commit is contained in:
Piyush Gupta
2025-08-25 11:27:04 +05:30
committed by GitHub
parent a3764f0316
commit b6a63edc88
2 changed files with 21 additions and 5 deletions

View File

@@ -138,7 +138,7 @@ export function OpenTextQuestion({
tabIndex={isCurrent ? 0 : -1}
aria-label="textarea"
id={question.id}
placeholder={getLocalizedValue(question.placeholder, languageCode)}
placeholder={getLocalizedValue(question.placeholder, languageCode, true)}
dir={dir}
required={question.required}
value={value}

View File

@@ -5,15 +5,31 @@ const isI18nObject = (obj: any): obj is TI18nString => {
return typeof obj === "object" && obj !== null && Object.keys(obj).includes("default");
};
export const getLocalizedValue = (value: TI18nString | undefined, languageId: string): string => {
// Matches \r\n, \n, \r, and their HTML entity variants
const ESCAPED_NEWLINES = /\\r\\n|
|\\n|\\r|
|
/g;
export const unescapeNewlines = (s: string): string => s.replace(ESCAPED_NEWLINES, "\n");
export const getLocalizedValue = (
value: TI18nString | undefined,
languageId: string,
replaceNewLines: boolean = false
): string => {
if (!value) {
return "";
}
let result = "";
if (isI18nObject(value)) {
if (typeof value[languageId] === "string") {
return value[languageId];
result = value[languageId];
} else {
result = value.default;
}
return value.default;
result = replaceNewLines ? unescapeNewlines(result) : result;
}
return "";
return result;
};