Blame view

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