Blame view

src/components/input-number/input-number.vue 11.1 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
          </div>
      </div>
  </template>
  <script>
42589ae3   Eager   fix(InputNumber):...
37
      import { oneOf, findComponentUpward } 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
          props: {
              max: {
                  type: Number,
                  default: Infinity
              },
              min: {
                  type: Number,
                  default: -Infinity
              },
              step: {
                  type: Number,
                  default: 1
              },
01a6bcd2   dailyvuejs   polish: input-number
83
84
85
              activeChange: {
                  type: Boolean,
                  default: true
457d6c69   huanghong   add prop active-c...
86
              },
7fa943eb   梁灏   init
87
88
89
90
91
92
              value: {
                  type: Number,
                  default: 1
              },
              size: {
                  validator (value) {
f00a037c   梁灏   some Components's...
93
                      return oneOf(value, ['small', 'large', 'default']);
85468042   梁灏   InputNumber suppo...
94
95
                  },
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
96
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
7fa943eb   梁灏   init
97
98
99
100
101
                  }
              },
              disabled: {
                  type: Boolean,
                  default: false
f15c216a   丁强   Input 组件增加autofoc...
102
103
              },
              autofocus: {
8d3a02a5   丁强   修改input组件 autofoc...
104
105
                  type: Boolean,
                  default: false
7959adf7   梁灏   fixed #862
106
              },
cb486838   Sergio Crisostomo   Add readonly prop...
107
108
109
110
              readonly: {
                  type: Boolean,
                  default: false
              },
7309b434   梁灏   InputNumber add `...
111
112
113
114
              editable: {
                  type: Boolean,
                  default: true
              },
7959adf7   梁灏   fixed #862
115
116
              name: {
                  type: String
7a05b6e5   梁灏   fixed #1810
117
118
119
              },
              precision: {
                  type: Number
acb79ba3   梁灏   fixed #433
120
121
122
              },
              elementId: {
                  type: String
ce176e21   梁灏   fixed #3081
123
124
125
126
127
128
              },
              formatter: {
                  type: Function
              },
              parser: {
                  type: Function
ff3d2a2e   xiaofengsha   inputNumber组件添加pl...
129
130
131
132
133
              },
              placeholder: {
                  type: String,
                  default: ''
              },
7fa943eb   梁灏   init
134
135
136
137
138
          },
          data () {
              return {
                  focused: false,
                  upDisabled: false,
c97c42ab   梁灏   support InputNumber
139
140
                  downDisabled: false,
                  currentValue: this.value
b0893113   jingsam   :art: add eslint
141
              };
7fa943eb   梁灏   init
142
143
144
145
146
147
148
149
150
151
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-focused`]: this.focused
                      }
b0893113   jingsam   :art: add eslint
152
                  ];
