Blame view

src/components/select/select-head.vue 7.22 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>
c9b86944   Sergio Crisostomo   Refactor Select!
8
9
          <div class="ivu-tag ivu-tag-checked" v-for="item in selectedMultiple">
              <span class="ivu-tag-text">{{ item.label }}</span>
9eba26fe   梁灏   update Select Icons
10
              <Icon type="ios-close" @click.native.stop="removeTag(item)"></Icon>
c9b86944   Sergio Crisostomo   Refactor Select!
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
          </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 #...
30
              @blur="onInputBlur"
c9b86944   Sergio Crisostomo   Refactor Select!
31
32
  
              ref="input">
9eba26fe   梁灏   update Select Icons
33
          <Icon type="ios-close-circle" :class="[prefixCls + '-arrow']" v-if="resetSelect" @click.native.stop="onClear"></Icon>
01d898dc   梁灏   fix #5561 , close...
34
          <Icon type="ios-arrow-down" :class="[prefixCls + '-arrow']" v-if="!resetSelect && !remote"></Icon>
c9b86944   Sergio Crisostomo   Refactor Select!
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
      </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
66
                  type: [String, Number, Array],
c9b86944   Sergio Crisostomo   Refactor Select!
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
              },
              values: {
                  type: Array,
                  default: () => []
              },
              clearable: {
                  type: [Function, Boolean],
                  default: false,
              },
              inputElementId: {
                  type: String
              },
              placeholder: {
                  type: String
              },
              queryProp: {
                  type: String,
                  default: ''
2739fc29   梁灏   Select add prefix...
85
86
87
88
              },
              prefix: {
                  type: String
              },
c9b86944   Sergio Crisostomo   Refactor Select!
89
90
91
92
93
94
95
96
97
98
99
100
101
102
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  query: '',
                  inputLength: 20,
                  remoteInitialLabel: this.initialLabel,
                  preventRemoteCall: false,
              };
          },
          computed: {
              singleDisplayClasses(){
                  const {filterable, multiple, showPlaceholder} = this;
                  return [{
2739fc29   梁灏   Select add prefix...
103
                      [prefixCls + '-head-with-prefix']: this.$slots.prefix || this.prefix,
c9b86944   Sergio Crisostomo   Refactor Select!
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
                      [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 #...
158
159
160
161
162
163
              onInputFocus(){
                  this.$emit('on-input-focus');
              },
              onInputBlur () {
                  if (!this.values.length) this.query = '';  // #5155
                  this.$emit('on-input-blur');
c9b86944   Sergio Crisostomo   Refactor Select!
164
165
166
167
168
169
170
              },
              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
171
                  this.$emit('on-keydown');
c9b86944   Sergio Crisostomo   Refactor Select!
172
173
174
175
176
177
178
179
180
181
              },
              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
182
183
184
              },
              onClear(){
                  this.$emit('on-clear');
c9b86944   Sergio Crisostomo   Refactor Select!
185
186
187
188
189
190
191
192
193
194
195
196
197
198
              }
          },
          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...
199
                  this.$nextTick(() => this.preventRemoteCall = false); // this should be after the query change setter above
c9b86944   Sergio Crisostomo   Refactor Select!
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
              },
              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>