Skip to content

Commit 5189b99

Browse files
author
Lionel Bijaoui
committed
new: new field html5 input
1 parent 67ed6f3 commit 5189b99

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

src/fields/fieldHtml5.vue

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<template lang="jade">
2+
.wrapper
3+
input.form-control(
4+
:type="schema.inputType",
5+
v-model="value",
6+
:disabled="disabled",
7+
8+
:accept="schema.accept",
9+
:alt="schema.alt",
10+
:autocomplete="schema.autocomplete",
11+
:checked="schema.checked",
12+
:dirname="schema.dirname",
13+
:formaction="schema.formaction",
14+
:formenctype="schema.formenctype",
15+
:formmethod="schema.formmethod",
16+
:formnovalidate="schema.formnovalidate",
17+
:formtarget="schema.formtarget",
18+
:height="schema.height",
19+
:list="schema.list",
20+
:max="schema.max",
21+
:maxlength="schema.maxlength",
22+
:min="schema.min",
23+
:multiple="schema.multiple",
24+
:pattern="schema.pattern",
25+
:placeholder="schema.placeholder",
26+
:readonly="schema.readonly",
27+
:required="schema.required",
28+
:size="schema.size",
29+
:src="schema.src",
30+
:step="schema.step",
31+
:width="schema.width",
32+
:files="schema.files")
33+
span.helper(v-if="schema.inputType === 'color' || schema.inputType === 'range'") {{ value }}
34+
</template>
35+
36+
<script>
37+
import abstractField from "./abstractField";
38+
39+
export default {
40+
mixins: [ abstractField ]
41+
};
42+
43+
</script>
44+
45+
<style lang="sass">
46+
.vue-form-generator .field-html5 {
47+
.wrapper {
48+
width: 100%;
49+
}
50+
input[type="radio"] {
51+
width: 100%;
52+
}
53+
input[type="color"] {
54+
width: 60px;
55+
}
56+
input[type="range"] {
57+
padding: 0;
58+
}
59+
60+
.helper {
61+
margin: auto 0.5em;
62+
}
63+
}
64+
</style>
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { expect } from "chai";
2+
import { createVueField, trigger, checkAttribute } from "../util";
3+
4+
import Vue from "vue";
5+
import fieldHtml5 from "src/fields/fieldHtml5.vue";
6+
7+
Vue.component("fieldHtml5", fieldHtml5);
8+
9+
let el, vm, field;
10+
11+
function createField(test, schema = {}, model = null, disabled = false, options) {
12+
[el, vm, field] = createVueField(test, "fieldHtml5", schema, model, disabled, options);
13+
}
14+
15+
describe("fieldHtml5.vue", function() {
16+
17+
describe("check template", () => {
18+
let schema = {
19+
type: "html5",
20+
inputType: "text",
21+
label: "Name",
22+
model: "name",
23+
autocomplete: "off",
24+
placeholder: "Field placeholder",
25+
readonly: false
26+
};
27+
let model = { name: "John Doe" };
28+
let input;
29+
30+
before(() => {
31+
createField(this, schema, model, false);
32+
input = el.getElementsByTagName("input")[0];
33+
});
34+
35+
it("should contain an input text element", () => {
36+
expect(field).to.be.exist;
37+
expect(field.$el).to.be.exist;
38+
39+
expect(input).to.be.defined;
40+
expect(input.type).to.be.equal("text");
41+
expect(input.classList.contains("form-control")).to.be.true;
42+
});
43+
44+
it("should contain the value", (done) => {
45+
vm.$nextTick(() => {
46+
expect(input.value).to.be.equal("John Doe");
47+
done();
48+
});
49+
});
50+
51+
let inputTypes = new Map([
52+
["text", ["autocomplete", "disabled", "placeholder", "readonly"]],
53+
["password", ["autocomplete", "disabled", "placeholder", "readonly"]],
54+
["checkbox", ["autocomplete", "disabled"]],
55+
// ["radio", [] ],
56+
// ["button", [] ],
57+
// ["submit", [] ],
58+
// ["reset", [] ],
59+
// ["file", [] ],
60+
// ["hidden", [] ],
61+
// ["image", [] ],
62+
// ["datetime", ],
63+
// ["datetime", ],
64+
// ["date", ],
65+
// ["month", ],
66+
// ["time", ],
67+
// ["week", ],
68+
["number", ["autocomplete", "disabled", "placeholder", "readonly"]],
69+
// ["range", ["autocomplete"]],
70+
["email", ["autocomplete", "disabled", "placeholder", "readonly"]],
71+
["url", ["autocomplete", "disabled", "placeholder", "readonly"]],
72+
// ["search", ],
73+
["tel", ["autocomplete", "disabled", "placeholder", "readonly"]],
74+
["color", ["autocomplete"]]
75+
]);
76+
for (let [inputType, attributes] of inputTypes) {
77+
78+
describe("change type of input", () => {
79+
80+
it("should become a " + inputType, function(done) {
81+
field.schema.inputType = inputType;
82+
vm.$nextTick(() => {
83+
expect(input.type).to.be.equal(inputType);
84+
done();
85+
});
86+
87+
});
88+
89+
describe("check optional attribute", () => {
90+
91+
attributes.forEach(function(name) {
92+
it("should set " + name, function(done) {
93+
checkAttribute(name, vm, input, field, schema, done);
94+
});
95+
96+
});
97+
98+
});
99+
100+
});
101+
}
102+
103+
it("input value should be the model value after changed", (done) => {
104+
model.name = "Jane Doe";
105+
vm.$nextTick(() => {
106+
expect(input.value).to.be.equal("Jane Doe");
107+
done();
108+
});
109+
110+
});
111+
112+
it("model value should be the input value if changed", (done) => {
113+
input.value = "John Smith";
114+
trigger(input, "input");
115+
116+
vm.$nextTick(() => {
117+
expect(model.name).to.be.equal("John Smith");
118+
done();
119+
});
120+
121+
});
122+
123+
});
124+
125+
});

0 commit comments

Comments
 (0)