7fa943eb   梁灏   init
153
154
155
156
157
158
159
160
161
162
163
              },
              handlerClasses () {
                  return `${prefixCls}-handler-wrap`;
              },
              upClasses () {
                  return [
                      `${prefixCls}-handler`,
                      `${prefixCls}-handler-up`,
                      {
                          [`${prefixCls}-handler-up-disabled`]: this.upDisabled
                      }
b0893113   jingsam   :art: add eslint
164
                  ];
7fa943eb   梁灏   init
165
166
              },
              innerUpClasses () {
95436eeb   梁灏   add InputNumber UI
167
                  return `${prefixCls}-handler-up-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-up`;
7fa943eb   梁灏   init
168
169
170
171
172
173
174
175
              },
              downClasses () {
                  return [
                      `${prefixCls}-handler`,
                      `${prefixCls}-handler-down`,
                      {
                          [`${prefixCls}-handler-down-disabled`]: this.downDisabled
                      }
b0893113   jingsam   :art: add eslint
176
                  ];
7fa943eb   梁灏   init
177
178
              },
              innerDownClasses () {
95436eeb   梁灏   add InputNumber UI
179
                  return `${prefixCls}-handler-down-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-down`;
7fa943eb   梁灏   init
180
181
182
              },
              inputWrapClasses () {
                  return `${prefixCls}-input-wrap`;
95436eeb   梁灏   add InputNumber UI
183
184
185
              },
              inputClasses () {
                  return `${prefixCls}-input`;
7a05b6e5   梁灏   fixed #1810
186
187
188
              },
              precisionValue () {
                  // can not display 1.0
4d093b50   huanghong   fixed input-numbe...
189
                  if(!this.currentValue) return this.currentValue;
cb2678c4   梁灏   fix InputNumber b...
190
                  return this.precision ? this.currentValue.toFixed(this.precision) : this.currentValue;
ce176e21   梁灏   fixed #3081
191
192
              },
              formatterValue () {
10d29acb   huanghong   InputNumber suppo...
193
                  if (this.formatter && this.precisionValue !== null) {
ce176e21   梁灏   fixed #3081
194
195
196
197
                      return this.formatter(this.precisionValue);
                  } else {
                      return this.precisionValue;
                  }
7fa943eb   梁灏   init
198
199
200
201
202
203
              }
          },
          methods: {
              preventDefault (e) {
                  e.preventDefault();
              },
1ff55186   梁灏   optimize InputNum...
204
205
206
              up (e) {
                  const targetVal = Number(e.target.value);
                  if (this.upDisabled && isNaN(targetVal)) {
7fa943eb   梁灏   init
207
208
                      return false;
                  }
1ff55186   梁灏   optimize InputNum...
209
                  this.changeStep('up', e);
7fa943eb   梁灏   init
210
              },
1ff55186   梁灏   optimize InputNum...
211
212
213
              down (e) {
                  const targetVal = Number(e.target.value);
                  if (this.downDisabled && isNaN(targetVal)) {
7fa943eb   梁灏   init
214
215
                      return false;
                  }
1ff55186   梁灏   optimize InputNum...
216
                  this.changeStep('down', e);
7fa943eb   梁灏   init
217
              },
1ff55186   梁灏   optimize InputNum...
218
              changeStep (type, e) {
cb486838   Sergio Crisostomo   Add readonly prop...
219
                  if (this.disabled || this.readonly) {
7fa943eb   梁灏   init
220
221
222
                      return false;
                  }
  
1ff55186   梁灏   optimize InputNum...
223
                  const targetVal = Number(e.target.value);
c97c42ab   梁灏   support InputNumber
224
                  let val = Number(this.currentValue);
7fa943eb   梁灏   init
225
226
227
228
229
                  const step = Number(this.step);
                  if (isNaN(val)) {
                      return false;
                  }
  
1ff55186   梁灏   optimize InputNum...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
                  // 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
248
                      val = addNum(val, step);
1ff55186   梁灏   optimize InputNum...
249
                  } else if (type === 'down') {
7fa943eb   梁灏   init
250
251
252
253
254
                      val = addNum(val, -step);
                  }
                  this.setValue(val);
              },
              setValue (val) {
cb2678c4   梁灏   fix InputNumber b...
255
                  // 如果 step 是小数,且没有设置 precision,是有问题的
10b42e26   Aresn   Update input-numb...
256
                  if (val && !isNaN(this.precision)) val = Number(Number(val).toFixed(this.precision));
42589ae3   Eager   fix(InputNumber):...
257
  
886aeeb0   huanghong   update input-number
258
                  const {min, max} = this;
7adf94a2   huanghong   update
259
260
261
262
263
                  if (val!==null) {
                      if (val > max) {
                          val = max;
                      } else if (val < min) {
                          val = min;
42589ae3   Eager   fix(InputNumber):...
264
                      }
7adf94a2   huanghong   update
265
                  }
42589ae3   Eager   fix(InputNumber):...
266
  
0b94936a   梁灏   fixed #28
267
                  this.$nextTick(() => {
c97c42ab   梁灏   support InputNumber
268
269
                      this.currentValue = val;
                      this.$emit('input', val);
4a260ed5   梁灏   update InputNumber
270
                      this.$emit('on-change', val);
cd78c9c4   梁灏   some comps suppor...
271
                      this.dispatch('FormItem', 'on-form-change', val);
0b94936a   梁灏   fixed #28
272
                  });
7fa943eb   梁灏   init
273
              },
e7bcd381   angela-1   实现Inputnumber在foc...
274
              focus (event) {
7fa943eb   梁灏   init
275
                  this.focused = true;
e7bcd381   angela-1   实现Inputnumber在foc...
276
                  this.$emit('on-focus', event);
7fa943eb   梁灏   init
277
              },
