Blame view

src/components/select/select-head.vue 6.73 KB
c9b86944   Sergio Crisostomo   Refactor Select!
1
2
3
4
  <template>
      <div @click="onHeaderClick">
          <div class="ivu-tag ivu-tag-checked" v-for="item in selectedMultiple">
              <span class="ivu-tag-text">{{ item.label }}</span>
9eba26fe   梁灏   update Select Icons
5
              <Icon type="ios-close" @click.native.stop="removeTag(item)"></Icon>
c9b86944   Sergio Crisostomo   Refactor Select!
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
          </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"
              @blur="onInputFocus"
  
              ref="input">
9eba26fe   梁灏   update Select Icons
28
29
          <Icon type="ios-close-circle" :class="[prefixCls + '-arrow']" v-if="resetSelect" @click.native.stop="onClear"></Icon>
          <Icon type="ios-arrow-down" :class="[prefixCls + '-arrow']" v-if="!resetSelect && !remote && !disabled"></Icon>
c9b86944   Sergio Crisostomo   Refactor Select!
30
31
32
33
34
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
      </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
61
                  type: [String, Number, Array],
c9b86944   Sergio Crisostomo   Refactor Select!
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
158
159
160
161
162
163
164
165
166
167
              },
              values: {
                  type: Array,
                  default: () => []
              },
              clearable: {
                  type: [Function, Boolean],
                  default: false,
              },
              inputElementId: {
                  type: String
              },
              placeholder: {
                  type: String
              },
              queryProp: {
                  type: String,
                  default: ''
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  query: '',
                  inputLength: 20,
                  remoteInitialLabel: this.initialLabel,
                  preventRemoteCall: false,
              };
          },
          computed: {
              singleDisplayClasses(){
                  const {filterable, multiple, showPlaceholder} = this;
                  return [{
                      [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: {
              onInputFocus(e){
                  this.$emit(e.type === 'focus' ? 'on-input-focus' : 'on-input-blur');
              },
              removeTag (value) {
                  if (this.disabled) return false;
                  this.dispatch('iSelect', 'on-select-selected', value);
              },
              resetInputState () {
                  this.inputLength = this.$refs.input.value.length * 12 + 20;
              },
              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
168
169
170
              },
              onClear(){
                  this.$emit('on-clear');
c9b86944   Sergio Crisostomo   Refactor Select!
171
172
173
174
175
176
177
178
179
180
181
182
183
184
              }
          },
          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...
185
                  this.$nextTick(() => this.preventRemoteCall = false); // this should be after the query change setter above
c9b86944   Sergio Crisostomo   Refactor Select!
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
              },
              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>