surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.2k stars 812 forks source link

Custom QuestionWrapperComponent #8545

Closed momaekwudi closed 3 months ago

momaekwudi commented 4 months ago

A question, Currently, I have achieved the effect of Figure 2 using the method shown in Figure 1. However, my expectation is that 123 will be displayed below the title. What should I do? Thank you

image image
JaneSjs commented 4 months ago

Hello @momaekwudi, Would you please elaborate on your main goal?

Thank you

momaekwudi commented 4 months ago

Hello @momaekwudi, Would you please elaborate on your main goal?

Thank you

Hi, @JaneSjs Okay, I expect to display custom components under each question, such as some images. that come from some of my custom attributes

image
JaneSjs commented 3 months ago

Hello @momaekwudi, Thank you for the update. The question React component defines the rendering for the entire question element. Therefore, your custom content appears above the question element. You can refer to our source code for more information on how survey questions are rendered: reactquestion.tsx.

If you wish to render a custom content under a question's title and description, override the renderQuestionContent function.

Consider the following code:

import { SurveyQuestion, ReactElementFactory } from "survey-react-ui";

class QuestionWrapperComponent extends SurveyQuestion {
  renderQuestionContent() {
    return (
      <div style={{ width: "100%" }}>
        <div className="fillDescription-with-question">
          <div>123</div>
        </div>
        <div>{super.renderQuestionContent()}</div>
      </div>
    );
  }
}
ReactElementFactory.Instance.registerElement("question", (props) => {
  return React.createElement(QuestionWrapperComponent, props);
});

image

Please feel free to reactivate this thread if you require further assistance.