Commit 3f281d6ce03a886cbab6d77b217f84c809a2a150

Authored by 梁灏
1 parent 4f5d3937

update RadioGroup

update RadioGroup
examples/routers/radio.vue
1 1 <template>
2 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 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 25 </div>
27 26 </template>
28 27 <script>
... ... @@ -30,13 +29,7 @@
30 29 data () {
31 30 return {
32 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 4 </div>
5 5 </template>
6 6 <script>
7   - import { oneOf } from '../../utils/assist';
  7 + import { oneOf, findComponentsDownward } from '../../utils/assist';
8 8 import Emitter from '../../mixins/emitter';
9 9  
10 10 const prefixCls = 'ivu-radio-group';
... ... @@ -34,7 +34,8 @@
34 34 },
35 35 data () {
36 36 return {
37   - currentValue: this.value
  37 + currentValue: this.value,
  38 + childrens: []
38 39 };
39 40 },
40 41 computed: {
... ... @@ -55,10 +56,14 @@
55 56 methods: {
56 57 updateValue () {
57 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 68 change (data) {
64 69 this.currentValue = data.value;
... ...
src/components/radio/radio.vue
... ... @@ -12,6 +12,7 @@
12 12 </label>
13 13 </template>
14 14 <script>
  15 + import { findComponentUpward } from '../../utils/assist';
15 16 import Emitter from '../../mixins/emitter';
16 17  
17 18 const prefixCls = 'ivu-radio';
... ... @@ -35,7 +36,8 @@
35 36 data () {
36 37 return {
37 38 currentValue: this.value,
38   - group: false
  39 + group: false,
  40 + parent: findComponentUpward(this, 'RadioGroup')
39 41 };
40 42 },
41 43 computed: {
... ... @@ -66,8 +68,8 @@
66 68 }
67 69 },
68 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 73 if (!this.group) {
72 74 this.updateValue();
73 75 }
... ... @@ -83,7 +85,7 @@
83 85 this.$emit('input', checked);
84 86  
85 87 if (this.group && this.label) {
86   - this.$parent.change({
  88 + this.parent.change({
87 89 value: this.label,
88 90 checked: this.value
89 91 });
... ...
src/utils/assist.js
... ... @@ -185,9 +185,9 @@ function findComponentUpward (content, componentName, componentNames) {
185 185 }
186 186 export {findComponentUpward};
187 187  
188   -// Find components downward
  188 +// Find component downward
189 189 function findComponentDownward (content, componentName) {
190   - let childrens = content.$children;
  190 + const childrens = content.$children;
191 191 let children = null;
192 192  
193 193 if (childrens.length) {
... ... @@ -212,4 +212,24 @@ function findComponentDownward (content, componentName) {
212 212 }
213 213 return children;
214 214 }
215   -export {findComponentDownward};
216 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 237 \ No newline at end of file
... ...