Browse Source

feat:[模板管理][暂存]

ouqian
luojie 1 month ago
parent
commit
beb9e6e8c6
2 changed files with 218 additions and 69 deletions
  1. +218
    -67
      src/components/Template/DecimalInput.vue
  2. +0
    -2
      src/components/Template/mixins/formPackageMixins.js

+ 218
- 67
src/components/Template/DecimalInput.vue View File

@ -35,20 +35,52 @@ export default {
},
data() {
return {
internalValue: this.value !== null && this.value !== undefined ? String(this.value) : ''
internalValue: this.value !== null && this.value !== undefined ? String(this.value) : '',
oldValue: null,
oldPattern: null,
patternRules: [
{ name: 'NA', pattern: /^NA$/i, inputPattern: /^N?A?$/i },
{ name: 'FRACTION', pattern: /^\d+(\/\d+)*$/, inputPattern: /^(\d+\/?)*$/ }
]
};
},
watch: {
value(newVal) {
//
if (newVal === '' || newVal == null) {
this.internalValue = '';
} else {
this.internalValue = this.handleDecimalDigits(String(newVal));
}
value: {
handler(newVal) {
if (newVal === '' || newVal == null) {
this.internalValue = '';
this.oldValue = null;
this.oldPattern = null;
} else {
const strVal = String(newVal);
this.internalValue = strVal;
this.updateOldValue(strVal);
}
},
immediate: true
}
},
methods: {
updateOldValue(val) {
for (const rule of this.patternRules) {
if (rule.pattern.test(val)) {
this.oldValue = val;
this.oldPattern = rule;
return true;
}
}
return false;
},
getMatchingRule(val) {
for (const rule of this.patternRules) {
if (rule.inputPattern.test(val)) {
return rule;
}
}
return null;
},
handleInput(val) {
if (val === '') {
this.internalValue = '';
@ -56,91 +88,142 @@ export default {
return;
}
// 1.
let cleaned = val
.replace(/[^\d.-]/g, '')
.replace(/^(-)\1+/, '$1'); //
// 2.
const firstDotIndex = cleaned.indexOf('.');
if (firstDotIndex !== -1) {
const before = cleaned.slice(0, firstDotIndex);
const after = cleaned.slice(firstDotIndex + 1).replace(/\./g, '');
cleaned = before + '.' + after;
}
// 3.
if (this.decimalDigits > 0 && cleaned.includes('.')) {
const [intPart, decPart = ''] = cleaned.split('.');
cleaned = intPart + '.' + decPart.slice(0, this.decimalDigits);
} else if (this.decimalDigits === 0) {
cleaned = cleaned.split('.')[0]; //
}
// 4. . -.
if (cleaned === '.') cleaned = '0.';
else if (cleaned === '-.') cleaned = '-0.';
else if (cleaned.startsWith('.')) cleaned = '0' + cleaned;
else if (cleaned.startsWith('-.')) cleaned = '-0.' + cleaned.slice(2);
// 5. 0
if (cleaned.includes('.')) {
//
const [int, dec] = cleaned.split('.');
// -0012 -1200 00 0-0 0 -0
let newInt = int;
if (/^-?0+\d/.test(int)) {
newInt = int.replace(/^-?0+(\d)/, '$1');
} else if (int === '' || int === '-') {
newInt = int + '0';
} else if (int === '00' || /^-00+$/.test(int)) {
newInt = int.startsWith('-') ? '-0' : '0';
const upperVal = val.toUpperCase();
let cleaned = val;
let matchedRule = null;
if (this.oldPattern) {
// oldValue
if (val.startsWith(this.oldValue)) {
// inputPattern
if (this.oldPattern.inputPattern.test(val)) {
matchedRule = this.oldPattern;
cleaned = val;
} else {
// FRACTION
if (this.oldPattern.name === 'FRACTION') {
cleaned = val.replace(/[^\d/]/g, '');
const parts = cleaned.split('/');
const validParts = [];
for (let i = 0; i < parts.length; i++) {
if (parts[i] !== '') {
validParts.push(parts[i]);
}
}
cleaned = validParts.join('/');
matchedRule = this.oldPattern;
} else {
cleaned = this.oldValue || '';
this.internalValue = cleaned;
return;
}
}
} else if (this.oldPattern.inputPattern.test(val)) {
// inputPattern
matchedRule = this.oldPattern;
cleaned = val;
} else {
//
matchedRule = this.getMatchingRule(val);
if (!matchedRule) {
cleaned = this.oldValue || '';
this.internalValue = cleaned;
return;
}
}
cleaned = newInt + '.' + dec;
} else {
//
if (/^-?0+\d/.test(cleaned)) {
cleaned = cleaned.replace(/^-?0+(\d)/, '$1');
} else if (cleaned === '00' || /^-00+$/.test(cleaned)) {
cleaned = cleaned.startsWith('-') ? '-0' : '0';
matchedRule = this.getMatchingRule(val);
if (matchedRule) {
if (matchedRule.name === 'FRACTION') {
cleaned = val.replace(/[^\d/]/g, '');
const parts = cleaned.split('/');
const validParts = [];
for (let i = 0; i < parts.length; i++) {
if (parts[i] !== '') {
validParts.push(parts[i]);
}
}
cleaned = validParts.join('/');
} else {
cleaned = upperVal;
}
} else {
cleaned = val
.replace(/[^\d.-]/g, '')
.replace(/^(-)\1+/, '$1');
const firstDotIndex = cleaned.indexOf('.');
if (firstDotIndex !== -1) {
const before = cleaned.slice(0, firstDotIndex);
const after = cleaned.slice(firstDotIndex + 1).replace(/\./g, '');
cleaned = before + '.' + after;
}
if (this.decimalDigits > 0 && cleaned.includes('.')) {
const [intPart, decPart = ''] = cleaned.split('.');
cleaned = intPart + '.' + decPart.slice(0, this.decimalDigits);
} else if (this.decimalDigits === 0) {
cleaned = cleaned.split('.')[0];
}
if (cleaned === '.') cleaned = '0.';
else if (cleaned === '-.') cleaned = '-0.';
else if (cleaned.startsWith('.')) cleaned = '0' + cleaned;
else if (cleaned.startsWith('-.')) cleaned = '-0.' + cleaned.slice(2);
if (cleaned.includes('.')) {
const [int, dec] = cleaned.split('.');
let newInt = int;
if (/^-?0+\d/.test(int)) {
newInt = int.replace(/^-?0+(\d)/, '$1');
} else if (int === '' || int === '-') {
newInt = int + '0';
} else if (int === '00' || /^-00+$/.test(int)) {
newInt = int.startsWith('-') ? '-0' : '0';
}
cleaned = newInt + '.' + dec;
} else {
if (/^-?0+\d/.test(cleaned)) {
cleaned = cleaned.replace(/^-?0+(\d)/, '$1');
} else if (cleaned === '00' || /^-00+$/.test(cleaned)) {
cleaned = cleaned.startsWith('-') ? '-0' : '0';
}
}
}
// '0' ''
}
this.internalValue = cleaned;
// emit
if (cleaned === '' || cleaned === '-') {
this.$emit('input', cleaned === '-' ? '-' : '');
} else if (matchedRule && matchedRule.name !== 'FRACTION') {
this.$emit('input', cleaned);
} else if (cleaned.includes('/')) {
this.$emit('input', cleaned);
} else {
const num = parseFloat(cleaned);
this.$emit('input', isNaN(num) ? '' : num);
}
},
handleDecimalDigits(val) {
const actVal = val || this.internalValue;
let finalValue = actVal.trim();
if (finalValue === '' || finalValue === '-') {
this.internalValue = '';
this.$emit('input', '');
return;
return '';
}
const num = parseFloat(finalValue);
if (isNaN(num)) {
this.internalValue = '';
this.$emit('input', '');
return;
return '';
}
//
let formatted = String(num);
//
if (!finalValue.includes('.')) {
formatted = String(Math.floor(num));
} else {
//
const decPart = finalValue.split('.')[1];
const actualDecimalDigits = decPart.length;
const displayDecimalDigits = Math.min(actualDecimalDigits, this.decimalDigits);
@ -150,12 +233,80 @@ export default {
},
handleBlur() {
let formatted = this.handleDecimalDigits(this.internalValue);
const val = this.internalValue.trim();
if (val === '') {
this.oldValue = null;
this.oldPattern = null;
this.$emit('input', '');
this.$emit('blur', '');
return;
}
const upperVal = val.toUpperCase();
for (const rule of this.patternRules) {
if (rule.pattern.test(upperVal)) {
this.oldValue = upperVal;
this.oldPattern = rule;
this.internalValue = upperVal;
this.$emit('input', upperVal);
this.$emit('blur', upperVal);
return;
}
}
if (val.includes('/')) {
const parts = val.split('/');
const validParts = parts.filter(part => part !== '');
if (validParts.length === 0) {
if (this.oldValue) {
this.internalValue = this.oldValue;
this.$emit('input', this.oldValue);
this.$emit('blur', this.oldValue);
} else {
this.internalValue = '';
this.$emit('input', '');
this.$emit('blur', '');
}
return;
}
if (validParts.length === 1) {
const result = validParts[0];
this.oldValue = result;
this.oldPattern = this.patternRules.find(r => r.name === 'FRACTION');
this.internalValue = result;
this.$emit('input', result);
this.$emit('blur', result);
return;
}
const formattedValue = validParts.join('/');
this.oldValue = formattedValue;
this.oldPattern = this.patternRules.find(r => r.name === 'FRACTION');
this.internalValue = formattedValue;
this.$emit('input', formattedValue);
this.$emit('blur', formattedValue);
return;
}
if (this.oldValue && this.oldPattern) {
const partialMatch = this.oldPattern.inputPattern && this.oldPattern.inputPattern.test(val);
if (!partialMatch) {
this.internalValue = this.oldValue;
this.$emit('input', this.oldValue);
this.$emit('blur', this.oldValue);
return;
}
}
let formatted = this.handleDecimalDigits(val);
this.internalValue = formatted;
// emit emit
this.$emit('input', parseFloat(formatted));
this.$emit('blur', parseFloat(formatted));
}
}
};
</script>
</script>

+ 0
- 2
src/components/Template/mixins/formPackageMixins.js View File

@ -67,7 +67,6 @@ export default {
},
getRegentItem(item, fieldCode = "type") {
const type = item[fieldCode];
console.log(item, "type")
return {
label: "",
type,
@ -391,7 +390,6 @@ export default {
const {otherCode} = o;
const otherValue = formFields[otherCode] || "";
const isShow = this.isShowOtherByRadioAndOther(radioValue,o)
console.log(o,radioValue,isShow,otherValue,"radioValue")
if(isShow&&!otherValue){
errors.push({
field: key,

Loading…
Cancel
Save