feat(Field): add formatter prop (#5534)

This commit is contained in:
neverland
2020-01-09 20:52:20 +08:00
committed by GitHub
parent 47c24e192c
commit 9452444a7d
6 changed files with 200 additions and 94 deletions
+12 -1
View File
@@ -20,6 +20,7 @@ export default createComponent({
leftIcon: String,
rightIcon: String,
clearable: Boolean,
formatter: Function,
maxlength: [Number, String],
labelWidth: [Number, String],
labelClass: null,
@@ -99,7 +100,6 @@ export default createComponent({
}
},
// native maxlength not work when type = number
format(target = this.$refs.input) {
if (!target) {
return;
@@ -108,6 +108,7 @@ export default createComponent({
let { value } = target;
const { maxlength } = this;
// native maxlength not work when type is number
if (isDef(maxlength) && value.length > maxlength) {
value = value.slice(0, maxlength);
target.value = value;
@@ -124,6 +125,16 @@ export default createComponent({
}
}
if (this.formatter) {
const originValue = value;
value = this.formatter(value);
if (value !== originValue) {
target.value = value;
}
}
return value;
},