Blame view

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