Blame view

src/components/input/input.vue 12.7 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>
67296591   梁灏   fixed #5296
5
              <i class="ivu-icon" :class="['ivu-icon-ios-close-circle', prefixCls + '-icon', prefixCls + '-icon-clear' , prefixCls + '-icon-normal']" v-if="clearable && currentValue && !disabled" @click="handleClear"></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>
fc7ef072   梁灏   support Input
9
              <transition name="fade">
509014fb   梁灏   update Icons
10
                  <i class="ivu-icon ivu-icon-ios-loading ivu-load-loop" :class="[prefixCls + '-icon', prefixCls + '-icon-validate']" v-if="!icon"></i>
fc7ef072   梁灏   support Input
11
              </transition>
0f822c9b   梁灏   add Input component
12
              <input
acb79ba3   梁灏   fixed #433
13
                  :id="elementId"
9968ddea   zero   Input increase au...
14
                  :autocomplete="autocomplete"
c17c5ad6   Sergio Crisostomo   normalize autocom...
15
                  :spellcheck="spellcheck"
545add71   yinheli   input 支持 focus 方法
16
                  ref="input"
6c145a04   梁灏   fixed #77
17
                  :type="type"
0f822c9b   梁灏   add Input component
18
19
20
21
                  :class="inputClasses"
                  :placeholder="placeholder"
                  :disabled="disabled"
                  :maxlength="maxlength"
0a48ac45   梁灏   Input add readonl...
22
                  :readonly="readonly"
731d69a2   梁灏   fixed #101
23
                  :name="name"
fc7ef072   梁灏   support Input
24
                  :value="currentValue"
c3a9f389   梁灏   update Input
25
                  :number="number"
f15c216a   丁强   Input 组件增加autofoc...
26
                  :autofocus="autofocus"
0a48ac45   梁灏   Input add readonl...
27
                  @keyup.enter="handleEnter"
9e7a3740   xingbofeng   fix: add keyup, k...
28
29
30
                  @keyup="handleKeyup"
                  @keypress="handleKeypress"
                  @keydown="handleKeydown"
0a48ac45   梁灏   Input add readonl...
31
                  @focus="handleFocus"
c46f385a   梁灏   update DatePicker
32
                  @blur="handleBlur"
f43bc792   梁灏   fix #5060
33
34
35
                  @compositionstart="handleComposition"
                  @compositionupdate="handleComposition"
                  @compositionend="handleComposition"
531cd165   梁灏   support DatePicke...
36
37
                  @input="handleInput"
                  @change="handleChange">
319f5f86   梁灏   fixed #554
38
              <div :class="[prefixCls + '-group-append']" v-if="append" v-show="slotReady"><slot name="append"></slot></div>
e2a877c4   梁灏   Input add search ...
39
40
41
42
              <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...
43
              <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
44
45
          </template>
          <textarea
0f822c9b   梁灏   add Input component
46
              v-else
acb79ba3   梁灏   fixed #433
47
              :id="elementId"
4f2dc7e3   梁灏   fixed #3086
48
              :wrap="wrap"
acb79ba3   梁灏   fixed #433
49
              :autocomplete="autocomplete"
c17c5ad6   Sergio Crisostomo   normalize autocom...
50
              :spellcheck="spellcheck"
fc7ef072   梁灏   support Input
51
              ref="textarea"
0f822c9b   梁灏   add Input component
52
53
              :class="textareaClasses"
              :style="textareaStyles"
7d5431d8   梁灏   update some style
54
              :placeholder="placeholder"
0f822c9b   梁灏   add Input component
55
56
57
              :disabled="disabled"
              :rows="rows"
              :maxlength="maxlength"
0a48ac45   梁灏   Input add readonl...
58
              :readonly="readonly"
731d69a2   梁灏   fixed #101
59
              :name="name"
a2f27a80   Lawrence Lee   fix #1084
60
              :value="currentValue"
67a87e8d   Aresn   fixed #1017
61
              :autofocus="autofocus"
0a48ac45   梁灏   Input add readonl...
62
              @keyup.enter="handleEnter"
9e7a3740   xingbofeng   fix: add keyup, k...
63
64
65
              @keyup="handleKeyup"
              @keypress="handleKeypress"
              @keydown="handleKeydown"
