Blame view

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