Skip to content

Commit 0c62ad8

Browse files
committed
Support of multiple checkbox and grouped radio elements
- Fixes #34 - Closes #35 - Fixes #32 - Fixes demsking/vue-json-schema-demo-elementui#1
2 parents 1cb03bc + 90592f2 commit 0c62ad8

File tree

6 files changed

+333
-195
lines changed

6 files changed

+333
-195
lines changed

README.md

+105-13
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,32 @@ This property indicates whether the value of the control can be automatically co
2626
- `novalidate` ***Boolean*** (*optional*)
2727
This Boolean attribute indicates that the form is not to be validated when submitted.
2828

29-
- `item-class` ***String*** (*optional*)
30-
Use this prop to enable inputs wrapping
31-
32-
- `data-class-error` ***String*** (*optional*) `default: 'form-error'`
29+
- `input-wrapping-class` ***String*** (*optional*)
30+
Define the inputs wrapping class. Leave `undefined` to disable input wrapping.
3331

3432
### events
35-
- `input` undefined
33+
- `input` Fired synchronously when the value of an element is changed.
3634

37-
- `change` Fired when an form input value is changed.
35+
- `change` Fired when a change to the element's value is committed by the user.
3836

39-
- `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.
37+
- `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, or after the `checkValidity()` of the element or its owner form is called.
4038

4139
- `submit` Fired when a form is submitted
4240

4341
### methods
4442
- `input(name)`
45-
Get a form input component
43+
Get a form input reference
44+
45+
- `form()`
46+
Get the form reference
47+
48+
- `checkValidity()`
49+
Checks whether the form has any constraints and whether it satisfies them. If the form fails its constraints, the browser fires a cancelable `invalid` event at the element, and then returns false.
4650

4751
- `reset()`
4852
Reset the value of all elements of the parent form.
4953

50-
- `submit(e)`
54+
- `submit(event)`
5155
Send the content of the form to the server
5256

5357
- `setErrorMessage(message)`
@@ -119,11 +123,8 @@ In your Vue file:
119123
```
120124

121125
## Use custom form elements
122-
123126
Use `FormSchema.setComponent(type, component[, props = {}])` to define custom element to use for rendering.
124-
125127
See [vue-json-schema-demo-elementui](https://github.com/demsking/vue-json-schema-demo-elementui) for a complete example.
126-
127128
```js
128129
// an element-ui example
129130

@@ -155,7 +156,7 @@ FormSchema.setComponent('button', Button, {
155156
})
156157

157158
// The third argument can also be a function that return an object
158-
FormSchema.setComponent('form', Form, (vm) => {
159+
FormSchema.setComponent('form', Form, ({ vm }) => {
159160
// vm is the FormSchema VM
160161

161162
const labelWidth = '120px'
@@ -169,9 +170,25 @@ FormSchema.setComponent('form', Form, (vm) => {
169170
}
170171
})
171172

173+
// returning the form props
172174
return { labelWidth, rules, model }
173175
})
174176

177+
// By default `<h1/>` is used to render the form title.
178+
// To override this, use the `title` type:
179+
FormSchema.setComponent('title', 'h2')
180+
181+
// By default `<p/>` is used to render the form description.
182+
// To override this, use the `description` type:
183+
FormSchema.setComponent('description', 'small')
184+
185+
// By default `<div/>` is used to render the message error.
186+
// To override this, use the `error` type:
187+
FormSchema.setComponent('error', 'el-alert', ({ vm }) => ({
188+
type: 'error',
189+
title: vm.error
190+
}))
191+
175192
export default {
176193
data: () => ({
177194
schema: {...}
@@ -181,5 +198,80 @@ export default {
181198
}
182199
```
183200

201+
## Multiple Checkbox elements
202+
To define multiple checkbox, use the [JSON Schema keyword `anyOf`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.27):
203+
204+
**schema.json**
205+
```json
206+
{
207+
"$schema": "http://json-schema.org/draft-04/schema#",
208+
"type": "object",
209+
"properties": {
210+
"multipleCheckbox": {
211+
"type": "array",
212+
"anyOf": [
213+
{ "value": "daily", "label": "Daily New" },
214+
{ "value": "promotion", "label": "Promotion" }
215+
]
216+
}
217+
}
218+
}
219+
220+
```
221+
222+
**component.vue**
223+
```html
224+
<script>
225+
import FormSchema from 'vue-json-schema'
226+
227+
FormSchema.setComponent('select', 'el-select', ({ item }) => {
228+
return { label: item.value }
229+
})
230+
231+
FormSchema.setComponent('checkboxgroup', 'el-checkbox-group')
232+
233+
export default { ... }
234+
</script>
235+
```
236+
237+
## Grouped Radio elements
238+
To group radio elements, use the [JSON Schema keyword `enum`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.23) and `attrs.type === 'radio'`:
239+
240+
**schema.json**
241+
```json
242+
{
243+
"$schema": "http://json-schema.org/draft-04/schema#",
244+
"type": "object",
245+
"properties": {
246+
"groupedRadio": {
247+
"type": "string",
248+
"enum": [
249+
{ "value": "daily", "label": "Daily New" },
250+
{ "value": "promotion", "label": "Promotion" }
251+
],
252+
"attrs": {
253+
"type": "radio"
254+
}
255+
}
256+
}
257+
}
258+
259+
```
260+
261+
**component.vue**
262+
```html
263+
<script>
264+
import FormSchema from 'vue-json-schema'
265+
266+
FormSchema.setComponent('select', 'el-radio', ({ item }) => {
267+
return { label: item.value }
268+
})
269+
270+
FormSchema.setComponent('radiogroup', 'el-radio-group')
271+
272+
export default { ... }
273+
</script>
274+
```
275+
184276
## License
185277
Under the MIT license. See [LICENSE](https://github.com/demsking/vue-json-schema/blob/master/LICENSE) file for more details.

0 commit comments

Comments
 (0)