Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lotas/survey-builder

Small interactive survey builder
https://github.com/lotas/survey-builder

survey survey-form svelte tailwind typescript

Last synced: 21 days ago
JSON representation

Small interactive survey builder

Awesome Lists containing this project

README

        

# Survey Builder

Small library to build custom surveys, export/import to JSON.

![](docs/2021-09-20-17-54-20.png)

[Live demo](https://yaraslav.com/survey-builder/)

# Installation

Use sources or include `dist/survey-builder.css`, `dist/survey-builder[.min].js` files.

# Usage

Library will register custom component in global scope `SurveyBuilderInit`:

```ts
function SurveyBuilderInit(SurveyBuilderOpts): void;

export interface SurveyBuilderOpts {
target: HTMLElement | Element; // i.e. document.getElementById('placeholder')
props: {
haveTitle?: boolean;
customInputNames?: boolean; // allow input of custom names for each question
snapshot?: SurveyBuilderSnapshot; // see below
onChange?: (snapshot: SurveyBuilderSnapshot) => void; // will be triggered on every change
onExport?: (snapshot: SurveyBuilderSnapshot) => void; // will be triggered on export click
debug?: boolean; // show debug info
showLabels?: boolean;
showExportButton?: boolean;
};
}
```

To use it:

```html


window.SurveyBuilderInit({
target: document.getElementById('element-root'),
props: {
haveTitle: false,
customInputNames: true,
debug: true,
showLabels: true,
showExportButton: true,
snapshot: snapshot: {
title: 'Sample survey',
questions: [
{
id: 'q-1',
title: 'User name',
name: 'userName',
type: 'input',
required: true,
},
{
id: 'q-2',
title: 'User rating',
name: 'userRating',
type: 'rating',
required: true,
options: 5,
},
{
id: 'q-3',
title: 'Account type',
name: 'accountType',
type: 'single',
required: true,
answers: [
{
id: 'a-1',
title: 'Private account',
type: 'text',
},
{
id: 'a-2',
title: 'Business account',
type: 'text',
},
],
},
],
},
onExport: (data) => {
console.log('Congrats: export', data)
},
onChange: (data) => {
console.log('Model changed', data)
},
},
})

```