Blame view

src/components/select/select.vue 27.6 KB
e355dd49   梁灏   add Select Component
1
  <template>
2fbe636b   梁灏   Select support a ...
2
      <div
2fbe636b   梁灏   Select support a ...
3
          :class="classes"
c9b86944   Sergio Crisostomo   Refactor Select!
4
5
          v-click-outside.capture="onClickOutside"
      >
e355dd49   梁灏   add Select Component
6
          <div
4aec6a66   梁灏   support Select
7
              ref="reference"
c9b86944   Sergio Crisostomo   Refactor Select!
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  
              :class="selectionCls"
              :tabindex="selectTabindex"
  
              @blur="toggleHeaderFocus"
              @focus="toggleHeaderFocus"
  
              @click="toggleMenu"
              @keydown.esc="handleKeydown"
              @keydown.enter="handleKeydown"
              @keydown.up="handleKeydown"
              @keydown.down="handleKeydown"
              @keydown.tab="handleKeydown"
              @keydown.delete="handleKeydown"
  
  
              @mouseenter="hasMouseHoverHead = true"
              @mouseleave="hasMouseHoverHead = false"
  
          >
fed3e09d   梁灏   add AutoComplete ...
28
              <slot name="input">
c9b86944   Sergio Crisostomo   Refactor Select!
29
30
31
32
33
34
                  <input type="hidden" :name="name" :value="publicValue">
                  <select-head
                      :filterable="filterable"
                      :multiple="multiple"
                      :values="values"
                      :clearable="canBeCleared"
fed3e09d   梁灏   add AutoComplete ...
35
                      :disabled="disabled"
c9b86944   Sergio Crisostomo   Refactor Select!
36
37
38
39
40
41
42
43
44
                      :remote="remote"
                      :input-element-id="elementId"
                      :initial-label="initialLabel"
                      :placeholder="placeholder"
                      :query-prop="query"
  
                      @on-query-change="onQueryChange"
                      @on-input-focus="isFocused = true"
                      @on-input-blur="isFocused = false"
7dbde804   Sergio Crisostomo   add on-clear event
45
                      @on-clear="clearSingleSelect"
c9b86944   Sergio Crisostomo   Refactor Select!
46
                  />
fed3e09d   梁灏   add AutoComplete ...
47
              </slot>
e355dd49   梁灏   add Select Component
48
          </div>
e09b07b7   huanghong   解决drop弹出动画异常
49
          <transition name="transition-drop">
595cfa72   梁灏   fixed #1187 #844 ...
50
              <Drop
ecaf8d51   梁灏   Date add transfer...
51
                  :class="dropdownCls"
595cfa72   梁灏   fixed #1187 #844 ...
52
53
54
55
                  v-show="dropVisible"
                  :placement="placement"
                  ref="dropdown"
                  :data-transfer="transfer"
c9b86944   Sergio Crisostomo   Refactor Select!
56
57
58
59
                  v-transfer-dom
              >
                  <ul v-show="showNotFoundLabel" :class="[prefixCls + '-not-found']"><li>{{ localeNotFoundText }}</li></ul>
                  <ul :class="prefixCls + '-dropdown-list'">
220161f5   Sergio Crisostomo   Clean up empty/nu...
60
61
62
63
64
65
                      <functional-options
                          v-if="(!remote) || (remote && !loading)"
                          :options="selectOptions"
                          :slot-update-hook="updateSlotOptions"
                          :slot-options="slotOptions"
                      ></functional-options>
c9b86944   Sergio Crisostomo   Refactor Select!
66
                  </ul>
01b54e30   梁灏   Select support re...
67
                  <ul v-show="loading" :class="[prefixCls + '-loading']">{{ localeLoadingText }}</ul>
4aec6a66   梁灏   support Select
68
69
              </Drop>
          </transition>
e355dd49   梁灏   add Select Component
70
71
72
73
      </div>
  </template>
  <script>
      import Icon from '../icon';
4aec6a66   梁灏   support Select
74
      import Drop from './dropdown.vue';
c9b86944   Sergio Crisostomo   Refactor Select!
75
      import vClickOutside from 'v-click-outside-x/index';
595cfa72   梁灏   fixed #1187 #844 ...
76
      import TransferDom from '../../directives/transfer-dom';
c9b86944   Sergio Crisostomo   Refactor Select!
77
      import { oneOf } from '../../utils/assist';
4aec6a66   梁灏   support Select
78
      import Emitter from '../../mixins/emitter';
e5337c81   梁灏   fixed some compon...
79
      import Locale from '../../mixins/locale';
c9b86944   Sergio Crisostomo   Refactor Select!
80
81
      import SelectHead from './select-head.vue';
      import FunctionalOptions from './functional-options.vue';
e355dd49   梁灏   add Select Component
82
83
  
      const prefixCls = 'ivu-select';
523e2c81   Sergio Crisostomo   correct match log...
84
85
      const optionRegexp = /^i-option$|^Option$/;
      const optionGroupRegexp = /option-?group/i;
