Skip to content

new: new field 'input' #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/fields/fieldInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<template lang="jade">
.wrapper
input.form-control(
:type="schema.inputType",
v-model="value",
:disabled="disabled",

:accept="schema.accept",
:alt="schema.alt",
:autocomplete="schema.autocomplete",
:checked="schema.checked",
:dirname="schema.dirname",
:formaction="schema.formaction",
:formenctype="schema.formenctype",
:formmethod="schema.formmethod",
:formnovalidate="schema.formnovalidate",
:formtarget="schema.formtarget",
:height="schema.height",
:list="schema.list",
:max="schema.max",
:maxlength="schema.maxlength",
:min="schema.min",
:multiple="schema.multiple",
:pattern="schema.pattern",
:placeholder="schema.placeholder",
:readonly="schema.readonly",
:required="schema.required",
:size="schema.size",
:src="schema.src",
:step="schema.step",
:width="schema.width",
:files="schema.files")
span.helper(v-if="schema.inputType === 'color' || schema.inputType === 'range'") {{ value }}
</template>

<script>
import abstractField from "./abstractField";

export default {
mixins: [ abstractField ],
methods: {
formatValueToField(value) {
switch(this.schema.inputType){
case "date":
return moment(value).format("YYYY-MM-DD");
case "datetime":
return moment(value).format();
case "datetime-local":
return moment(value).format("YYYY-MM-DDTHH:mm:ss");
default:
return value;
}
},
formatValueToModel(value) {
console.log(this.schema.inputType, typeof value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this log message and after it I will merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Member Author

@lionel-bijaoui lionel-bijaoui Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to merge, no ? I think you just approved the changes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just waited the green lights :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I did not get it 👍

if (value != null) {
if (this.schema.inputType === "date" ||
this.schema.inputType === "datetime" ||
this.schema.inputType === "datetimelocal") {
return new Date(value).getTime();
}else{
return value;
}
} else {
return value;
}
}
}
};

</script>

<style lang="sass">
.vue-form-generator .field-input {
.wrapper {
width: 100%;
}
input[type="radio"] {
width: 100%;
}
input[type="color"] {
width: 60px;
}
input[type="range"] {
padding: 0;
}

.helper {
margin: auto 0.5em;
}
}
</style>
125 changes: 125 additions & 0 deletions test/unit/specs/fields/fieldInput.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { expect } from "chai";
import { createVueField, trigger, checkAttribute } from "../util";

import Vue from "vue";
import fieldInput from "src/fields/fieldInput.vue";

Vue.component("fieldInput", fieldInput);

let el, vm, field;

function createField(test, schema = {}, model = null, disabled = false, options) {
[el, vm, field] = createVueField(test, "fieldInput", schema, model, disabled, options);
}

describe("fieldInput.vue", function() {

describe("check template", () => {
let schema = {
type: "input",
inputType: "text",
label: "Name",
model: "name",
autocomplete: "off",
placeholder: "Field placeholder",
readonly: false
};
let model = { name: "John Doe" };
let input;

before(() => {
createField(this, schema, model, false);
input = el.getElementsByTagName("input")[0];
});

it("should contain an input text element", () => {
expect(field).to.be.exist;
expect(field.$el).to.be.exist;

expect(input).to.be.defined;
expect(input.type).to.be.equal("text");
expect(input.classList.contains("form-control")).to.be.true;
});

it("should contain the value", (done) => {
vm.$nextTick(() => {
expect(input.value).to.be.equal("John Doe");
done();
});
});

let inputTypes = new Map([
["text", ["autocomplete", "disabled", "placeholder", "readonly"]],
["password", ["autocomplete", "disabled", "placeholder", "readonly"]],
["checkbox", ["autocomplete", "disabled"]],
// ["radio", [] ],
// ["button", [] ],
// ["submit", [] ],
// ["reset", [] ],
// ["file", [] ],
// ["hidden", [] ],
// ["image", [] ],
// ["datetime", ],
// ["datetime", ],
// ["date", ],
// ["month", ],
// ["time", ],
// ["week", ],
["number", ["autocomplete", "disabled", "placeholder", "readonly"]],
// ["range", ["autocomplete"]],
["email", ["autocomplete", "disabled", "placeholder", "readonly"]],
["url", ["autocomplete", "disabled", "placeholder", "readonly"]],
// ["search", ],
["tel", ["autocomplete", "disabled", "placeholder", "readonly"]],
["color", ["autocomplete"]]
]);
for (let [inputType, attributes] of inputTypes) {

describe("change type of input", () => {

it("should become a " + inputType, function(done) {
field.schema.inputType = inputType;
vm.$nextTick(() => {
expect(input.type).to.be.equal(inputType);
done();
});

});

describe("check optional attribute", () => {

attributes.forEach(function(name) {
it("should set " + name, function(done) {
checkAttribute(name, vm, input, field, schema, done);
});

});

});

});
}

it("input value should be the model value after changed", (done) => {
model.name = "Jane Doe";
vm.$nextTick(() => {
expect(input.value).to.be.equal("Jane Doe");
done();
});

});

it("model value should be the input value if changed", (done) => {
input.value = "John Smith";
trigger(input, "input");

vm.$nextTick(() => {
expect(model.name).to.be.equal("John Smith");
done();
});

});

});

});