11# vue-json-schema
2-
32Vue component form based on JSON Schema
43
54[ ![ Build Status] ( https://travis-ci.org/demsking/vue-json-schema.svg?branch=master )] ( https://travis-ci.org/demsking/vue-json-schema )
6- [ ![ bitHound Overall Score] ( https://www.bithound.io/github/demsking/vue-json-schema/badges/score.svg )] ( https://www.bithound.io/github/demsking/vue-json-schema )
7- [ ![ bitHound Dependencies] ( https://www.bithound.io/github/demsking/vue-json-schema/badges/dependencies.svg )] ( https://www.bithound.io/github/demsking/vue-json-schema/master/dependencies/npm )
8- [ ![ bitHound Code] ( https://www.bithound.io/github/demsking/vue-json-schema/badges/code.svg )] ( https://www.bithound.io/github/demsking/vue-json-schema )
9-
105
116## Install
127``` sh
138npm install --save vue-json-schema
149```
1510
11+ ## Demo
12+ - [ Demo with ElementUI] ( https://github.com/demsking/vue-json-schema-demo-elementui )
13+
1614## FormSchema API
15+
1716### props
1817- ` schema ` *** Object*** (* required* )
1918The JSON Schema object. Use the ` v-if ` directive to load asynchronous schema.
@@ -27,35 +26,38 @@ This property indicates whether the value of the control can be automatically co
2726- ` novalidate ` *** Boolean*** (* optional* )
2827This Boolean attribute indicates that the form is not to be validated when submitted.
2928
30- - ` data-class-error ` *** String*** (* optional* ) ` default: 'uk-form-danger' `
29+ - ` item-class ` *** String*** (* optional* )
30+ Use this prop to enable inputs wrapping
31+
32+ - ` data-class-error ` *** String*** (* optional* ) ` default: 'form-error' `
3133
3234### events
35+ - ` input ` undefined
36+
3337- ` change ` Fired when an form input value is changed.
3438
3539- ` invalid ` Fired when a submittable element has been checked and doesn't satisfy its constraints. The validity of submittable elements is checked before submitting their owner form.
3640
3741- ` submit ` Fired when a form is submitted
3842
3943### methods
40- - ` input() `
44+ - ` input(name ) `
4145Get a form input component
4246
4347- ` reset() `
4448Reset the value of all elements of the parent form.
4549
46- - ` submit() `
50+ - ` submit(e ) `
4751Send the content of the form to the server
4852
49- - ` setErrorMessage() `
53+ - ` setErrorMessage(message ) `
5054Set a message error.
5155
5256- ` clearErrorMessage() `
5357clear the message error.
5458
55-
5659## Usage
5760Define your [ JSON Schema] ( http://json-schema.org ) file:
58-
5961``` json
6062{
6163 "$schema" : " http://json-schema.org/draft-04/schema#" ,
@@ -88,9 +90,7 @@ Define your [JSON Schema](http://json-schema.org) file:
8890 "required" : [" name" , " email" , " lists" ]
8991}
9092```
91-
9293In your Vue file:
93-
9494``` html
9595<template >
9696 <form-schema :schema =" schema" v-model =" model" @submit =" submit" >
@@ -99,7 +99,7 @@ In your Vue file:
9999</template >
100100
101101<script >
102- import FormSchema from ' vue-form -schema'
102+ import FormSchema from ' vue-json -schema'
103103 import schema from ' ./schema/newsletter-subscription.json'
104104
105105 export default {
@@ -118,6 +118,68 @@ In your Vue file:
118118 </script >
119119```
120120
121- ## License
121+ ## Use custom form elements
122+
123+ Use ` FormSchema.setComponent(type, component[, props = {}]) ` to define custom element to use for rendering.
124+
125+ See [ vue-json-schema-demo-elementui] ( https://github.com/demsking/vue-json-schema-demo-elementui ) for a complete example.
126+
127+ ``` js
128+ // an element-ui example
129+
130+ import FormSchema from ' vue-json-schema'
131+ import {
132+ Form ,
133+ FormItem ,
134+ Input ,
135+ Radio ,
136+ Checkbox ,
137+ Select ,
138+ Option ,
139+ Button
140+ } from ' element-ui'
141+
142+ FormSchema .setComponent (' label' , FormItem)
143+ FormSchema .setComponent (' email' , Input)
144+ FormSchema .setComponent (' text' , Input)
145+ FormSchema .setComponent (' textarea' , Input)
146+ FormSchema .setComponent (' checkbox' , Checkbox)
147+ FormSchema .setComponent (' radio' , Radio)
148+ FormSchema .setComponent (' select' , Select)
149+ FormSchema .setComponent (' option' , Option)
150+
151+ // Use the third argument to define props of the component
152+ FormSchema .setComponent (' button' , Button, {
153+ type: ' primary' ,
154+ label: ' Subscribe'
155+ })
156+
157+ // The third argument can also be a function that return an object
158+ FormSchema .setComponent (' form' , Form, (vm ) => {
159+ // vm is the FormSchema VM
160+
161+ const labelWidth = ' 120px'
162+ const model = vm .data
163+ const rules = {}
164+
165+ vm .fields .forEach ((field ) => {
166+ rules[field .name ] = {
167+ required: field .required ,
168+ message: field .title
169+ }
170+ })
171+
172+ return { labelWidth, rules, model }
173+ })
174+
175+ export default {
176+ data : () => ({
177+ schema: {... }
178+ }),
179+ // ...
180+ components: { FormSchema }
181+ }
182+ ```
122183
184+ ## License
123185Under the MIT license. See [ LICENSE] ( https://github.com/demsking/vue-json-schema/blob/master/LICENSE ) file for more details.
0 commit comments