2c2d2fd4   msidolphin   fix
278
              blur (e) {
7fa943eb   梁灏   init
279
                  this.focused = false;
a2a18a46   Aresn   Update input-numb...
280
                  this.$emit('on-blur');
42589ae3   Eager   fix(InputNumber):...
281
282
283
                  if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
                      this.dispatch('FormItem', 'on-form-blur', this.currentValue);
                  }
2c2d2fd4   msidolphin   fix
284
                  if (!this.activeChange) {
f0e5fabc   Aresn   Update input-numb...
285
                      this.change(e);
2c2d2fd4   msidolphin   fix
286
                  }
7fa943eb   梁灏   init
287
288
289
290
              },
              keyDown (e) {
                  if (e.keyCode === 38) {
                      e.preventDefault();
1ff55186   梁灏   optimize InputNum...
291
                      this.up(e);
7fa943eb   梁灏   init
292
293
                  } else if (e.keyCode === 40) {
                      e.preventDefault();
1ff55186   梁灏   optimize InputNum...
294
                      this.down(e);
7fa943eb   梁灏   init
295
296
297
                  }
              },
              change (event) {
ef1465af   susiwen   Fix #5577 Slider ...
298
                  if (event.type == 'change') return;
42589ae3   Eager   fix(InputNumber):...
299
300
  
                  if (event.type == 'input' && !this.activeChange) return;
7fa943eb   梁灏   init
301
                  let val = event.target.value.trim();
ce176e21   梁灏   fixed #3081
302
303
304
                  if (this.parser) {
                      val = this.parser(val);
                  }
42589ae3   Eager   fix(InputNumber):...
305
  
8115f0b8   Sergio Crisostomo   fix regex and mak...
306
                  const isEmptyString = val.length === 0;
10d29acb   huanghong   InputNumber suppo...
307
308
309
310
                  if(isEmptyString){
                      this.setValue(null);
                      return;
                  }
42589ae3   Eager   fix(InputNumber):...
311
312
                  if (event.type == 'input' && val.match(/^\-?\.?$|\.$/)) return; // prevent fire early if decimal. If no more input the change event will fire later
  
886aeeb0   huanghong   update input-number
313
                  val = Number(val);
a14d6dd2   Sergio Crisostomo   fix checking when...
314
  
886aeeb0   huanghong   update input-number
315
                  if (!isNaN(val)) {
c97c42ab   梁灏   support InputNumber
316
                      this.currentValue = val;
886aeeb0   huanghong   update input-number
317
                      this.setValue(val);
7fa943eb   梁灏   init
318
                  } else {
c97c42ab   梁灏   support InputNumber
319
                      event.target.value = this.currentValue;
7fa943eb   梁灏   init
320
                  }
0b94936a   梁灏   fixed #28
321
322
              },
              changeVal (val) {
c82e714c   Sergio Crisostomo   call change on in...
323
                  val = Number(val);
c82e714c   Sergio Crisostomo   call change on in...
324
                  if (!isNaN(val)) {
7fa943eb   梁灏   init
325
                      const step = this.step;
0b94936a   梁灏   fixed #28
326
327
328
  
                      this.upDisabled = val + step > this.max;
                      this.downDisabled = val - step < this.min;
7fa943eb   梁灏   init
329
330
331
332
333
                  } else {
                      this.upDisabled = true;
                      this.downDisabled = true;
                  }
              }
0b94936a   梁灏   fixed #28
334
          },
c97c42ab   梁灏   support InputNumber
335
336
          mounted () {
              this.changeVal(this.currentValue);
0b94936a   梁灏   fixed #28
337
338
339
          },
          watch: {
              value (val) {
c97c42ab   梁灏   support InputNumber
340
341
342
                  this.currentValue = val;
              },
              currentValue (val) {
0b94936a   梁灏   fixed #28
343
                  this.changeVal(val);
fa66bfb1   Rijn   add watchers for ...
344
345
346
347
348
349
              },
              min () {
                  this.changeVal(this.currentValue);
              },
              max () {
                  this.changeVal(this.currentValue);
0b94936a   梁灏   fixed #28
350
              }
7fa943eb   梁灏   init
351
          }
b0893113   jingsam   :art: add eslint
352
      };
c7a67856   子凡   fix
353
  </script>