Blame view

src/components/cascader/cascader.vue 12.6 KB
0a48ac45   梁灏   Input add readonl...
1
  <template>
c463ab87   梁灏   add Cascader
2
      <div :class="classes" v-clickoutside="handleClose">
75e5c6a5   梁灏   Cascader support ...
3
4
5
          <div :class="[prefixCls + '-rel']" @click="toggleOpen">
              <slot>
                  <i-input
3ae11e85   梁灏   update Cascader
6
                      ref="input"
5a9a12cd   梁灏   update Cascader
7
                      :readonly="!filterable"
75e5c6a5   梁灏   Cascader support ...
8
                      :disabled="disabled"
3ae11e85   梁灏   update Cascader
9
                      :value="displayInputRender"
7ec0b533   梁灏   Cascader support ...
10
                      @on-change="handleInput"
75e5c6a5   梁灏   Cascader support ...
11
                      :size="size"
3ae11e85   梁灏   update Cascader
12
13
14
15
16
                      :placeholder="inputPlaceholder"></i-input>
                  <div
                      :class="[prefixCls + '-label']"
                      v-show="filterable && query === ''"
                      @click="handleFocus">{{ displayRender }}</div>
47a7f21d   梁灏   support Cascader
17
                  <Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.native.stop="clearSelect"></Icon>
75e5c6a5   梁灏   Cascader support ...
18
19
20
                  <Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
              </slot>
          </div>
47a7f21d   梁灏   support Cascader
21
22
23
24
          <transition name="slide-up">
              <Drop v-show="visible">
                  <div>
                      <Caspanel
7ec0b533   梁灏   Cascader support ...
25
                          v-show="!filterable || (filterable && query === '')"
47a7f21d   梁灏   support Cascader
26
27
28
29
30
31
                          ref="caspanel"
                          :prefix-cls="prefixCls"
                          :data="data"
                          :disabled="disabled"
                          :change-on-select="changeOnSelect"
                          :trigger="trigger"></Caspanel>
952ddb51   梁灏   update Cascader
32
                      <div :class="[prefixCls + '-dropdown']" v-show="filterable && query !== '' && querySelections.length">
7ec0b533   梁灏   Cascader support ...
33
34
35
36
37
38
                          <ul :class="[selectPrefixCls + '-dropdown-list']">
                              <li
                                  :class="[selectPrefixCls + '-item', {
                                      [selectPrefixCls + '-item-disabled']: item.disabled
                                  }]"
                                  v-for="(item, index) in querySelections"
4e23e47c   梁灏   update Cascader
39
                                  @click="handleSelectItem(index)" v-html="item.display"></li>
7ec0b533   梁灏   Cascader support ...
40
41
                          </ul>
                      </div>
952ddb51   梁灏   update Cascader
42
                      <ul v-show="filterable && query !== '' && !querySelections.length" :class="[prefixCls + '-not-found-tip']"><li>{{ localeNotFoundText }}</li></ul>
47a7f21d   梁灏   support Cascader
43
44
45
                  </div>
              </Drop>
          </transition>
6ff31952   梁灏   optimize Input sh...
46
      </div>
0a48ac45   梁灏   Input add readonl...
47
48
  </template>
  <script>
6ff31952   梁灏   optimize Input sh...
49
      import iInput from '../input/input.vue';
47a7f21d   梁灏   support Cascader
50
      import Drop from '../select/dropdown.vue';
c463ab87   梁灏   add Cascader
51
52
      import Icon from '../icon/icon.vue';
      import Caspanel from './caspanel.vue';
6ff31952   梁灏   optimize Input sh...
53
      import clickoutside from '../../directives/clickoutside';
c463ab87   梁灏   add Cascader
54
      import { oneOf } from '../../utils/assist';
47a7f21d   梁灏   support Cascader
55
      import Emitter from '../../mixins/emitter';
3ae11e85   梁灏   update Cascader
56
      import Locale from '../../mixins/locale';
