Blame view

src/components/input-number/input-number.vue 10.7 KB
7fa943eb   梁灏   init
1
2
3
4
5
  <template>
      <div :class="wrapClasses">
          <div :class="handlerClasses">
              <a
                  @click="up"
7fa943eb   梁灏   init
6
                  :class="upClasses">
95436eeb   梁灏   add InputNumber UI
7
                  <span :class="innerUpClasses" @click="preventDefault"></span>
7fa943eb   梁灏   init
8
9
10
              </a>
              <a
                  @click="down"
7fa943eb   梁灏   init
11
                  :class="downClasses">
95436eeb   梁灏   add InputNumber UI
12
                  <span :class="innerDownClasses" @click="preventDefault"></span>
7fa943eb   梁灏   init
13
14
15
16
              </a>
          </div>
          <div :class="inputWrapClasses">
              <input
acb79ba3   梁灏   fixed #433
17
                  :id="elementId"
7fa943eb   梁灏   init
18
19
                  :class="inputClasses"
                  :disabled="disabled"
c7a67856   子凡   fix
20
                  autocomplete="off"
c17c5ad6   Sergio Crisostomo   normalize autocom...
21
                  spellcheck="false"
f15c216a   丁强   Input 组件增加autofoc...
22
                  :autofocus="autofocus"
7fa943eb   梁灏   init
23
24
25
                  @focus="focus"
                  @blur="blur"
                  @keydown.stop="keyDown"
c82e714c   Sergio Crisostomo   call change on in...
26
                  @input="change"
3d4fd6df   angela-1   在safari浏览器中需要去除mo...
27
                  @mouseup="preventDefault"
7fa943eb   梁灏   init
28
                  @change="change"
7309b434   梁灏   InputNumber add `...
29
                  :readonly="readonly || !editable"
7959adf7   梁灏   fixed #862
30
                  :name="name"
ff3d2a2e   xiaofengsha   inputNumber组件添加pl...
31
32
                  :value="formatterValue"
                  :placeholder="placeholder">
7fa943eb   梁灏   init
33
34
35
36
37
          </div>
      </div>
  </template>
  <script>
      import { oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
38
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
39
40
  
      const prefixCls = 'ivu-input-number';
95436eeb   梁灏   add InputNumber UI
41
      const iconPrefixCls = 'ivu-icon';
7fa943eb   梁灏   init
42
  
7fa943eb   梁灏   init
43
      function addNum (num1, num2) {
17e1fcf1   梁灏   init DatePicker
44
          let sq1, sq2, m;
7fa943eb   梁灏   init
45
          try {
b0893113   jingsam   :art: add eslint
46
              sq1 = num1.toString().split('.')[1].length;
7fa943eb   梁灏   init
47
48
49
50
51
          }
          catch (e) {
              sq1 = 0;
          }
          try {
b0893113   jingsam   :art: add eslint
52
              sq2 = num2.toString().split('.')[1].length;
7fa943eb   梁灏   init
53
54
55
56
57
58
59
60
61
62
63
          }
          catch (e) {
              sq2 = 0;
          }
  //        if (sq1 === 0 || sq2 === 0) {
  //            return num1 + num2;
  //        } else {
  //            m = Math.pow(10, Math.max(sq1, sq2));
  //            return (num1 * m + num2 * m) / m;
  //        }
          m = Math.pow(10, Math.max(sq1, sq2));
075f6215   leonisme   Fix a bug in inpu...
64
          return (Math.round(num1 * m) + Math.round(num2 * m)) / m;
7fa943eb   梁灏   init
65
66
67
      }
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
68
          name: 'InputNumber',
cd78c9c4   梁灏   some comps suppor...
69
          mixins: [ Emitter ],
7fa943eb   梁灏   init
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
          props: {
              max: {
                  type: Number,
                  default: Infinity
              },
              min: {
                  type: Number,
                  default: -Infinity
              },
              step: {
                  type: Number,
                  default: 1
              },
              value: {
                  type: Number,
                  default: 1
              },
              size: {
                  validator (value) {
f00a037c   梁灏   some Components's...
89
                      return oneOf(value, ['small', 'large', 'default']);
85468042   梁灏   InputNumber suppo...
90
91
                  },
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
92
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
7fa943eb   梁灏   init
93
94
95
96
97
                  }
              },
              disabled: {
                  type: Boolean,
                  default: false
f15c216a   丁强   Input 组件增加autofoc...
98
99
              },
              autofocus: {
8d3a02a5   丁强   修改input组件 autofoc...
100
101
                  type: Boolean,
                  default: false
7959adf7   梁灏   fixed #862
102
              },
cb486838   Sergio Crisostomo   Add readonly prop...
103
104
105
106
              readonly: {
                  type: Boolean,
                  default: false
              },
7309b434   梁灏   InputNumber add `...
107
108
109
110
              editable: {
                  type: Boolean,
                  default: true
              },
