Skip to content

Commit 7cdcb66

Browse files
VdustRmarcosmoura
authored andcommitted
feat(MdCheckBox): True / false value supporting (#1703)
* feat(MdCheckBox): True / false value supporting support true-value and false-value for checkboxes BREAKING CHANGE: checkbox without setting value is true / false as default fix #1701 * fix(MdCheckbox): better native value binding
1 parent a4e3619 commit 7cdcb66

File tree

4 files changed

+98
-20
lines changed

4 files changed

+98
-20
lines changed

docs/app/pages/Components/Checkbox/Checkbox.vue

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<example src="./examples/RegularCheckboxes.vue" />
22
<example src="./examples/CheckboxHueColors.vue" />
3+
<example src="./examples/TrueFalseValue.vue" />
34

45
<template>
56
<page-container centered :title="$t('pages.checkbox.title')">
@@ -12,6 +13,7 @@
1213

1314
<code-example title="Checkbox" :component="examples['regular-checkboxes']" />
1415
<code-example title="Hue Colors" :component="examples['checkbox-hue-colors']" />
16+
<code-example title="True / False Value" :component="examples['true-false-value']" />
1517

1618
<api-item title="API - md-checkbox">
1719
<p>The following options can be applied to all checkboxes:</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<template>
2+
<div>
3+
<div class="block">
4+
<div class="title">Without <code>:true-value</code> / <code>:false-value</code></div>
5+
<div class="input">
6+
<md-checkbox v-model="withoutSetValue">{{withoutSetValue|jsonStringify}}</md-checkbox>
7+
</div>
8+
</div>
9+
10+
<md-divider />
11+
12+
<div class="block">
13+
<div class="title">With <code>:true-value</code> / <code>:false-value</code></div>
14+
<div class="input">
15+
<md-checkbox v-model="withSetValue" true-value="true" false-value="false">{{withSetValue|jsonStringify}}</md-checkbox>
16+
</div>
17+
</div>
18+
19+
<md-divider />
20+
21+
<div class="block">
22+
<div class="title">Native checkbox with <code>:true-value</code> / <code>:false-value</code></div>
23+
<div class="input">
24+
<label><input type="checkbox" v-model="native" true-value="true" false-value="false" value="test" />{{native|jsonStringify}}</label>
25+
</div>
26+
</div>
27+
</div>
28+
</template>
29+
30+
<script>
31+
export default {
32+
name: 'TrueFalseValue',
33+
data () {
34+
return {
35+
withoutSetValue: null,
36+
withSetValue: null,
37+
native: null
38+
}
39+
},
40+
41+
filters: {
42+
jsonStringify (val) {
43+
return JSON.stringify(val)
44+
}
45+
}
46+
}
47+
</script>
48+
49+
<style lang="scss">
50+
.block:not(:first-child) {
51+
margin-top: 32px;
52+
}
53+
54+
.title {
55+
font: 1.2em;
56+
}
57+
</style>

src/components/MdCheckbox/MdCheckbox.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="md-checkbox" :class="[$mdActiveTheme, checkClasses]">
33
<div class="md-checkbox-container" @click.stop="toggleCheck">
44
<md-ripple md-centered :md-active.sync="rippleActive" :md-disabled="disabled">
5-
<input type="checkbox" v-bind="{ id, name, disabled, required, value }" :indeterminate.prop="indeterminate">
5+
<input type="checkbox" v-bind="attrs" :indeterminate.prop="indeterminate">
66
</md-ripple>
77
</div>
88

src/components/MdCheckbox/MdCheckboxMixin.js

+38-19
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ export default {
88
model: [String, Boolean, Object, Number, Array],
99
value: {
1010
type: [String, Boolean, Object, Number],
11-
default: 'on'
1211
},
1312
name: [String, Number],
1413
required: Boolean,
1514
disabled: Boolean,
16-
indeterminate: Boolean
15+
indeterminate: Boolean,
16+
trueValue: {
17+
default: true
18+
},
19+
falseValue: {
20+
default: false
21+
}
1722
},
1823
model: {
1924
prop: 'model',
@@ -23,30 +28,48 @@ export default {
2328
rippleActive: false
2429
}),
2530
computed: {
31+
attrs () {
32+
const attrs = {
33+
id: this.id,
34+
name: this.name,
35+
disabled: this.disabled,
36+
required: this.required,
37+
'true-value': this.trueValue,
38+
'false-value': this.falseValue
39+
}
40+
41+
if (this.$options.propsData.hasOwnProperty('value')) {
42+
if (this.value === null || typeof this.value !== 'object') {
43+
attrs.value = (this.value === null || this.value === undefined) ? '' : String(this.value)
44+
}
45+
}
46+
47+
return attrs
48+
},
2649
isSelected () {
2750
if (this.isModelArray) {
2851
return this.model.includes(this.value)
2952
}
3053

31-
if (this.isModelBoolean && this.value === 'on') {
32-
return this.model
54+
if (this.hasValue) {
55+
return this.model === this.value
3356
}
3457

35-
return this.model === this.value
58+
return this.model === this.trueValue
3659
},
3760
isModelArray () {
3861
return Array.isArray(this.model)
3962
},
40-
isModelBoolean () {
41-
return typeof this.model === 'boolean'
42-
},
4363
checkClasses () {
4464
return {
4565
'md-checked': this.isSelected,
4666
'md-disabled': this.disabled,
4767
'md-required': this.required,
4868
'md-indeterminate': this.indeterminate
4969
}
70+
},
71+
hasValue () {
72+
return this.$options.propsData.hasOwnProperty('value')
5073
}
5174
},
5275
methods: {
@@ -68,26 +91,22 @@ export default {
6891

6992
this.$emit('change', newModel)
7093
},
71-
handleStringCheckbox () {
72-
if (!this.isSelected) {
73-
this.$emit('change', this.value)
74-
} else {
75-
this.$emit('change', null)
76-
}
94+
handleSingleSelectCheckbox () {
95+
this.$emit('change', this.isSelected ? null : this.value)
7796
},
78-
handleBooleanCheckbox () {
79-
this.$emit('change', !this.model)
97+
handleSimpleCheckbox () {
98+
this.$emit('change', this.isSelected ? this.falseValue : this.trueValue)
8099
},
81100
toggleCheck () {
82101
if (!this.disabled) {
83102
this.rippleActive = true
84103

85104
if (this.isModelArray) {
86105
this.handleArrayCheckbox()
87-
} else if (this.isModelBoolean) {
88-
this.handleBooleanCheckbox()
106+
} else if (this.hasValue) {
107+
this.handleSingleSelectCheckbox()
89108
} else {
90-
this.handleStringCheckbox()
109+
this.handleSimpleCheckbox()
91110
}
92111
}
93112
}

0 commit comments

Comments
 (0)