Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use onElementWrapperComponentName event for getting renderig string #8420

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading