Commit d24f50824831f4b6541693716f0aa09942abcbf7
1 parent
a81253ec
Support trueValue and falseValue for radio
Showing
2 changed files
with
25 additions
and
9 deletions
Show diff stats
examples/routers/radio.vue
| 1 | 1 | <template> |
| 2 | 2 | <div> |
| 3 | + <Radio true-value="true" false-value="false" v-model="testValue">test</Radio> {{ testValue }} | |
| 3 | 4 | <Radio-group v-model="date.sex"> |
| 4 | 5 | <div v-if="show"> |
| 5 | - <Radio label="male"></Radio> | |
| 6 | - <Radio label="female"></Radio> | |
| 6 | + <Radio label="male" true-value="true" false-value="false"></Radio> | |
| 7 | + <Radio label="female" true-value="true" false-value="false"></Radio> | |
| 7 | 8 | </div> |
| 8 | 9 | </Radio-group> |
| 10 | + {{ date }} | |
| 9 | 11 | <Button @click="handleChange">change</Button> |
| 10 | 12 | </div> |
| 11 | 13 | </template> |
| ... | ... | @@ -16,7 +18,8 @@ |
| 16 | 18 | date: { |
| 17 | 19 | sex: 'male' |
| 18 | 20 | }, |
| 19 | - show: false | |
| 21 | + show: false, | |
| 22 | + testValue: null | |
| 20 | 23 | } |
| 21 | 24 | }, |
| 22 | 25 | methods: { | ... | ... |
src/components/radio/radio.vue
| ... | ... | @@ -22,7 +22,15 @@ |
| 22 | 22 | mixins: [ Emitter ], |
| 23 | 23 | props: { |
| 24 | 24 | value: { |
| 25 | - type: Boolean, | |
| 25 | + type: [String, Number, Boolean], | |
| 26 | + default: false | |
| 27 | + }, | |
| 28 | + trueValue: { | |
| 29 | + type: [String, Number, Boolean], | |
| 30 | + default: true | |
| 31 | + }, | |
| 32 | + falseValue: { | |
| 33 | + type: [String, Number, Boolean], | |
| 26 | 34 | default: false |
| 27 | 35 | }, |
| 28 | 36 | label: { |
| ... | ... | @@ -83,7 +91,9 @@ |
| 83 | 91 | |
| 84 | 92 | const checked = event.target.checked; |
| 85 | 93 | this.currentValue = checked; |
| 86 | - this.$emit('input', checked); | |
| 94 | + | |
| 95 | + let value = checked ? this.trueValue : this.falseValue; | |
| 96 | + this.$emit('input', value); | |
| 87 | 97 | |
| 88 | 98 | if (this.group && this.label !== undefined) { |
| 89 | 99 | this.parent.change({ |
| ... | ... | @@ -92,16 +102,19 @@ |
| 92 | 102 | }); |
| 93 | 103 | } |
| 94 | 104 | if (!this.group) { |
| 95 | - this.$emit('on-change', checked); | |
| 96 | - this.dispatch('FormItem', 'on-form-change', checked); | |
| 105 | + this.$emit('on-change', value); | |
| 106 | + this.dispatch('FormItem', 'on-form-change', value); | |
| 97 | 107 | } |
| 98 | 108 | }, |
| 99 | 109 | updateValue () { |
| 100 | - this.currentValue = this.value; | |
| 110 | + this.currentValue = this.value === this.trueValue; | |
| 101 | 111 | } |
| 102 | 112 | }, |
| 103 | 113 | watch: { |
| 104 | - value () { | |
| 114 | + value (val) { | |
| 115 | + if (val !== this.trueValue && val !== this.falseValue) { | |
| 116 | + throw 'Value should be trueValue or falseValue.'; | |
| 117 | + } | |
| 105 | 118 | this.updateValue(); |
| 106 | 119 | } |
| 107 | 120 | } | ... | ... |