feat(input): 数字输入整数位数限制功能
- 在 fastVue.base.js 中实现整数位数配置选项 integerDigits - 添加 checkIntegerDigitsInput 函数验证整数位数输入限制 - 实现 limitIntegerDigits 函数控制数值字符串中的整数位数 - 在键盘处理和粘贴事件中集成整数位数限制逻辑 - 更新掉期交易价格精度配置支持 quantityIntegerDigits 选项 - 扩展精度辅助函数支持整数位数配置和验证规则 - 添加完整的单元测试覆盖整数位数限制功能
This commit is contained in:
@@ -297,6 +297,8 @@
|
||||
!options.append && (options.append = '');
|
||||
let precision = parseInt(options.precision) || 0;
|
||||
options.precision = precision < 0 ? 0 : precision;
|
||||
let integerDigits = parseInt(options.integerDigits) || 0;
|
||||
options.integerDigits = integerDigits > 0 ? integerDigits : 0;
|
||||
options.negative = !!options.negative;
|
||||
_options = options;
|
||||
setValue(getValue());
|
||||
@@ -336,6 +338,35 @@
|
||||
return this.value.substring(0, this.selectionStart).indexOf('.') < 0 && this.value.substring(this.selectionEnd, this.value.length).indexOf('.') < 0;
|
||||
}
|
||||
|
||||
function checkIntegerDigitsInput() {
|
||||
if (!_options.integerDigits) return true;
|
||||
let valueEnd = this.value.length;
|
||||
if (_options.append && this.value.endsWith(_options.append)) valueEnd -= _options.append.length;
|
||||
const dotIndex = this.value.indexOf('.');
|
||||
const integerEnd = dotIndex < 0 ? valueEnd : dotIndex;
|
||||
if (this.selectionStart > integerEnd) return true;
|
||||
const selectionEnd = Math.min(this.selectionEnd, integerEnd);
|
||||
const integerText = this.value.substring(0, this.selectionStart) + this.value.substring(selectionEnd, integerEnd);
|
||||
return integerText.replace(/\D/g, '').length < _options.integerDigits;
|
||||
}
|
||||
|
||||
function limitIntegerDigits(value) {
|
||||
if (!_options.integerDigits) return value;
|
||||
let digits = 0;
|
||||
let hasDot = false;
|
||||
let result = '';
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
const ch = value.charAt(index);
|
||||
if (ch === '.' || ch === '。') hasDot = true;
|
||||
if (ch >= '0' && ch <= '9' && !hasDot) {
|
||||
if (digits >= _options.integerDigits) continue;
|
||||
digits++;
|
||||
}
|
||||
result += ch;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function __keyHandle(event) {
|
||||
if (!event) return false;
|
||||
|
||||
@@ -385,6 +416,7 @@
|
||||
if (charCode < 48 || charCode > 57 && (charCode < 96 || charCode > 105)) {
|
||||
return false;
|
||||
}
|
||||
if (!checkIntegerDigitsInput.call(this)) return false;
|
||||
if (this.selectionStart === 0) {
|
||||
return !this.value || this.value.charAt(this.selectionEnd) !== '-';
|
||||
}
|
||||
@@ -445,6 +477,7 @@
|
||||
// 粘贴进来的是显示值(可能带 %/‱ 后缀),先按 __change 同款口径解析为模型值再回显,
|
||||
// 避免 setValue 把显示值再乘以 100/10000(#EQD-5914 债券价格粘贴 ×100)
|
||||
let f = '';
|
||||
this.value = limitIntegerDigits(this.value);
|
||||
if (this.value && this.value !== _options.append) {
|
||||
f = parseFloat(this.value.replaceAll(",", "")) || 0;
|
||||
if (_options.append === '%' || _options.percent == true) f /= 100;
|
||||
@@ -455,6 +488,7 @@
|
||||
if (_chnInput >= 0) {
|
||||
__onChineseInput.call(this, _chnInput);
|
||||
}
|
||||
this.value = limitIntegerDigits(this.value);
|
||||
if (!_options.append || !this.value) return;
|
||||
let appended = true;
|
||||
if (this.value !== _options.append) {
|
||||
|
||||
Reference in New Issue
Block a user