diff --git a/src/survey.ts b/src/survey.ts index 765e11aa5d..359c367a27 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -1920,13 +1920,14 @@ export class SurveyModel extends SurveyElementCore return this.getRendererContextForString(this, locStr); } public getRendererForString(element: Question | PanelModel | PageModel | SurveyModel, name: string): string { - const renderAs = this.getBuiltInRendererForString(element, name); + let renderAs = this.getBuiltInRendererForString(element, name); + renderAs = this.elementWrapperComponentNameCore(renderAs, element, "string"); const options: TextRenderAsEvent = { element: element, name: name, renderAs: renderAs }; this.onTextRenderAs.fire(this, options); return options.renderAs; } - public getRendererContextForString(element: Base, locStr: LocalizableString) { - return locStr; + public getRendererContextForString(element: Base, locStr: LocalizableString): any { + return this.elementWrapperDataCore(locStr, element, "string"); } getExpressionDisplayValue( question: Question, diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 673dd57ea8..45adc6a611 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -14474,6 +14474,48 @@ QUnit.test("onTextRenderAs event", function (assert) { assert.equal(locString.renderAs, customRendererView); assert.equal(renderAs, customRendererView); }); +QUnit.test("onElementWrapperComponentName event vs string getRenderer", function (assert) { + const survey = new SurveyModel(); + const questionName = "any question"; + const locString = new LocalizableString(survey, false, "name"); + + let renderAs = survey.getRenderer(questionName); + + const customRendererView = "my-custom-renderer-view"; + const customRendererEdit = "my-custom-renderer-edit"; + survey.onElementWrapperComponentName.add((s, e) => { + if(e.wrapperName !== "string") return; + if (s.isDesignMode) e.componentName = customRendererEdit; + else e.componentName = customRendererView; + }); + + renderAs = survey.getRenderer(questionName); + assert.equal(locString.renderAs, customRendererView); + assert.equal(renderAs, customRendererView); + + survey.setDesignMode(true); + renderAs = survey.getRenderer(questionName); + assert.equal(locString.renderAs, customRendererEdit); + assert.equal(renderAs, customRendererEdit); + + survey.setDesignMode(false); + renderAs = survey.getRenderer(questionName); + assert.equal(locString.renderAs, customRendererView); + assert.equal(renderAs, customRendererView); +}); +QUnit.test("onElementWrapperComponentData event vs getRendererContextForString", function (assert) { + const survey = new SurveyModel(); + survey.addNewPage("page1"); + const locString = new LocalizableString(survey, false, "name"); + + survey.onElementWrapperComponentData.add((s, e) => { + if(e.wrapperName !== "string") return; + e.data = { str: e.data, el: e.element }; + }); + const res = survey.getRendererContextForString(survey.pages[0], locString); + assert.equal(res.str.name, "name", "string is correct"); + assert.equal(res.el.name, "page1", "element is correct"); +}); QUnit.test("Make inputs read-only in design-mode for V2", function (assert) { settings.supportCreatorV2 = true;