Blame view

src/components/input/input.vue 16.4 KB
7fa943eb   梁灏   init
1
  <template>
7d5431d8   梁灏   update some style
2
      <div :class="wrapClasses">
0f822c9b   梁灏   add Input component
3
          <template v-if="type !== 'textarea'">
319f5f86   梁灏   fixed #554
4
              <div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-show="slotReady"><slot name="prepend"></slot></div>
75460ce6   FEI   input upgrade
5
              <i class="ivu-icon" :class="['ivu-icon-ios-close-circle', prefixCls + '-icon', prefixCls + '-icon-clear' , prefixCls + '-icon-normal']" v-if="clearable && currentValue && !itemDisabled" @click="handleClear" :style="clearableStyles"></i>
a73ae72b   梁灏   fixed #2884
6
              <i class="ivu-icon" :class="['ivu-icon-' + icon, prefixCls + '-icon', prefixCls + '-icon-normal']" v-else-if="icon" @click="handleIconClick"></i>
e2a877c4   梁灏   Input add search ...
7
              <i class="ivu-icon ivu-icon-ios-search" :class="[prefixCls + '-icon', prefixCls + '-icon-normal', prefixCls + '-search-icon']" v-else-if="search && enterButton === false" @click="handleSearch"></i>
42d5412a   梁灏   Input add prop pr...
8
              <span class="ivu-input-suffix" v-else-if="showSuffix"><slot name="suffix"><i class="ivu-icon" :class="['ivu-icon-' + suffix]" v-if="suffix"></i></slot></span>
75460ce6   FEI   input upgrade
9
10
11
12
13
              <span class="ivu-input-word-count" v-else-if="showWordLimit">{{ textLength }}/{{ upperLimit }}</span>
              <span class="ivu-input-suffix" v-else-if="password" @click="handleToggleShowPassword">
                  <i class="ivu-icon ivu-icon-ios-eye-outline" v-if="showPassword"></i>
                  <i class="ivu-icon ivu-icon-ios-eye-off-outline" v-else></i>
              </span>
fc7ef072   梁灏   support Input
14
              <transition name="fade">
509014fb   梁灏   update Icons
15
                  <i class="ivu-icon ivu-icon-ios-loading ivu-load-loop" :class="[prefixCls + '-icon', prefixCls + '-icon-validate']" v-if="!icon"></i>
fc7ef072   梁灏   support Input
16
              </transition>
0f822c9b   梁灏   add Input component
17
              <input
acb79ba3   梁灏   fixed #433
18
                  :id="elementId"
9968ddea   zero   Input increase au...
19
                  :autocomplete="autocomplete"
c17c5ad6   Sergio Crisostomo   normalize autocom...
20
                  :spellcheck="spellcheck"
545add71   yinheli   input 支持 focus 方法
21
                  ref="input"
75460ce6   FEI   input upgrade
22
                  :type="currentType"
0f822c9b   梁灏   add Input component
23
24
                  :class="inputClasses"
                  :placeholder="placeholder"
75460ce6   FEI   input upgrade
25
                  :disabled="itemDisabled"
0f822c9b   梁灏   add Input component
26
                  :maxlength="maxlength"
0a48ac45   梁灏   Input add readonl...
27
                  :readonly="readonly"
731d69a2   梁灏   fixed #101
28
                  :name="name"
fc7ef072   梁灏   support Input
29
                  :value="currentValue"
c3a9f389   梁灏   update Input
30
                  :number="number"
f15c216a   丁强   Input 组件增加autofoc...
31
                  :autofocus="autofocus"
0a48ac45   梁灏   Input add readonl...
32
                  @keyup.enter="handleEnter"
9e7a3740   xingbofeng   fix: add keyup, k...
33
34
35
                  @keyup="handleKeyup"
                  @keypress="handleKeypress"
                  @keydown="handleKeydown"
0a48ac45   梁灏   Input add readonl...
36
                  @focus="handleFocus"
c46f385a   梁灏   update DatePicker
37
                  @blur="handleBlur"
f43bc792   梁灏   fix #5060
38
39
40
                  @compositionstart="handleComposition"
                  @compositionupdate="handleComposition"
                  @compositionend="handleComposition"
531cd165   梁灏   support DatePicke...
41
42
                  @input="handleInput"
                  @change="handleChange">
319f5f86   梁灏   fixed #554
43
              <div :class="[prefixCls + '-group-append']" v-if="append" v-show="slotReady"><slot name="append"></slot></div>
