Blame view

src/components/cascader/cascader.vue 6.72 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 {
c463ab87   梁灏   add Cascader
39
40
          components: { iInput, Dropdown, Icon, Caspanel },
          directives: { clickoutside },
0a48ac45   梁灏   Input add readonl...
41
          props: {
6ff31952   梁灏   optimize Input sh...
42
43
44
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
45
                      return [];
6ff31952   梁灏   optimize Input sh...
46
47
48
                  }
              },
              value: {
9ec927b1   梁灏   update Cascader
49
50
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
51
                      return [];
9ec927b1   梁灏   update Cascader
52
                  }
6ff31952   梁灏   optimize Input sh...
53
54
55
56
57
58
59
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              clearable: {
                  type: Boolean,
c463ab87   梁灏   add Cascader
60
                  default: true
6ff31952   梁灏   optimize Input sh...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
              },
              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
83
                  default (label) {
bd4e3b9b   梁灏   update Cascader
84
                      return label.join(' / ');
6ff31952   梁灏   optimize Input sh...
85
86
                  }
              }
0a48ac45   梁灏   Input add readonl...
87
88
89
          },
          data () {
              return {
6ff31952   梁灏   optimize Input sh...
90
                  prefixCls: prefixCls,
c463ab87   梁灏   add Cascader
91
92
                  visible: false,
                  selected: [],
2810d8c7   梁灏   fixed #183
93
94
                  tmpSelected: [],
                  updatingValue: false    // to fix set value in changeOnSelect type
b0893113   jingsam   :art: add eslint
95
              };
0a48ac45   梁灏   Input add readonl...
96
97
          },
          computed: {
c463ab87   梁灏   add Cascader
98
99
100
101
              classes () {
                  return [
                      `${prefixCls}`,
                      {
165bb7c9   梁灏   update Cascader
102
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
05b5dd7b   梁灏   update Cascader
103
104
                          [`${prefixCls}-visible`]: this.visible,
                          [`${prefixCls}-disabled`]: this.disabled
c463ab87   梁灏   add Cascader
105
                      }
b0893113   jingsam   :art: add eslint
106
                  ];
c463ab87   梁灏   add Cascader
107
108
109
110
              },
              showCloseIcon () {
                  return this.value && this.value.length && this.clearable;
              },
6ff31952   梁灏   optimize Input sh...
111
112
113
114
115
116
              displayRender () {
                  let label = [];
                  for (let i = 0; i < this.selected.length; i++) {
                      label.push(this.selected[i].label);
                  }
  
165bb7c9   梁灏   update Cascader
117
                  return this.renderFormat(label, this.selected);
6ff31952   梁灏   optimize Input sh...
118
              }
0a48ac45   梁灏   Input add readonl...
119
120
          },
          methods: {
c463ab87   梁灏   add Cascader
121
              clearSelect () {
165bb7c9   梁灏   update Cascader
122
123
124
125
126
                  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
127
128
129
130
              },
              handleClose () {
                  this.visible = false;
              },
75e5c6a5   梁灏   Cascader support ...
131
              toggleOpen () {
2aa0aa6e   Rijn   fix bug: cascader...
132
                  if (this.disabled) return false;
75e5c6a5   梁灏   Cascader support ...
133
134
135
136
137
138
                  if (this.visible) {
                      this.handleClose();
                  } else {
                      this.onFocus();
                  }
              },
c463ab87   梁灏   add Cascader
139
140
              onFocus () {
                  this.visible = true;
165bb7c9   梁灏   update Cascader
141
142
143
                  if (!this.value.length) {
                      this.$broadcast('on-clear');
                  }
c463ab87   梁灏   add Cascader
144
145
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
146
147
                  this.tmpSelected = result;
              },
165bb7c9   梁灏   update Cascader
148
149
150
151
152
153
154
155
              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
156
                      this.$dispatch('on-form-change', this.value, JSON.parse(JSON.stringify(this.selected)));
165bb7c9   梁灏   update Cascader
157
                  }
c463ab87   梁灏   add Cascader
158
159
160
              }
          },
          ready () {
165bb7c9   梁灏   update Cascader
161
              this.updateSelected(true);
bd4e3b9b   梁灏   update Cascader
162
163
164
165
166
167
168
169
170
171
172
173
174
          },
          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
175
  
165bb7c9   梁灏   update Cascader
176
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
177
                          this.updatingValue = true;
165bb7c9   梁灏   update Cascader
178
179
                          this.value = newVal;
                          this.emitValue(this.value, oldVal);
bd4e3b9b   梁灏   update Cascader
180
181
182
183
184
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
3b005863   梁灏   update Cascader
185
186
187
188
189
190
              },
              'on-form-blur' () {
                  return false;
              },
              'on-form-change' () {
                  return false;
bd4e3b9b   梁灏   update Cascader
191
192
193
194
195
196
197
198
199
              }
          },
          watch: {
              visible (val) {
                  if (val) {
                      if (this.value.length) {
                          this.updateSelected();
                      }
                  }
f46ebc38   梁灏   fixed #130
200
201
              },
              value () {
2810d8c7   梁灏   fixed #183
202
203
204
205
206
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
bd4e3b9b   梁灏   update Cascader
207
              }
0a48ac45   梁灏   Input add readonl...
208
          }
b0893113   jingsam   :art: add eslint
209
210
      };
  </script>