Blame view

src/components/select/select.vue 21 KB
e355dd49   梁灏   add Select Component
1
2
3
  <template>
      <div :class="classes" v-clickoutside="handleClose">
          <div
d6342fe1   jingsam   fixed ie bug
4
              :class="[prefixCls + '-selection']"
e355dd49   梁灏   add Select Component
5
6
              v-el:reference
              @click="toggleMenu">
294e2412   梁灏   update Select com...
7
              <div class="ivu-tag" v-for="item in selectedMultiple">
e355dd49   梁灏   add Select Component
8
9
10
                  <span class="ivu-tag-text">{{ item.label }}</span>
                  <Icon type="ios-close-empty" @click.stop="removeTag($index)"></Icon>
              </div>
d6342fe1   jingsam   fixed ie bug
11
12
              <span :class="[prefixCls + '-placeholder']" v-show="showPlaceholder && !filterable">{{ placeholder }}</span>
              <span :class="[prefixCls + '-selected-value']" v-show="!showPlaceholder && !multiple && !filterable">{{ selectedSingle }}</span>
e355dd49   梁灏   add Select Component
13
14
              <input
                  type="text"
e355dd49   梁灏   add Select Component
15
16
                  v-if="filterable"
                  v-model="query"
d6342fe1   jingsam   fixed ie bug
17
                  :class="[prefixCls + '-input']"
e4ce9917   梁灏   update Select com...
18
                  :placeholder="showPlaceholder ? placeholder : ''"
e4ebd304   梁灏   update Select com...
19
                  :style="inputStyle"
e4ce9917   梁灏   update Select com...
20
21
22
23
                  @blur="handleBlur"
                  @keydown="resetInputState"
                  @keydown.delete="handleInputDelete"
                  v-el:input>
d6342fe1   jingsam   fixed ie bug
24
25
              <Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.stop="clearSingleSelect"></Icon>
              <Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
e355dd49   梁灏   add Select Component
26
27
          </div>
          <Dropdown v-show="visible" transition="slide-up" v-ref:dropdown>
d6342fe1   jingsam   fixed ie bug
28
              <ul v-show="notFound" :class="[prefixCls + '-not-found']"><li>{{ notFoundText }}</li></ul>
3e855e34   梁灏   fixed #46
29
              <ul v-else :class="[prefixCls + '-dropdown-list']" v-el:options><slot></slot></ul>
e355dd49   梁灏   add Select Component
30
31
32
33
34
35
36
          </Dropdown>
      </div>
  </template>
  <script>
      import Icon from '../icon';
      import Dropdown from './dropdown.vue';
      import clickoutside from '../../directives/clickoutside';
3e855e34   梁灏   fixed #46
37
      import { oneOf, MutationObserver } from '../../utils/assist';
d33b5143   梁灏   support i18n
38
      import { t } from '../../locale';