e2a877c4   梁灏   Input add search ...
44
45
46
47
              <div :class="[prefixCls + '-group-append', prefixCls + '-search']" v-else-if="search && enterButton" @click="handleSearch">
                  <i class="ivu-icon ivu-icon-ios-search" v-if="enterButton === true"></i>
                  <template v-else>{{ enterButton }}</template>
              </div>
42d5412a   梁灏   Input add prop pr...
48
              <span class="ivu-input-prefix" v-else-if="showPrefix"><slot name="prefix"><i class="ivu-icon" :class="['ivu-icon-' + prefix]" v-if="prefix"></i></slot></span>
0f822c9b   梁灏   add Input component
49
          </template>
75460ce6   FEI   input upgrade
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
          <template v-else>
              <textarea
                  :id="elementId"
                  :wrap="wrap"
                  :autocomplete="autocomplete"
                  :spellcheck="spellcheck"
                  ref="textarea"
                  :class="textareaClasses"
                  :style="textareaStyles"
                  :placeholder="placeholder"
                  :disabled="itemDisabled"
                  :rows="rows"
                  :maxlength="maxlength"
                  :readonly="readonly"
                  :name="name"
                  :value="currentValue"
                  :autofocus="autofocus"
                  @keyup.enter="handleEnter"
                  @keyup="handleKeyup"
                  @keypress="handleKeypress"
                  @keydown="handleKeydown"
                  @focus="handleFocus"
                  @blur="handleBlur"
                  @compositionstart="handleComposition"
                  @compositionupdate="handleComposition"
                  @compositionend="handleComposition"
                  @input="handleInput">
              </textarea>
              <span class="ivu-input-word-count" v-if="showWordLimit">{{ textLength }}/{{ upperLimit }}</span>
          </template>
7d5431d8   梁灏   update some style
80
      </div>
7fa943eb   梁灏   init
81
82
  </template>
  <script>
21dad188   梁灏   prevent dispatch ...
83
      import { oneOf, findComponentUpward } from '../../utils/assist';
0f822c9b   梁灏   add Input component
84
      import calcTextareaHeight from '../../utils/calcTextareaHeight';
cd78c9c4   梁灏   some comps suppor...
85
      import Emitter from '../../mixins/emitter';
75460ce6   FEI   input upgrade
86
      import mixinsForm from '../../mixins/form';
