Blame view

src/components/cascader/cascader.vue 7.88 KB
0a48ac45   梁灏   Input add readonl...
1
  <template>
c463ab87   梁灏   add Cascader
2
      <div :class="classes" v-clickoutside="handleClose">
75e5c6a5   梁灏   Cascader support ...
3
4
5
6
7
          <div :class="[prefixCls + '-rel']" @click="toggleOpen">
              <slot>
                  <i-input
                      readonly
                      :disabled="disabled"
47a7f21d   梁灏   support Cascader
8
                      v-model="displayRender"
75e5c6a5   梁灏   Cascader support ...
9
10
                      :size="size"
                      :placeholder="placeholder"></i-input>
47a7f21d   梁灏   support Cascader
11
                  <Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.native.stop="clearSelect"></Icon>
75e5c6a5   梁灏   Cascader support ...
12
13
14
                  <Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
              </slot>
          </div>
47a7f21d   梁灏   support Cascader
15
16
17
18
19
20
21
22
23
24
25
26
27
          <transition name="slide-up">
              <Drop v-show="visible">
                  <div>
                      <Caspanel
                          ref="caspanel"
                          :prefix-cls="prefixCls"
                          :data="data"
                          :disabled="disabled"
                          :change-on-select="changeOnSelect"
                          :trigger="trigger"></Caspanel>
                  </div>
              </Drop>
          </transition>
6ff31952   梁灏   optimize Input sh...
28
      </div>
0a48ac45   梁灏   Input add readonl...
29
30
  </template>
  <script>
6ff31952   梁灏   optimize Input sh...
31
      import iInput from '../input/input.vue';
47a7f21d   梁灏   support Cascader
32
      import Drop from '../select/dropdown.vue';
c463ab87   梁灏   add Cascader
33
34
      import Icon from '../icon/icon.vue';
      import Caspanel from './caspanel.vue';
6ff31952   梁灏   optimize Input sh...
35
      import clickoutside from '../../directives/clickoutside';
c463ab87   梁灏   add Cascader
36
      import { oneOf } from '../../utils/assist';
47a7f21d   梁灏   support Cascader
37
      import Emitter from '../../mixins/emitter';
6ff31952   梁灏   optimize Input sh...
38
39
40
  
      const prefixCls = 'ivu-cascader';
  
0a48ac45   梁灏   Input add readonl...
41
      export default {
34ee7b4a   梁灏   support Tree & ad...
42
          name: 'Cascader',
47a7f21d   梁灏   support Cascader
43
44
          mixins: [ Emitter ],
          components: { iInput, Drop, Icon, Caspanel },
c463ab87   梁灏   add Cascader
45
          directives: { clickoutside },
0a48ac45   梁灏   Input add readonl...
46
          props: {
6ff31952   梁灏   optimize Input sh...
47
48
49
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
50
                      return [];
6ff31952   梁灏   optimize Input sh...
51
52
53
                  }
              },
              value: {
9ec927b1   梁灏   update Cascader
54
55
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
56
                      return [];
9ec927b1   梁灏   update Cascader
57
                  }
6ff31952   梁灏   optimize Input sh...
58
59
60
61
62
63
64
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              clearable: {
                  type: Boolean,
c463ab87   梁灏   add Cascader
65
                  default: true
6ff31952   梁灏   optimize Input sh...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
              },
              placeholder: {
                  type: String,
                  default: '请选择'
              },
              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
88
                  default (label) {
bd4e3b9b   梁灏   update Cascader
89
                      return label.join(' / ');
6ff31952   梁灏   optimize Input sh...
90
                  }
f7ffdac5   梁灏   Cascader support ...
91
92
93
              },
              loadData: {
                  type: Function
6ff31952   梁灏   optimize Input sh...
94
              }
