|
28 | 28 | </span> |
29 | 29 | <el-input |
30 | 30 | ref="input" |
31 | | - :value="currentValue" |
| 31 | + :value="currentInputValue" |
32 | 32 | :disabled="inputNumberDisabled" |
33 | 33 | :size="inputNumberSize" |
34 | 34 | :max="max" |
|
96 | 96 | default: '' |
97 | 97 | }, |
98 | 98 | name: String, |
99 | | - label: String |
| 99 | + label: String, |
| 100 | + precision: { |
| 101 | + type: Number, |
| 102 | + validator(val) { |
| 103 | + return val >= 0 && val === parseInt(val, 10); |
| 104 | + } |
| 105 | + } |
100 | 106 | }, |
101 | 107 | data() { |
102 | 108 | return { |
|
108 | 114 | immediate: true, |
109 | 115 | handler(value) { |
110 | 116 | let newVal = value === undefined ? value : Number(value); |
111 | | - if (newVal !== undefined && isNaN(newVal)) return; |
| 117 | + if (newVal !== undefined) { |
| 118 | + if (isNaN(newVal)) { |
| 119 | + return; |
| 120 | + } |
| 121 | + if (this.precision !== undefined) { |
| 122 | + newVal = this.toPrecision(newVal, this.precision); |
| 123 | + } |
| 124 | + } |
112 | 125 | if (newVal >= this.max) newVal = this.max; |
113 | 126 | if (newVal <= this.min) newVal = this.min; |
114 | 127 | this.currentValue = newVal; |
|
123 | 136 | maxDisabled() { |
124 | 137 | return this._increase(this.value, this.step) > this.max; |
125 | 138 | }, |
126 | | - precision() { |
127 | | - const { value, step, getPrecision } = this; |
128 | | - return Math.max(getPrecision(value), getPrecision(step)); |
| 139 | + numPrecision() { |
| 140 | + const { value, step, getPrecision, precision } = this; |
| 141 | + const stepPrecision = getPrecision(step); |
| 142 | + if (precision !== undefined) { |
| 143 | + if (stepPrecision > precision) { |
| 144 | + console.warn('[Element Warn][InputNumber]precision should not be less than the decimal places of step'); |
| 145 | + } |
| 146 | + return precision; |
| 147 | + } else { |
| 148 | + return Math.max(getPrecision(value), stepPrecision); |
| 149 | + } |
129 | 150 | }, |
130 | 151 | controlsAtRight() { |
131 | 152 | return this.controlsPosition === 'right'; |
|
138 | 159 | }, |
139 | 160 | inputNumberDisabled() { |
140 | 161 | return this.disabled || (this.elForm || {}).disabled; |
| 162 | + }, |
| 163 | + currentInputValue() { |
| 164 | + const currentValue = this.currentValue; |
| 165 | + if (typeof currentValue === 'number' && this.precision !== undefined) { |
| 166 | + return currentValue.toFixed(this.precision); |
| 167 | + } else { |
| 168 | + return currentValue; |
| 169 | + } |
141 | 170 | } |
142 | 171 | }, |
143 | 172 | methods: { |
144 | 173 | toPrecision(num, precision) { |
145 | | - if (precision === undefined) precision = this.precision; |
| 174 | + if (precision === undefined) precision = this.numPrecision; |
146 | 175 | return parseFloat(parseFloat(Number(num).toFixed(precision))); |
147 | 176 | }, |
148 | 177 | getPrecision(value) { |
|
158 | 187 | _increase(val, step) { |
159 | 188 | if (typeof val !== 'number' && val !== undefined) return this.currentValue; |
160 | 189 |
|
161 | | - const precisionFactor = Math.pow(10, this.precision); |
| 190 | + const precisionFactor = Math.pow(10, this.numPrecision); |
162 | 191 | // Solve the accuracy problem of JS decimal calculation by converting the value to integer. |
163 | 192 | return this.toPrecision((precisionFactor * val + precisionFactor * step) / precisionFactor); |
164 | 193 | }, |
165 | 194 | _decrease(val, step) { |
166 | 195 | if (typeof val !== 'number' && val !== undefined) return this.currentValue; |
167 | 196 |
|
168 | | - const precisionFactor = Math.pow(10, this.precision); |
| 197 | + const precisionFactor = Math.pow(10, this.numPrecision); |
169 | 198 |
|
170 | 199 | return this.toPrecision((precisionFactor * val - precisionFactor * step) / precisionFactor); |
171 | 200 | }, |
|
183 | 212 | }, |
184 | 213 | handleBlur(event) { |
185 | 214 | this.$emit('blur', event); |
186 | | - this.$refs.input.setCurrentValue(this.currentValue); |
| 215 | + this.$refs.input.setCurrentValue(this.currentInputValue); |
187 | 216 | }, |
188 | 217 | handleFocus(event) { |
189 | 218 | this.$emit('focus', event); |
190 | 219 | }, |
191 | 220 | setCurrentValue(newVal) { |
192 | 221 | const oldVal = this.currentValue; |
| 222 | + if (typeof newVal === 'number' && this.precision !== undefined) { |
| 223 | + newVal = this.toPrecision(newVal, this.precision); |
| 224 | + } |
193 | 225 | if (newVal >= this.max) newVal = this.max; |
194 | 226 | if (newVal <= this.min) newVal = this.min; |
195 | 227 | if (oldVal === newVal) { |
196 | | - this.$refs.input.setCurrentValue(this.currentValue); |
| 228 | + this.$refs.input.setCurrentValue(this.currentInputValue); |
197 | 229 | return; |
198 | 230 | } |
199 | 231 | this.$emit('input', newVal); |
|
0 commit comments