7fa943eb   梁灏   init
87
88
89
90
  
      const prefixCls = 'ivu-input';
  
      export default {
06322514   梁灏   support Radio
91
          name: 'Input',
75460ce6   FEI   input upgrade
92
          mixins: [ Emitter, mixinsForm ],
7fa943eb   梁灏   init
93
94
          props: {
              type: {
0f822c9b   梁灏   add Input component
95
                  validator (value) {
344a6c4e   梁灏   fixed #5422
96
                      return oneOf(value, ['text', 'textarea', 'password', 'url', 'email', 'date', 'number', 'tel']);
0f822c9b   梁灏   add Input component
97
                  },
7fa943eb   梁灏   init
98
99
100
101
                  default: 'text'
              },
              value: {
                  type: [String, Number],
fc7ef072   梁灏   support Input
102
                  default: ''
7fa943eb   梁灏   init
103
              },
7fa943eb   梁灏   init
104
105
              size: {
                  validator (value) {
b6bda1dc   梁灏   update ColorPicker
106
                      return oneOf(value, ['small', 'large', 'default']);
d18a7ee8   梁灏   Input support glo...
107
108
                  },
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
109
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
7fa943eb   梁灏   init
110
                  }
0f822c9b   梁灏   add Input component
111
112
113
114
115
116
              },
              placeholder: {
                  type: String,
                  default: ''
              },
              maxlength: {
75460ce6   FEI   input upgrade
117
                  type: [String, Number]
0f822c9b   梁灏   add Input component
118
119
120
121
122
123
124
125
126
127
128
129
130
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              icon: String,
              autosize: {
                  type: [Boolean, Object],
                  default: false
              },
              rows: {
                  type: Number,
                  default: 2
0a48ac45   梁灏   Input add readonl...
131
132
133
134
              },
              readonly: {
                  type: Boolean,
                  default: false
731d69a2   梁灏   fixed #101
135
136
137
              },
              name: {
                  type: String
c3a9f389   梁灏   update Input
138
139
140
141
              },
              number: {
                  type: Boolean,
                  default: false
f15c216a   丁强   Input 组件增加autofoc...
142
143
              },
              autofocus: {
8d3a02a5   丁强   修改input组件 autofoc...
144
145
                  type: Boolean,
                  default: false
9968ddea   zero   Input increase au...
146
              },
c17c5ad6   Sergio Crisostomo   normalize autocom...
147
148
149
150
              spellcheck: {
                  type: Boolean,
                  default: false
              },
9968ddea   zero   Input increase au...
151
              autocomplete: {
0d9cc2e4   李通洲   feat(Input): remo...
152
                  type: String,
9968ddea   zero   Input increase au...
153
                  default: 'off'
acb79ba3   梁灏   fixed #433
154
              },
a73ae72b   梁灏   fixed #2884
155
156
157
158
              clearable: {
                  type: Boolean,
                  default: false
              },
acb79ba3   梁灏   fixed #433
159
160
              elementId: {
                  type: String
4f2dc7e3   梁灏   fixed #3086
161
162
163
164
165
166
              },
              wrap: {
                  validator (value) {
                      return oneOf(value, ['hard', 'soft']);
                  },
                  default: 'soft'
42d5412a   梁灏   Input add prop pr...
167
168
169
170
171
172
173
174
              },
              prefix: {
                  type: String,
                  default: ''
              },
              suffix: {
                  type: String,
                  default: ''
e2a877c4   梁灏   Input add search ...
175
176
177
178
179
180
181
182
              },
              search: {
                  type: Boolean,
                  default: false
              },
              enterButton: {
                  type: [Boolean, String],
                  default: false
75460ce6   FEI   input upgrade
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
              },
              // 4.0.0
              showWordLimit: {
                  type: Boolean,
                  default: false
              },
              // 4.0.0
              password: {
                  type: Boolean,
                  default: false
              },
              // 4.5.0
              border: {
                  type: Boolean,
                  default: true
7fa943eb   梁灏   init
198
199
200
201
              }
          },
          data () {
              return {
fc7ef072   梁灏   support Input
202
                  currentValue: this.value,
0f822c9b   梁灏   add Input component
203
                  prefixCls: prefixCls,
6ff31952   梁灏   optimize Input sh...
204
                  slotReady: false,
42d5412a   梁灏   Input add prop pr...
205
                  textareaStyles: {},
75460ce6   FEI   input upgrade
206
207
208
                  isOnComposition: false,
                  showPassword: false,
                  clearableIconOffset: 0
b0893113   jingsam   :art: add eslint
209
              };
7fa943eb   梁灏   init
210
211
          },
          computed: {
75460ce6   FEI   input upgrade
212
213
214
215
216
              currentType () {
                  let type = this.type;
                  if (type === 'password' && this.password && this.showPassword) type = 'text';
                  return type;
              },
f29a59f5   FEI   cancel
217
218
219
220
221
222
223
224
225
226
              prepend () {
                  let state = false;
                  if (this.type !== 'textarea') state = this.$slots.prepend !== undefined;
                  return state;
              },
              append () {
                  let state = false;
                  if (this.type !== 'textarea') state = this.$slots.append !== undefined;
                  return state;
              },
75460ce6   FEI   input upgrade
227
228
229
230
231
232
233
234
235
236
              showPrefix () {
                  let state = false;
                  if (this.type !== 'textarea') state = this.prefix !== '' || this.$slots.prefix !== undefined;
                  return state;
              },
              showSuffix () {
                  let state = false;
                  if (this.type !== 'textarea') state = this.suffix !== '' || this.$slots.suffix !== undefined;
                  return state;
              },
7d5431d8   梁灏   update some style
237
              wrapClasses () {
0f822c9b   梁灏   add Input component
238
239
240
                  return [
                      `${prefixCls}-wrapper`,
                      {
12418c6a   梁灏   fixed #74
241
                          [`${prefixCls}-wrapper-${this.size}`]: !!this.size,
75460ce6   FEI   input upgrade
242
                          [`${prefixCls}-type-${this.type}`]: this.type,
e2a877c4   梁灏   Input add search ...
243
244
                          [`${prefixCls}-group`]: this.prepend || this.append || (this.search && this.enterButton),
                          [`${prefixCls}-group-${this.size}`]: (this.prepend || this.append || (this.search && this.enterButton)) && !!this.size,
e2affe49   梁灏   fixed #397
245
                          [`${prefixCls}-group-with-prepend`]: this.prepend,
e2a877c4   梁灏   Input add search ...
246
247
                          [`${prefixCls}-group-with-append`]: this.append || (this.search && this.enterButton),
                          [`${prefixCls}-hide-icon`]: this.append,  // #554
75460ce6   FEI   input upgrade
248
249
                          [`${prefixCls}-with-search`]: (this.search && this.enterButton),
                          [`${prefixCls}-wrapper-disabled`]: this.itemDisabled // #685
0f822c9b   梁灏   add Input component
250
                      }
b0893113   jingsam   :art: add eslint
251
                  ];
0f822c9b   梁灏   add Input component
252
253
254
255
256
257
              },
              inputClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
75460ce6   FEI   input upgrade
258
259
                          [`${prefixCls}-disabled`]: this.itemDisabled,
                          [`${prefixCls}-no-border`]: !this.border,
42d5412a   梁灏   Input add prop pr...
260
                          [`${prefixCls}-with-prefix`]: this.showPrefix,
e2a877c4   梁灏   Input add search ...
261
                          [`${prefixCls}-with-suffix`]: this.showSuffix || (this.search && this.enterButton === false)
0f822c9b   梁灏   add Input component
262
                      }
b0893113   jingsam   :art: add eslint
263
                  ];
