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