e355dd49   梁灏   add Select Component
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  
      const prefixCls = 'ivu-select';
  
      export default {
          components: { Icon, Dropdown },
          directives: { clickoutside },
          props: {
              model: {
                  type: [String, Number, Array],
                  default: ''
              },
              multiple: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              clearable: {
                  type: Boolean,
                  default: false
              },
              placeholder: {
                  type: String,
d33b5143   梁灏   support i18n
64
65
66
                  default () {
                      return t('i.select.placeholder');
                  }
e355dd49   梁灏   add Select Component
67
68
69
70
71
72
73
74
75
76
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              filterMethod: {
                  type: Function
              },
              size: {
                  validator (value) {
6932b4d7   梁灏   update Page compo...
77
                      return oneOf(value, ['small', 'large', 'default']);
e355dd49   梁灏   add Select Component
78
79
80
81
82
                  }
              },
              labelInValue: {
                  type: Boolean,
                  default: false
294e2412   梁灏   update Select com...
83
84
85
              },
              notFoundText: {
                  type: String,
d33b5143   梁灏   support i18n
86
87
88
                  default () {
                      return t('i.select.noMatch');
                  }
e355dd49   梁灏   add Select Component
89
90
91
92
93
94
95
96
97
98
99
100
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  visible: false,
                  options: [],
                  optionInstances: [],
                  selectedSingle: '',    // label
                  selectedMultiple: [],
                  focusIndex: 0,
                  query: '',
e4ce9917   梁灏   update Select com...
101
                  inputLength: 20,
3e855e34   梁灏   fixed #46
102
103
                  notFound: false,
                  slotChangeDuration: false    // if slot change duration and in multiple, set true and after slot change, set false
b0893113   jingsam   :art: add eslint
104
              };
e355dd49   梁灏   add Select Component
105
106
107
108
          },
          computed: {
              classes () {
                  return [
4b7138b9   梁灏   fixed some bugs
109
                      `${prefixCls}`,
e355dd49   梁灏   add Select Component
110
                      {
4b7138b9   梁灏   fixed some bugs
111
112
113
114
115
116
                          [`${prefixCls}-visible`]: this.visible,
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-multiple`]: this.multiple,
                          [`${prefixCls}-single`]: !this.multiple,
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
                          [`${prefixCls}-${this.size}`]: !!this.size
e355dd49   梁灏   add Select Component
117
                      }
b0893113   jingsam   :art: add eslint
118
                  ];
e355dd49   梁灏   add Select Component
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
              },
              showPlaceholder () {
                  let status = false;
  
                  if ((typeof this.model) === 'string') {
                      if (this.model === '') {
                          status = true;
                      }
                  } else if (Array.isArray(this.model)) {
                      if (!this.model.length) {
                          status = true;
                      }
                  }
  
                  return status;
              },
              showCloseIcon () {
                  return !this.multiple && this.clearable && !this.showPlaceholder;
              },
              inputStyle () {
                  let style = {};
  
                  if (this.multiple) {
e4ce9917   梁灏   update Select com...
142
143
144
                      if (this.showPlaceholder) {
                          style.width = '100%';
                      } else {
4b7138b9   梁灏   fixed some bugs
145
                          style.width = `${this.inputLength}px`;
e4ce9917   梁灏   update Select com...
146
                      }
e355dd49   梁灏   add Select Component
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                  }
  
                  return style;
              }
          },
          methods: {
              toggleMenu () {
                  if (this.disabled) {
                      return false;
                  }
  
                  this.visible = !this.visible;
              },
              hideMenu () {
                  this.visible = false;
                  this.focusIndex = 0;
                  this.$broadcast('on-select-close');
              },
              // find option component
              findChild (cb) {
                  const find = function (child) {
                      const name = child.$options.componentName;
  
                      if (name) {
                          cb(child);
                      } else if (child.$children.length) {
                          child.$children.forEach((innerChild) => {
                              find(innerChild, cb);
                          });
                      }
                  };
  
                  if (this.optionInstances.length) {
                      this.optionInstances.forEach((child) => {
                          find(child);
b0893113   jingsam   :art: add eslint
182
                      });
e355dd49   梁灏   add Select Component
183
184
185
186
187
188
                  } else {
                      this.$children.forEach((child) => {
                          find(child);
                      });
                  }
              },
3e855e34   梁灏   fixed #46
189
              updateOptions (init, slot = false) {
e355dd49   梁灏   add Select Component
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                  let options = [];
                  let index = 1;
  
                  this.findChild((child) => {
                      options.push({
                          value: child.value,
                          label: (child.label === undefined) ? child.$el.innerHTML : child.label
                      });
                      child.index = index++;
  
                      if (init) {
                          this.optionInstances.push(child);
                      }
                  });
  
                  this.options = options;
  
                  if (init) {
3e855e34   梁灏   fixed #46
208
209
                      this.updateSingleSelected(true, slot);
                      this.updateMultipleSelected(true, slot);
e355dd49   梁灏   add Select Component
210
211
                  }
              },
3e855e34   梁灏   fixed #46
212
              updateSingleSelected (init = false, slot = false) {
e355dd49   梁灏   add Select Component
213
214
215
                  const type = typeof this.model;
  
                  if (type === 'string' || type === 'number') {
3e855e34   梁灏   fixed #46
216
217
                      let findModel = false;
  
e355dd49   梁灏   add Select Component
218
219
220
                      for (let i = 0; i < this.options.length; i++) {
                          if (this.model === this.options[i].value) {
                              this.selectedSingle = this.options[i].label;
3e855e34   梁灏   fixed #46
221
                              findModel = true;
e355dd49   梁灏   add Select Component
222
223
224
                              break;
                          }
                      }
3e855e34   梁灏   fixed #46
225
226
227
228
229
  
                      if (slot && !findModel) {
                          this.model = '';
                          this.query = '';
                      }
e355dd49   梁灏   add Select Component
230
231
232
233
234
235
236
237
238
239
                  }
  
                  this.toggleSingleSelected(this.model, init);
              },
              clearSingleSelect () {
                  if (this.showCloseIcon) {
                      this.findChild((child) => {
                          child.selected = false;
                      });
                      this.model = '';
e4ebd304   梁灏   update Select com...
240
241
242
243
  
                      if (this.filterable) {
                          this.query = '';
                      }
e355dd49   梁灏   add Select Component
244
245
                  }
              },
3e855e34   梁灏   fixed #46
246
              updateMultipleSelected (init = false, slot = false) {
e355dd49   梁灏   add Select Component
247
248
249
250
251
252
253
254
255
256
257
258
259
                  if (this.multiple && Array.isArray(this.model)) {
                      let selected = [];
  
                      for (let i = 0; i < this.model.length; i++) {
                          const model = this.model[i];
  
                          for (let j = 0; j < this.options.length; j++) {
                              const option = this.options[j];
  
                              if (model === option.value) {
                                  selected.push({
                                      value: option.value,
                                      label: option.label
b0893113   jingsam   :art: add eslint
260
                                  });
e355dd49   梁灏   add Select Component
261
262
263
264
265
                              }
                          }
                      }
  
                      this.selectedMultiple = selected;
e355dd49   梁灏   add Select Component
266
  
3e855e34   梁灏   fixed #46
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
                      if (slot) {
                          let selectedModel = [];
  
                          for (let i = 0; i < selected.length; i++) {
                              selectedModel.push(selected[i].value);
                          }
  
                          // if slot change and remove a selected option, emit user
                          if (this.model.length === selectedModel.length) {
                              this.slotChangeDuration = true;
                          }
  
                          this.model = selectedModel;
                      }
                  }
e355dd49   梁灏   add Select Component
282
283
284
285
286
287
288
                  this.toggleMultipleSelected(this.model, init);
              },
              removeTag (index) {
                  if (this.disabled) {
                      return false;
                  }
                  this.model.splice(index, 1);
e4ce9917   梁灏   update Select com...
289
290
291
292
293
294
  
                  if (this.filterable && this.visible) {
                      this.$els.input.focus();
                  }
  
                  this.$broadcast('on-update-popper');
e355dd49   梁灏   add Select Component
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
              },
              // to select option for single
              toggleSingleSelected (value, init = false) {
                  if (!this.multiple) {
                      let label = '';
  
                      this.findChild((child) => {
                          if (child.value === value) {
                              child.selected = true;
                              label = (child.label === undefined) ? child.$el.innerHTML : child.label;
                          } else {
                              child.selected = false;
                          }
                      });
  
                      this.hideMenu();
  
                      if (!init) {
                          if (this.labelInValue) {
                              this.$emit('on-change', {
                                  value: value,
                                  label: label
                              });
3ad47566   梁灏   update Select
318
319
320
321
                              this.$dispatch('on-form-change', {
                                  value: value,
                                  label: label
                              });
e355dd49   梁灏   add Select Component
322
323
                          } else {
                              this.$emit('on-change', value);
3ad47566   梁灏   update Select
324
                              this.$dispatch('on-form-change', value);
e355dd49   梁灏   add Select Component
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
                          }
                      }
                  }
              },
              // to select option for multiple
              toggleMultipleSelected (value, init = false) {
                  if (this.multiple) {
                      let hybridValue = [];
                      for (let i = 0; i < value.length; i++) {
                          hybridValue.push({
                              value: value[i]
                          });
                      }
  
                      this.findChild((child) => {
                          const index = value.indexOf(child.value);
  
                          if (index >= 0) {
                              child.selected = true;
                              hybridValue[index].label = (child.label === undefined) ? child.$el.innerHTML : child.label;
                          } else {
                              child.selected = false;
                          }
                      });
  
                      if (!init) {
                          if (this.labelInValue) {
                              this.$emit('on-change', hybridValue);
3ad47566   梁灏   update Select
353
                              this.$dispatch('on-form-change', hybridValue);
e355dd49   梁灏   add Select Component
354
355
                          } else {
                              this.$emit('on-change', value);
3ad47566   梁灏   update Select
356
                              this.$dispatch('on-form-change', value);
e355dd49   梁灏   add Select Component
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
                          }
                      }
                  }
              },
              handleClose () {
                  this.hideMenu();
              },
              handleKeydown (e) {
                  if (this.visible) {
                      const keyCode = e.keyCode;
                      // Esc slide-up
                      if (keyCode === 27) {
                          e.preventDefault();
                          this.hideMenu();
                      }
                      // next
                      if (keyCode === 40) {
                          e.preventDefault();
                          this.navigateOptions('next');
                      }
                      // prev
                      if (keyCode === 38) {
                          e.preventDefault();
                          this.navigateOptions('prev');
                      }
                      // enter
                      if (keyCode === 13) {
                          e.preventDefault();
  
                          this.findChild((child) => {
                              if (child.isFocus) {
                                  child.select();
                              }
                          });
                      }
                  }
              },
              navigateOptions (direction) {
                  if (direction === 'next') {
                      const next = this.focusIndex + 1;
                      this.focusIndex = (this.focusIndex === this.options.length) ? 1 : next;
                  } else if (direction === 'prev') {
                      const prev = this.focusIndex - 1;
                      this.focusIndex = (this.focusIndex <= 1) ? this.options.length : prev;
                  }
  
                  let child_status = {
e4ebd304   梁灏   update Select com...
404
405
                      disabled: false,
                      hidden: false
e355dd49   梁灏   add Select Component
406
407
                  };
  
e4ebd304   梁灏   update Select com...
408
409
                  let find_deep = false;    // can next find allowed
  
e355dd49   梁灏   add Select Component
410
411
412
                  this.findChild((child) => {
                      if (child.index === this.focusIndex) {
                          child_status.disabled = child.disabled;
e4ebd304   梁灏   update Select com...
413
                          child_status.hidden = child.hidden;
e355dd49   梁灏   add Select Component
414
  
e4ebd304   梁灏   update Select com...
415
                          if (!child.disabled && !child.hidden) {
e355dd49   梁灏   add Select Component
416
417
418
419
420
                              child.isFocus = true;
                          }
                      } else {
                          child.isFocus = false;
                      }
e4ebd304   梁灏   update Select com...
421
422
423
424
  
                      if (!child.hidden && !child.disabled) {
                          find_deep = true;
                      }
e355dd49   梁灏   add Select Component
425
426
427
428
                  });
  
                  this.resetScrollTop();
  
e4ebd304   梁灏   update Select com...
429
                  if ((child_status.disabled || child_status.hidden) && find_deep) {
e355dd49   梁灏   add Select Component
430
431
432
433
434
435
436
437
438
439
440
441
442
443
                      this.navigateOptions(direction);
                  }
              },
              resetScrollTop () {
                  const index = this.focusIndex - 1;
                  let bottomOverflowDistance = this.optionInstances[index].$el.getBoundingClientRect().bottom - this.$refs.dropdown.$el.getBoundingClientRect().bottom;
                  let topOverflowDistance = this.optionInstances[index].$el.getBoundingClientRect().top - this.$refs.dropdown.$el.getBoundingClientRect().top;
  
                  if (bottomOverflowDistance > 0) {
                      this.$refs.dropdown.$el.scrollTop += bottomOverflowDistance;
                  }
                  if (topOverflowDistance < 0) {
                      this.$refs.dropdown.$el.scrollTop += topOverflowDistance;
                  }
e4ebd304   梁灏   update Select com...
444
445
446
447
448
449
              },
              handleBlur () {
                  setTimeout(() => {
                      const model = this.model;
  
                      if (this.multiple) {
eda30489   huangliangxiang   可搜索下拉菜单 输入无匹配时,移出...
450
                          this.query = '';
e4ebd304   梁灏   update Select com...
451
452
453
454
                      } else {
                          if (model !== '') {
                              this.findChild((child) => {
                                  if (child.value === model) {
1363abdc   Rijn   fixed #178
455
                                      this.query = child.label === undefined ? child.searchLabel : child.label;
e4ebd304   梁灏   update Select com...
456
457
                                  }
                              });
eda30489   huangliangxiang   可搜索下拉菜单 输入无匹配时,移出...
458
459
                          } else {
                              this.query = '';
e4ebd304   梁灏   update Select com...
460
461
462
                          }
                      }
                  }, 300);
e4ce9917   梁灏   update Select com...
463
464
465
466
467
468
469
470
              },
              resetInputState () {
                  this.inputLength = this.$els.input.value.length * 12 + 20;
              },
              handleInputDelete () {
                  if (this.multiple && this.model.length && this.query === '') {
                      this.removeTag(this.model.length - 1);
                  }
3e855e34   梁灏   fixed #46
471
472
473
474
475
              },
              // use when slot changed
              slotChange () {
                  this.options = [];
                  this.optionInstances = [];
2f0b086d   梁灏   fixed #116
476
477
478
479
              },
              setQuery (query) {
                  if (!this.filterable) return;
                  this.query = query;
e355dd49   梁灏   add Select Component
480
481
              }
          },
d509d2b2   梁灏   fixed #192
482
          compiled () {
e2006187   梁灏   fixed #159
483
484
485
486
487
488
489
490
491
492
493
494
495
496
              if (!this.multiple && this.filterable && this.model) {
                  this.findChild((child) => {
                      if (this.model === child.value) {
                          if (child.label) {
                              this.query = child.label;
                          } else if (child.searchLabel) {
                              this.query = child.searchLabel;
                          } else {
                              this.query = child.value;
                          }
                      }
                  });
              }
  
e355dd49   梁灏   add Select Component
497
498
              this.updateOptions(true);
              document.addEventListener('keydown', this.handleKeydown);
3e855e34   梁灏   fixed #46
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  
              // watch slot changed
              if (MutationObserver) {
                  this.observer = new MutationObserver(() => {
                      this.slotChange();
                      this.updateOptions(true, true);
                  });
  
                  this.observer.observe(this.$els.options, {
  //                attributes: true,
                      childList: true,
                      characterData: true,
                      subtree: true
                  });
              }
e355dd49   梁灏   add Select Component
514
515
516
          },
          beforeDestroy () {
              document.removeEventListener('keydown', this.handleKeydown);
3e855e34   梁灏   fixed #46
517
518
519
              if (this.observer) {
                  this.observer.disconnect();
              }
e355dd49   梁灏   add Select Component
520
521
522
523
          },
          watch: {
              model () {
                  if (this.multiple) {
3e855e34   梁灏   fixed #46
524
525
526
527
528
                      if (this.slotChangeDuration) {
                          this.slotChangeDuration = false;
                      } else {
                          this.updateMultipleSelected();
                      }
e355dd49   梁灏   add Select Component
529
530
531
532
533
534
                  } else {
                      this.updateSingleSelected();
                  }
              },
              visible (val) {
                  if (val) {
e4ce9917   梁灏   update Select com...
535
536
537
                      if (this.multiple && this.filterable) {
                          this.$els.input.focus();
                      }
e355dd49   梁灏   add Select Component
538
539
                      this.$broadcast('on-update-popper');
                  } else {
b7cf983e   梁灏   update Select com...
540
541
542
543
                      if (this.filterable) {
                          this.$els.input.blur();
                      }
                      this.$broadcast('on-destroy-popper');
e355dd49   梁灏   add Select Component
544
                  }
e4ebd304   梁灏   update Select com...
545
546
547
              },
              query (val) {
                  this.$broadcast('on-query-change', val);
e4ce9917   梁灏   update Select com...
548
549
550
551
552
553
554
555
556
557
                  let is_hidden = true;
  
                  this.$nextTick(() => {
                      this.findChild((child) => {
                          if (!child.hidden) {
                              is_hidden = false;
                          }
                      });
                      this.notFound = is_hidden;
                  });
e355dd49   梁灏   add Select Component
558
559
560
561
562
563
564
565
566
567
568
569
570
              }
          },
          events: {
              'on-select-selected' (value) {
                  if (this.model === value) {
                      this.hideMenu();
                  } else {
                      if (this.multiple) {
                          const index = this.model.indexOf(value);
                          if (index >= 0) {
                              this.removeTag(index);
                          } else {
                              this.model.push(value);
e4ce9917   梁灏   update Select com...
571
572
573
574
575
576
                              this.$broadcast('on-update-popper');
                          }
  
                          if (this.filterable) {
                              this.query = '';
                              this.$els.input.focus();
e355dd49   梁灏   add Select Component
577
578
579
                          }
                      } else {
                          this.model = value;
e4ebd304   梁灏   update Select com...
580
581
582
583
  
                          if (this.filterable) {
                              this.findChild((child) => {
                                  if (child.value === value) {
1363abdc   Rijn   fixed #178
584
                                      this.query = child.label === undefined ? child.searchLabel : child.label;
e4ebd304   梁灏   update Select com...
585
586
587
                                  }
                              });
                          }
e355dd49   梁灏   add Select Component
588
589
590
591
                      }
                  }
              }
          }
b0893113   jingsam   :art: add eslint
592
      };
d6342fe1   jingsam   fixed ie bug
593
  </script>