Skip to content

Commit 1cb03bc

Browse files
committed
Support of Custom Element
This fixes #5 and #4
2 parents 5115c20 + d2c1040 commit 1cb03bc

14 files changed

Lines changed: 4676 additions & 1193 deletions

.README_FOOTER.md

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

.README_HEADER.md

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

.npmignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
test
2+
src
3+
lib
4+
build
5+
config
6+
.directory
7+
.eslintrc
8+
.gitignore
9+
.travis.yml
10+
Makefile
11+
*.tgz
12+
*.js
13+
*.bak
14+
*.vue

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- '4'
3+
- '6'
44
script:
55
- npm install
66
- npm test

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
.SILENT: readme
2+
13
readme:
2-
@cat .README_HEADER.md > ./README.md
3-
@./node_modules/.bin/vuedoc.md --ignore-name --level 3 component.vue >> ./README.md
4-
@cat .README_FOOTER.md >> ./README.md
4+
./node_modules/.bin/vuedoc.md component.vue \
5+
--ignore-name \
6+
--ignore-data \
7+
--section 'FormSchema API' \
8+
--output ./README.md

README.md

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
# vue-json-schema
2-
32
Vue 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
138
npm 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*)
1918
The 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*)
2827
This 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)`
4145
Get a form input component
4246

4347
- `reset()`
4448
Reset the value of all elements of the parent form.
4549

46-
- `submit()`
50+
- `submit(e)`
4751
Send the content of the form to the server
4852

49-
- `setErrorMessage()`
53+
- `setErrorMessage(message)`
5054
Set a message error.
5155

5256
- `clearErrorMessage()`
5357
clear the message error.
5458

55-
5659
## Usage
5760
Define 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-
9293
In 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
123185
Under the MIT license. See [LICENSE](https://github.com/demsking/vue-json-schema/blob/master/LICENSE) file for more details.

build.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
process.env.NODE_ENV = 'production'
4+
5+
const ora = require('ora')
6+
const rm = require('rimraf')
7+
const path = require('path')
8+
const chalk = require('chalk')
9+
const webpack = require('webpack')
10+
const webpackConfig = require('./webpack.config')
11+
12+
const spinner = ora('building for production...')
13+
spinner.start()
14+
15+
rm(path.join(__dirname, 'dist'), err => {
16+
if (err) {
17+
throw err
18+
}
19+
20+
webpack(webpackConfig, function (err, stats) {
21+
spinner.stop()
22+
23+
if (err) {
24+
throw err
25+
}
26+
27+
process.stdout.write(stats.toString({
28+
colors: true,
29+
modules: false,
30+
children: false,
31+
chunks: false,
32+
chunkModules: false
33+
}) + '\n\n')
34+
35+
if (stats.hasErrors()) {
36+
console.log(chalk.red(' Build failed with errors.\n'))
37+
process.exit(1)
38+
}
39+
40+
console.log(chalk.cyan(' Build complete.\n'))
41+
console.log(chalk.yellow(
42+
' Tip: built files are meant to be served over an HTTP server.\n' +
43+
' Opening index.html over file:// won\'t work.\n'
44+
))
45+
})
46+
})

0 commit comments

Comments
 (0)