Blame view

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