0a48ac45   梁灏   Input add readonl...
66
              @focus="handleFocus"
c46f385a   梁灏   update DatePicker
67
              @blur="handleBlur"
f43bc792   梁灏   fix #5060
68
69
70
              @compositionstart="handleComposition"
              @compositionupdate="handleComposition"
              @compositionend="handleComposition"
fc7ef072   梁灏   support Input
71
              @input="handleInput">
0f822c9b   梁灏   add Input component
72
          </textarea>
7d5431d8   梁灏   update some style
73
      </div>
7fa943eb   梁灏   init
74
75
  </template>
  <script>
21dad188   梁灏   prevent dispatch ...
76
      import { oneOf, findComponentUpward } from '../../utils/assist';
0f822c9b   梁灏   add Input component
77
      import calcTextareaHeight from '../../utils/calcTextareaHeight';
cd78c9c4   梁灏   some comps suppor...
78
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
79
80
81
82
  
      const prefixCls = 'ivu-input';
  
      export default {
06322514   梁灏   support Radio
83
          name: 'Input',
cd78c9c4   梁灏   some comps suppor...
84
          mixins: [ Emitter ],
7fa943eb   梁灏   init
85
86
          props: {
              type: {
0f822c9b   梁灏   add Input component
87
                  validator (value) {
344a6c4e   梁灏   fixed #5422
88
                      return oneOf(value, ['text', 'textarea', 'password', 'url', 'email', 'date', 'number', 'tel']);
0f822c9b   梁灏   add Input component
89
                  },
7fa943eb   梁灏   init
90
91
92
93
                  default: 'text'
              },
              value: {
                  type: [String, Number],
fc7ef072   梁灏   support Input
94
                  default: ''
7fa943eb   梁灏   init
95
              },
7fa943eb   梁灏   init
96
97
              size: {
                  validator (value) {
b6bda1dc   梁灏   update ColorPicker
98
                      return oneOf(value, ['small', 'large', 'default']);
d18a7ee8   梁灏   Input support glo...
99
100
                  },
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
101
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
7fa943eb   梁灏   init
102
                  }
0f822c9b   梁灏   add Input component
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
              },
              placeholder: {
                  type: String,
                  default: ''
              },
              maxlength: {
                  type: Number
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              icon: String,
              autosize: {
                  type: [Boolean, Object],
                  default: false
              },
              rows: {
                  type: Number,
                  default: 2
0a48ac45   梁灏   Input add readonl...
123
124
125
126
              },
              readonly: {
                  type: Boolean,
                  default: false
731d69a2   梁灏   fixed #101
127
128
129
              },
              name: {
                  type: String
c3a9f389   梁灏   update Input
130
131
132
133
              },
              number: {
                  type: Boolean,
                  default: false
f15c216a   丁强   Input 组件增加autofoc...
134
135
              },
              autofocus: {
8d3a02a5   丁强   修改input组件 autofoc...
136
137
                  type: Boolean,
                  default: false
9968ddea   zero   Input increase au...
138
              },
c17c5ad6   Sergio Crisostomo   normalize autocom...
139
140
141
142
              spellcheck: {
                  type: Boolean,
                  default: false
              },
9968ddea   zero   Input increase au...
143
              autocomplete: {
41918ed8   梁灏   update Input
144
145
146
                  validator (value) {
                      return oneOf(value, ['on', 'off']);
                  },
9968ddea   zero   Input increase au...
147
                  default: 'off'
acb79ba3   梁灏   fixed #433
148
              },
a73ae72b   梁灏   fixed #2884
149
150
151
152
              clearable: {
                  type: Boolean,
                  default: false
              },
acb79ba3   梁灏   fixed #433
153
154
              elementId: {
                  type: String
4f2dc7e3   梁灏   fixed #3086
155
156
157
158
159
160
              },
              wrap: {
                  validator (value) {
                      return oneOf(value, ['hard', 'soft']);
                  },
                  default: 'soft'
42d5412a   梁灏   Input add prop pr...
161
162
163
164
165
166
167
168
              },
              prefix: {
                  type: String,
                  default: ''
              },
              suffix: {
                  type: String,
                  default: ''
e2a877c4   梁灏   Input add search ...
169
170
171
172
173
174
175
176
              },
              search: {
                  type: Boolean,
                  default: false
              },
              enterButton: {
                  type: [Boolean, String],
                  default: false
7fa943eb   梁灏   init
177
178
179
180
              }
          },
          data () {
              return {
fc7ef072   梁灏   support Input
181
                  currentValue: this.value,
0f822c9b   梁灏   add Input component
182
183
184
                  prefixCls: prefixCls,
                  prepend: true,
                  append: true,
6ff31952   梁灏   optimize Input sh...
185
                  slotReady: false,
42d5412a   梁灏   Input add prop pr...
186
187
                  textareaStyles: {},
                  showPrefix: false,
f43bc792   梁灏   fix #5060
188
189
                  showSuffix: false,
                  isOnComposition: false
b0893113   jingsam   :art: add eslint
190
              };
7fa943eb   梁灏   init
191
192
          },
          computed: {
7d5431d8   梁灏   update some style
193
              wrapClasses () {
0f822c9b   梁灏   add Input component
194
195
196
                  return [
                      `${prefixCls}-wrapper`,
                      {
12418c6a   梁灏   fixed #74
197
                          [`${prefixCls}-wrapper-${this.size}`]: !!this.size,
0f822c9b   梁灏   add Input component
198
                          [`${prefixCls}-type`]: this.type,
e2a877c4   梁灏   Input add search ...
199
200
                          [`${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
201
                          [`${prefixCls}-group-with-prepend`]: this.prepend,
e2a877c4   梁灏   Input add search ...
202
203
204
                          [`${prefixCls}-group-with-append`]: this.append || (this.search && this.enterButton),
                          [`${prefixCls}-hide-icon`]: this.append,  // #554
                          [`${prefixCls}-with-search`]: (this.search && this.enterButton)
0f822c9b   梁灏   add Input component
205
                      }
b0893113   jingsam   :art: add eslint
206
                  ];
0f822c9b   梁灏   add Input component
207
208
209
210
211
212
              },
              inputClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
42d5412a   梁灏   Input add prop pr...
213
214
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-with-prefix`]: this.showPrefix,
e2a877c4   梁灏   Input add search ...
215
                          [`${prefixCls}-with-suffix`]: this.showSuffix || (this.search && this.enterButton === false)
0f822c9b   梁灏   add Input component
216
                      }
b0893113   jingsam   :art: add eslint
217
                  ];
7d5431d8   梁灏   update some style
218
              },
0f822c9b   梁灏   add Input component
219
              textareaClasses () {
7fa943eb   梁灏   init
220
221
222
                  return [
                      `${prefixCls}`,
                      {
0f822c9b   梁灏   add Input component
223
                          [`${prefixCls}-disabled`]: this.disabled
7fa943eb   梁灏   init
224
                      }
b0893113   jingsam   :art: add eslint
225
                  ];
7fa943eb   梁灏   init
226
              }
0f822c9b   梁灏   add Input component
227
228
          },
          methods: {
cd50d0d6   young   fix(input): all a...
229
230
              handleEnter (event) {
                  this.$emit('on-enter', event);
1e175649   梁灏   Input add @on-sea...
231
                  if (this.search) this.$emit('on-search', this.currentValue);
0f822c9b   梁灏   add Input component
232
              },
9e7a3740   xingbofeng   fix: add keyup, k...
233
234
235
236
237
238
239
240
241
              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...
242
243
              handleIconClick (event) {
                  this.$emit('on-click', event);
0f822c9b   梁灏   add Input component
244
              },
cd50d0d6   young   fix(input): all a...
245
246
              handleFocus (event) {
                  this.$emit('on-focus', event);
0a48ac45   梁灏   Input add readonl...
247
              },
cd50d0d6   young   fix(input): all a...
248
              handleBlur (event) {
57737d74   muei   Update input.vue
249
                  this.$emit('on-blur', event);
aa9fc758   梁灏   update Transfer
250
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
21dad188   梁灏   prevent dispatch ...
251
252
                      this.dispatch('FormItem', 'on-form-blur', this.currentValue);
                  }
0a48ac45   梁灏   Input add readonl...
253
              },
f43bc792   梁灏   fix #5060
254
255
256
257
258
259
260
261
262
              handleComposition(event) {
                  if (event.type === 'compositionstart') {
                      this.isOnComposition = true;
                  }
                  if (event.type === 'compositionend') {
                      this.isOnComposition = false;
                      this.handleInput(event);
                  }
              },
fc7ef072   梁灏   support Input
263
              handleInput (event) {
f43bc792   梁灏   fix #5060
264
265
                  if (this.isOnComposition) return;
  
85ed4df8   梁灏   fixed Input bug
266
                  let value = event.target.value;
751d5c94   王亚星   fix: #4048
267
                  if (this.number && value !== '') value = Number.isNaN(Number(value)) ? value : Number(value);
fc7ef072   梁灏   support Input
268
269
                  this.$emit('input', value);
                  this.setCurrentValue(value);
e1874103   梁灏   update DatePicker
270
                  this.$emit('on-change', event);
c46f385a   梁灏   update DatePicker
271
              },
531cd165   梁灏   support DatePicke...
272
273
274
              handleChange (event) {
                  this.$emit('on-input-change', event);
              },
fc7ef072   梁灏   support Input
275
276
277
278
279
280
              setCurrentValue (value) {
                  if (value === this.currentValue) return;
                  this.$nextTick(() => {
                      this.resizeTextarea();
                  });
                  this.currentValue = value;
aa9fc758   梁灏   update Transfer
281
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
21dad188   梁灏   prevent dispatch ...
282
283
                      this.dispatch('FormItem', 'on-form-change', value);
                  }
fc7ef072   梁灏   support Input
284
              },
