Blame view

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