Blame view

src/components/select/select-head.vue 7.88 KB
c9b86944   Sergio Crisostomo   Refactor Select!
1
2
  <template>
      <div @click="onHeaderClick">
2739fc29   梁灏   Select add prefix...
3
4
5
6
7
          <span :class="[prefixCls + '-prefix']" v-if="$slots.prefix || prefix">
              <slot name="prefix">
                  <Icon :type="prefix" v-if="prefix" />
              </slot>
          </span>
90399f84   梁灏   fix #5568 ,close ...
8
9
10
11
          <div
              class="ivu-tag ivu-tag-checked"
              v-for="(item, index) in selectedMultiple"
              v-if="maxTagCount === undefined || index < maxTagCount">
c9b86944   Sergio Crisostomo   Refactor Select!
12
              <span class="ivu-tag-text">{{ item.label }}</span>
9eba26fe   梁灏   update Select Icons
13
              <Icon type="ios-close" @click.native.stop="removeTag(item)"></Icon>
90399f84   梁灏   fix #5568 ,close ...
14
15
16
17
18
          </div><div class="ivu-tag ivu-tag-checked" v-if="maxTagCount !== undefined && selectedMultiple.length > maxTagCount">
              <span class="ivu-tag-text ivu-select-max-tag">
                  <template v-if="maxTagPlaceholder">{{ maxTagPlaceholder }}</template>
                  <template v-else>+ {{ selectedMultiple.length - maxTagCount }}...</template>
              </span>
c9b86944   Sergio Crisostomo   Refactor Select!
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
          </div>
          <span
              :class="singleDisplayClasses"
              v-show="singleDisplayValue"
          >{{ singleDisplayValue }}</span>
          <input
              :id="inputElementId"
              type="text"
              v-if="filterable"
              v-model="query"
              :disabled="disabled"
              :class="[prefixCls + '-input']"
              :placeholder="showPlaceholder ? localePlaceholder : ''"
              :style="inputStyle"
              autocomplete="off"
              spellcheck="false"
              @keydown="resetInputState"
              @keydown.delete="handleInputDelete"
              @focus="onInputFocus"
32349491   梁灏   fix #5155 close #...
38
              @blur="onInputBlur"
c9b86944   Sergio Crisostomo   Refactor Select!
39
40
  
              ref="input">
9eba26fe   梁灏   update Select Icons
41
          <Icon type="ios-close-circle" :class="[prefixCls + '-arrow']" v-if="resetSelect" @click.native.stop="onClear"></Icon>
01d898dc   梁灏   fix #5561 , close...
42
          <Icon type="ios-arrow-down" :class="[prefixCls + '-arrow']" v-if="!resetSelect && !remote"></Icon>
