Blame view

src/components/cascader/cascader.vue 7.41 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)));
cd78c9c4   梁灏   some comps suppor...
165
166
167
168
                      this.dispatch('FormItem', 'on-form-change', {
                          value: this.currentValue,
                          selected: JSON.parse(JSON.stringify(this.selected))
                      });
165bb7c9   梁灏   update Cascader
169
                  }
c463ab87   梁灏   add Cascader
170
171
              }
          },
47a7f21d   梁灏   support Cascader
172
          mounted () {
165bb7c9   梁灏   update Cascader
173
              this.updateSelected(true);
47a7f21d   梁灏   support Cascader
174
175
176
177
178
179
180
              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
181
                  if (lastValue || changeOnSelect) {
47a7f21d   梁灏   support Cascader
182
                      const oldVal = JSON.stringify(this.currentValue);
bd4e3b9b   梁灏   update Cascader
183
184
185
186
187
188
                      this.selected = this.tmpSelected;
  
                      let newVal = [];
                      this.selected.forEach((item) => {
                          newVal.push(item.value);
                      });
c463ab87   梁灏   add Cascader
189
  
165bb7c9   梁灏   update Cascader
190
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
191
                          this.updatingValue = true;
47a7f21d   梁灏   support Cascader
192
193
                          this.currentValue = newVal;
                          this.emitValue(this.currentValue, oldVal);
bd4e3b9b   梁灏   update Cascader
194
195
196
197
198
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
47a7f21d   梁灏   support Cascader
199
              });
bd4e3b9b   梁灏   update Cascader
200
          },
bd4e3b9b   梁灏   update Cascader
201
202
203
          watch: {
              visible (val) {
                  if (val) {
47a7f21d   梁灏   support Cascader
204
                      if (this.currentValue.length) {
bd4e3b9b   梁灏   update Cascader
205
206
207
                          this.updateSelected();
                      }
                  }
f46ebc38   梁灏   fixed #130
208
              },
47a7f21d   梁灏   support Cascader
209
210
              value (val) {
                  this.currentValue = val;
3bbb6b5f   梁灏   fixed #488
211
                  if (!val.length) this.selected = [];
47a7f21d   梁灏   support Cascader
212
213
214
              },
              currentValue () {
                  this.$emit('input', this.currentValue);
2810d8c7   梁灏   fixed #183
215
216
217
218
219
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
bd4e3b9b   梁灏   update Cascader
220
              }
0a48ac45   梁灏   Input add readonl...
221
          }
b0893113   jingsam   :art: add eslint
222
223
      };
  </script>