Skip to content

Commit 6790935

Browse files
authored
Merge pull request #40 from 14nrv/dev
Merge Dev
2 parents 9d2fd70 + 0221d3b commit 6790935

File tree

8 files changed

+109
-19
lines changed

8 files changed

+109
-19
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
[![Edit vue-form-json-demo](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/mxow346yj?autoresize=1&hidenavigation=1&module=%2Fsrc%2FApp.vue&view=preview)
1010

11-
## Generate a responsive vue form with validation and bulma style, from [json](https://github.com/14nrv/vue-form-json/blob/master/src/components/Form/fields.json)
11+
## Generate a responsive vue form with validation, from [an array](https://github.com/14nrv/vue-form-json/blob/master/src/components/Form/fields.json)
1212
All fields are required and input text by default.
1313
Once submitted, an event 'formSubmitted' is emitted on $root with the formName and all values.
1414

1515
## Key Features
1616
- [x] Generate a form from json / array (formFields props)
17-
- [x] Bulma style
17+
- [x] Bulma classes by default (that can be overwritten)
1818
- [x] Responsive
1919
- [x] Fields on multiples columns
2020
```js
@@ -24,16 +24,18 @@ Once submitted, an event 'formSubmitted' is emitted on $root with the formName a
2424
```js
2525
const formFields = [{ label: 'the label', value: 'the value' }]
2626
```
27-
- [x] Validation & VeeValidate [simple rules validation](https://baianat.github.io/vee-validate/guide/rules.html)
27+
- [x] [Simple rules validation](https://logaretm.github.io/vee-validate/guide/rules.html#rules)
2828
```js
2929
const formFields = [{ label: 'the label', rules: { is_not: 'label' } }]
3030
```
31+
- [x] Cross field validation
3132
- [x] Custom attr (class, data-*, ...) on .field & real fields (input, textarea...)
3233
```js
3334
const formFields = [{
34-
label: 'the label',
3535
attr: { class: 'classOnInput' },
36-
field: { attr: { class: 'classOnFieldClassName' } }
36+
alternativeLabel: 'an alternative label text that will be displayed',
37+
field: { attr: { class: 'classOnFieldClassName' } },
38+
label: 'the label'
3739
}]
3840
```
3941
- [x] Named slot everywhere inside form

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-form-json",
33
"version": "3.0.1",
4-
"description": "Generate a vue form with validation and bulma style, from json",
4+
"description": "Generate a vue form with validation from an array",
55
"author": "14nrv",
66
"license": "MIT",
77
"scripts": {
@@ -10,7 +10,7 @@
1010
"build": "vue-cli-service build --formats commonjs,umd-min --target lib --name vue-form-json ./src/components/Form/Form.vue",
1111
"lint": "vue-cli-service lint",
1212
"test": "NODE_ENV=test vue-cli-service test:unit --coverage",
13-
"test:tdd": "NODE_ENV=test vue-cli-service test:unit --coverage --watchAll",
13+
"test:tdd": "yarn test --watchAll",
1414
"semantic-release": "semantic-release"
1515
},
1616
"husky": {
@@ -64,7 +64,7 @@
6464
"dependencies": {
6565
"ramda": "^0.27.0",
6666
"slugify": "^1.4.4",
67-
"vee-validate": "^3.4.0",
67+
"vee-validate": "^3.4.2",
6868
"vue": "^2.6.12"
6969
},
7070
"devDependencies": {

src/components/Fields/Control.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
tag="div"
44
:vid="item.vid"
55
:rules="getRules"
6-
:name="item.name || item.label"
6+
:name="item.name || item.label | slugify"
77
:immediate="!!item.value"
88
v-slot="{ errors, required, ariaInput }")
99

@@ -22,6 +22,7 @@
2222
</template>
2323

2424
<script>
25+
import { slug } from '@/helpers'
2526
import Input from '@/components/Fields/Input'
2627
import Select from '@/components/Fields/Select'
2728
import Textarea from '@/components/Fields/Textarea'
@@ -32,6 +33,9 @@ const NOT_NORMAL_INPUT = ['textarea', 'select', 'checkbox', 'radio']
3233
3334
export default {
3435
name: 'Control',
36+
filters: {
37+
slugify: value => slug(value)
38+
},
3539
components: {
3640
appInput: Input,
3741
appSelect: Select,

src/components/Fields/Label.spec.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import matchers from 'jest-vue-matcher'
2+
import { mount } from '@vue/test-utils'
3+
import Label from '@/components/Fields/Label'
4+
5+
const LABEL = 'the label'
6+
let wrapper
7+
8+
describe('Label', () => {
9+
beforeEach(() => {
10+
wrapper = mount(Label, {
11+
propsData: {
12+
item: {
13+
label: LABEL
14+
}
15+
}
16+
})
17+
expect.extend(matchers(wrapper))
18+
})
19+
20+
afterEach(() => {
21+
wrapper.destroy()
22+
})
23+
24+
it('shows a label', () => {
25+
expect('label').toHaveText(LABEL)
26+
})
27+
28+
it('shows an alternative label', async () => {
29+
const ALTERNATIVE_LABEL = 'an alternative label'
30+
31+
await wrapper.setProps({
32+
item: {
33+
label: LABEL,
34+
alternativeLabel: ALTERNATIVE_LABEL
35+
}
36+
})
37+
38+
expect('label').toHaveText(ALTERNATIVE_LABEL)
39+
expect('label').not.toHaveText(LABEL)
40+
})
41+
42+
it('hides the label', async () => {
43+
await wrapper.setProps({
44+
item: {
45+
label: LABEL,
46+
showLabel: false
47+
}
48+
})
49+
50+
expect('label').not.toBeADomElement()
51+
})
52+
53+
it('has a for attribute by default', () => {
54+
expect('label').toHaveAttribute('for', 'the-label')
55+
})
56+
57+
it('has a custom for attribute', async () => {
58+
const ATTR_FOR = 'a-custom-for-attribute'
59+
60+
await wrapper.setProps({
61+
item: {
62+
label: LABEL,
63+
for: ATTR_FOR
64+
}
65+
})
66+
67+
expect('label').toHaveAttribute('for', ATTR_FOR)
68+
})
69+
70+
it('is mandatory by default', () => {
71+
expect('label').toHaveText('*')
72+
})
73+
74+
it('is not mandatory', async () => {
75+
await wrapper.setProps({
76+
item: {
77+
label: LABEL,
78+
isRequired: false
79+
}
80+
})
81+
82+
expect('label').not.toHaveText('*')
83+
})
84+
})

src/components/Fields/Label.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template lang="pug">
2-
label.label(:for="item.label | slugify", v-if="item.showLabel !== false")
3-
p {{ item.label }}
2+
label.label(:for="item.for || item.label | slugify", v-if="item.showLabel !== false")
3+
p {{ item.alternativeLabel || item.label }}
44
span.helpLabel.has-text-grey-light.is-size-7.is-italic(v-if="item.help") {{ item.help }}
55
span(v-if="item.isRequired !== false")
66
sup.has-text-grey-light.is-size-7 *

src/components/Form/Form.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe('Form', () => {
208208
expect($errorMessage).toBeADomElement()
209209

210210
const errorMessage = wrapper.find($errorMessage).text()
211-
expect(errorMessage).toBe('The Password field format is invalid')
211+
expect(errorMessage).toBe('The password field format is invalid')
212212

213213
type(PASSWORD_VALUE, $inputPassword)
214214
await flush()
@@ -261,7 +261,7 @@ describe('Form', () => {
261261
expect(`${$inputFirstName}.is-danger`).toBeADomElement()
262262

263263
const errorMessage = wrapper.find($errorMessage).text()
264-
expect(errorMessage).toBe('The First Name field must be at least 4 characters')
264+
expect(errorMessage).toBe('The first-name field must be at least 4 characters')
265265
})
266266
})
267267

src/components/Form/FormError.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Form Error', () => {
6969
expect('.is-danger').toBeADomElement()
7070
expect(`${$inputTest} ~ .icon.is-right`).toBeADomElement()
7171

72-
expect($error).toHaveText(`The ${LABEL_INPUT} field must be at least ${MIN_LENGTH} characters`, $error)
72+
expect($error).toHaveText(`The ${LABEL_INPUT_SLUGIFY} field must be at least ${MIN_LENGTH} characters`, $error)
7373

7474
expect('input[type=submit]').toHaveAttribute('disabled', 'disabled')
7575
})
@@ -92,7 +92,7 @@ describe('Form Error', () => {
9292
await flush()
9393

9494
expect(`${INPUT_NUMBER}.is-danger`).toBeADomElement()
95-
expect($error).toHaveText(`The Number field must be ${val} or ${isMinValue ? 'more' : 'less'}`)
95+
expect($error).toHaveText(`The number field must be ${val} or ${isMinValue ? 'more' : 'less'}`)
9696
})
9797
}
9898

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13251,10 +13251,10 @@ vary@~1.1.2:
1325113251
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
1325213252
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
1325313253

13254-
vee-validate@^3.4.0:
13255-
version "3.4.0"
13256-
resolved "https://registry.yarnpkg.com/vee-validate/-/vee-validate-3.4.0.tgz#dcdefcd45d43823c8b26aceee55f067ef56ca800"
13257-
integrity sha512-qxcqrGYr2gchZOjyhBKu7G4NOyOTmH6vk8APGdxAiL/0Iy2QZP/vcOYNbx7+zMhg7P3q418AseHR1sbckB5I+A==
13254+
vee-validate@^3.4.2:
13255+
version "3.4.2"
13256+
resolved "https://registry.yarnpkg.com/vee-validate/-/vee-validate-3.4.2.tgz#888a900ae845e1e02ad5f8ee688b41408e3892db"
13257+
integrity sha512-ORDhNuFsMZaRatPBsuLX98nPG+xtDAkJ/JBnRNTxgqWl7y6qCT6VTLKAYhTvvBOVu+KnINUvg4+x/54rjZZ9sA==
1325813258

1325913259
vendors@^1.0.0:
1326013260
version "1.0.4"

0 commit comments

Comments
 (0)