Commit a9131058abfaee847e14feff0066d8ff7ed129bf

Authored by 梁灏
1 parent 04e5e3cd

optimize Select event broadcast

optimize Select event broadcast
examples/routers/select.vue
... ... @@ -196,7 +196,16 @@
196 196 <Row>
197 197 <i-col span="12" style="padding-right:10px">
198 198 <Select v-model="model11" filterable>
199   - <i-option v-for="item in cityList" :value="item.value">{{ item.label }}</i-option>
  199 + <Option-group label="123">
  200 + <i-option value="beijing">北京市</i-option>
  201 + <i-option value="shanghai">上海市</i-option>
  202 + </Option-group>
  203 + <Option-group label="456">
  204 + <i-option value="shenzhen">深圳市</i-option>
  205 + <i-option value="hangzhou">杭州市</i-option>
  206 + </Option-group>
  207 + <i-option value="nanjing">南京市</i-option>
  208 + <i-option value="chongqing">重庆市</i-option>
200 209 </Select>
201 210 </i-col>
202 211 <i-col span="12">
... ...
src/components/select/select.vue
... ... @@ -36,7 +36,7 @@
36 36 import Icon from '../icon';
37 37 import Drop from './dropdown.vue';
38 38 import clickoutside from '../../directives/clickoutside';
39   - import { oneOf, MutationObserver } from '../../utils/assist';
  39 + import { oneOf, MutationObserver, findComponentDownward } from '../../utils/assist';
40 40 import { t } from '../../locale';
41 41 import Emitter from '../../mixins/emitter';
42 42  
... ... @@ -507,6 +507,7 @@
507 507 document.addEventListener('keydown', this.handleKeydown);
508 508  
509 509 // watch slot changed
  510 + // todo 在 child 的 mounted 和 beforeDestroy 里处理
510 511 if (MutationObserver) {
511 512 this.observer = new MutationObserver(() => {
512 513 this.modelToQuery();
... ... @@ -590,9 +591,12 @@
590 591 }
591 592 },
592 593 query (val) {
593   - // todo 这里会重复
594   - this.broadcast('OptionGroup', 'on-query-change', val);
595   - this.broadcast('iOption', 'on-query-change', val);
  594 + if (findComponentDownward(this, 'OptionGroup')) {
  595 + this.broadcast('OptionGroup', 'on-query-change', val);
  596 + this.broadcast('iOption', 'on-query-change', val);
  597 + } else {
  598 + this.broadcast('iOption', 'on-query-change', val);
  599 + }
596 600 let is_hidden = true;
597 601  
598 602 this.$nextTick(() => {
... ...
src/utils/assist.js
... ... @@ -183,4 +183,33 @@ function findComponentUpward (content, componentName, componentNames) {
183 183 }
184 184 return parent;
185 185 }
186   -export {findComponentUpward};
187 186 \ No newline at end of file
  187 +export {findComponentUpward};
  188 +
  189 +// Find components downward
  190 +function findComponentDownward (content, componentName) {
  191 + let childrens = content.$children;
  192 + let children = null;
  193 +
  194 + if (childrens.length) {
  195 + childrens.forEach(child => {
  196 + const name = child.$options.name;
  197 + if (name === componentName) {
  198 + children = child;
  199 + }
  200 + });
  201 +
  202 + for (let i = 0; i < childrens.length; i++) {
  203 + const child = childrens[i];
  204 + const name = child.$options.name;
  205 + if (name === componentName) {
  206 + children = child;
  207 + break;
  208 + } else {
  209 + children = findComponentDownward(child, componentName);
  210 + if (children) break;
  211 + }
  212 + }
  213 + }
  214 + return children;
  215 +}
  216 +export {findComponentDownward};
188 217 \ No newline at end of file
... ...