Skip to content

Commit 2866c3f

Browse files
committed
1 parent 38f45ba commit 2866c3f

File tree

11 files changed

+735
-61
lines changed

11 files changed

+735
-61
lines changed

build/webpack.base.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
extensions: ['.js', '.vue', '.json'],
2525
alias: {
2626
'vue$': 'vue/dist/vue.esm.js',
27-
'@': resolve('src'),
27+
'@': resolve('example'),
2828
}
2929
},
3030
module: {

example/App.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
12
<template>
23
<div id="app">
3-
<img src="./assets/logo.png">
4-
<HelloWorld/>
4+
<Subscription/>
55
</div>
66
</template>
77

88
<script>
9-
import HelloWorld from './components/HelloWorld'
9+
import Subscription from './components/Subscription'
1010
1111
export default {
1212
name: 'app',
1313
components: {
14-
HelloWorld
14+
Subscription
1515
}
1616
}
1717
</script>
@@ -25,4 +25,4 @@ export default {
2525
color: #2c3e50;
2626
margin-top: 60px;
2727
}
28-
</style>
28+
</style>

example/components/HelloWorld.vue

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<template>
2+
<el-card class="form">
3+
<json-editor ref="JsonEditor" :schema="schema" v-model="model">
4+
<el-button type="primary" @click="submit">Subscribe</el-button>
5+
<el-button type="reset">Reset</el-button>
6+
</json-editor>
7+
</el-card>
8+
</template>
9+
10+
<script>
11+
import JsonEditor from '../../src/JsonEditor'
12+
import { Option } from 'element-ui'
13+
JsonEditor.setComponent('form', 'el-form', ({ vm }) => {
14+
// vm is the JsonEditor VM
15+
const labelWidth = '120px'
16+
const model = vm.data
17+
const rules = {}
18+
vm.fields.forEach((field) => {
19+
const type = field.schemaType === 'array' && field.type === 'radio'
20+
? 'string'
21+
: field.schemaType
22+
const required = field.required
23+
const message = field.title
24+
const trigger = ['radio', 'checkbox', 'select'].includes(field.type)
25+
? 'change' : 'blur'
26+
// http://element.eleme.io/#/en-US/component/form#validation
27+
rules[field.name] = { type, required, message, trigger }
28+
})
29+
// returning the form props
30+
return { labelWidth, rules, model }
31+
})
32+
// http://element.eleme.io/#/en-US/component/form#validation
33+
JsonEditor.setComponent('label', 'el-form-item', ({ field }) => ({
34+
prop: field.name
35+
}))
36+
JsonEditor.setComponent('email', 'el-input')
37+
JsonEditor.setComponent('text', 'el-input')
38+
JsonEditor.setComponent('textarea', 'el-input')
39+
JsonEditor.setComponent('checkbox', 'el-checkbox')
40+
JsonEditor.setComponent('switch', 'el-switch')
41+
JsonEditor.setComponent('radio', 'el-radio')
42+
JsonEditor.setComponent('select', 'el-select')
43+
// You can also use the component object
44+
JsonEditor.setComponent('option', Option)
45+
// By default `<h1/>` is used to render the form title.
46+
// To override this, use the `title` type:
47+
JsonEditor.setComponent('title', 'h2')
48+
// By default `<p/>` is used to render the form title.
49+
// To override this, use the `description` type:
50+
JsonEditor.setComponent('description', 'small')
51+
// By default `<div/>` is used to render the message error.
52+
// To override this, use the `error` type:
53+
JsonEditor.setComponent('error', 'el-alert', ({ vm }) => ({
54+
type: 'error',
55+
title: vm.error
56+
}))
57+
export default {
58+
data: () => ({
59+
schema: require('@/schema/newsletter'),
60+
model: {}
61+
}),
62+
methods: {
63+
submit (e) {
64+
// this.$refs.JsonEditor.form() returns the ElementUI's form instance
65+
this.$refs.JsonEditor.form().validate((valid) => {
66+
if (valid) {
67+
// this.model contains the valid data according your JSON Schema.
68+
// You can submit your model to the server here
69+
console.log(JSON.stringify(this.model))
70+
this.$refs.JsonEditor.clearErrorMessage()
71+
} else {
72+
this.$refs.JsonEditor.setErrorMessage('Please fill out the required fields')
73+
return false
74+
}
75+
})
76+
}
77+
},
78+
components: { JsonEditor }
79+
}
80+
</script>
81+
82+
<style>
83+
.form {
84+
text-align: left;
85+
width: 600px;
86+
margin: auto;
87+
}
88+
h2 {
89+
font-size: 1.7em;
90+
text-align: center;
91+
margin-top: 0;
92+
margin-bottom: .2em
93+
}
94+
h2 + small {
95+
display: block;
96+
text-align: center;
97+
margin-bottom: 1.2em
98+
}
99+
small {
100+
line-height: 20px;
101+
display: block;
102+
}
103+
.el-alert {
104+
margin-bottom: 15px
105+
}
106+
</style>

example/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import Vue from 'vue'
44
import App from './App'
55

6+
import ElementUI from 'element-ui'
7+
import 'element-ui/lib/theme-chalk/index.css'
8+
import locale from 'element-ui/lib/locale/lang/en'
9+
10+
Vue.use(ElementUI, { locale })
11+
612
Vue.config.productionTip = false
713

814
/* eslint-disable no-new */

example/schema/newsletter.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"title": "Newsletter Subscription",
5+
"description": "Sign up for free newsletters and get more delivered to your inbox",
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"minLength": 8,
10+
"maxLength": 80,
11+
"title": "Full Name",
12+
"attrs": {
13+
"placeholder": "Your Full Name",
14+
"title": "Please enter your full name"
15+
}
16+
},
17+
"email": {
18+
"type": "string",
19+
"maxLength": 120,
20+
"title": "Email",
21+
"attrs": {
22+
"type": "email",
23+
"placeholder": "Your Email",
24+
"title": "Please enter your email"
25+
}
26+
},
27+
"lists": {
28+
"type": "string",
29+
"title": "List",
30+
"enum": ["Daily New", "Promotion"],
31+
"attrs": {
32+
"placeholder": "Select your list subscription",
33+
"title": "Please select your list subscription"
34+
}
35+
},
36+
"source": {
37+
"type": "string",
38+
"maxLength": 120,
39+
"title": "Source",
40+
"description": "Ex. Using the NPM Search Engine",
41+
"attrs": {
42+
"type": "textarea",
43+
"placeholder": "How did you hear about us?"
44+
}
45+
},
46+
"agree": {
47+
"type": "boolean",
48+
"title": "Agree",
49+
"description": "You agree to receive occasional updates and special offers for vue-json-schema updates.",
50+
"default": false,
51+
"attrs": {
52+
"type": "checkbox"
53+
}
54+
}
55+
},
56+
"additionalProperties": false,
57+
"required": ["name", "email", "lists"]
58+
}

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"chalk": "^2.0.1",
2626
"copy-webpack-plugin": "^4.0.1",
2727
"css-loader": "^0.28.0",
28+
"element-ui": "^2.0.5",
2829
"eslint": "^3.19.0",
2930
"eslint-friendly-formatter": "^3.0.0",
3031
"eslint-loader": "^1.7.1",
@@ -34,21 +35,21 @@
3435
"file-loader": "^1.1.4",
3536
"friendly-errors-webpack-plugin": "^1.6.1",
3637
"html-webpack-plugin": "^2.30.1",
37-
"webpack-bundle-analyzer": "^2.9.0",
3838
"node-notifier": "^5.1.2",
39+
"optimize-css-assets-webpack-plugin": "^3.2.0",
40+
"ora": "^1.2.0",
41+
"portfinder": "^1.0.13",
3942
"postcss-import": "^11.0.0",
4043
"postcss-loader": "^2.0.8",
44+
"rimraf": "^2.6.0",
4145
"semver": "^5.3.0",
4246
"shelljs": "^0.7.6",
43-
"optimize-css-assets-webpack-plugin": "^3.2.0",
44-
"ora": "^1.2.0",
45-
"rimraf": "^2.6.0",
4647
"url-loader": "^0.5.8",
4748
"vue-loader": "^13.3.0",
4849
"vue-style-loader": "^3.0.1",
4950
"vue-template-compiler": "^2.5.2",
50-
"portfinder": "^1.0.13",
5151
"webpack": "^3.6.0",
52+
"webpack-bundle-analyzer": "^2.9.0",
5253
"webpack-dev-server": "^2.9.1",
5354
"webpack-merge": "^4.1.0"
5455
},

0 commit comments

Comments
 (0)