Blame view

src/components/cascader/cascader.vue 15.2 KB
0a48ac45   梁灏   Input add readonl...
1
  <template>
c463ab87   梁灏   add Cascader
2
      <div :class="classes" v-clickoutside="handleClose">
a9cd43b3   梁灏   Cascader add tran...
3
          <div :class="[prefixCls + '-rel']" @click="toggleOpen" ref="reference">
0460a1e8   梁灏   fixed #812
4
              <input type="hidden" :name="name" :value="currentValue">
75e5c6a5   梁灏   Cascader support ...
5
6
              <slot>
                  <i-input
acb79ba3   梁灏   fixed #433
7
                      :element-id="elementId"
3ae11e85   梁灏   update Cascader
8
                      ref="input"
5a9a12cd   梁灏   update Cascader
9
                      :readonly="!filterable"
75e5c6a5   梁灏   Cascader support ...
10
                      :disabled="disabled"
3ae11e85   梁灏   update Cascader
11
                      :value="displayInputRender"
7ec0b533   梁灏   Cascader support ...
12
                      @on-change="handleInput"
75e5c6a5   梁灏   Cascader support ...
13
                      :size="size"
3ae11e85   梁灏   update Cascader
14
15
16
17
18
                      :placeholder="inputPlaceholder"></i-input>
                  <div
                      :class="[prefixCls + '-label']"
                      v-show="filterable && query === ''"
                      @click="handleFocus">{{ displayRender }}</div>
47a7f21d   梁灏   support Cascader
19
                  <Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.native.stop="clearSelect"></Icon>
75e5c6a5   梁灏   Cascader support ...
20
21
22
                  <Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
              </slot>
          </div>
e09b07b7   huanghong   解决drop弹出动画异常
23
          <transition name="transition-drop">
a9cd43b3   梁灏   Cascader add tran...
24
25
26
27
28
29
              <Drop
                  v-show="visible"
                  :class="{ [prefixCls + '-transfer']: transfer }"
                  ref="drop"
                  :data-transfer="transfer"
                  v-transfer-dom>
47a7f21d   梁灏   support Cascader
30
31
                  <div>
                      <Caspanel
7ec0b533   梁灏   Cascader support ...
32
                          v-show="!filterable || (filterable && query === '')"
47a7f21d   梁灏   support Cascader
33
34
35
36
37
38
                          ref="caspanel"
                          :prefix-cls="prefixCls"
                          :data="data"
                          :disabled="disabled"
                          :change-on-select="changeOnSelect"
                          :trigger="trigger"></Caspanel>
952ddb51   梁灏   update Cascader
39
                      <div :class="[prefixCls + '-dropdown']" v-show="filterable && query !== '' && querySelections.length">
7ec0b533   梁灏   Cascader support ...
40
41
42
43
44
45
                          <ul :class="[selectPrefixCls + '-dropdown-list']">
                              <li
                                  :class="[selectPrefixCls + '-item', {
                                      [selectPrefixCls + '-item-disabled']: item.disabled
                                  }]"
                                  v-for="(item, index) in querySelections"
4e23e47c   梁灏   update Cascader
46
                                  @click="handleSelectItem(index)" v-html="item.display"></li>
7ec0b533   梁灏   Cascader support ...
47
48
                          </ul>
                      </div>
952ddb51   梁灏   update Cascader
49
                      <ul v-show="filterable && query !== '' && !querySelections.length" :class="[prefixCls + '-not-found-tip']"><li>{{ localeNotFoundText }}</li></ul>
47a7f21d   梁灏   support Cascader
50
51
52
                  </div>
              </Drop>
          </transition>
6ff31952   梁灏   optimize Input sh...
53
      </div>
0a48ac45   梁灏   Input add readonl...
54
55
  </template>
  <script>
6ff31952   梁灏   optimize Input sh...
56
      import iInput from '../input/input.vue';
47a7f21d   梁灏   support Cascader
57
      import Drop from '../select/dropdown.vue';
c463ab87   梁灏   add Cascader
58
59
      import Icon from '../icon/icon.vue';
      import Caspanel from './caspanel.vue';