c9b86944   Sergio Crisostomo   Refactor Select!
86
87
88
89
90
91
92
93
94
95
  
      const findChild = (instance, checkFn) => {
          let match = checkFn(instance);
          if (match) return instance;
          for (let i = 0, l = instance.$children.length; i < l; i++){
              const child = instance.$children[i];
              match = findChild(child, checkFn);
              if (match) return match;
          }
      };
e355dd49   梁灏   add Select Component
96
  
06a74f9e   Sergio Crisostomo   Allow select to n...
97
98
      const findOptionsInVNode = (node) => {
          const opts = node.componentOptions;
523e2c81   Sergio Crisostomo   correct match log...
99
          if (opts && opts.tag.match(optionRegexp)) return [node];
7d14e70c   Sergio Crisostomo   Include both node...
100
101
102
          if (!node.children && (!opts || !opts.children)) return [];
          const children = [...(node.children || []),  ...(opts && opts.children || [])];
          const options = children.reduce(
06a74f9e   Sergio Crisostomo   Allow select to n...
103
104
105
106
107
108
109
110
111
              (arr, el) => [...arr, ...findOptionsInVNode(el)], []
          ).filter(Boolean);
          return options.length > 0 ? options : [];
      };
  
      const extractOptions = (options) => options.reduce((options, slotEntry) => {
          return options.concat(findOptionsInVNode(slotEntry));
      }, []);
  
aa21cdf9   Sergio Crisostomo   Process also shal...
112
113
114
115
116
117
118
119
120
121
122
123
124
      const applyProp = (node, propName, value) => {
          return {
              ...node,
              componentOptions: {
                  ...node.componentOptions,
                  propsData: {
                      ...node.componentOptions.propsData,
                      [propName]: value,
                  }
              }
          };
      };
  
31788df3   Sergio Crisostomo   Normalise behavio...
125
126
      const ANIMATION_TIMEOUT = 300;
  