6ff31952   梁灏   optimize Input sh...
57
58
  
      const prefixCls = 'ivu-cascader';
7ec0b533   梁灏   Cascader support ...
59
      const selectPrefixCls = 'ivu-select';
6ff31952   梁灏   optimize Input sh...
60
  
0a48ac45   梁灏   Input add readonl...
61
      export default {
34ee7b4a   梁灏   support Tree & ad...
62
          name: 'Cascader',
3ae11e85   梁灏   update Cascader
63
          mixins: [ Emitter, Locale ],
47a7f21d   梁灏   support Cascader
64
          components: { iInput, Drop, Icon, Caspanel },
c463ab87   梁灏   add Cascader
65
          directives: { clickoutside },
0a48ac45   梁灏   Input add readonl...
66
          props: {
6ff31952   梁灏   optimize Input sh...
67
68
69
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
70
                      return [];
6ff31952   梁灏   optimize Input sh...
71
72
73
                  }
              },
              value: {
9ec927b1   梁灏   update Cascader
74
75
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
76
                      return [];
9ec927b1   梁灏   update Cascader
77
                  }
6ff31952   梁灏   optimize Input sh...
78
79
80
81
82
83
84
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              clearable: {
                  type: Boolean,
c463ab87   梁灏   add Cascader
85
                  default: true
6ff31952   梁灏   optimize Input sh...
86
87
              },
              placeholder: {
3ae11e85   梁灏   update Cascader
88
                  type: String
6ff31952   梁灏   optimize Input sh...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
              },
              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
107
                  default (label) {
bd4e3b9b   梁灏   update Cascader
108
                      return label.join(' / ');
6ff31952   梁灏   optimize Input sh...
109
                  }
f7ffdac5   梁灏   Cascader support ...
110
111
112
              },
              loadData: {
                  type: Function
5a9a12cd   梁灏   update Cascader
113
114
115
116
              },
              filterable: {
                  type: Boolean,
                  default: false
952ddb51   梁灏   update Cascader
117
118
119
              },
              notFoundText: {
                  type: String
6ff31952   梁灏   optimize Input sh...
120
              }
