n20-common-lib 2.22.75 → 2.22.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
| ------------ | ------------- | ----------------- | --------------------------------- |
|
|
24
24
|
| value | Number/String | undefined | 绑定值 |
|
|
25
25
|
| stringMode | Boolean | false | 高精度模式,非空绑定值和 input/change 事件值使用字符串 |
|
|
26
|
+
| isString | Boolean | false | stringMode 兼容别名;小数位超过15位时自动按 true 处理 |
|
|
26
27
|
| type | String | 'money' | 输入框类型,可选值:'money'、'rate'、'number' |
|
|
27
28
|
| maxlength | Number | 16 | 最大输入长度 |
|
|
28
29
|
| min | Number/String | -9999999999999.99 | 最小值限制;高精度模式可传字符串 |
|
|
@@ -113,7 +114,7 @@
|
|
|
113
114
|
|
|
114
115
|
### 20 位小数汇率
|
|
115
116
|
|
|
116
|
-
`stringMode`
|
|
117
|
+
`stringMode` 和 `isString` 默认关闭,原有金额/利率的精度、数值事件保持原样。任一配置开启,或 `dNum` / `format` 超过 15 位安全小数位时,组件会自动进入高精度字符串模式。输入、回显、步进、范围比较和变更事件使用高精度十进制处理,失焦时按最大位数四舍五入并移除尾部 `0`,例如 `0.200000` 回传 `"0.2"`。
|
|
117
118
|
|
|
118
119
|
下面 `cl-*` 是默认注册前缀;业务项目按自己的 `Vue.use(n20, { prefix })` 替换标签。
|
|
119
120
|
|
|
@@ -141,9 +142,9 @@ export default {
|
|
|
141
142
|
</script>
|
|
142
143
|
```
|
|
143
144
|
|
|
144
|
-
- `stringMode` 不改变 `dNum`、`format` 和 `maxlength` 默认值。`format` 仍优先于 `dNum`;有旧 `format`
|
|
145
|
+
- `stringMode` / `isString` 不改变 `dNum`、`format` 和 `maxlength` 默认值。`format` 仍优先于 `dNum`;有旧 `format` 配置时需同步调整。小数位超过 15 位时即使两个开关都为 `false`,也会自动使用字符串模式。
|
|
145
146
|
- `maxlength` 统计全部字符,默认 16。20 位小数加上 `0.` 至少需要 22;按整数位、负号和小数点配置总长度。示例的 35 可容纳默认范围的 13 位整数、负号、小数点和 20 位小数。
|
|
146
|
-
-
|
|
147
|
+
- 默认按十进制四舍五入到配置位数并移除尾部 `0`;`rangeAuto` 开启后至少按配置位数处理,并保留更多已有小数位。
|
|
147
148
|
- `min`、`max`、`step` 的字符串形式用于高精度模式;普通模式继续传 Number。`step` 使用正值。`stepStrictly` 的中点方向与原有 `Math.round` 一致。
|
|
148
149
|
- 可粘贴科学计数法,提交展开为普通十进制字符串。空值、NaN、Infinity、非十进制输入不作为有效数字。
|
|
149
150
|
- 按上下箭头改变输入框内容,提交时机与旧模式相同,在 change/blur 时同步绑定值。
|
|
@@ -36,6 +36,7 @@ import { $lc } from '../../utils/i18n/index'
|
|
|
36
36
|
import N from '../../utils/numberPor'
|
|
37
37
|
import { Decimal, parseDecimal } from '../../utils/decimal'
|
|
38
38
|
|
|
39
|
+
const MAX_SAFE_DECIMAL_PLACES = 15
|
|
39
40
|
|
|
40
41
|
export default {
|
|
41
42
|
name: 'InputNumber',
|
|
@@ -50,6 +51,11 @@ export default {
|
|
|
50
51
|
type: Boolean,
|
|
51
52
|
default: false
|
|
52
53
|
},
|
|
54
|
+
// isString 是 stringMode 的兼容别名;超过安全小数位时会自动生效。
|
|
55
|
+
isString: {
|
|
56
|
+
type: Boolean,
|
|
57
|
+
default: false
|
|
58
|
+
},
|
|
53
59
|
/**
|
|
54
60
|
* 输入框类型
|
|
55
61
|
* - money: 金额类型,默认保留2位小数,启用千分位格式化
|
|
@@ -162,7 +168,7 @@ export default {
|
|
|
162
168
|
watch: {
|
|
163
169
|
value: {
|
|
164
170
|
handler(val) {
|
|
165
|
-
if (this.
|
|
171
|
+
if (this.isStringMode()) {
|
|
166
172
|
this.setStringDisplay(val)
|
|
167
173
|
return
|
|
168
174
|
}
|
|
@@ -199,10 +205,13 @@ export default {
|
|
|
199
205
|
immediate: true
|
|
200
206
|
},
|
|
201
207
|
fNum() {
|
|
202
|
-
if (this.
|
|
208
|
+
if (this.isStringMode()) this.setStringDisplay(this.value)
|
|
203
209
|
}
|
|
204
210
|
},
|
|
205
211
|
methods: {
|
|
212
|
+
isStringMode() {
|
|
213
|
+
return this.stringMode || this.isString || this.fNum > MAX_SAFE_DECIMAL_PLACES
|
|
214
|
+
},
|
|
206
215
|
// rangeAuto 允许已有小数位超过配置值,其余情况按配置位数四舍五入。
|
|
207
216
|
formatStringValue(value, originalValue = value) {
|
|
208
217
|
const decimal = parseDecimal(value)
|
|
@@ -212,8 +221,12 @@ export default {
|
|
|
212
221
|
const places = this.rangeAuto
|
|
213
222
|
? Math.max(this.fNum, decimal.decimalPlaces(), fraction ? fraction[1].length : 0)
|
|
214
223
|
: this.fNum
|
|
215
|
-
//
|
|
216
|
-
|
|
224
|
+
// 先舍入再输出,并移除尾部 0,避免保存值与接口回显值不一致。
|
|
225
|
+
const fixedValue = decimal.toDecimalPlaces(places).toFixed(places)
|
|
226
|
+
const trimmedValue = fixedValue
|
|
227
|
+
.replace(/(\.\d*?[1-9])0+$/, '$1')
|
|
228
|
+
.replace(/\.0+$/, '')
|
|
229
|
+
return trimmedValue === '-0' ? '0' : trimmedValue
|
|
217
230
|
},
|
|
218
231
|
setStringDisplay(value) {
|
|
219
232
|
const text = this.formatStringValue(value)
|
|
@@ -266,7 +279,7 @@ export default {
|
|
|
266
279
|
},
|
|
267
280
|
stepFn(ev) {
|
|
268
281
|
if ((ev.code === 'ArrowUp' || ev.code === 'ArrowDown') && !this.disabled) {
|
|
269
|
-
if (this.
|
|
282
|
+
if (this.isStringMode()) {
|
|
270
283
|
const value = parseDecimal(this.valueStr)
|
|
271
284
|
const step = parseDecimal(this.step)
|
|
272
285
|
if (value && step && step.gt(0)) {
|
|
@@ -296,7 +309,7 @@ export default {
|
|
|
296
309
|
}
|
|
297
310
|
},
|
|
298
311
|
inputFn(valStr) {
|
|
299
|
-
if (this.
|
|
312
|
+
if (this.isStringMode()) {
|
|
300
313
|
// 编辑中的符号和小数点暂时保留,完整值在失焦时统一规范化。
|
|
301
314
|
if (['', '-', '+', '.', '-.', '+.'].includes(valStr) || parseDecimal(valStr)) {
|
|
302
315
|
this.preValue = valStr
|
|
@@ -318,7 +331,7 @@ export default {
|
|
|
318
331
|
this.changeIng = false
|
|
319
332
|
})
|
|
320
333
|
|
|
321
|
-
if (this.
|
|
334
|
+
if (this.isStringMode()) {
|
|
322
335
|
this.changeStringValue(valStr)
|
|
323
336
|
return
|
|
324
337
|
}
|