Blame view

src/components/cascader/cascader.vue 5.81 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
90
                  visible: false,
                  selected: [],
                  tmpSelected: []
b0893113   jingsam   :art: add eslint
91
              };
0a48ac45   梁灏   Input add readonl...
92
93
          },
          computed: {
c463ab87   梁灏   add Cascader
94
95
96
97
              classes () {
                  return [
                      `${prefixCls}`,
                      {
165bb7c9   梁灏   update Cascader
98
                          [`${prefixCls}-show-clear`]: this.showCloseIcon,
05b5dd7b   梁灏   update Cascader
99
100
                          [`${prefixCls}-visible`]: this.visible,
                          [`${prefixCls}-disabled`]: this.disabled
c463ab87   梁灏   add Cascader
101
                      }
b0893113   jingsam   :art: add eslint
102
                  ];
c463ab87   梁灏   add Cascader
103
104
105
106
              },
              showCloseIcon () {
                  return this.value && this.value.length && this.clearable;
              },
6ff31952   梁灏   optimize Input sh...
107
108
109
110
111
112
              displayRender () {
                  let label = [];
                  for (let i = 0; i < this.selected.length; i++) {
                      label.push(this.selected[i].label);
                  }
  
165bb7c9   梁灏   update Cascader
113
                  return this.renderFormat(label, this.selected);
6ff31952   梁灏   optimize Input sh...
114
              }
0a48ac45   梁灏   Input add readonl...
115
116
          },
          methods: {
c463ab87   梁灏   add Cascader
117
              clearSelect () {
165bb7c9   梁灏   update Cascader
118
119
120
121
122
                  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
123
124
125
126
127
128
              },
              handleClose () {
                  this.visible = false;
              },
              onFocus () {
                  this.visible = true;
165bb7c9   梁灏   update Cascader
129
130
131
                  if (!this.value.length) {
                      this.$broadcast('on-clear');
                  }
c463ab87   梁灏   add Cascader
132
133
              },
              updateResult (result) {
bd4e3b9b   梁灏   update Cascader
134
135
                  this.tmpSelected = result;
              },
165bb7c9   梁灏   update Cascader
136
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)));
                  }
c463ab87   梁灏   add Cascader
145
146
147
              }
          },
          ready () {
165bb7c9   梁灏   update Cascader
148
              this.updateSelected(true);
bd4e3b9b   梁灏   update Cascader
149
150
151
152
153
154
155
156
157
158
159
160
161
          },
          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
162
  
165bb7c9   梁灏   update Cascader
163
164
165
                      if (!fromInit) {
                          this.value = newVal;
                          this.emitValue(this.value, oldVal);
bd4e3b9b   梁灏   update Cascader
166
167
168
169
170
171
172
173
174
175
176
177
178
179
                      }
                  }
                  if (lastValue && !fromInit) {
                      this.handleClose();
                  }
              }
          },
          watch: {
              visible (val) {
                  if (val) {
                      if (this.value.length) {
                          this.updateSelected();
                      }
                  }
f46ebc38   梁灏   fixed #130
180
181
182
              },
              value () {
                  this.updateSelected();
bd4e3b9b   梁灏   update Cascader
183
              }
0a48ac45   梁灏   Input add readonl...
184
          }
b0893113   jingsam   :art: add eslint
185
186
      };
  </script>