c9b86944   Sergio Crisostomo   Refactor Select!
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      </div>
  </template>
  <script>
      import Icon from '../icon';
      import Emitter from '../../mixins/emitter';
      import Locale from '../../mixins/locale';
  
      const prefixCls = 'ivu-select';
  
      export default {
          name: 'iSelectHead',
          mixins: [ Emitter, Locale ],
          components: { Icon },
          props: {
              disabled: {
                  type: Boolean,
                  default: false
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              multiple: {
                  type: Boolean,
                  default: false
              },
              remote: {
                  type: Boolean,
                  default: false
              },
              initialLabel: {
21f69406   郑敏   fixed #3817 #3836
74
                  type: [String, Number, Array],
c9b86944   Sergio Crisostomo   Refactor Select!
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
              },
              values: {
                  type: Array,
                  default: () => []
              },
              clearable: {
                  type: [Function, Boolean],
                  default: false,
              },
              inputElementId: {
                  type: String
              },
              placeholder: {
                  type: String
              },
              queryProp: {
                  type: String,
                  default: ''
2739fc29   梁灏   Select add prefix...
93
94
95
96
              },
              prefix: {
                  type: String
              },
90399f84   梁灏   fix #5568 ,close ...
97
98
99
100
101
102
103
104
              // 3.4.0
              maxTagCount: {
                  type: Number
              },
              // 3.4.0
              maxTagPlaceholder: {
                  type: String
              }
c9b86944   Sergio Crisostomo   Refactor Select!
105
106
107
108
109
110
111
112
113
114
115
116
117
118
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  query: '',
                  inputLength: 20,
                  remoteInitialLabel: this.initialLabel,
                  preventRemoteCall: false,
              };
          },
          computed: {
              singleDisplayClasses(){
                  const {filterable, multiple, showPlaceholder} = this;
                  return [{
2739fc29   梁灏   Select add prefix...
119
                      [prefixCls + '-head-with-prefix']: this.$slots.prefix || this.prefix,
c9b86944   Sergio Crisostomo   Refactor Select!
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                      [prefixCls + '-placeholder']: showPlaceholder && !filterable,
                      [prefixCls + '-selected-value']: !showPlaceholder && !multiple && !filterable,
                  }];
              },
              singleDisplayValue(){
                  if ((this.multiple && this.values.length > 0) || this.filterable) return '';
                  return `${this.selectedSingle}` || this.localePlaceholder;
              },
              showPlaceholder () {
                  let status = false;
                  if (!this.multiple) {
                      const value = this.values[0];
                      if (typeof value === 'undefined' || String(value).trim() === ''){
                          status = !this.remoteInitialLabel;
                      }
                  } else {
                      if (!this.values.length > 0) {
                          status = true;
                      }
                  }
                  return status;
              },
              resetSelect(){
                  return !this.showPlaceholder && this.clearable;
              },
              inputStyle () {
                  let style = {};
  
                  if (this.multiple) {
                      if (this.showPlaceholder) {
                          style.width = '100%';
                      } else {
                          style.width = `${this.inputLength}px`;
                      }
                  }
  
                  return style;
              },
              localePlaceholder () {
                  if (this.placeholder === undefined) {
                      return this.t('i.select.placeholder');
                  } else {
                      return this.placeholder;
                  }
              },
              selectedSingle(){
                  const selected = this.values[0];
                  return selected ? selected.label : (this.remoteInitialLabel || '');
              },
              selectedMultiple(){
                  return this.multiple ? this.values : [];
              }
          },
          methods: {
32349491   梁灏   fix #5155 close #...
174
175
176
177
178
179
              onInputFocus(){
                  this.$emit('on-input-focus');
              },
              onInputBlur () {
                  if (!this.values.length) this.query = '';  // #5155
                  this.$emit('on-input-blur');
c9b86944   Sergio Crisostomo   Refactor Select!
180
181
182
183
184
185
186
              },
              removeTag (value) {
                  if (this.disabled) return false;
                  this.dispatch('iSelect', 'on-select-selected', value);
              },
              resetInputState () {
                  this.inputLength = this.$refs.input.value.length * 12 + 20;
47f03c54   梁灏   fix #4273
187
                  this.$emit('on-keydown');
c9b86944   Sergio Crisostomo   Refactor Select!
188
189
190
191
192
193
194
195
196
197
              },
              handleInputDelete () {
                  if (this.multiple && this.selectedMultiple.length && this.query === '') {
                      this.removeTag(this.selectedMultiple[this.selectedMultiple.length - 1]);
                  }
              },
              onHeaderClick(e){
                  if (this.filterable && e.target === this.$el){
                      this.$refs.input.focus();
                  }
7dbde804   Sergio Crisostomo   add on-clear event
198
199
200
              },
              onClear(){
                  this.$emit('on-clear');
c9b86944   Sergio Crisostomo   Refactor Select!
201
202
203
204
205
206
207
208
209
210
211
212
213
214
              }
          },
          watch: {
              values ([value]) {
                  if (!this.filterable) return;
                  this.preventRemoteCall = true;
                  if (this.multiple){
                      this.query = '';
                      this.preventRemoteCall = false; // this should be after the query change setter above
                      return;
                  }
                  // #982
                  if (typeof value === 'undefined' || value === '' || value === null) this.query = '';
                  else this.query = value.label;
c3304bce   Sergio Crisostomo   correct unchanged...
215
                  this.$nextTick(() => this.preventRemoteCall = false); // this should be after the query change setter above
c9b86944   Sergio Crisostomo   Refactor Select!
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
              },
              query (val) {
                  if (this.preventRemoteCall) {
                      this.preventRemoteCall = false;
                      return;
                  }
  
                  this.$emit('on-query-change', val);
              },
              queryProp(query){
                  if (query !== this.query) this.query = query;
              },
          }
      };
  </script>