Blame view

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