Blame view

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