0a48ac45   梁灏   Input add readonl...
95
96
97
          },
          data () {
              return {
6ff31952   梁灏   optimize Input sh...
98
                  prefixCls: prefixCls,
c463ab87   梁灏   add Cascader
99
100
                  visible: false,
                  selected: [],
2810d8c7   梁灏   fixed #183
101
                  tmpSelected: [],
47a7f21d   梁灏   support Cascader
102
103
                  updatingValue: false,    // to fix set value in changeOnSelect type
                  currentValue: this.value
b0893113   jingsam   :art: add eslint
104
              };
0a48ac45   梁灏   Input add readonl...
105
106
          },
          computed: {
c463ab87   梁灏   add Cascader
107
108
109
110
              classes () {
                  return [
                      `${prefixCls}`,
                      {
165bb7c9   梁灏   update Cascader
111
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
05b5dd7b   梁灏   update Cascader
112
113
                          [`${prefixCls}-visible`]: this.visible,
                          [`${prefixCls}-disabled`]: this.disabled
c463ab87   梁灏   add Cascader
114
                      }
b0893113   jingsam   :art: add eslint
115
                  ];
c463ab87   梁灏   add Cascader
116
117
              },
              showCloseIcon () {
65b41a2d   梁灏   fixed #635
118
                  return this.currentValue && this.currentValue.length && this.clearable && !this.disabled;
c463ab87   梁灏   add Cascader
119
              },
6ff31952   梁灏   optimize Input sh...
120
121
122
123
124
125
              displayRender () {
                  let label = [];
                  for (let i = 0; i < this.selected.length; i++) {
                      label.push(this.selected[i].label);
                  }
  
165bb7c9   梁灏   update Cascader
126
                  return this.renderFormat(label, this.selected);
6ff31952   梁灏   optimize Input sh...
127
              }
0a48ac45   梁灏   Input add readonl...
128
129
          },
          methods: {
c463ab87   梁灏   add Cascader
130
              clearSelect () {
65b41a2d   梁灏   fixed #635
131
                  if (this.disabled) return false;
47a7f21d   梁灏   support Cascader
132
133
                  const oldVal = JSON.stringify(this.currentValue);
                  this.currentValue = this.selected = this.tmpSelected = [];
165bb7c9   梁灏   update Cascader
134
                  this.handleClose();
47a7f21d   梁灏   support Cascader
135
136
137
                  this.emitValue(this.currentValue, oldVal);
  //                this.$broadcast('on-clear');
                  this.broadcast('Caspanel', 'on-clear');
c463ab87   梁灏   add Cascader
138
139
140
141
              },
              handleClose () {
                  this.visible = false;
              },
75e5c6a5   梁灏   Cascader support ...
142
              toggleOpen () {
2aa0aa6e   Rijn   fix bug: cascader...
143
                  if (this.disabled) return false;
75e5c6a5   梁灏   Cascader support ...
144
145
146
147
148
149
                  if (this.visible) {
                      this.handleClose();
                  } else {
                      this.onFocus();
                  }
              },
c463ab87   梁灏   add Cascader
150
151
              onFocus () {
                  this.visible = true;
47a7f21d   梁灏   support Cascader
152
153
                  if (!this.currentValue.length) {
                      this.broadcast('Caspanel', 'on-clear');
165bb7c9   梁灏   update Cascader
154
                  }
c463ab87   梁灏   add Cascader
155
156
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
157
158
                  this.tmpSelected = result;
              },
165bb7c9   梁灏   update Cascader
159
160
              updateSelected (init = false) {
                  if (!this.changeOnSelect || init) {
47a7f21d   梁灏   support Cascader
161
162
163
                      this.broadcast('Caspanel', 'on-find-selected', {
                          value: this.currentValue
                      });
165bb7c9   梁灏   update Cascader
164
165
166
167
                  }
              },
              emitValue (val, oldVal) {
                  if (JSON.stringify(val) !== oldVal) {
47a7f21d   梁灏   support Cascader
168
                      this.$emit('on-change', this.currentValue, JSON.parse(JSON.stringify(this.selected)));
cc419499   梁灏   fixed #525
169
170
171
172
173
                      this.$nextTick(() => {
                          this.dispatch('FormItem', 'on-form-change', {
                              value: this.currentValue,
                              selected: JSON.parse(JSON.stringify(this.selected))
                          });
cd78c9c4   梁灏   some comps suppor...
174
                      });
165bb7c9   梁灏   update Cascader
175
                  }
c463ab87   梁灏   add Cascader
176
177
              }
          },
687c4562   梁灏   fixed #810
178
          created () {
47a7f21d   梁灏   support Cascader
179
180
181
182
183
184
185
              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
186
                  if (lastValue || changeOnSelect) {
47a7f21d   梁灏   support Cascader
187
                      const oldVal = JSON.stringify(this.currentValue);
bd4e3b9b   梁灏   update Cascader
188
189
190
191
192
193
                      this.selected = this.tmpSelected;
  
                      let newVal = [];
                      this.selected.forEach((item) => {
                          newVal.push(item.value);
                      });
c463ab87   梁灏   add Cascader
194
  
165bb7c9   梁灏   update Cascader
195
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
196
                          this.updatingValue = true;
47a7f21d   梁灏   support Cascader
197
198
                          this.currentValue = newVal;
                          this.emitValue(this.currentValue, oldVal);
bd4e3b9b   梁灏   update Cascader
199
200
201
202
203
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
47a7f21d   梁灏   support Cascader
204
              });
bd4e3b9b   梁灏   update Cascader
205
          },
687c4562   梁灏   fixed #810
206
207
208
          mounted () {
              this.updateSelected(true);
          },
bd4e3b9b   梁灏   update Cascader
209
210
211
          watch: {
              visible (val) {
                  if (val) {
47a7f21d   梁灏   support Cascader
212
                      if (this.currentValue.length) {
bd4e3b9b   梁灏   update Cascader
213
214
215
                          this.updateSelected();
                      }
                  }
bb1a3c38   梁灏   fixed #593
216
                  this.$emit('on-visible-change', val);
f46ebc38   梁灏   fixed #130
217
              },
47a7f21d   梁灏   support Cascader
218
219
              value (val) {
                  this.currentValue = val;
3bbb6b5f   梁灏   fixed #488
220
                  if (!val.length) this.selected = [];
47a7f21d   梁灏   support Cascader
221
222
223
              },
              currentValue () {
                  this.$emit('input', this.currentValue);
2810d8c7   梁灏   fixed #183
224
225
226
227
228
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
48af1359   梁灏   fixed #553
229
              },
f7ffdac5   梁灏   Cascader support ...
230
231
232
233
234
              data: {
                  deep: true,
                  handler () {
                      this.$nextTick(() => this.updateSelected());
                  }
bd4e3b9b   梁灏   update Cascader
235
              }
0a48ac45   梁灏   Input add readonl...
236
          }
b0893113   jingsam   :art: add eslint
237
238
      };
  </script>