Blame view

src/components/cascader/cascader.vue 7.59 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
91
                  }
              }
0a48ac45   梁灏   Input add readonl...
92
93
94
          },
          data () {
              return {
6ff31952   梁灏   optimize Input sh...
95
                  prefixCls: prefixCls,
c463ab87   梁灏   add Cascader
96
97
                  visible: false,
                  selected: [],
2810d8c7   梁灏   fixed #183
98
                  tmpSelected: [],
47a7f21d   梁灏   support Cascader
99
100
                  updatingValue: false,    // to fix set value in changeOnSelect type
                  currentValue: this.value
b0893113   jingsam   :art: add eslint
101
              };
0a48ac45   梁灏   Input add readonl...
102
103
          },
          computed: {
c463ab87   梁灏   add Cascader
104
105
106
107
              classes () {
                  return [
                      `${prefixCls}`,
                      {
165bb7c9   梁灏   update Cascader
108
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
05b5dd7b   梁灏   update Cascader
109
110
                          [`${prefixCls}-visible`]: this.visible,
                          [`${prefixCls}-disabled`]: this.disabled
c463ab87   梁灏   add Cascader
111
                      }
b0893113   jingsam   :art: add eslint
112
                  ];
c463ab87   梁灏   add Cascader
113
114
              },
              showCloseIcon () {
47a7f21d   梁灏   support Cascader
115
                  return this.currentValue && this.currentValue.length && this.clearable;
c463ab87   梁灏   add Cascader
116
              },
6ff31952   梁灏   optimize Input sh...
117
118
119
120
121
122
              displayRender () {
                  let label = [];
                  for (let i = 0; i < this.selected.length; i++) {
                      label.push(this.selected[i].label);
                  }
  
165bb7c9   梁灏   update Cascader
123
                  return this.renderFormat(label, this.selected);
6ff31952   梁灏   optimize Input sh...
124
              }
0a48ac45   梁灏   Input add readonl...
125
126
          },
          methods: {
c463ab87   梁灏   add Cascader
127
              clearSelect () {
47a7f21d   梁灏   support Cascader
128
129
                  const oldVal = JSON.stringify(this.currentValue);
                  this.currentValue = this.selected = this.tmpSelected = [];
165bb7c9   梁灏   update Cascader
130
                  this.handleClose();
47a7f21d   梁灏   support Cascader
131
132
133
                  this.emitValue(this.currentValue, oldVal);
  //                this.$broadcast('on-clear');
                  this.broadcast('Caspanel', 'on-clear');
c463ab87   梁灏   add Cascader
134
135
136
137
              },
              handleClose () {
                  this.visible = false;
              },
75e5c6a5   梁灏   Cascader support ...
138
              toggleOpen () {
2aa0aa6e   Rijn   fix bug: cascader...
139
                  if (this.disabled) return false;
75e5c6a5   梁灏   Cascader support ...
140
141
142
143
144
145
                  if (this.visible) {
                      this.handleClose();
                  } else {
                      this.onFocus();
                  }
              },
c463ab87   梁灏   add Cascader
146
147
              onFocus () {
                  this.visible = true;
47a7f21d   梁灏   support Cascader
148
149
                  if (!this.currentValue.length) {
                      this.broadcast('Caspanel', 'on-clear');
165bb7c9   梁灏   update Cascader
150
                  }
c463ab87   梁灏   add Cascader
151
152
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
153
154
                  this.tmpSelected = result;
              },
165bb7c9   梁灏   update Cascader
155
156
              updateSelected (init = false) {
                  if (!this.changeOnSelect || init) {
47a7f21d   梁灏   support Cascader
157
158
159
                      this.broadcast('Caspanel', 'on-find-selected', {
                          value: this.currentValue
                      });
165bb7c9   梁灏   update Cascader
160
161
162
163
                  }
              },
              emitValue (val, oldVal) {
                  if (JSON.stringify(val) !== oldVal) {
47a7f21d   梁灏   support Cascader
164
                      this.$emit('on-change', this.currentValue, JSON.parse(JSON.stringify(this.selected)));
cc419499   梁灏   fixed #525
165
166
167
168
169
                      this.$nextTick(() => {
                          this.dispatch('FormItem', 'on-form-change', {
                              value: this.currentValue,
                              selected: JSON.parse(JSON.stringify(this.selected))
                          });
cd78c9c4   梁灏   some comps suppor...
170
                      });
165bb7c9   梁灏   update Cascader
171
                  }
c463ab87   梁灏   add Cascader
172
173
              }
          },
47a7f21d   梁灏   support Cascader
174
          mounted () {
165bb7c9   梁灏   update Cascader
175
              this.updateSelected(true);
47a7f21d   梁灏   support Cascader
176
177
178
179
180
181
182
              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
183
                  if (lastValue || changeOnSelect) {
47a7f21d   梁灏   support Cascader
184
                      const oldVal = JSON.stringify(this.currentValue);
bd4e3b9b   梁灏   update Cascader
185
186
187
188
189
190
                      this.selected = this.tmpSelected;
  
                      let newVal = [];
                      this.selected.forEach((item) => {
                          newVal.push(item.value);
                      });
c463ab87   梁灏   add Cascader
191
  
165bb7c9   梁灏   update Cascader
192
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
193
                          this.updatingValue = true;
47a7f21d   梁灏   support Cascader
194
195
                          this.currentValue = newVal;
                          this.emitValue(this.currentValue, oldVal);
bd4e3b9b   梁灏   update Cascader
196
197
198
199
200
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
47a7f21d   梁灏   support Cascader
201
              });
bd4e3b9b   梁灏   update Cascader
202
          },
bd4e3b9b   梁灏   update Cascader
203
204
205
          watch: {
              visible (val) {
                  if (val) {
47a7f21d   梁灏   support Cascader
206
                      if (this.currentValue.length) {
bd4e3b9b   梁灏   update Cascader
207
208
209
                          this.updateSelected();
                      }
                  }
f46ebc38   梁灏   fixed #130
210
              },
47a7f21d   梁灏   support Cascader
211
212
              value (val) {
                  this.currentValue = val;
3bbb6b5f   梁灏   fixed #488
213
                  if (!val.length) this.selected = [];
47a7f21d   梁灏   support Cascader
214
215
216
              },
              currentValue () {
                  this.$emit('input', this.currentValue);
2810d8c7   梁灏   fixed #183
217
218
219
220
221
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
48af1359   梁灏   fixed #553
222
223
224
              },
              data () {
                  this.$nextTick(() => this.updateSelected());
bd4e3b9b   梁灏   update Cascader
225
              }
0a48ac45   梁灏   Input add readonl...
226
          }
b0893113   jingsam   :art: add eslint
227
228
      };
  </script>