Blame view

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