6ff31952   梁灏   optimize Input sh...
60
      import clickoutside from '../../directives/clickoutside';
a9cd43b3   梁灏   Cascader add tran...
61
      import TransferDom from '../../directives/transfer-dom';
c463ab87   梁灏   add Cascader
62
      import { oneOf } from '../../utils/assist';
47a7f21d   梁灏   support Cascader
63
      import Emitter from '../../mixins/emitter';
3ae11e85   梁灏   update Cascader
64
      import Locale from '../../mixins/locale';
6ff31952   梁灏   optimize Input sh...
65
66
  
      const prefixCls = 'ivu-cascader';
7ec0b533   梁灏   Cascader support ...
67
      const selectPrefixCls = 'ivu-select';
6ff31952   梁灏   optimize Input sh...
68
  
0a48ac45   梁灏   Input add readonl...
69
      export default {
34ee7b4a   梁灏   support Tree & ad...
70
          name: 'Cascader',
3ae11e85   梁灏   update Cascader
71
          mixins: [ Emitter, Locale ],
47a7f21d   梁灏   support Cascader
72
          components: { iInput, Drop, Icon, Caspanel },
a9cd43b3   梁灏   Cascader add tran...
73
          directives: { clickoutside, TransferDom },
0a48ac45   梁灏   Input add readonl...
74
          props: {
6ff31952   梁灏   optimize Input sh...
75
76
77
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
78
                      return [];
6ff31952   梁灏   optimize Input sh...
79
80
81
                  }
              },
              value: {
9ec927b1   梁灏   update Cascader
82
83
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
84
                      return [];
9ec927b1   梁灏   update Cascader
85
                  }
6ff31952   梁灏   optimize Input sh...
86
87
88
89
90
91
92
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              clearable: {
                  type: Boolean,
c463ab87   梁灏   add Cascader
93
                  default: true
6ff31952   梁灏   optimize Input sh...
94
95
              },
              placeholder: {
3ae11e85   梁灏   update Cascader
96
                  type: String
6ff31952   梁灏   optimize Input sh...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large']);
                  }
              },
              trigger: {
                  validator (value) {
                      return oneOf(value, ['click', 'hover']);
                  },
                  default: 'click'
              },
              changeOnSelect: {
                  type: Boolean,
                  default: false
              },
              renderFormat: {
                  type: Function,
05b5dd7b   梁灏   update Cascader
115
                  default (label) {
bd4e3b9b   梁灏   update Cascader
116
                      return label.join(' / ');
6ff31952   梁灏   optimize Input sh...
117
                  }
f7ffdac5   梁灏   Cascader support ...
118
119
120
              },
              loadData: {
                  type: Function
5a9a12cd   梁灏   update Cascader
121
122
123
124
              },
              filterable: {
                  type: Boolean,
                  default: false
952ddb51   梁灏   update Cascader
125
126
127
              },
              notFoundText: {
                  type: String
a9cd43b3   梁灏   Cascader add tran...
128
129
130
131
              },
              transfer: {
                  type: Boolean,
                  default: false
0460a1e8   梁灏   fixed #812
132
133
134
              },
              name: {
                  type: String
acb79ba3   梁灏   fixed #433
135
136
137
              },
              elementId: {
                  type: String
6ff31952   梁灏   optimize Input sh...
138
              }
