Skip to content

Commit c86d62d

Browse files
Merge pull request #3 from surveyjs/nuxt3-migration
Migrate to Nuxt 3
2 parents 494fa08 + 13f8dda commit c86d62d

File tree

17 files changed

+7135
-286
lines changed

17 files changed

+7135
-286
lines changed

.editorconfig

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,37 @@
1-
# Y
1+
# SurveyJS Form Builder + Nuxt Demo Example
22

3-
## Build Setup
3+
This demo shows how to add [SurveyJS Form Builder / Survey Creator](https://surveyjs.io/survey-creator/documentation/overview) to a Nuxt application.
44

5-
```bash
6-
# install dependencies
7-
$ npm install
8-
9-
# serve with hot reload at localhost:3000
10-
$ npm run dev
11-
12-
# build for production and launch server
13-
$ npm run build
14-
$ npm run start
5+
## Run the Application
156

16-
# generate static project
17-
$ npm run generate
7+
```bash
8+
git clone https://github.com/surveyjs/surveyjs-nuxtjs.git
9+
cd surveyjs-nuxtjs
10+
npm i
11+
npm run dev
1812
```
1913

20-
For detailed explanation on how things work, check out the [documentation](https://nuxtjs.org).
21-
22-
## Special Directories
23-
24-
You can create the following extra directories, some of which have special behaviors. Only `pages` is required; you can delete them if you don't want to use their functionality.
25-
26-
### `assets`
27-
28-
The assets directory contains your uncompiled assets such as Stylus or Sass files, images, or fonts.
29-
30-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/assets).
31-
32-
### `components`
33-
34-
The components directory contains your Vue.js components. Components make up the different parts of your page and can be reused and imported into your pages, layouts and even other components.
35-
36-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/components).
37-
38-
### `layouts`
39-
40-
Layouts are a great help when you want to change the look and feel of your Nuxt app, whether you want to include a sidebar or have distinct layouts for mobile and desktop.
41-
42-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/layouts).
43-
44-
45-
### `pages`
46-
47-
This directory contains your application views and routes. Nuxt will read all the `*.vue` files inside this directory and setup Vue Router automatically.
48-
49-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/get-started/routing).
50-
51-
### `plugins`
52-
53-
The plugins directory contains JavaScript plugins that you want to run before instantiating the root Vue.js Application. This is the place to add Vue plugins and to inject functions or constants. Every time you need to use `Vue.use()`, you should create a file in `plugins/` and add its path to plugins in `nuxt.config.js`.
54-
55-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/plugins).
56-
57-
### `static`
14+
Open http://localhost:3000 in your web browser.
5815

59-
This directory contains your static files. Each file inside this directory is mapped to `/`.
16+
## How to add SurveyJS Form Builder to your Nuxt application
6017

61-
Example: `/static/robots.txt` is mapped as `/robots.txt`.
6218

63-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/static).
19+
1. Install the `survey-creator-vue` npm package:
20+
21+
```bash
22+
npm install survey-creator-vue --save
23+
```
6424

65-
### `store`
25+
2. Create a file in the [`plugins`](./plugins/) directory and install SurveyJS Vue plugins in this file using the `nuxtApp.vueApp.use()` method.
26+
3. Open the [`next.config.ts`](./nuxt.config.ts) file and do the following in it:
27+
1. List the plugin file in the `plugins` array.
28+
2. List SurveyJS style sheets in the `css` array.
29+
4. Create a component and configure Survey Creator in it (see the [`components/SurveyCreator.vue`](./components/SurveyCreator.vue) file).
30+
5. Add the component to a page (see the [`app.vue`](./app.vue) file). In this example, the component is wrapped in the `<ClientOnly>` tag because Survey Creator uses `localStorage` to save and restore survey JSON schemas. If your application doesn't use this functionality, you can render Survey Creator on the server.
6631
67-
This directory contains your Vuex store files. Creating a file in this directory automatically activates Vuex.
32+
## SurveyJS Resources
6833
69-
More information about the usage of this directory in [the documentation](https://nuxtjs.org/docs/2.x/directory-structure/store).
34+
- [Website](https://surveyjs.io/)
35+
- [Documentation](https://surveyjs.io/survey-creator/documentation/overview)
36+
- [Starter Demos](https://surveyjs.io/survey-creator/examples/free-nps-survey-template/reactjs)
37+
- [What's New](https://surveyjs.io/stay-updated/major-updates/2023)

app.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div id="surveyCreator">
3+
<ClientOnly fallback-tag="div" fallback="Loading Survey Creator...">
4+
<SurveyCreator />
5+
</ClientOnly>
6+
</div>
7+
</template>
8+
9+
<style>
10+
#surveyCreator {
11+
height: 100vh;
12+
width: 100vw;
13+
}
14+
15+
body {
16+
margin: 0;
17+
}
18+
</style>

components/NuxtLogo.vue

Lines changed: 0 additions & 11 deletions
This file was deleted.

components/SurveyCreator.vue

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1-
<template>
2-
<div id="surveyCreator"></div>
3-
</template>
4-
5-
<script>
6-
import { SurveyCreator } from "survey-creator-knockout";
7-
import "survey-core/defaultV2.min.css";
8-
import "survey-creator-core/survey-creator-core.min.css";
9-
10-
import { Serializer } from "survey-core";
1+
<script setup lang="ts">
2+
import type { ICreatorOptions } from "survey-creator-core";
3+
import { SurveyCreatorModel } from "survey-creator-core";
114
12-
Serializer.addProperty("question", "tag:number");
5+
const creatorOptions: ICreatorOptions = {
6+
showLogicTab: true,
7+
isAutoSave: true
8+
};
139
14-
export default {
15-
name: "survey-creator",
16-
data() {
17-
return {};
18-
},
19-
mounted() {
20-
const creatorOptions = {
21-
showLogicTab: true,
22-
isAutoSave: true
23-
};
24-
const creator = new SurveyCreator(creatorOptions);
10+
const defaultJson = {
11+
pages: [{
12+
name: "Name",
13+
elements: [{
14+
name: "FirstName",
15+
title: "Enter your first name:",
16+
type: "text"
17+
}, {
18+
name: "LastName",
19+
title: "Enter your last name:",
20+
type: "text"
21+
}]
22+
}]
23+
};
2524
26-
creator.saveSurveyFunc = function() {
27-
console.log(JSON.stringify(this.text));
28-
};
29-
creator.render("surveyCreator");
30-
}
31-
25+
const creator = new SurveyCreatorModel(creatorOptions);
26+
creator.text = window.localStorage.getItem("survey-json") || JSON.stringify(defaultJson);
27+
creator.saveSurveyFunc = (saveNo: number, callback: Function) => {
28+
window.localStorage.setItem("survey-json", creator.text);
29+
callback(saveNo, true);
3230
};
3331
</script>
3432

35-
<style scoped>
36-
#surveyCreator {
37-
height: 100vh;
38-
width: 100vw;
39-
}
40-
</style>
33+
<template>
34+
<SurveyCreatorComponent :model="creator" />
35+
</template>

components/Tutorial.vue

Lines changed: 0 additions & 52 deletions
This file was deleted.

nuxt.config.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

nuxt.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// https://nuxt.com/docs/api/configuration/nuxt-config
2+
export default defineNuxtConfig({
3+
compatibilityDate: '2024-04-03',
4+
devtools: { enabled: true },
5+
css: [
6+
'survey-core/defaultV2.min.css',
7+
'survey-creator-core/survey-creator-core.min.css'
8+
],
9+
plugins: ['~/plugins/surveyjs-form-builder.client.ts']
10+
})

0 commit comments

Comments
 (0)