0f822c9b   梁灏   add Input component
285
286
287
288
289
290
291
292
293
              resizeTextarea () {
                  const autosize = this.autosize;
                  if (!autosize || this.type !== 'textarea') {
                      return false;
                  }
  
                  const minRows = autosize.minRows;
                  const maxRows = autosize.maxRows;
  
fc7ef072   梁灏   support Input
294
                  this.textareaStyles = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
545add71   yinheli   input 支持 focus 方法
295
              },
fed3e09d   梁灏   add AutoComplete ...
296
              focus () {
545add71   yinheli   input 支持 focus 方法
297
298
299
300
301
                  if (this.type === 'textarea') {
                      this.$refs.textarea.focus();
                  } else {
                      this.$refs.input.focus();
                  }
fed3e09d   梁灏   add AutoComplete ...
302
303
304
305
306
307
308
              },
              blur () {
                  if (this.type === 'textarea') {
                      this.$refs.textarea.blur();
                  } else {
                      this.$refs.input.blur();
                  }
a73ae72b   梁灏   fixed #2884
309
310
311
312
313
314
              },
              handleClear () {
                  const e = { target: { value: '' } };
                  this.$emit('input', '');
                  this.setCurrentValue('');
                  this.$emit('on-change', e);
dde57741   梁灏   fix #5527
315
                  this.$emit('on-clear');
e2a877c4   梁灏   Input add search ...
316
317
              },
              handleSearch () {
1e175649   梁灏   Input add @on-sea...
318
                  if (this.disabled) return false;
e2a877c4   梁灏   Input add search ...
319
                  this.$refs.input.focus();
1e175649   梁灏   Input add @on-sea...
320
                  this.$emit('on-search', this.currentValue);
0f822c9b   梁灏   add Input component
321
322
323
              }
          },
          watch: {
fc7ef072   梁灏   support Input
324
325
              value (val) {
                  this.setCurrentValue(val);
0f822c9b   梁灏   add Input component
326
327
              }
          },
fc7ef072   梁灏   support Input
328
329
330
331
          mounted () {
              if (this.type !== 'textarea') {
                  this.prepend = this.$slots.prepend !== undefined;
                  this.append = this.$slots.append !== undefined;
42d5412a   梁灏   Input add prop pr...
332
333
                  this.showPrefix = this.prefix !== '' || this.$slots.prefix !== undefined;
                  this.showSuffix = this.suffix !== '' || this.$slots.suffix !== undefined;
fc7ef072   梁灏   support Input
334
335
336
337
338
339
              } else {
                  this.prepend = false;
                  this.append = false;
              }
              this.slotReady = true;
              this.resizeTextarea();
7fa943eb   梁灏   init
340
          }
b0893113   jingsam   :art: add eslint
341
342
      };
  </script>