e355dd49   梁灏   add Select Component
127
      export default {
8f5b1686   梁灏   fixed #196
128
          name: 'iSelect',
e5337c81   梁灏   fixed some compon...
129
          mixins: [ Emitter, Locale ],
c9b86944   Sergio Crisostomo   Refactor Select!
130
131
          components: { FunctionalOptions, Drop, Icon, SelectHead },
          directives: { clickOutside: vClickOutside.directive, TransferDom },
e355dd49   梁灏   add Select Component
132
          props: {
4aec6a66   梁灏   support Select
133
              value: {
e355dd49   梁灏   add Select Component
134
135
136
                  type: [String, Number, Array],
                  default: ''
              },
98bf25b3   梁灏   fixed #1286
137
              // 使用时,也得设置 value 才行
ddc35c9a   梁灏   fixed #952
138
139
140
141
              label: {
                  type: [String, Number, Array],
                  default: ''
              },
e355dd49   梁灏   add Select Component
142
143
144
145
146
147
148
149
150
151
152
153
154
              multiple: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              clearable: {
                  type: Boolean,
                  default: false
              },
              placeholder: {
e5337c81   梁灏   fixed some compon...
155
                  type: String
e355dd49   梁灏   add Select Component
156
157
158
159
160
161
162
163
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              filterMethod: {
                  type: Function
              },
01b54e30   梁灏   Select support re...
164
165
166
167
168
169
170
171
172
173
              remoteMethod: {
                  type: Function
              },
              loading: {
                  type: Boolean,
                  default: false
              },
              loadingText: {
                  type: String
              },
e355dd49   梁灏   add Select Component
174
175
              size: {
                  validator (value) {
6932b4d7   梁灏   update Page compo...
176
                      return oneOf(value, ['small', 'large', 'default']);
e355dd49   梁灏   add Select Component
177
178
179
180
181
                  }
              },
              labelInValue: {
                  type: Boolean,
                  default: false
294e2412   梁灏   update Select com...
182
183
              },
              notFoundText: {
e5337c81   梁灏   fixed some compon...
184
                  type: String
f89dd9c2   梁灏   Paeg、Select add p...
185
186
187
188
189
190
              },
              placement: {
                  validator (value) {
                      return oneOf(value, ['top', 'bottom']);
                  },
                  default: 'bottom'
595cfa72   梁灏   fixed #1187 #844 ...
191
192
193
194
              },
              transfer: {
                  type: Boolean,
                  default: false
fed3e09d   梁灏   add AutoComplete ...
195
196
197
198
199
              },
              // Use for AutoComplete
              autoComplete: {
                  type: Boolean,
                  default: false
0460a1e8   梁灏   fixed #812
200
201
202
              },
              name: {
                  type: String
acb79ba3   梁灏   fixed #433
203
204
205
              },
              elementId: {
                  type: String
e355dd49   梁灏   add Select Component
206
207
              }
          },
c9b86944   Sergio Crisostomo   Refactor Select!
208
209
210
211
          mounted(){
              this.$on('on-select-selected', this.onOptionClick);
  
              // set the initial values if there are any
7f63e58c   Sergio Crisostomo   Make possible for...
212
              if (this.values.length > 0 && !this.remote && this.selectOptions.length > 0){
220161f5   Sergio Crisostomo   Clean up empty/nu...
213
                  this.values = this.values.map(this.getOptionData).filter(Boolean);
c9b86944   Sergio Crisostomo   Refactor Select!
214
              }
7f63e58c   Sergio Crisostomo   Make possible for...
215
216
217
218
  
              if (this.values.length > 0 && this.selectOptions.length === 0){
                  this.hasExpectedValue = this.values;
              }
c9b86944   Sergio Crisostomo   Refactor Select!
219
          },
e355dd49   梁灏   add Select Component
220
          data () {
c9b86944   Sergio Crisostomo   Refactor Select!
221
  
e355dd49   梁灏   add Select Component
222
223
              return {
                  prefixCls: prefixCls,
c9b86944   Sergio Crisostomo   Refactor Select!
224
225
                  values: this.getInitialValue(),
                  dropDownWidth: 0,
e355dd49   梁灏   add Select Component
226
                  visible: false,
c9b86944   Sergio Crisostomo   Refactor Select!
227
228
                  focusIndex: -1,
                  isFocused: false,
e355dd49   梁灏   add Select Component
229
                  query: '',
c9b86944   Sergio Crisostomo   Refactor Select!
230
231
232
233
234
                  initialLabel: this.label,
                  hasMouseHoverHead: false,
                  slotOptions: this.$slots.default,
                  caretPosition: -1,
                  lastRemoteQuery: '',
31788df3   Sergio Crisostomo   Normalise behavio...
235
                  unchangedQuery: true,
7f63e58c   Sergio Crisostomo   Make possible for...
236
                  hasExpectedValue: false,
45bcc14d   Sergio Crisostomo   prevent calling r...
237
                  preventRemoteCall: false,
b0893113   jingsam   :art: add eslint
238
              };
e355dd49   梁灏   add Select Component
239
240
241
242
          },
          computed: {
              classes () {
                  return [
4b7138b9   梁灏   fixed some bugs
243
                      `${prefixCls}`,
e355dd49   梁灏   add Select Component
244
                      {
4b7138b9   梁灏   fixed some bugs
245
246
247
248
249
250
                          [`${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
251
                      }
b0893113   jingsam   :art: add eslint
252
                  ];
e355dd49   梁灏   add Select Component
253
              },
ecaf8d51   梁灏   Date add transfer...
254
255
256
              dropdownCls () {
                  return {
                      [prefixCls + '-dropdown-transfer']: this.transfer,
fed3e09d   梁灏   add AutoComplete ...
257
258
259
260
261
262
                      [prefixCls + '-multiple']: this.multiple && this.transfer,
                      ['ivu-auto-complete']: this.autoComplete,
                  };
              },
              selectionCls () {
                  return {
c9b86944   Sergio Crisostomo   Refactor Select!
263
264
                      [`${prefixCls}-selection`]: !this.autoComplete,
                      [`${prefixCls}-selection-focused`]: this.isFocused
ecaf8d51   梁灏   Date add transfer...
265
266
                  };
              },
31788df3   Sergio Crisostomo   Normalise behavio...
267
268
              queryStringMatchesSelectedOption(){
                  const selectedOptions = this.values[0];
5266c905   Sergio Crisostomo   Trim label so we ...
269
270
271
                  if (!selectedOptions) return false;
                  const [query, label] = [this.query, selectedOptions.label].map(str => (str || '').trim());
                  return !this.multiple && this.unchangedQuery && query === label;
31788df3   Sergio Crisostomo   Normalise behavio...
272
              },
e5337c81   梁灏   fixed some compon...
273
              localeNotFoundText () {
c9b86944   Sergio Crisostomo   Refactor Select!
274
                  if (typeof this.notFoundText === 'undefined') {
e5337c81   梁灏   fixed some compon...
275
276
277
278
                      return this.t('i.select.noMatch');
                  } else {
                      return this.notFoundText;
                  }
f89dd9c2   梁灏   Paeg、Select add p...
279
              },
01b54e30   梁灏   Select support re...
280
              localeLoadingText () {
c9b86944   Sergio Crisostomo   Refactor Select!
281
                  if (typeof this.loadingText === 'undefined') {
01b54e30   梁灏   Select support re...
282
283
284
285
286
                      return this.t('i.select.loading');
                  } else {
                      return this.loadingText;
                  }
              },
f89dd9c2   梁灏   Paeg、Select add p...
287
288
              transitionName () {
                  return this.placement === 'bottom' ? 'slide-up' : 'slide-down';
ec98f3c3   梁灏   update Select
289
290
291
              },
              dropVisible () {
                  let status = true;
bc348e7e   Sergio Crisostomo   adapt to auto-com...
292
293
                  const noOptions = !this.selectOptions || this.selectOptions.length === 0;
                  if (!this.loading && this.remote && this.query === '' && noOptions) status = false;
fed3e09d   梁灏   add AutoComplete ...
294
  
bc348e7e   Sergio Crisostomo   adapt to auto-com...
295
                  if (this.autoComplete && noOptions) status = false;
fed3e09d   梁灏   add AutoComplete ...
296
  
ec98f3c3   梁灏   update Select
297
                  return this.visible && status;
29264399   梁灏   update Select
298
              },
c9b86944   Sergio Crisostomo   Refactor Select!
299
300
              showNotFoundLabel () {
                  const {loading, remote, selectOptions} = this;
bc348e7e   Sergio Crisostomo   adapt to auto-com...
301
                  return selectOptions && selectOptions.length === 0 && (!remote || (remote && !loading));
e355dd49   梁灏   add Select Component
302
              },
c9b86944   Sergio Crisostomo   Refactor Select!
303
304
305
              publicValue(){
                  if (this.labelInValue){
                      return this.multiple ? this.values : this.values[0];
e355dd49   梁灏   add Select Component
306
                  } else {
c9b86944   Sergio Crisostomo   Refactor Select!
307
                      return this.multiple ? this.values.map(option => option.value) : (this.values[0] || {}).value;
e355dd49   梁灏   add Select Component
308
309
                  }
              },
c9b86944   Sergio Crisostomo   Refactor Select!
310
311
312
313
314
315
316
              canBeCleared(){
                  const uiStateMatch = this.hasMouseHoverHead || this.active;
                  const qualifiesForClear = !this.multiple && this.clearable;
                  return uiStateMatch && qualifiesForClear && this.reset; // we return a function
              },
              selectOptions() {
                  const selectOptions = [];
06a74f9e   Sergio Crisostomo   Allow select to n...
317
                  const slotOptions = (this.slotOptions || []);
c9b86944   Sergio Crisostomo   Refactor Select!
318
319
                  let optionCounter = -1;
                  const currentIndex = this.focusIndex;
220161f5   Sergio Crisostomo   Clean up empty/nu...
320
                  const selectedValues = this.values.filter(Boolean).map(({value}) => value);
06a74f9e   Sergio Crisostomo   Allow select to n...
321
322
323
324
325
326
327
328
329
330
                  if (this.autoComplete) {
                      const copyChildren = (node, fn) => {
                          return {
                              ...node,
                              children: (node.children || []).map(fn).map(child => copyChildren(child, fn))
                          };
                      };
                      const autoCompleteOptions = extractOptions(slotOptions);
                      const selectedSlotOption = autoCompleteOptions[currentIndex];
  
aa21cdf9   Sergio Crisostomo   Process also shal...
331
332
333
334
335
336
337
                      return slotOptions.map(node => {
                          if (node === selectedSlotOption) return applyProp(node, 'isFocused', true);
                          return copyChildren(node, (child) => {
                              if (child !== selectedSlotOption) return child;
                              return applyProp(child, 'isFocused', true);
                          });
                      });
06a74f9e   Sergio Crisostomo   Allow select to n...
338
                  }
bc348e7e   Sergio Crisostomo   adapt to auto-com...
339
  
06a74f9e   Sergio Crisostomo   Allow select to n...
340
                  for (let option of slotOptions) {
e355dd49   梁灏   add Select Component
341
  
b6c069ca   Sergio Crisostomo   reset query if op...
342
343
                      const cOptions = option.componentOptions;
                      if (!cOptions) continue;
b6c069ca   Sergio Crisostomo   reset query if op...
344
345
                      if (cOptions.tag.match(optionGroupRegexp)){
                          let children = cOptions.children;
e355dd49   梁灏   add Select Component
346
  
c9b86944   Sergio Crisostomo   Refactor Select!
347
348
349
350
351
352
                          // remove filtered children
                          if (this.filterable){
                              children = children.filter(
                                  ({componentOptions}) => this.validateOption(componentOptions)
                              );
                          }
e355dd49   梁灏   add Select Component
353
  
b6c069ca   Sergio Crisostomo   reset query if op...
354
                          cOptions.children = children.map(opt => {
c9b86944   Sergio Crisostomo   Refactor Select!
355
356
357
                              optionCounter = optionCounter + 1;
                              return this.processOption(opt, selectedValues, optionCounter === currentIndex);
                          });
3e855e34   梁灏   fixed #46
358
  
c9b86944   Sergio Crisostomo   Refactor Select!
359
                          // keep the group if it still has children
b6c069ca   Sergio Crisostomo   reset query if op...
360
                          if (cOptions.children.length > 0) selectOptions.push({...option});
c9b86944   Sergio Crisostomo   Refactor Select!
361
362
                      } else {
                          // ignore option if not passing filter
b6c069ca   Sergio Crisostomo   reset query if op...
363
                          const optionPassesFilter = this.filterable ? this.validateOption(cOptions) : option;
c9b86944   Sergio Crisostomo   Refactor Select!
364
                          if (!optionPassesFilter) continue;
3e855e34   梁灏   fixed #46
365
  
c9b86944   Sergio Crisostomo   Refactor Select!
366
367
                          optionCounter = optionCounter + 1;
                          selectOptions.push(this.processOption(option, selectedValues, optionCounter === currentIndex));
3e855e34   梁灏   fixed #46
368
                      }
e355dd49   梁灏   add Select Component
369
370
                  }
  
c9b86944   Sergio Crisostomo   Refactor Select!
371
                  return selectOptions;
e355dd49   梁灏   add Select Component
372
              },
c9b86944   Sergio Crisostomo   Refactor Select!
373
              flatOptions(){
06a74f9e   Sergio Crisostomo   Allow select to n...
374
                  return extractOptions(this.selectOptions);
c9b86944   Sergio Crisostomo   Refactor Select!
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
              },
              selectTabindex(){
                  return this.disabled || this.filterable ? -1 : 0;
              },
              remote(){
                  return typeof this.remoteMethod === 'function';
              }
          },
          methods: {
              setQuery(query){ // PUBLIC API
                  if (query) {
                      this.onQueryChange(query);
                      return;
                  }
                  if (query === null) {
                      this.onQueryChange('');
                      this.values = [];
e355dd49   梁灏   add Select Component
392
393
                  }
              },
c9b86944   Sergio Crisostomo   Refactor Select!
394
              clearSingleSelect(){ // PUBLIC API
7dbde804   Sergio Crisostomo   add on-clear event
395
                  this.$emit('on-clear');
c3304bce   Sergio Crisostomo   correct unchanged...
396
                  this.hideMenu();
c9b86944   Sergio Crisostomo   Refactor Select!
397
398
399
400
                  if (this.clearable) this.values = [];
              },
              getOptionData(value){
                  const option = this.flatOptions.find(({componentOptions}) => componentOptions.propsData.value === value);
7f63e58c   Sergio Crisostomo   Make possible for...
401
                  if (!option) return null;
c9b86944   Sergio Crisostomo   Refactor Select!
402
403
404
405
406
407
408
409
410
411
412
                  const textContent = option.componentOptions.children.reduce((str, child) => str + (child.text || ''), '');
                  const label = option.componentOptions.propsData.label || textContent || '';
                  return {
                      value: value,
                      label: label,
                  };
              },
              getInitialValue(){
                  const {multiple, value} = this;
                  let initialValue = Array.isArray(value) ? value : [value];
                  if (!multiple && (typeof initialValue[0] === 'undefined' || String(initialValue[0]).trim() === '')) initialValue = [];
220161f5   Sergio Crisostomo   Clean up empty/nu...
413
                  return initialValue.filter(Boolean);
c9b86944   Sergio Crisostomo   Refactor Select!
414
415
416
417
418
419
420
421
422
423
424
425
426
              },
              processOption(option, values, isFocused){
                  if (!option.componentOptions) return option;
                  const optionValue = option.componentOptions.propsData.value;
                  const disabled = option.componentOptions.propsData.disabled;
                  const isSelected = values.includes(optionValue);
  
                  const propsData = {
                      ...option.componentOptions.propsData,
                      selected: isSelected,
                      isFocused: isFocused,
                      disabled: typeof disabled === 'undefined' ? false : disabled !== false,
                  };
e355dd49   梁灏   add Select Component
427
  
c9b86944   Sergio Crisostomo   Refactor Select!
428
429
430
431
432
                  return {
                      ...option,
                      componentOptions: {
                          ...option.componentOptions,
                          propsData: propsData
e355dd49   梁灏   add Select Component
433
                      }
c9b86944   Sergio Crisostomo   Refactor Select!
434
435
                  };
              },
e355dd49   梁灏   add Select Component
436
  
c9b86944   Sergio Crisostomo   Refactor Select!
437
              validateOption({elm, propsData}){
31788df3   Sergio Crisostomo   Normalise behavio...
438
                  if (this.queryStringMatchesSelectedOption) return true;
c9b86944   Sergio Crisostomo   Refactor Select!
439
440
441
442
                  const value = propsData.value;
                  const label = propsData.label || '';
                  const textContent = elm && elm.textContent || '';
                  const stringValues = JSON.stringify([value, label, textContent]);
5266c905   Sergio Crisostomo   Trim label so we ...
443
                  const query = this.query.toLowerCase().trim();
c3304bce   Sergio Crisostomo   correct unchanged...
444
                  return stringValues.toLowerCase().includes(query);
e355dd49   梁灏   add Select Component
445
              },
d87ce40a   梁灏   update Select
446
  
c9b86944   Sergio Crisostomo   Refactor Select!
447
448
449
              toggleMenu (e, force) {
                  if (this.disabled || this.autoComplete) {
                      return false;
d87ce40a   梁灏   update Select
450
                  }
d87ce40a   梁灏   update Select
451
  
c9b86944   Sergio Crisostomo   Refactor Select!
452
453
454
                  this.visible = typeof force !== 'undefined' ? force : !this.visible;
                  if (this.visible){
                      this.dropDownWidth = this.$el.getBoundingClientRect().width;
cf753854   Sergio Crisostomo   Corrections after...
455
                      this.broadcast('Drop', 'on-update-popper');
e4ce9917   梁灏   update Select com...
456
                  }
e355dd49   梁灏   add Select Component
457
              },
c9b86944   Sergio Crisostomo   Refactor Select!
458
459
              hideMenu () {
                  this.toggleMenu(null, false);
31788df3   Sergio Crisostomo   Normalise behavio...
460
                  setTimeout(() => this.unchangedQuery = true, ANIMATION_TIMEOUT);
e355dd49   梁灏   add Select Component
461
              },
c9b86944   Sergio Crisostomo   Refactor Select!
462
463
464
465
              onClickOutside(event){
                  if (this.visible) {
  
                      if (this.filterable) {
ae7579e9   Sergio Crisostomo   Fix input getters...
466
                          const input = this.$el.querySelector('input[type="text"]');
c9b86944   Sergio Crisostomo   Refactor Select!
467
468
469
470
                          this.caretPosition = input.selectionStart;
                          this.$nextTick(() => {
                              const caretPosition = this.caretPosition === -1 ? input.value.length : this.caretPosition;
                              input.setSelectionRange(caretPosition, caretPosition);
e355dd49   梁灏   add Select Component
471
472
473
                          });
                      }
  
ae7579e9   Sergio Crisostomo   Fix input getters...
474
                      if (!this.autoComplete) event.stopPropagation();
c9b86944   Sergio Crisostomo   Refactor Select!
475
476
477
478
479
480
                      event.preventDefault();
                      this.hideMenu();
                      this.isFocused = true;
                  } else {
                      this.caretPosition = -1;
                      this.isFocused = false;
e355dd49   梁灏   add Select Component
481
482
                  }
              },
c9b86944   Sergio Crisostomo   Refactor Select!
483
              reset(){
31788df3   Sergio Crisostomo   Normalise behavio...
484
                  this.unchangedQuery = true;
c9b86944   Sergio Crisostomo   Refactor Select!
485
                  this.values = [];
e355dd49   梁灏   add Select Component
486
487
              },
              handleKeydown (e) {
c9b86944   Sergio Crisostomo   Refactor Select!
488
489
490
491
                  if (e.key === 'Backspace'){
                      return; // so we don't call preventDefault
                  }
  
e355dd49   梁灏   add Select Component
492
                  if (this.visible) {
c9b86944   Sergio Crisostomo   Refactor Select!
493
494
495
496
497
                      e.preventDefault();
                      if (e.key === 'Tab'){
                          e.stopPropagation();
                      }
  
e355dd49   梁灏   add Select Component
498
                      // Esc slide-up
c9b86944   Sergio Crisostomo   Refactor Select!
499
                      if (e.key === 'Escape') {
16fc6361   Sergio Crisostomo   stop propagation ...
500
                          e.stopPropagation();
e355dd49   梁灏   add Select Component
501
502
503
                          this.hideMenu();
                      }
                      // next
c9b86944   Sergio Crisostomo   Refactor Select!
504
505
                      if (e.key === 'ArrowUp') {
                          this.navigateOptions(-1);
e355dd49   梁灏   add Select Component
506
507
                      }
                      // prev
c9b86944   Sergio Crisostomo   Refactor Select!
508
509
                      if (e.key === 'ArrowDown') {
                          this.navigateOptions(1);
e355dd49   梁灏   add Select Component
510
511
                      }
                      // enter
7e3fc4a5   Sergio Crisostomo   close the menu if...
512
513
                      if (e.key === 'Enter') {
                          if (this.focusIndex === -1) return this.hideMenu();
c9b86944   Sergio Crisostomo   Refactor Select!
514
515
516
                          const optionComponent = this.flatOptions[this.focusIndex];
                          const option = this.getOptionData(optionComponent.componentOptions.propsData.value);
                          this.onOptionClick(option);
e355dd49   梁灏   add Select Component
517
                      }
c9b86944   Sergio Crisostomo   Refactor Select!
518
519
520
                  } else {
                      const keysThatCanOpenSelect = ['ArrowUp', 'ArrowDown'];
                      if (keysThatCanOpenSelect.includes(e.key)) this.toggleMenu(null, true);
e355dd49   梁灏   add Select Component
521
522
                  }
  
e355dd49   梁灏   add Select Component
523
  
c9b86944   Sergio Crisostomo   Refactor Select!
524
525
526
              },
              navigateOptions(direction){
                  const optionsLength = this.flatOptions.length - 1;
e4ebd304   梁灏   update Select com...
527
  
c9b86944   Sergio Crisostomo   Refactor Select!
528
529
530
                  let index = this.focusIndex + direction;
                  if (index < 0) index = optionsLength;
                  if (index > optionsLength) index = 0;
e355dd49   梁灏   add Select Component
531
  
c9b86944   Sergio Crisostomo   Refactor Select!
532
533
534
535
536
537
538
                  // find nearest option in case of disabled options in between
                  if (direction > 0){
                      let nearestActiveOption = -1;
                      for (let i = 0; i < this.flatOptions.length; i++){
                          const optionIsActive = !this.flatOptions[i].componentOptions.propsData.disabled;
                          if (optionIsActive) nearestActiveOption = i;
                          if (nearestActiveOption >= index) break;
e355dd49   梁灏   add Select Component
539
                      }
c9b86944   Sergio Crisostomo   Refactor Select!
540
541
542
543
544
545
546
                      index = nearestActiveOption;
                  } else {
                      let nearestActiveOption = this.flatOptions.length;
                      for (let i = optionsLength; i >= 0; i--){
                          const optionIsActive = !this.flatOptions[i].componentOptions.propsData.disabled;
                          if (optionIsActive) nearestActiveOption = i;
                          if (nearestActiveOption <= index) break;
e4ebd304   梁灏   update Select com...
547
                      }
c9b86944   Sergio Crisostomo   Refactor Select!
548
                      index = nearestActiveOption;
e355dd49   梁灏   add Select Component
549
                  }
e355dd49   梁灏   add Select Component
550
  
c9b86944   Sergio Crisostomo   Refactor Select!
551
                  this.focusIndex = index;
e4ebd304   梁灏   update Select com...
552
              },
c9b86944   Sergio Crisostomo   Refactor Select!
553
554
555
556
557
558
              onOptionClick(option) {
                  if (this.multiple){
  
                      // keep the query for remote select
                      if (this.remote) this.lastRemoteQuery = this.lastRemoteQuery || this.query;
                      else this.lastRemoteQuery = '';
e4ebd304   梁灏   update Select com...
559
  
c9b86944   Sergio Crisostomo   Refactor Select!
560
561
562
                      const valueIsSelected = this.values.find(({value}) => value === option.value);
                      if (valueIsSelected){
                          this.values = this.values.filter(({value}) => value !== option.value);
e4ebd304   梁灏   update Select com...
563
                      } else {
c9b86944   Sergio Crisostomo   Refactor Select!
564
                          this.values = this.values.concat(option);
e4ebd304   梁灏   update Select com...
565
                      }
c9b86944   Sergio Crisostomo   Refactor Select!
566
567
568
  
                      this.isFocused = true; // so we put back focus after clicking with mouse on option elements
                  } else {
5266c905   Sergio Crisostomo   Trim label so we ...
569
                      this.query = String(option.label).trim();
c9b86944   Sergio Crisostomo   Refactor Select!
570
571
572
573
574
                      this.values = [option];
                      this.lastRemoteQuery = '';
                      this.hideMenu();
                  }
  
52cfcd66   Sergio Crisostomo   Keep last selecte...
575
576
577
578
579
                  this.focusIndex = this.flatOptions.findIndex((opt) => {
                      if (!opt || !opt.componentOptions) return false;
                      return opt.componentOptions.propsData.value === option.value;
                  });
  
c9b86944   Sergio Crisostomo   Refactor Select!
580
                  if (this.filterable){
ae7579e9   Sergio Crisostomo   Fix input getters...
581
582
                      const inputField = this.$el.querySelector('input[type="text"]');
                      if (!this.autoComplete) this.$nextTick(() => inputField.focus());
e4ce9917   梁灏   update Select com...
583
                  }
88ef37f5   Aresn   fixed in multiple...
584
                  this.broadcast('Drop', 'on-update-popper');
3e855e34   梁灏   fixed #46
585
              },
c9b86944   Sergio Crisostomo   Refactor Select!
586
              onQueryChange(query) {
c3304bce   Sergio Crisostomo   correct unchanged...
587
                  if (query.length > 0 && query !== this.query) this.visible = true;
2f0b086d   梁灏   fixed #116
588
                  this.query = query;
c3304bce   Sergio Crisostomo   correct unchanged...
589
                  this.unchangedQuery = this.visible;
9c3a3e7d   YikaJ   更新 Select 组件
590
              },
c9b86944   Sergio Crisostomo   Refactor Select!
591
592
593
              toggleHeaderFocus({type}){
                  if (this.disabled) {
                      return;
15b72d31   梁灏   fixed #566
594
                  }
c9b86944   Sergio Crisostomo   Refactor Select!
595
                  this.isFocused = type === 'focus';
98bf25b3   梁灏   fixed #1286
596
              },
c9b86944   Sergio Crisostomo   Refactor Select!
597
598
              updateSlotOptions(){
                  this.slotOptions = this.$slots.default;
e355dd49   梁灏   add Select Component
599
600
              }
          },
e355dd49   梁灏   add Select Component
601
          watch: {
c9b86944   Sergio Crisostomo   Refactor Select!
602
603
604
605
606
              value(value){
                  const {getInitialValue, getOptionData, publicValue} = this;
  
                  if (value === '') this.values = [];
                  else if (JSON.stringify(value) !== JSON.stringify(publicValue)) {
220161f5   Sergio Crisostomo   Clean up empty/nu...
607
                      this.$nextTick(() => this.values = getInitialValue().map(getOptionData).filter(Boolean));
e355dd49   梁灏   add Select Component
608
                  }
c9b86944   Sergio Crisostomo   Refactor Select!
609
610
611
612
613
614
615
616
617
618
              },
              values(now, before){
                  const newValue = JSON.stringify(now);
                  const oldValue = JSON.stringify(before);
                  const shouldEmitInput = newValue !== oldValue;
  
                  if (shouldEmitInput) {
                      // v-model is always just the value, event with labelInValue === true
                      const vModelValue = this.labelInValue ?
                          (this.multiple ? this.publicValue.map(({value}) => value)
220161f5   Sergio Crisostomo   Clean up empty/nu...
619
620
                              :
                              this.publicValue.value) : this.publicValue;
c9b86944   Sergio Crisostomo   Refactor Select!
621
622
623
                      this.$emit('input', vModelValue); // to update v-model
                      this.$emit('on-change', this.publicValue);
                      this.dispatch('FormItem', 'on-form-change', this.publicValue);
219e5c92   梁灏   fixed #957
624
                  }
e355dd49   梁灏   add Select Component
625
              },
c9b86944   Sergio Crisostomo   Refactor Select!
626
627
628
629
              query (query) {
                  this.$emit('on-query-change', query);
                  const {remoteMethod, lastRemoteQuery} = this;
                  const hasValidQuery = query !== '' && (query !== lastRemoteQuery || !lastRemoteQuery);
45bcc14d   Sergio Crisostomo   prevent calling r...
630
631
                  const shouldCallRemoteMethod = remoteMethod && hasValidQuery && !this.preventRemoteCall;
                  this.preventRemoteCall = false; // remove the flag
c9b86944   Sergio Crisostomo   Refactor Select!
632
633
634
635
636
637
638
639
640
  
                  if (shouldCallRemoteMethod){
                      this.focusIndex = -1;
                      const promise = this.remoteMethod(query);
                      this.initialLabel = '';
                      if (promise && promise.then){
                          promise.then(options => {
                              if (options) this.options = options;
                          });
b7cf983e   梁灏   update Select com...
641
                      }
e355dd49   梁灏   add Select Component
642
                  }
c9b86944   Sergio Crisostomo   Refactor Select!
643
                  if (query !== '' && this.remote) this.lastRemoteQuery = query;
e4ebd304   梁灏   update Select com...
644
              },
c9b86944   Sergio Crisostomo   Refactor Select!
645
646
647
648
649
650
              loading(state){
                  if (state === false){
                      this.updateSlotOptions();
                  }
              },
              isFocused(focused){
ae7579e9   Sergio Crisostomo   Fix input getters...
651
                  const el = this.filterable ? this.$el.querySelector('input[type="text"]') : this.$el;
c9b86944   Sergio Crisostomo   Refactor Select!
652
                  el[this.isFocused ? 'focus' : 'blur']();
d8bb1771   windywany   let select compon...
653
  
c9b86944   Sergio Crisostomo   Refactor Select!
654
655
656
                  // restore query value in filterable single selects
                  const [selectedOption] = this.values;
                  if (selectedOption && this.filterable && !this.multiple && !focused){
5266c905   Sergio Crisostomo   Trim label so we ...
657
                      const selectedLabel = String(selectedOption.label || selectedOption.value).trim();
9ca6671c   Sergio Crisostomo   Check for selecte...
658
                      if (selectedLabel && this.query !== selectedLabel) {
45bcc14d   Sergio Crisostomo   prevent calling r...
659
660
661
                          this.preventRemoteCall = true;
                          this.query = selectedLabel;
                      }
c9b86944   Sergio Crisostomo   Refactor Select!
662
663
664
                  }
              },
              focusIndex(index){
06a74f9e   Sergio Crisostomo   Allow select to n...
665
                  if (index < 0 || this.autoComplete) return;
c9b86944   Sergio Crisostomo   Refactor Select!
666
667
668
669
670
                  // update scroll
                  const optionValue = this.flatOptions[index].componentOptions.propsData.value;
                  const optionInstance = findChild(this, ({$options}) => {
                      return $options.componentName === 'select-item' && $options.propsData.value === optionValue;
                  });
e4ce9917   梁灏   update Select com...
671
  
c9b86944   Sergio Crisostomo   Refactor Select!
672
673
674
675
676
677
678
                  let bottomOverflowDistance = optionInstance.$el.getBoundingClientRect().bottom - this.$refs.dropdown.$el.getBoundingClientRect().bottom;
                  let topOverflowDistance = optionInstance.$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;
01b54e30   梁灏   Select support re...
679
                  }
cf753854   Sergio Crisostomo   Corrections after...
680
681
682
              },
              dropVisible(open){
                  this.broadcast('Drop', open ? 'on-update-popper' : 'on-destroy-popper');
7f63e58c   Sergio Crisostomo   Make possible for...
683
              },
f7f65c84   Sergio Crisostomo   reset query only ...
684
              selectOptions(){
7f63e58c   Sergio Crisostomo   Make possible for...
685
                  if (this.hasExpectedValue){
220161f5   Sergio Crisostomo   Clean up empty/nu...
686
                      this.values = this.values.map(this.getOptionData).filter(Boolean);
7f63e58c   Sergio Crisostomo   Make possible for...
687
688
                      this.hasExpectedValue = false;
                  }
b6c069ca   Sergio Crisostomo   reset query if op...
689
  
f7f65c84   Sergio Crisostomo   reset query only ...
690
                  if (this.slotOptions && this.slotOptions.length === 0){
b6c069ca   Sergio Crisostomo   reset query if op...
691
692
                      this.query = '';
                  }
1376a01a   Sergio Crisostomo   Emit on-open-chan...
693
694
695
              },
              visible(state){
                  this.$emit('on-open-change', state);
e355dd49   梁灏   add Select Component
696
697
              }
          }
b0893113   jingsam   :art: add eslint
698
      };
d6342fe1   jingsam   fixed ie bug
699
  </script>