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 | <template> | 1 | <template> |
| 2 | <div> | 2 | <div> |
| 3 | + <Radio true-value="true" false-value="false" v-model="testValue">test</Radio> {{ testValue }} | ||
| 3 | <Radio-group v-model="date.sex"> | 4 | <Radio-group v-model="date.sex"> |
| 4 | <div v-if="show"> | 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 | </div> | 8 | </div> |
| 8 | </Radio-group> | 9 | </Radio-group> |
| 10 | + {{ date }} | ||
| 9 | <Button @click="handleChange">change</Button> | 11 | <Button @click="handleChange">change</Button> |
| 10 | </div> | 12 | </div> |
| 11 | </template> | 13 | </template> |
| @@ -16,7 +18,8 @@ | @@ -16,7 +18,8 @@ | ||
| 16 | date: { | 18 | date: { |
| 17 | sex: 'male' | 19 | sex: 'male' |
| 18 | }, | 20 | }, |
| 19 | - show: false | 21 | + show: false, |
| 22 | + testValue: null | ||
| 20 | } | 23 | } |
| 21 | }, | 24 | }, |
| 22 | methods: { | 25 | methods: { |
src/components/radio/radio.vue
| @@ -22,7 +22,15 @@ | @@ -22,7 +22,15 @@ | ||
| 22 | mixins: [ Emitter ], | 22 | mixins: [ Emitter ], |
| 23 | props: { | 23 | props: { |
| 24 | value: { | 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 | default: false | 34 | default: false |
| 27 | }, | 35 | }, |
| 28 | label: { | 36 | label: { |
| @@ -83,7 +91,9 @@ | @@ -83,7 +91,9 @@ | ||
| 83 | 91 | ||
| 84 | const checked = event.target.checked; | 92 | const checked = event.target.checked; |
| 85 | this.currentValue = checked; | 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 | if (this.group && this.label !== undefined) { | 98 | if (this.group && this.label !== undefined) { |
| 89 | this.parent.change({ | 99 | this.parent.change({ |
| @@ -92,16 +102,19 @@ | @@ -92,16 +102,19 @@ | ||
| 92 | }); | 102 | }); |
| 93 | } | 103 | } |
| 94 | if (!this.group) { | 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 | updateValue () { | 109 | updateValue () { |
| 100 | - this.currentValue = this.value; | 110 | + this.currentValue = this.value === this.trueValue; |
| 101 | } | 111 | } |
| 102 | }, | 112 | }, |
| 103 | watch: { | 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 | this.updateValue(); | 118 | this.updateValue(); |
| 106 | } | 119 | } |
| 107 | } | 120 | } |