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