7d5431d8   梁灏   update some style
264
              },
0f822c9b   梁灏   add Input component
265
              textareaClasses () {
7fa943eb   梁灏   init
266
267
268
                  return [
                      `${prefixCls}`,
                      {
75460ce6   FEI   input upgrade
269
270
                          [`${prefixCls}-disabled`]: this.itemDisabled,
                          [`${prefixCls}-no-border`]: !this.border
7fa943eb   梁灏   init
271
                      }
b0893113   jingsam   :art: add eslint
272
                  ];
75460ce6   FEI   input upgrade
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
              },
              upperLimit () {
                  return this.maxlength;
              },
              textLength () {
                  if (typeof this.value === 'number') {
                      return String(this.value).length;
                  }
  
                  return (this.value || '').length;
              },
              clearableStyles () {
                  const style = {};
                  let offset = this.clearableIconOffset;
                  if (offset) style.transform = `translateX(-${offset}px)`;
                  return style;
7fa943eb   梁灏   init
289
              }
0f822c9b   梁灏   add Input component
290
291
          },
          methods: {
cd50d0d6   young   fix(input): all a...
292
293
              handleEnter (event) {
                  this.$emit('on-enter', event);
1e175649   梁灏   Input add @on-sea...
294
                  if (this.search) this.$emit('on-search', this.currentValue);
0f822c9b   梁灏   add Input component
295
              },
9e7a3740   xingbofeng   fix: add keyup, k...
296
297
298
299
300
301
302
303
304
              handleKeydown (event) {
                  this.$emit('on-keydown', event);
              },
              handleKeypress(event) {
                  this.$emit('on-keypress', event);
              },
              handleKeyup (event) {
                  this.$emit('on-keyup', event);
              },
cd50d0d6   young   fix(input): all a...
305
306
              handleIconClick (event) {
                  this.$emit('on-click', event);
0f822c9b   梁灏   add Input component
307
              },
cd50d0d6   young   fix(input): all a...
308
309
              handleFocus (event) {
                  this.$emit('on-focus', event);
0a48ac45   梁灏   Input add readonl...
310
              },
cd50d0d6   young   fix(input): all a...
311
              handleBlur (event) {
57737d74   muei   Update input.vue
312
                  this.$emit('on-blur', event);
aa9fc758   梁灏   update Transfer
313
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
21dad188   梁灏   prevent dispatch ...
314
315
                      this.dispatch('FormItem', 'on-form-blur', this.currentValue);
                  }
0a48ac45   梁灏   Input add readonl...
316
              },
f43bc792   梁灏   fix #5060
317
318
319
320
321
322
323
324
325
              handleComposition(event) {
                  if (event.type === 'compositionstart') {
                      this.isOnComposition = true;
                  }
                  if (event.type === 'compositionend') {
                      this.isOnComposition = false;
                      this.handleInput(event);
                  }
              },
fc7ef072   梁灏   support Input
326
              handleInput (event) {
f43bc792   梁灏   fix #5060
327
328
                  if (this.isOnComposition) return;
  
85ed4df8   梁灏   fixed Input bug
329
                  let value = event.target.value;
751d5c94   王亚星   fix: #4048
330
                  if (this.number && value !== '') value = Number.isNaN(Number(value)) ? value : Number(value);
fc7ef072   梁灏   support Input
331
332
                  this.$emit('input', value);
                  this.setCurrentValue(value);
e1874103   梁灏   update DatePicker
333
                  this.$emit('on-change', event);
c46f385a   梁灏   update DatePicker
334
              },
