Blame view

src/components/cascader/cascader.vue 6.67 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
132
133
134
135
136
137
              toggleOpen () {
                  if (this.visible) {
                      this.handleClose();
                  } else {
                      this.onFocus();
                  }
              },
c463ab87   梁灏   add Cascader
138
139
              onFocus () {
                  this.visible = true;
165bb7c9   梁灏   update Cascader
140
141
142
                  if (!this.value.length) {
                      this.$broadcast('on-clear');
                  }
c463ab87   梁灏   add Cascader
143
144
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
145
146
                  this.tmpSelected = result;
              },
165bb7c9   梁灏   update Cascader
147
148
149
150
151
152
153
154
              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
155
                      this.$dispatch('on-form-change', this.value, JSON.parse(JSON.stringify(this.selected)));
165bb7c9   梁灏   update Cascader
156
                  }
c463ab87   梁灏   add Cascader
157
158
159
              }
          },
          ready () {
165bb7c9   梁灏   update Cascader
160
              this.updateSelected(true);
bd4e3b9b   梁灏   update Cascader
161
162
163
164
165
166
167
168
169
170
171
172
173
          },
          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
174
  
165bb7c9   梁灏   update Cascader
175
                      if (!fromInit) {
2810d8c7   梁灏   fixed #183
176
                          this.updatingValue = true;
165bb7c9   梁灏   update Cascader
177
178
                          this.value = newVal;
                          this.emitValue(this.value, oldVal);
bd4e3b9b   梁灏   update Cascader
179
180
181
182
183
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
3b005863   梁灏   update Cascader
184
185
186
187
188
189
              },
              'on-form-blur' () {
                  return false;
              },
              'on-form-change' () {
                  return false;
bd4e3b9b   梁灏   update Cascader
190
191
192
193
194
195
196
197
198
              }
          },
          watch: {
              visible (val) {
                  if (val) {
                      if (this.value.length) {
                          this.updateSelected();
                      }
                  }
f46ebc38   梁灏   fixed #130
199
200
              },
              value () {
2810d8c7   梁灏   fixed #183
201
202
203
204
205
                  if (this.updatingValue) {
                      this.updatingValue = false;
                      return;
                  }
                  this.updateSelected(true);
bd4e3b9b   梁灏   update Cascader
206
              }
0a48ac45   梁灏   Input add readonl...
207
          }
b0893113   jingsam   :art: add eslint
208
209
      };
  </script>