0a48ac45   梁灏   Input add readonl...
139
140
141
          },
          data () {
              return {
6ff31952   梁灏   optimize Input sh...
142
                  prefixCls: prefixCls,
7ec0b533   梁灏   Cascader support ...
143
                  selectPrefixCls: selectPrefixCls,
c463ab87   梁灏   add Cascader
144
145
                  visible: false,
                  selected: [],
2810d8c7   梁灏   fixed #183
146
                  tmpSelected: [],
47a7f21d   梁灏   support Cascader
147
                  updatingValue: false,    // to fix set value in changeOnSelect type
7ec0b533   梁灏   Cascader support ...
148
                  currentValue: this.value,
933afc7a   梁灏   fixed #950
149
150
151
                  query: '',
                  validDataStr: '',
                  isLoadedChildren: false    // #950
b0893113   jingsam   :art: add eslint
152
              };
0a48ac45   梁灏   Input add readonl...
153
154
          },
          computed: {
c463ab87   梁灏   add Cascader
155
156
157
158
              classes () {
                  return [
                      `${prefixCls}`,
                      {
165bb7c9   梁灏   update Cascader
159
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
3ae11e85   梁灏   update Cascader
160
                          [`${prefixCls}-size-${this.size}`]: !!this.size,
05b5dd7b   梁灏   update Cascader
161
                          [`${prefixCls}-visible`]: this.visible,
952ddb51   梁灏   update Cascader
162
163
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-not-found`]: this.filterable && this.query !== '' && !this.querySelections.length
c463ab87   梁灏   add Cascader
164
                      }
b0893113   jingsam   :art: add eslint
165
                  ];
c463ab87   梁灏   add Cascader
166
167
              },
              showCloseIcon () {
65b41a2d   梁灏   fixed #635
168
                  return this.currentValue && this.currentValue.length && this.clearable && !this.disabled;
c463ab87   梁灏   add Cascader
169
              },
6ff31952   梁灏   optimize Input sh...
170
171
172
173
174
175
              displayRender () {
                  let label = [];
                  for (let i = 0; i < this.selected.length; i++) {
                      label.push(this.selected[i].label);
                  }
  
165bb7c9   梁灏   update Cascader
176
                  return this.renderFormat(label, this.selected);
7ec0b533   梁灏   Cascader support ...
177
              },
3ae11e85   梁灏   update Cascader
178
179
180
181
182
183
184
185
186
187
188
189
190
              displayInputRender () {
                  return this.filterable ? '' : this.displayRender;
              },
              localePlaceholder () {
                  if (this.placeholder === undefined) {
                      return this.t('i.select.placeholder');
                  } else {
                      return this.placeholder;
                  }
              },
              inputPlaceholder () {
                  return this.filterable && this.currentValue.length ? null : this.localePlaceholder;
              },
952ddb51   梁灏   update Cascader
191
192
193
194
195
196
197
              localeNotFoundText () {
                  if (this.notFoundText === undefined) {
                      return this.t('i.select.noMatch');
                  } else {
                      return this.notFoundText;
                  }
              },
7ec0b533   梁灏   Cascader support ...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
              querySelections () {
                  let selections = [];
                  function getSelections (arr, label, value) {
                      for (let i = 0; i < arr.length; i++) {
                          let item = arr[i];
                          item.__label = label ? label + ' / ' + item.label : item.label;
                          item.__value = value ? value + ',' + item.value : item.value;
  
                          if (item.children && item.children.length) {
                              getSelections(item.children, item.__label, item.__value);
                              delete item.__label;
                              delete item.__value;
                          } else {
                              selections.push({
                                  label: item.__label,
                                  value: item.__value,
4e23e47c   梁灏   update Cascader
214
                                  display: item.__label,
7ec0b533   梁灏   Cascader support ...
215
216
217
218
219
220
221
                                  item: item,
                                  disabled: !!item.disabled
                              });
                          }
                      }
                  }
                  getSelections(this.data);
f7caa832   GLChen   Update cascader.vue
222
                  selections = selections.filter(item => {
3288b530   梁灏   update
223
                      return item.label ? item.label.indexOf(this.query) > -1 : false;
f7caa832   GLChen   Update cascader.vue
224
                  }).map(item => {
4e23e47c   梁灏   update Cascader
225
226
227
                      item.display = item.display.replace(new RegExp(this.query, 'g'), `<span>${this.query}</span>`);
                      return item;
                  });
7ec0b533   梁灏   Cascader support ...
228
                  return selections;
6ff31952   梁灏   optimize Input sh...
229
              }
0a48ac45   梁灏   Input add readonl...
230
231
          },
          methods: {
c463ab87   梁灏   add Cascader
232
              clearSelect () {
65b41a2d   梁灏   fixed #635
233
                  if (this.disabled) return false;
47a7f21d   梁灏   support Cascader
234
235
                  const oldVal = JSON.stringify(this.currentValue);
                  this.currentValue = this.selected = this.tmpSelected = [];
165bb7c9   梁灏   update Cascader
236
                  this.handleClose();
47a7f21d   梁灏   support Cascader
237
238
239
                  this.emitValue(this.currentValue, oldVal);
  //                this.$broadcast('on-clear');
                  this.broadcast('Caspanel', 'on-clear');
c463ab87   梁灏   add Cascader
240
241
242
243
              },
              handleClose () {
                  this.visible = false;
              },
75e5c6a5   梁灏   Cascader support ...
244
              toggleOpen () {
2aa0aa6e   Rijn   fix bug: cascader...
245
                  if (this.disabled) return false;
75e5c6a5   梁灏   Cascader support ...
246
                  if (this.visible) {
7ec0b533   梁灏   Cascader support ...
247
                      if (!this.filterable) this.handleClose();
75e5c6a5   梁灏   Cascader support ...
248
249
250
251
                  } else {
                      this.onFocus();
                  }
              },
c463ab87   梁灏   add Cascader
252
253
              onFocus () {
                  this.visible = true;
47a7f21d   梁灏   support Cascader
254
255
                  if (!this.currentValue.length) {
                      this.broadcast('Caspanel', 'on-clear');
165bb7c9   梁灏   update Cascader
256
                  }
c463ab87   梁灏   add Cascader
257
258
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
259
260
                  this.tmpSelected = result;
              },
5dc44ccf   梁灏   fixed #2793
261
262
263
              updateSelected (init = false, changeOnSelectDataChange = false) {
                  // #2793 changeOnSelectDataChange used for changeOnSelect when data changed and set value
                  if (!this.changeOnSelect || init || changeOnSelectDataChange) {
47a7f21d   梁灏   support Cascader
264
265
266
                      this.broadcast('Caspanel', 'on-find-selected', {
                          value: this.currentValue
                      });
165bb7c9   梁灏   update Cascader
267
268
269
270
                  }
              },
              emitValue (val, oldVal) {
                  if (JSON.stringify(val) !== oldVal) {
47a7f21d   梁灏   support Cascader
271
                      this.$emit('on-change', this.currentValue, JSON.parse(JSON.stringify(this.selected)));
cc419499   梁灏   fixed #525
272
273
274
275
276
                      this.$nextTick(() => {
                          this.dispatch('FormItem', 'on-form-change', {
                              value: this.currentValue,
                              selected: JSON.parse(JSON.stringify(this.selected))
                          });
cd78c9c4   梁灏   some comps suppor...
277
                      });
165bb7c9   梁灏   update Cascader
278
                  }
7ec0b533   梁灏   Cascader support ...
279
280
281
282
283
284
285
286
              },
              handleInput (event) {
                  this.query = event.target.value;
              },
              handleSelectItem (index) {
                  const item = this.querySelections[index];
  
                  if (item.item.disabled) return false;
7ec0b533   梁灏   Cascader support ...
287
                  this.query = '';
3ae11e85   梁灏   update Cascader
288
                  this.$refs.input.currentValue = '';
7ec0b533   梁灏   Cascader support ...
289
290
291
292
                  const oldVal = JSON.stringify(this.currentValue);
                  this.currentValue = item.value.split(',');
                  this.emitValue(this.currentValue, oldVal);
                  this.handleClose();
3ae11e85   梁灏   update Cascader
293
294
295
              },
              handleFocus () {
                  this.$refs.input.focus();
933afc7a   梁灏   fixed #950
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
              },
              // 排除 loading 后的 data,避免重复触发 updateSelect
              getValidData (data) {
                  function deleteData (item) {
                      const new_item = Object.assign({}, item);
                      if ('loading' in new_item) {
                          delete new_item.loading;
                      }
                      if ('__value' in new_item) {
                          delete new_item.__value;
                      }
                      if ('__label' in new_item) {
                          delete new_item.__label;
                      }
                      if ('children' in new_item && new_item.children.length) {
                          new_item.children = new_item.children.map(i => deleteData(i));
                      }
                      return new_item;
                  }
  
                  return data.map(item => deleteData(item));
c463ab87   梁灏   add Cascader
317
318
              }
          },
687c4562   梁灏   fixed #810
319
          created () {
933afc7a   梁灏   fixed #950
320
              this.validDataStr = JSON.stringify(this.getValidData(this.data));
47a7f21d   梁灏   support Cascader
321
322
323
324
325
326
327
              this.$on('on-result-change', (params) => {
                  // lastValue: is click the final val
                  // fromInit: is this emit from update value
                  const lastValue = params.lastValue;
                  const changeOnSelect = params.changeOnSelect;
                  const fromInit = params.fromInit;
  
bd4e3b9b   梁灏   update Cascader
328
                  if (lastValue || changeOnSelect) {
47a7f21d   梁灏   support Cascader
329
                      const oldVal = JSON.stringify(this.currentValue);
bd4e3b9b   梁灏   update Cascader
330
331
332
333
334
335
                      this.selected = this.tmpSelected;
  
                      let newVal = [];
                      this.selected.forEach((item) => {
                          newVal.push(item.value);
                      });
c463ab87   梁灏   add Cascader
336
  
165bb7c9   梁灏   update Cascader
337
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
338
                          this.updatingValue = true;
47a7f21d   梁灏   support Cascader
339
340
                          this.currentValue = newVal;
                          this.emitValue(this.currentValue, oldVal);
bd4e3b9b   梁灏   update Cascader
341
342
343
344
345
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
47a7f21d   梁灏   support Cascader
346
              });
bd4e3b9b   梁灏   update Cascader
347
          },
687c4562   梁灏   fixed #810
348
349
350
          mounted () {
              this.updateSelected(true);
          },
bd4e3b9b   梁灏   update Cascader
351
352
353
          watch: {
              visible (val) {
                  if (val) {
47a7f21d   梁灏   support Cascader
354
                      if (this.currentValue.length) {
bd4e3b9b   梁灏   update Cascader
355
356
                          this.updateSelected();
                      }
a9cd43b3   梁灏   Cascader add tran...
357
358
359
                      if (this.transfer) {
                          this.$refs.drop.update();
                      }
605bd2ae   huanghong   解决Cascader 下拉弹出位置问题
360
                      this.broadcast('Drop', 'on-update-popper');
3ae11e85   梁灏   update Cascader
361
362
363
364
365
                  } else {
                      if (this.filterable) {
                          this.query = '';
                          this.$refs.input.currentValue = '';
                      }
a9cd43b3   梁灏   Cascader add tran...
366
367
368
                      if (this.transfer) {
                          this.$refs.drop.destroy();
                      }
605bd2ae   huanghong   解决Cascader 下拉弹出位置问题
369
                      this.broadcast('Drop', 'on-destroy-popper');
bd4e3b9b   梁灏   update Cascader
370
                  }
bb1a3c38   梁灏   fixed #593
371
                  this.$emit('on-visible-change', val);
f46ebc38   梁灏   fixed #130
372
              },
47a7f21d   梁灏   support Cascader
373
374
              value (val) {
                  this.currentValue = val;
3bbb6b5f   梁灏   fixed #488
375
                  if (!val.length) this.selected = [];
47a7f21d   梁灏   support Cascader
376
377
378
              },
              currentValue () {
                  this.$emit('input', this.currentValue);
2810d8c7   梁灏   fixed #183
379
380
381
382
383
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
48af1359   梁灏   fixed #553
384
              },
f7ffdac5   梁灏   Cascader support ...
385
386
387
              data: {
                  deep: true,
                  handler () {
933afc7a   梁灏   fixed #950
388
389
390
391
                      const validDataStr = JSON.stringify(this.getValidData(this.data));
                      if (validDataStr !== this.validDataStr) {
                          this.validDataStr = validDataStr;
                          if (!this.isLoadedChildren) {
5dc44ccf   梁灏   fixed #2793
392
                              this.$nextTick(() => this.updateSelected(false, this.changeOnSelect));
933afc7a   梁灏   fixed #950
393
394
395
                          }
                          this.isLoadedChildren = false;
                      }
f7ffdac5   梁灏   Cascader support ...
396
                  }
bd4e3b9b   梁灏   update Cascader
397
              }
0a48ac45   梁灏   Input add readonl...
398
          }
b0893113   jingsam   :art: add eslint
399
400
      };
  </script>