531cd165   梁灏   support DatePicke...
335
336
337
              handleChange (event) {
                  this.$emit('on-input-change', event);
              },
fc7ef072   梁灏   support Input
338
339
340
341
342
343
              setCurrentValue (value) {
                  if (value === this.currentValue) return;
                  this.$nextTick(() => {
                      this.resizeTextarea();
                  });
                  this.currentValue = value;
aa9fc758   梁灏   update Transfer
344
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
21dad188   梁灏   prevent dispatch ...
345
346
                      this.dispatch('FormItem', 'on-form-change', value);
                  }
fc7ef072   梁灏   support Input
347
              },
0f822c9b   梁灏   add Input component
348
349
350
351
352
353
354
355
356
              resizeTextarea () {
                  const autosize = this.autosize;
                  if (!autosize || this.type !== 'textarea') {
                      return false;
                  }
  
                  const minRows = autosize.minRows;
                  const maxRows = autosize.maxRows;
  
fc7ef072   梁灏   support Input
357
                  this.textareaStyles = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
545add71   yinheli   input 支持 focus 方法
358
              },
75460ce6   FEI   input upgrade
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
              focus (option) {
                  const $el = this.type === 'textarea' ? this.$refs.textarea : this.$refs.input;
                  $el.focus(option);
                  // Selection content
                  const { cursor } = option || {};
                  if (cursor) {
                      const len = $el.value.length;
  
                      switch (cursor) {
                          case 'start':
                              $el.setSelectionRange(0, 0);
                              break;
                          case 'end':
                              $el.setSelectionRange(len, len);
                              break;
                          default:
                              $el.setSelectionRange(0, len);
                      }
545add71   yinheli   input 支持 focus 方法
377
                  }
fed3e09d   梁灏   add AutoComplete ...
378
379
380
381
382
383
384
              },
              blur () {
                  if (this.type === 'textarea') {
                      this.$refs.textarea.blur();
                  } else {
                      this.$refs.input.blur();
                  }
a73ae72b   梁灏   fixed #2884
385
386
387
388
389
390
              },
              handleClear () {
                  const e = { target: { value: '' } };
                  this.$emit('input', '');
                  this.setCurrentValue('');
                  this.$emit('on-change', e);
dde57741   梁灏   fix #5527
391
                  this.$emit('on-clear');
e2a877c4   梁灏   Input add search ...
392
393
              },
              handleSearch () {
75460ce6   FEI   input upgrade
394
                  if (this.itemDisabled) return false;
e2a877c4   梁灏   Input add search ...
395
                  this.$refs.input.focus();
1e175649   梁灏   Input add @on-sea...
396
                  this.$emit('on-search', this.currentValue);
75460ce6   FEI   input upgrade
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
              },
              handleToggleShowPassword () {
                  if (this.itemDisabled) return false;
                  this.showPassword = !this.showPassword;
                  this.focus();
                  const len = this.currentValue.length;
                  setTimeout(() => {
                      this.$refs.input.setSelectionRange(len, len);
                  }, 0);
              },
              handleCalcIconOffset () {
                  const $el = this.$el.querySelectorAll('.ivu-input-group-append')[0];
                  if ($el) {
                      this.clearableIconOffset = $el.offsetWidth;
                  } else {
                      this.clearableIconOffset = 0;
                  }
0f822c9b   梁灏   add Input component
414
415
416
              }
          },
          watch: {
fc7ef072   梁灏   support Input
417
418
              value (val) {
                  this.setCurrentValue(val);
75460ce6   FEI   input upgrade
419
420
421
              },
              type () {
                  this.$nextTick(this.handleCalcIconOffset);
0f822c9b   梁灏   add Input component
422
423
              }
          },
fc7ef072   梁灏   support Input
424
          mounted () {
fc7ef072   梁灏   support Input
425
426
              this.slotReady = true;
              this.resizeTextarea();
75460ce6   FEI   input upgrade
427
428
429
430
              this.handleCalcIconOffset();
          },
          updated () {
              this.$nextTick(this.handleCalcIconOffset);
7fa943eb   梁灏   init
431
          }
b0893113   jingsam   :art: add eslint
432
433
      };
  </script>