Skip to content

Commit

Permalink
🆕 implement #14
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Jul 1, 2016
1 parent 84b0a2c commit c6d0a76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions dev/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
user.role = fakerator.random.arrayElement(roles).id;
//user.mobile = fakerator.phone.phoneNumber();
user.avatar = fakerator.internet.avatar();
user.sex = fakerator.random.arrayElement(["male", "female"]);

user.skills = fakerator.utimes(fakerator.random.arrayElement, 2, skills);

Expand Down
12 changes: 12 additions & 0 deletions dev/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ module.exports = {

},

{
type: "switch",
label: "Sex (switch field)",
model: "sex",
multi: true,
default: "male",
textOn: "Female",
textOff: "Male",
valueOn: "female",
valueOff: "male"
},

{
type: "slider",
label: "Rank (slider field)",
Expand Down
23 changes: 22 additions & 1 deletion src/fields/fieldSwitch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,28 @@
import abstractField from "./abstractField";
export default {
mixins: [ abstractField ]
mixins: [ abstractField ],
methods: {
formatValueToField(value) {
if (value != null && this.schema.valueOn)
return value == this.schema.valueOn;
return value;
},
formatValueToModel(value) {
if (value != null && this.schema.valueOn) {
if (value)
return this.schema.valueOn;
else
return this.schema.valueOff;
}
return value;
}
}
};
</script>

Expand Down
46 changes: 46 additions & 0 deletions test/unit/specs/fields/fieldSwitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,50 @@ describe("FieldSwitch.vue", () => {

});

describe("check template with custom On/Off values", () => {
let schema = {
type: "switch",
model: "sex",
textOn: "Female",
textOff: "Male",
valueOn: "female",
valueOff: "male"
};
let model = { sex: "female" };
let input;

before( () => {
createField(schema, model, false);
input = el.querySelector("input");
});

it("check input value", (done) => {
vm.$nextTick( () => {
expect(input.checked).to.be.true;
done();
});
});

it("input value should be the model value after changed", (done) => {
model.sex = "male";
vm.$nextTick( () => {
expect(input.checked).to.be.false;
done();
});

});

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

vm.$nextTick( () => {
expect(model.sex).to.be.equal("female");
done();
});

});

});

});

0 comments on commit c6d0a76

Please sign in to comment.