Blame view

src/components/input-number/input-number.vue 8.95 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
19
20
              </a>
          </div>
          <div :class="inputWrapClasses">
              <input
                  :class="inputClasses"
                  :disabled="disabled"
c7a67856   子凡   fix
21
                  autocomplete="off"
f15c216a   丁强   Input 组件增加autofoc...
22
                  :autofocus="autofocus"
7fa943eb   梁灏   init
23
24
25
26
                  @focus="focus"
                  @blur="blur"
                  @keydown.stop="keyDown"
                  @change="change"
7959adf7   梁灏   fixed #862
27
                  :name="name"
7a05b6e5   梁灏   fixed #1810
28
                  :value="precisionValue">
7fa943eb   梁灏   init
29
30
31
32
33
          </div>
      </div>
  </template>
  <script>
      import { oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
34
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
35
36
  
      const prefixCls = 'ivu-input-number';
95436eeb   梁灏   add InputNumber UI
37
      const iconPrefixCls = 'ivu-icon';
7fa943eb   梁灏   init
38
39
  
      function isValueNumber (value) {
8df29435   Rijn   changed regular e...
40
          return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(value + '');
7fa943eb   梁灏   init
41
42
      }
      function addNum (num1, num2) {
17e1fcf1   梁灏   init DatePicker
43
          let sq1, sq2, m;
7fa943eb   梁灏   init
44
          try {
b0893113   jingsam   :art: add eslint
45
              sq1 = num1.toString().split('.')[1].length;
7fa943eb   梁灏   init
46
47
48
49
50
          }
          catch (e) {
              sq1 = 0;
          }
          try {
b0893113   jingsam   :art: add eslint
51
              sq2 = num2.toString().split('.')[1].length;
7fa943eb   梁灏   init
52
53
54
55
56
57
58
59
60
61
62
          }
          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...
63
          return (Math.round(num1 * m) + Math.round(num2 * m)) / m;
7fa943eb   梁灏   init
64
65
66
      }
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
67
          name: 'InputNumber',
cd78c9c4   梁灏   some comps suppor...
68
          mixins: [ Emitter ],
7fa943eb   梁灏   init
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
          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...
88
                      return oneOf(value, ['small', 'large', 'default']);
7fa943eb   梁灏   init
89
90
91
92
93
                  }
              },
              disabled: {
                  type: Boolean,
                  default: false
f15c216a   丁强   Input 组件增加autofoc...
94
95
              },
              autofocus: {
8d3a02a5   丁强   修改input组件 autofoc...
96
97
                  type: Boolean,
                  default: false
7959adf7   梁灏   fixed #862
98
99
100
              },
              name: {
                  type: String
7a05b6e5   梁灏   fixed #1810
101
102
103
              },
              precision: {
                  type: Number
7fa943eb   梁灏   init
104
105
106
107
108
109
              }
          },
          data () {
              return {
                  focused: false,
                  upDisabled: false,
c97c42ab   梁灏   support InputNumber
110
111
                  downDisabled: false,
                  currentValue: this.value
b0893113   jingsam   :art: add eslint
112
              };
7fa943eb   梁灏   init
113
114
115
116
117
118
119
120
121
122
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-focused`]: this.focused
                      }
b0893113   jingsam   :art: add eslint
123
                  ];
7fa943eb   梁灏   init
124
125
126
127
128
129
130
131
132
133
134
              },
              handlerClasses () {
                  return `${prefixCls}-handler-wrap`;
              },
              upClasses () {
                  return [
                      `${prefixCls}-handler`,
                      `${prefixCls}-handler-up`,
                      {
                          [`${prefixCls}-handler-up-disabled`]: this.upDisabled
                      }
b0893113   jingsam   :art: add eslint
135
                  ];
7fa943eb   梁灏   init
136
137
              },
              innerUpClasses () {
95436eeb   梁灏   add InputNumber UI
138
                  return `${prefixCls}-handler-up-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-up`;
7fa943eb   梁灏   init
139
140
141
142
143
144
145
146
              },
              downClasses () {
                  return [
                      `${prefixCls}-handler`,
                      `${prefixCls}-handler-down`,
                      {
                          [`${prefixCls}-handler-down-disabled`]: this.downDisabled
                      }
b0893113   jingsam   :art: add eslint
147
                  ];
7fa943eb   梁灏   init
148
149
              },
              innerDownClasses () {
95436eeb   梁灏   add InputNumber UI
150
                  return `${prefixCls}-handler-down-inner ${iconPrefixCls} ${iconPrefixCls}-ios-arrow-down`;
7fa943eb   梁灏   init
151
152
153
              },
              inputWrapClasses () {
                  return `${prefixCls}-input-wrap`;
95436eeb   梁灏   add InputNumber UI
154
155
156
              },
              inputClasses () {
                  return `${prefixCls}-input`;
7a05b6e5   梁灏   fixed #1810
157
158
159
160
              },
              precisionValue () {
                  // can not display 1.0
                  return this.currentValue.toFixed(this.precision);
7fa943eb   梁灏   init
161
162
163
164
165
166
              }
          },
          methods: {
              preventDefault (e) {
                  e.preventDefault();
              },
1ff55186   梁灏   optimize InputNum...
167
168
169
              up (e) {
                  const targetVal = Number(e.target.value);
                  if (this.upDisabled && isNaN(targetVal)) {
7fa943eb   梁灏   init
170
171
                      return false;
                  }
1ff55186   梁灏   optimize InputNum...
172
                  this.changeStep('up', e);
7fa943eb   梁灏   init
173
              },
1ff55186   梁灏   optimize InputNum...
174
175
176
              down (e) {
                  const targetVal = Number(e.target.value);
                  if (this.downDisabled && isNaN(targetVal)) {
7fa943eb   梁灏   init
177
178
                      return false;
                  }
1ff55186   梁灏   optimize InputNum...
179
                  this.changeStep('down', e);
7fa943eb   梁灏   init
180
              },
1ff55186   梁灏   optimize InputNum...
181
              changeStep (type, e) {
7fa943eb   梁灏   init
182
183
184
185
                  if (this.disabled) {
                      return false;
                  }
  
1ff55186   梁灏   optimize InputNum...
186
                  const targetVal = Number(e.target.value);
c97c42ab   梁灏   support InputNumber
187
                  let val = Number(this.currentValue);
7fa943eb   梁灏   init
188
189
190
191
192
                  const step = Number(this.step);
                  if (isNaN(val)) {
                      return false;
                  }
  
1ff55186   梁灏   optimize InputNum...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
                  // 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
211
                      val = addNum(val, step);
1ff55186   梁灏   optimize InputNum...
212
                  } else if (type === 'down') {
7fa943eb   梁灏   init
213
214
215
216
217
                      val = addNum(val, -step);
                  }
                  this.setValue(val);
              },
              setValue (val) {
7a05b6e5   梁灏   fixed #1810
218
                  val = Number(Number(val).toFixed(this.precision));
0b94936a   梁灏   fixed #28
219
                  this.$nextTick(() => {
c97c42ab   梁灏   support InputNumber
220
221
                      this.currentValue = val;
                      this.$emit('input', val);
4a260ed5   梁灏   update InputNumber
222
                      this.$emit('on-change', val);
cd78c9c4   梁灏   some comps suppor...
223
                      this.dispatch('FormItem', 'on-form-change', val);
0b94936a   梁灏   fixed #28
224
                  });
7fa943eb   梁灏   init
225
226
227
228
229
230
231
232
233
234
              },
              focus () {
                  this.focused = true;
              },
              blur () {
                  this.focused = false;
              },
              keyDown (e) {
                  if (e.keyCode === 38) {
                      e.preventDefault();
1ff55186   梁灏   optimize InputNum...
235
                      this.up(e);
7fa943eb   梁灏   init
236
237
                  } else if (e.keyCode === 40) {
                      e.preventDefault();
1ff55186   梁灏   optimize InputNum...
238
                      this.down(e);
7fa943eb   梁灏   init
239
240
241
242
243
244
245
246
247
248
                  }
              },
              change (event) {
                  let val = event.target.value.trim();
  
                  const max = this.max;
                  const min = this.min;
  
                  if (isValueNumber(val)) {
                      val = Number(val);
c97c42ab   梁灏   support InputNumber
249
                      this.currentValue = val;
0b94936a   梁灏   fixed #28
250
  
7fa943eb   梁灏   init
251
252
253
254
255
256
257
258
                      if (val > max) {
                          this.setValue(max);
                      } else if (val < min) {
                          this.setValue(min);
                      } else {
                          this.setValue(val);
                      }
                  } else {
c97c42ab   梁灏   support InputNumber
259
                      event.target.value = this.currentValue;
7fa943eb   梁灏   init
260
                  }
0b94936a   梁灏   fixed #28
261
262
              },
              changeVal (val) {
7fa943eb   梁灏   init
263
264
265
                  if (isValueNumber(val) || val === 0) {
                      val = Number(val);
                      const step = this.step;
0b94936a   梁灏   fixed #28
266
267
268
  
                      this.upDisabled = val + step > this.max;
                      this.downDisabled = val - step < this.min;
7fa943eb   梁灏   init
269
270
271
272
273
                  } else {
                      this.upDisabled = true;
                      this.downDisabled = true;
                  }
              }
0b94936a   梁灏   fixed #28
274
          },
c97c42ab   梁灏   support InputNumber
275
276
          mounted () {
              this.changeVal(this.currentValue);
0b94936a   梁灏   fixed #28
277
278
279
          },
          watch: {
              value (val) {
c97c42ab   梁灏   support InputNumber
280
281
282
                  this.currentValue = val;
              },
              currentValue (val) {
0b94936a   梁灏   fixed #28
283
                  this.changeVal(val);
fa66bfb1   Rijn   add watchers for ...
284
285
286
287
288
289
              },
              min () {
                  this.changeVal(this.currentValue);
              },
              max () {
                  this.changeVal(this.currentValue);
0b94936a   梁灏   fixed #28
290
              }
7fa943eb   梁灏   init
291
          }
b0893113   jingsam   :art: add eslint
292
      };
c7a67856   子凡   fix
293
  </script>