7959adf7   梁灏   fixed #862
111
112
              name: {
                  type: String
7a05b6e5   梁灏   fixed #1810
113
114
115
              },
              precision: {
                  type: Number
acb79ba3   梁灏   fixed #433
116
117
118
              },
              elementId: {
                  type: String
ce176e21   梁灏   fixed #3081
119
120
121
122
123
124
              },
              formatter: {
                  type: Function
              },
              parser: {
                  type: Function
ff3d2a2e   xiaofengsha   inputNumber组件添加pl...
125
126
127
128
129
              },
              placeholder: {
                  type: String,
                  default: ''
              },
7fa943eb   梁灏   init
130
131
132
133
134
          },
          data () {
              return {
                  focused: false,
                  upDisabled: false,
c97c42ab   梁灏   support InputNumber
135
136
                  downDisabled: false,
                  currentValue: this.value
b0893113   jingsam   :art: add eslint
137
              };
7fa943eb   梁灏   init
138
139
140
141
142
143
144
145
146
147
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-focused`]: this.focused
                      }
b0893113   jingsam   :art: add eslint
148
                  ];
7fa943eb   梁灏   init
149
150
151
152
153
154
155
156
157
158
159
              },
              handlerClasses () {
                  return `${prefixCls}-handler-wrap`;
              },
              upClasses () {
                  return [
                      `${prefixCls}-handler`,
                      `${prefixCls}-handler-up`,
                      {
                          [`${prefixCls}-handler-up-disabled`]: this.upDisabled
                      }
b0893113   jingsam   :art: add eslint
160
                  ];
7fa943eb   梁灏   init
161
162
              },
              innerUpClasses () {
95436eeb   梁灏   add InputNumber UI
163
                  return `${prefixCls}-handler-up-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-up`;
7fa943eb   梁灏   init
164
165
166
167
168
169
170
171
              },
              downClasses () {
                  return [
                      `${prefixCls}-handler`,
                      `${prefixCls}-handler-down`,
                      {
                          [`${prefixCls}-handler-down-disabled`]: this.downDisabled
                      }
b0893113   jingsam   :art: add eslint
172
                  ];
7fa943eb   梁灏   init
173
174
              },
              innerDownClasses () {
95436eeb   梁灏   add InputNumber UI
175
                  return `${prefixCls}-handler-down-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-down`;
7fa943eb   梁灏   init
176
177
178
              },
              inputWrapClasses () {
                  return `${prefixCls}-input-wrap`;
95436eeb   梁灏   add InputNumber UI
179
180
181
              },
              inputClasses () {
                  return `${prefixCls}-input`;
7a05b6e5   梁灏   fixed #1810
182
183
184
              },
              precisionValue () {
                  // can not display 1.0
4d093b50   huanghong   fixed input-numbe...
185
                  if(!this.currentValue) return this.currentValue;
cb2678c4   梁灏   fix InputNumber b...
186
                  return this.precision ? this.currentValue.toFixed(this.precision) : this.currentValue;
ce176e21   梁灏   fixed #3081
187
188
              },
              formatterValue () {
10d29acb   huanghong   InputNumber suppo...
189
                  if (this.formatter && this.precisionValue !== null) {
ce176e21   梁灏   fixed #3081
190
191
192
193
                      return this.formatter(this.precisionValue);
                  } else {
                      return this.precisionValue;
                  }
7fa943eb   梁灏   init
194
195
196
197
198
199
              }
          },
          methods: {
              preventDefault (e) {
                  e.preventDefault();
              },
1ff55186   梁灏   optimize InputNum...
200
201
202
              up (e) {
                  const targetVal = Number(e.target.value);
                  if (this.upDisabled && isNaN(targetVal)) {
7fa943eb   梁灏   init
203
204
                      return false;
                  }
1ff55186   梁灏   optimize InputNum...
205
                  this.changeStep('up', e);
7fa943eb   梁灏   init
206
              },
1ff55186   梁灏   optimize InputNum...
207
208
209
              down (e) {
                  const targetVal = Number(e.target.value);
                  if (this.downDisabled && isNaN(targetVal)) {
7fa943eb   梁灏   init
210
211
                      return false;
                  }
1ff55186   梁灏   optimize InputNum...
212
                  this.changeStep('down', e);
7fa943eb   梁灏   init
213
              },
1ff55186   梁灏   optimize InputNum...
214
              changeStep (type, e) {
cb486838   Sergio Crisostomo   Add readonly prop...
215
                  if (this.disabled || this.readonly) {
7fa943eb   梁灏   init
216
217
218
                      return false;
                  }
  
1ff55186   梁灏   optimize InputNum...
219
                  const targetVal = Number(e.target.value);
c97c42ab   梁灏   support InputNumber
220
                  let val = Number(this.currentValue);
7fa943eb   梁灏   init
221
222
223
224
225
                  const step = Number(this.step);
                  if (isNaN(val)) {
                      return false;
                  }
  
1ff55186   梁灏   optimize InputNum...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
                  // input a number, and key up or down
                  if (!isNaN(targetVal)) {
                      if (type === 'up') {
                          if (addNum(targetVal, step) <= this.max) {
                              val = targetVal;
                          } else {
                              return false;
                          }
                      } else if (type === 'down') {
                          if (addNum(targetVal, -step) >= this.min) {
                              val = targetVal;
                          } else {
                              return false;
                          }
                      }
                  }
  
                  if (type === 'up') {
7fa943eb   梁灏   init
244
                      val = addNum(val, step);
1ff55186   梁灏   optimize InputNum...
245
                  } else if (type === 'down') {
7fa943eb   梁灏   init
246
247
248
249
250
                      val = addNum(val, -step);
                  }
                  this.setValue(val);
              },
              setValue (val) {
cb2678c4   梁灏   fix InputNumber b...
251
                  // 如果 step 是小数,且没有设置 precision,是有问题的
10b42e26   Aresn   Update input-numb...
252
                  if (val && !isNaN(this.precision)) val = Number(Number(val).toFixed(this.precision));
7adf94a2   huanghong   update
253
                  
886aeeb0   huanghong   update input-number
254
                  const {min, max} = this;
7adf94a2   huanghong   update
255
256
257
258
259
260
261
262
                  if (val!==null) {
                      if (val > max) {
                          val = max;
                      } else if (val < min) {
                          val = min;
                      } 
                  }
                  
0b94936a   梁灏   fixed #28
263
                  this.$nextTick(() => {
c97c42ab   梁灏   support InputNumber
264
265
                      this.currentValue = val;
                      this.$emit('input', val);
4a260ed5   梁灏   update InputNumber
266
                      this.$emit('on-change', val);
cd78c9c4   梁灏   some comps suppor...
267
                      this.dispatch('FormItem', 'on-form-change', val);
0b94936a   梁灏   fixed #28
268
                  });
7fa943eb   梁灏   init
269
              },
e7bcd381   angela-1   实现Inputnumber在foc...
270
              focus (event) {
7fa943eb   梁灏   init
271
                  this.focused = true;
e7bcd381   angela-1   实现Inputnumber在foc...
272
                  this.$emit('on-focus', event);
7fa943eb   梁灏   init
273
274
              },
              blur () {
7fa943eb   梁灏   init
275
                  this.focused = false;
a2a18a46   Aresn   Update input-numb...
276
                  this.$emit('on-blur');
7fa943eb   梁灏   init
277
278
279
280
              },
              keyDown (e) {
                  if (e.keyCode === 38) {
                      e.preventDefault();
1ff55186   梁灏   optimize InputNum...
281
                      this.up(e);
7fa943eb   梁灏   init
282
283
                  } else if (e.keyCode === 40) {
                      e.preventDefault();
1ff55186   梁灏   optimize InputNum...
284
                      this.down(e);
7fa943eb   梁灏   init
285
286
287
                  }
              },
              change (event) {
7adf94a2   huanghong   update
288
                  
886aeeb0   huanghong   update input-number
289
                  if (event.type == 'input') return; 
7fa943eb   梁灏   init
290
                  let val = event.target.value.trim();
ce176e21   梁灏   fixed #3081
291
292
293
                  if (this.parser) {
                      val = this.parser(val);
                  }
886aeeb0   huanghong   update input-number
294
                  
8115f0b8   Sergio Crisostomo   fix regex and mak...
295
                  const isEmptyString = val.length === 0;
10d29acb   huanghong   InputNumber suppo...
296
297
298
299
                  if(isEmptyString){
                      this.setValue(null);
                      return;
                  }
886aeeb0   huanghong   update input-number
300
301
302
                  //if (event.type == 'input' && val.match(/^\-?\.?$|\.$/)) return; // prevent fire early if decimal. If no more input the change event will fire later 
                  
                  val = Number(val);
a14d6dd2   Sergio Crisostomo   fix checking when...
303
  
886aeeb0   huanghong   update input-number
304
                  if (!isNaN(val)) {
c97c42ab   梁灏   support InputNumber
305
                      this.currentValue = val;
886aeeb0   huanghong   update input-number
306
                      this.setValue(val);
7fa943eb   梁灏   init
307
                  } else {
c97c42ab   梁灏   support InputNumber
308
                      event.target.value = this.currentValue;
7fa943eb   梁灏   init
309
                  }
0b94936a   梁灏   fixed #28
310
311
              },
              changeVal (val) {
c82e714c   Sergio Crisostomo   call change on in...
312
                  val = Number(val);
886aeeb0   huanghong   update input-number
313
                  //this.setValue(val);
c82e714c   Sergio Crisostomo   call change on in...
314
                  if (!isNaN(val)) {
7fa943eb   梁灏   init
315
                      const step = this.step;
0b94936a   梁灏   fixed #28
316
317
318
  
                      this.upDisabled = val + step > this.max;
                      this.downDisabled = val - step < this.min;
7fa943eb   梁灏   init
319
320
321
322
323
                  } else {
                      this.upDisabled = true;
                      this.downDisabled = true;
                  }
              }
0b94936a   梁灏   fixed #28
324
          },
c97c42ab   梁灏   support InputNumber
325
326
          mounted () {
              this.changeVal(this.currentValue);
0b94936a   梁灏   fixed #28
327
328
329
          },
          watch: {
              value (val) {
c97c42ab   梁灏   support InputNumber
330
331
332
                  this.currentValue = val;
              },
              currentValue (val) {
0b94936a   梁灏   fixed #28
333
                  this.changeVal(val);
fa66bfb1   Rijn   add watchers for ...
334
335
336
337
338
339
              },
              min () {
                  this.changeVal(this.currentValue);
              },
              max () {
                  this.changeVal(this.currentValue);
0b94936a   梁灏   fixed #28
340
              }
7fa943eb   梁灏   init
341
          }
b0893113   jingsam   :art: add eslint
342
      };
c7a67856   子凡   fix
343
  </script>