React LogoSurvey Creator

A Survey Creator is a powerful visual tool that allows users, often non-technical ones, to design and build surveys, quizzes, forms, or polls through a drag-and-drop interface. Instead of writing code, users can visually add various question types (e.g., single-choice, multi-choice, text input, rating, matrix), define their properties, set up complex logic (e.g., conditional visibility, branching), and customize the appearance, all within a user-friendly environment.

The primary benefits of using a Survey Creator include:

1. Empowerment of Non-Developers: Business users, researchers, or marketers can independently create and manage surveys without relying on developers.
2. Accelerated Development: Significantly reduces the time and effort required to design and deploy surveys compared to coding them from scratch.
3. Rich Functionality: Offers a wide range of pre-built question types, validation rules, and logic options that are complex to implement manually.
4. Consistency and Professionalism: Helps maintain a consistent look and feel across surveys and ensures adherence to best practices in survey design.
5. Output in Standard Formats: Typically outputs the survey structure and data in a standard format, most commonly JSON, which can then be easily consumed by an application to render the survey or save its data.

In the context of modern web development, particularly with frameworks like React, Survey Creators are often integrated as UI components. Libraries like SurveyJS Creator provide a robust set of React components that can be embedded into an application. Developers can initialize the creator, provide it with an initial survey definition (if editing an existing one), and then capture the generated JSON when the user saves or finishes designing the survey. This JSON can then be stored in a database and used by a 'Survey Runner' component to display the survey to respondents.

Example Code

import React, { useState, useCallback } from 'react';
import { SurveyCreator, SurveyCreatorComponent } from 'survey-creator-react';
import { StylesManager, Model } from 'survey-core';

// Import Survey Creator's default styles
import 'survey-creator-core/survey-creator-core.css';

// Apply default SurveyJS styles (optional, but good for consistency)
StylesManager.applyTheme('defaultV2');

function SurveyCreatorApp() {
  // Initial survey JSON (optional, for editing an existing survey)
  const initialSurveyJson = {
    "pages": [
      {
        "name": "page1",
        "elements": [
          {
            "type": "text",
            "name": "question1",
            "title": "What is your name?"
          }
        ]
      }
    ]
  };

  // State to hold the current survey JSON from the creator
  const [currentSurveyJson, setCurrentSurveyJson] = useState(initialSurveyJson);

  // Create a SurveyCreator instance outside the component render to avoid re-creation
  // This instance can be configured further if needed.
  const creator = new SurveyCreator();
  creator.text = JSON.stringify(initialSurveyJson);

  // Callback to handle survey changes (e.g., when the user saves the survey)
  const handleSurveyChange = useCallback((sender, options) => {
    // The 'sender' is the SurveyCreator instance itself.
    // 'options' might contain details about what changed.
    // We'll get the current JSON directly from the creator instance.
    const newJson = JSON.parse(sender.text);
    console.log("Survey JSON updated:", newJson);
    setCurrentSurveyJson(newJson);
  }, []);

  // Attach the event listener to the creator instance
  // This should be done once, e.g., using useEffect or by attaching directly to the instance.
  // For simplicity, we'll assume it's set up when the creator is instantiated or in a useEffect.
  // A more robust way might involve useEffect to manage event subscriptions.
  // For this example, we'll simulate saving the JSON from the creator on a 'save' equivalent action.
  // SurveyCreatorComponent internally listens to changes, so we can just grab 'creator.text'
  // when an explicit save button is pressed or use an event provided by the library.

  return (
    <div style={{ height: 'calc(100vh - 20px)', width: '100%', padding: '10px' }}>
      <h1>My Survey Creator</h1>
      <div style={{ height: '70%', border: '1px solid #ccc' }}>
        {/* Render the SurveyCreatorComponent */}
        <SurveyCreatorComponent creator={creator} onSaveSurvey={handleSurveyChange} />
      </div>
      <div style={{ marginTop: '20px' }}>
        <h2>Current Survey JSON:</h2>
        <pre style={{ backgroundColor: '#f0f0f0', padding: '10px', borderRadius: '5px', maxHeight: '200px', overflowY: 'auto' }}>
          {JSON.stringify(currentSurveyJson, null, 2)}
        </pre>
        <button
          onClick={() => creator.saveSurvey()}
          style={{
            padding: '10px 20px',
            fontSize: '16px',
            cursor: 'pointer',
            marginTop: '10px',
            backgroundColor: '#007bff',
            color: 'white',
            border: 'none',
            borderRadius: '5px'
          }}
        >
          Save Survey
        </button>
      </div>
    </div>
  );
}

export default SurveyCreatorApp;