SurveyBuilder
A visual, drag-and-drop survey editor that lets users create and edit surveys without writing JSON. It outputs a standard SurveySchema and includes a live preview and JSON editor.
Import
import { SurveyBuilder } from 'react-minimal-survey-builder/builder';Props
interface SurveyBuilderProps {
/** Current schema value (controlled) */
value?: SurveySchema;
/** Called when the schema changes */
onChange?: (schema: SurveySchema) => void;
/** Custom class name */
className?: string;
/** Show the live preview panel */
showPreview?: boolean;
/** Show the JSON editor panel */
showJson?: boolean;
/** Customize builder layout */
layout?: 'horizontal' | 'vertical';
}valueThe current survey schema. Makes the builder a controlled component.
onChangeCallback fired whenever the schema changes. Receives the updated SurveySchema.
classNameCSS class applied to the root builder container.
showPreviewWhen true, a live preview tab renders the survey in real-time. Defaults to true.
showJsonWhen true, a JSON editor tab shows the raw schema. Useful for debugging. Defaults to false.
layoutControls the panel layout. 'horizontal' places panels side-by-side; 'vertical' stacks them.
Basic Usage
import { useState } from 'react';
import { SurveyBuilder } from 'react-minimal-survey-builder/builder';
import type { SurveySchema } from 'react-minimal-survey-builder';
function BuilderPage() {
const [schema, setSchema] = useState<SurveySchema>({
id: 'new-survey',
pages: [{ id: 'page1', questions: [] }],
});
return (
<SurveyBuilder
value={schema}
onChange={setSchema}
showPreview
showJson
/>
);
}Tabs
The builder provides up to three tabs that let users switch between different views:
Editor
The main editing interface. Users can add pages, add questions, configure question properties (label, type, options, validation, visibility), and reorder items via drag-and-drop.
Preview
A live preview that renders the survey using SurveyRenderer. Users can interact with the preview to test navigation, validation, and conditional logic. Enabled via showPreview.
JSON
A raw JSON editor showing the current schema. Changes in the JSON editor are synced back to the visual editor. Enabled via showJson.
Drag & Drop
The builder uses native HTML5 drag-and-drop for reordering questions within a page and moving questions between pages. See the Drag & Drop page for details.
Question Editor
Clicking a question in the editor opens a panel where users can configure:
| Property | Description |
|---|---|
Label | The question text shown to respondents |
Type | text, textarea, radio, checkbox, select, number, email, date, rating |
Required | Whether the question must be answered |
Placeholder | Hint text inside the input |
Description | Help text below the label |
Options | Choices for radio, checkbox, and select types |
Validation | Validation rules (minLength, maxLength, pattern, etc.) |
Visible If | Condition expression for conditional display |
Exporting the Schema
function BuilderWithExport() {
const [schema, setSchema] = useState<SurveySchema>({
id: 'my-survey',
pages: [{ id: 'page1', questions: [] }],
});
const handleExport = () => {
const json = JSON.stringify(schema, null, 2);
// Save to API, download as file, copy to clipboard, etc.
navigator.clipboard.writeText(json);
alert('Schema copied to clipboard!');
};
return (
<div>
<SurveyBuilder value={schema} onChange={setSchema} showPreview />
<button onClick={handleExport}>Export JSON</button>
</div>
);
}Layout Options
// Side-by-side panels (default)
<SurveyBuilder value={schema} onChange={setSchema} layout="horizontal" />
// Stacked panels
<SurveyBuilder value={schema} onChange={setSchema} layout="vertical" />What's Next?
- - Learn about drag & drop in detail
- - Add validation rules to questions
- - Set up conditional logic
- - See the full API reference