Commit 3f281d6ce03a886cbab6d77b217f84c809a2a150
1 parent
4f5d3937
update RadioGroup
update RadioGroup
Showing
4 changed files
with
62 additions
and
42 deletions
Show diff stats
examples/routers/radio.vue
| 1 | <template> | 1 | <template> |
| 2 | <div> | 2 | <div> |
| 3 | - <Radio v-model="single" @on-change="c">Radio</Radio> | ||
| 4 | - <Radio-group v-model="phone" type="button" @on-change="c"> | ||
| 5 | - <Radio label="apple"> | ||
| 6 | - <Icon type="social-apple"></Icon> | ||
| 7 | - <span>Apple</span> | ||
| 8 | - </Radio> | ||
| 9 | - <Radio label="android"> | ||
| 10 | - <Icon type="social-android"></Icon> | ||
| 11 | - <span>Android</span> | ||
| 12 | - </Radio> | ||
| 13 | - <Radio label="windows"> | ||
| 14 | - <Icon type="social-windows"></Icon> | ||
| 15 | - <span>Windows</span> | ||
| 16 | - </Radio> | 3 | + <Radio-group v-model="phone"> |
| 4 | + <Row> | ||
| 5 | + <i-col span="8"> | ||
| 6 | + <Radio label="apple"> | ||
| 7 | + <Icon type="social-apple"></Icon> | ||
| 8 | + <span>Apple</span> | ||
| 9 | + </Radio> | ||
| 10 | + </i-col> | ||
| 11 | + <i-col span="8"> | ||
| 12 | + <Radio label="android"> | ||
| 13 | + <Icon type="social-android"></Icon> | ||
| 14 | + <span>Android</span> | ||
| 15 | + </Radio> | ||
| 16 | + </i-col> | ||
| 17 | + <i-col span="8"> | ||
| 18 | + <Radio label="windows"> | ||
| 19 | + <Icon type="social-windows"></Icon> | ||
| 20 | + <span>Windows</span> | ||
| 21 | + </Radio> | ||
| 22 | + </i-col> | ||
| 23 | + </Row> | ||
| 17 | </Radio-group> | 24 | </Radio-group> |
| 18 | - <Radio-group v-model="animal"> | ||
| 19 | - <Radio label="金斑蝶"></Radio> | ||
| 20 | - <Radio label="爪哇犀牛"></Radio> | ||
| 21 | - <Radio label="印度黑羚"></Radio> | ||
| 22 | - </Radio-group> | ||
| 23 | - {{ phone }} | ||
| 24 | - <div @click="phone = 'apple'">apple</div> | ||
| 25 | - <div @click="single = true"> single</div>{{ single }} | ||
| 26 | </div> | 25 | </div> |
| 27 | </template> | 26 | </template> |
| 28 | <script> | 27 | <script> |
| @@ -30,13 +29,7 @@ | @@ -30,13 +29,7 @@ | ||
| 30 | data () { | 29 | data () { |
| 31 | return { | 30 | return { |
| 32 | phone: 'apple', | 31 | phone: 'apple', |
| 33 | - animal: '爪哇犀牛', | ||
| 34 | - single: false | ||
| 35 | - } | ||
| 36 | - }, | ||
| 37 | - methods: { | ||
| 38 | - c (data) { | ||
| 39 | - console.log(data) | 32 | + animal: '爪哇犀牛' |
| 40 | } | 33 | } |
| 41 | } | 34 | } |
| 42 | } | 35 | } |
src/components/radio/radio-group.vue
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | </div> | 4 | </div> |
| 5 | </template> | 5 | </template> |
| 6 | <script> | 6 | <script> |
| 7 | - import { oneOf } from '../../utils/assist'; | 7 | + import { oneOf, findComponentsDownward } from '../../utils/assist'; |
| 8 | import Emitter from '../../mixins/emitter'; | 8 | import Emitter from '../../mixins/emitter'; |
| 9 | 9 | ||
| 10 | const prefixCls = 'ivu-radio-group'; | 10 | const prefixCls = 'ivu-radio-group'; |
| @@ -34,7 +34,8 @@ | @@ -34,7 +34,8 @@ | ||
| 34 | }, | 34 | }, |
| 35 | data () { | 35 | data () { |
| 36 | return { | 36 | return { |
| 37 | - currentValue: this.value | 37 | + currentValue: this.value, |
| 38 | + childrens: [] | ||
| 38 | }; | 39 | }; |
| 39 | }, | 40 | }, |
| 40 | computed: { | 41 | computed: { |
| @@ -55,10 +56,14 @@ | @@ -55,10 +56,14 @@ | ||
| 55 | methods: { | 56 | methods: { |
| 56 | updateValue () { | 57 | updateValue () { |
| 57 | const value = this.value; | 58 | const value = this.value; |
| 58 | - this.$children.forEach((child) => { | ||
| 59 | - child.currentValue = value == child.label; | ||
| 60 | - child.group = true; | ||
| 61 | - }); | 59 | + this.childrens = findComponentsDownward(this, 'Radio'); |
| 60 | + | ||
| 61 | + if (this.childrens) { | ||
| 62 | + this.childrens.forEach(child => { | ||
| 63 | + child.currentValue = value == child.label; | ||
| 64 | + child.group = true; | ||
| 65 | + }); | ||
| 66 | + } | ||
| 62 | }, | 67 | }, |
| 63 | change (data) { | 68 | change (data) { |
| 64 | this.currentValue = data.value; | 69 | this.currentValue = data.value; |
src/components/radio/radio.vue
| @@ -12,6 +12,7 @@ | @@ -12,6 +12,7 @@ | ||
| 12 | </label> | 12 | </label> |
| 13 | </template> | 13 | </template> |
| 14 | <script> | 14 | <script> |
| 15 | + import { findComponentUpward } from '../../utils/assist'; | ||
| 15 | import Emitter from '../../mixins/emitter'; | 16 | import Emitter from '../../mixins/emitter'; |
| 16 | 17 | ||
| 17 | const prefixCls = 'ivu-radio'; | 18 | const prefixCls = 'ivu-radio'; |
| @@ -35,7 +36,8 @@ | @@ -35,7 +36,8 @@ | ||
| 35 | data () { | 36 | data () { |
| 36 | return { | 37 | return { |
| 37 | currentValue: this.value, | 38 | currentValue: this.value, |
| 38 | - group: false | 39 | + group: false, |
| 40 | + parent: findComponentUpward(this, 'RadioGroup') | ||
| 39 | }; | 41 | }; |
| 40 | }, | 42 | }, |
| 41 | computed: { | 43 | computed: { |
| @@ -66,8 +68,8 @@ | @@ -66,8 +68,8 @@ | ||
| 66 | } | 68 | } |
| 67 | }, | 69 | }, |
| 68 | mounted () { | 70 | mounted () { |
| 69 | - // todo 使用 while向上查找 | ||
| 70 | - if (this.$parent && this.$parent.$options.name === 'RadioGroup') this.group = true; | 71 | + this.parent = findComponentUpward(this, 'RadioGroup'); |
| 72 | + if (this.parent) this.group = true; | ||
| 71 | if (!this.group) { | 73 | if (!this.group) { |
| 72 | this.updateValue(); | 74 | this.updateValue(); |
| 73 | } | 75 | } |
| @@ -83,7 +85,7 @@ | @@ -83,7 +85,7 @@ | ||
| 83 | this.$emit('input', checked); | 85 | this.$emit('input', checked); |
| 84 | 86 | ||
| 85 | if (this.group && this.label) { | 87 | if (this.group && this.label) { |
| 86 | - this.$parent.change({ | 88 | + this.parent.change({ |
| 87 | value: this.label, | 89 | value: this.label, |
| 88 | checked: this.value | 90 | checked: this.value |
| 89 | }); | 91 | }); |
src/utils/assist.js
| @@ -185,9 +185,9 @@ function findComponentUpward (content, componentName, componentNames) { | @@ -185,9 +185,9 @@ function findComponentUpward (content, componentName, componentNames) { | ||
| 185 | } | 185 | } |
| 186 | export {findComponentUpward}; | 186 | export {findComponentUpward}; |
| 187 | 187 | ||
| 188 | -// Find components downward | 188 | +// Find component downward |
| 189 | function findComponentDownward (content, componentName) { | 189 | function findComponentDownward (content, componentName) { |
| 190 | - let childrens = content.$children; | 190 | + const childrens = content.$children; |
| 191 | let children = null; | 191 | let children = null; |
| 192 | 192 | ||
| 193 | if (childrens.length) { | 193 | if (childrens.length) { |
| @@ -212,4 +212,24 @@ function findComponentDownward (content, componentName) { | @@ -212,4 +212,24 @@ function findComponentDownward (content, componentName) { | ||
| 212 | } | 212 | } |
| 213 | return children; | 213 | return children; |
| 214 | } | 214 | } |
| 215 | -export {findComponentDownward}; | ||
| 216 | \ No newline at end of file | 215 | \ No newline at end of file |
| 216 | +export {findComponentDownward}; | ||
| 217 | + | ||
| 218 | +// Find components downward | ||
| 219 | +function findComponentsDownward (content, componentName, components = []) { | ||
| 220 | + const childrens = content.$children; | ||
| 221 | + | ||
| 222 | + if (childrens.length) { | ||
| 223 | + childrens.forEach(child => { | ||
| 224 | + const name = child.$options.name; | ||
| 225 | + const childs = child.$children; | ||
| 226 | + | ||
| 227 | + if (name === componentName) components.push(child); | ||
| 228 | + if (childs.length) { | ||
| 229 | + const findChilds = findComponentsDownward(child, componentName, components); | ||
| 230 | + if (findChilds) components.concat(findChilds); | ||
| 231 | + } | ||
| 232 | + }); | ||
| 233 | + } | ||
| 234 | + return components; | ||
| 235 | +} | ||
| 236 | +export {findComponentsDownward}; | ||
| 217 | \ No newline at end of file | 237 | \ No newline at end of file |