0a48ac45   梁灏   Input add readonl...
121
122
123
          },
          data () {
              return {
6ff31952   梁灏   optimize Input sh...
124
                  prefixCls: prefixCls,
7ec0b533   梁灏   Cascader support ...
125
                  selectPrefixCls: selectPrefixCls,
c463ab87   梁灏   add Cascader
126
127
                  visible: false,
                  selected: [],
2810d8c7   梁灏   fixed #183
128
                  tmpSelected: [],
47a7f21d   梁灏   support Cascader
129
                  updatingValue: false,    // to fix set value in changeOnSelect type
7ec0b533   梁灏   Cascader support ...
130
131
                  currentValue: this.value,
                  query: ''
b0893113   jingsam   :art: add eslint
132
              };
0a48ac45   梁灏   Input add readonl...
133
134
          },
          computed: {
c463ab87   梁灏   add Cascader
135
136
137
138
              classes () {
                  return [
                      `${prefixCls}`,
                      {
165bb7c9   梁灏   update Cascader
139
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
3ae11e85   梁灏   update Cascader
140
                          [`${prefixCls}-size-${this.size}`]: !!this.size,
05b5dd7b   梁灏   update Cascader
141
                          [`${prefixCls}-visible`]: this.visible,
952ddb51   梁灏   update Cascader
142
143
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-not-found`]: this.filterable && this.query !== '' && !this.querySelections.length
c463ab87   梁灏   add Cascader
144
                      }
b0893113   jingsam   :art: add eslint
145
                  ];
c463ab87   梁灏   add Cascader
146
147
              },
              showCloseIcon () {
65b41a2d   梁灏   fixed #635
148
                  return this.currentValue && this.currentValue.length && this.clearable && !this.disabled;
c463ab87   梁灏   add Cascader
149
              },
6ff31952   梁灏   optimize Input sh...
150
151
152
153
154
155
              displayRender () {
                  let label = [];
                  for (let i = 0; i < this.selected.length; i++) {
                      label.push(this.selected[i].label);
                  }
  
165bb7c9   梁灏   update Cascader
156
                  return this.renderFormat(label, this.selected);
7ec0b533   梁灏   Cascader support ...
157
              },
3ae11e85   梁灏   update Cascader
158
159
160
161
162
163
164
165
166
167
168
169
170
              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
171
172
173
174
175
176
177
              localeNotFoundText () {
                  if (this.notFoundText === undefined) {
                      return this.t('i.select.noMatch');
                  } else {
                      return this.notFoundText;
                  }
              },
7ec0b533   梁灏   Cascader support ...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
              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
194
                                  display: item.__label,
7ec0b533   梁灏   Cascader support ...
195
196
197
198
199
200
201
                                  item: item,
                                  disabled: !!item.disabled
                              });
                          }
                      }
                  }
                  getSelections(this.data);
4e23e47c   梁灏   update Cascader
202
203
204
205
                  selections = selections.filter(item => item.label.indexOf(this.query) > -1).map(item => {
                      item.display = item.display.replace(new RegExp(this.query, 'g'), `<span>${this.query}</span>`);
                      return item;
                  });
7ec0b533   梁灏   Cascader support ...
206
                  return selections;
6ff31952   梁灏   optimize Input sh...
207
              }
0a48ac45   梁灏   Input add readonl...
208
209
          },
          methods: {
c463ab87   梁灏   add Cascader
210
              clearSelect () {
65b41a2d   梁灏   fixed #635
211
                  if (this.disabled) return false;
47a7f21d   梁灏   support Cascader
212
213
                  const oldVal = JSON.stringify(this.currentValue);
                  this.currentValue = this.selected = this.tmpSelected = [];
165bb7c9   梁灏   update Cascader
214
                  this.handleClose();
47a7f21d   梁灏   support Cascader
215
216
217
                  this.emitValue(this.currentValue, oldVal);
  //                this.$broadcast('on-clear');
                  this.broadcast('Caspanel', 'on-clear');
c463ab87   梁灏   add Cascader
218
219
220
221
              },
              handleClose () {
                  this.visible = false;
              },
75e5c6a5   梁灏   Cascader support ...
222
              toggleOpen () {
2aa0aa6e   Rijn   fix bug: cascader...
223
                  if (this.disabled) return false;
75e5c6a5   梁灏   Cascader support ...
224
                  if (this.visible) {
7ec0b533   梁灏   Cascader support ...
225
                      if (!this.filterable) this.handleClose();
75e5c6a5   梁灏   Cascader support ...
226
227
228
229
                  } else {
                      this.onFocus();
                  }
              },
c463ab87   梁灏   add Cascader
230
231
              onFocus () {
                  this.visible = true;
47a7f21d   梁灏   support Cascader
232
233
                  if (!this.currentValue.length) {
                      this.broadcast('Caspanel', 'on-clear');
165bb7c9   梁灏   update Cascader
234
                  }
c463ab87   梁灏   add Cascader
235
236
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
237
238
                  this.tmpSelected = result;
              },
165bb7c9   梁灏   update Cascader
239
240
              updateSelected (init = false) {
                  if (!this.changeOnSelect || init) {
47a7f21d   梁灏   support Cascader
241
242
243
                      this.broadcast('Caspanel', 'on-find-selected', {
                          value: this.currentValue
                      });
165bb7c9   梁灏   update Cascader
244
245
246
247
                  }
              },
              emitValue (val, oldVal) {
                  if (JSON.stringify(val) !== oldVal) {
47a7f21d   梁灏   support Cascader
248
                      this.$emit('on-change', this.currentValue, JSON.parse(JSON.stringify(this.selected)));
cc419499   梁灏   fixed #525
249
250
251
252
253
                      this.$nextTick(() => {
                          this.dispatch('FormItem', 'on-form-change', {
                              value: this.currentValue,
                              selected: JSON.parse(JSON.stringify(this.selected))
                          });
cd78c9c4   梁灏   some comps suppor...
254
                      });
165bb7c9   梁灏   update Cascader
255
                  }
7ec0b533   梁灏   Cascader support ...
256
257
258
259
260
261
262
263
              },
              handleInput (event) {
                  this.query = event.target.value;
              },
              handleSelectItem (index) {
                  const item = this.querySelections[index];
  
                  if (item.item.disabled) return false;
7ec0b533   梁灏   Cascader support ...
264
                  this.query = '';
3ae11e85   梁灏   update Cascader
265
                  this.$refs.input.currentValue = '';
7ec0b533   梁灏   Cascader support ...
266
267
268
269
                  const oldVal = JSON.stringify(this.currentValue);
                  this.currentValue = item.value.split(',');
                  this.emitValue(this.currentValue, oldVal);
                  this.handleClose();
3ae11e85   梁灏   update Cascader
270
271
272
              },
              handleFocus () {
                  this.$refs.input.focus();
c463ab87   梁灏   add Cascader
273
274
              }
          },
687c4562   梁灏   fixed #810
275
          created () {
47a7f21d   梁灏   support Cascader
276
277
278
279
280
281
282
              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
283
                  if (lastValue || changeOnSelect) {
47a7f21d   梁灏   support Cascader
284
                      const oldVal = JSON.stringify(this.currentValue);
bd4e3b9b   梁灏   update Cascader
285
286
287
288
289
290
                      this.selected = this.tmpSelected;
  
                      let newVal = [];
                      this.selected.forEach((item) => {
                          newVal.push(item.value);
                      });
c463ab87   梁灏   add Cascader
291
  
165bb7c9   梁灏   update Cascader
292
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
293
                          this.updatingValue = true;
47a7f21d   梁灏   support Cascader
294
295
                          this.currentValue = newVal;
                          this.emitValue(this.currentValue, oldVal);
bd4e3b9b   梁灏   update Cascader
296
297
298
299
300
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
47a7f21d   梁灏   support Cascader
301
              });
bd4e3b9b   梁灏   update Cascader
302
          },
687c4562   梁灏   fixed #810
303
304
305
          mounted () {
              this.updateSelected(true);
          },
bd4e3b9b   梁灏   update Cascader
306
307
308
          watch: {
              visible (val) {
                  if (val) {
47a7f21d   梁灏   support Cascader
309
                      if (this.currentValue.length) {
bd4e3b9b   梁灏   update Cascader
310
311
                          this.updateSelected();
                      }
3ae11e85   梁灏   update Cascader
312
313
314
315
316
                  } else {
                      if (this.filterable) {
                          this.query = '';
                          this.$refs.input.currentValue = '';
                      }
bd4e3b9b   梁灏   update Cascader
317
                  }
bb1a3c38   梁灏   fixed #593
318
                  this.$emit('on-visible-change', val);
f46ebc38   梁灏   fixed #130
319
              },
47a7f21d   梁灏   support Cascader
320
321
              value (val) {
                  this.currentValue = val;
3bbb6b5f   梁灏   fixed #488
322
                  if (!val.length) this.selected = [];
47a7f21d   梁灏   support Cascader
323
324
325
              },
              currentValue () {
                  this.$emit('input', this.currentValue);
2810d8c7   梁灏   fixed #183
326
327
328
329
330
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
48af1359   梁灏   fixed #553
331
              },
f7ffdac5   梁灏   Cascader support ...
332
333
334
335
336
              data: {
                  deep: true,
                  handler () {
                      this.$nextTick(() => this.updateSelected());
                  }
bd4e3b9b   梁灏   update Cascader
337
              }
0a48ac45   梁灏   Input add readonl...
338
          }
b0893113   jingsam   :art: add eslint
339
340
      };
  </script>