Skip to content

Commit

Permalink
优化金额框验证
Browse files Browse the repository at this point in the history
删除表单指定类型数据的防抖相关代码
  • Loading branch information
eiixy committed Jan 4, 2021
1 parent 4d96fe1 commit 498f149
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 88 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sczts-form-generator",
"version": "0.2.2",
"version": "0.2.3",
"description": "一款基于 element-ui 的表单设计器",
"main": "index.js",
"scripts": {
Expand Down
95 changes: 20 additions & 75 deletions src/render.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,39 @@
</template>

<script>
import { Common, Random } from "sczts-helpers";
export default {
props: {
keyword: {
type: String,
default: "form_generator" + Random.string(6)
},
gutter: {
// 分栏间隔
type: Number,
default: 20
default: 20,
},
displayType: {
// label 位置
type: String,
default: "right" // right/left/top
default: "right", // right/left/top
},
labelWidth: {
// label 宽度
type: Number,
default: 100
default: 100,
},
size: {
// 表单控件大小
type: String,
default: "medium" // medium / small / mini
default: "medium", // medium / small / mini
},
forms: {
// 表单配置
type: Array
type: Array,
},
value: {
// 表单数据
type: Object,
default: () => {
return {};
}
},
},
changeDelay: {
// 监听可编辑条件项表单值的防抖延时
type: Number,
default: 1000
}
},
created() {
this.forms.forEach((form, index) => {
Expand All @@ -93,84 +83,39 @@ export default {
{
required: true,
message: form.attributes.title + "不能为空",
trigger: "blur"
}
trigger: "blur",
},
];
}
if (form.type == "amount") {
this.rules[form.key] = [
{
pattern: "/^(([1-9][0-9]*)|(([0].d{1,2}|[1-9][0-9]*.d{1,2})))$/",
message: form.attributes.title + "必须为金额",
trigger: "blur",
},
];
}
});
if (Common.empty(this.watchKeys)) {
this.$emit("beforeChange");
this.$emit("change");
}
},
data() {
return {
rules: {}
rules: {},
};
},
watch: {
formsData(val, old_val) {
this.$emit("input", val);
/**
* 监听可编辑条件项表单值的完成及改变
*/
let change = false;
let required = true;
for (let item in val) {
if (this.watchKeys.includes(item)) {
if (!val[item]) {
required = false;
break;
}
if (val[item] != old_val[item]) {
change = true;
}
}
}
if (change && required) {
this.$emit("beforeChange");
Common.debounce(
this.keyword,
() => this.$emit("change"),
this.changeDelay
)();
}
}
},
computed: {
/**
* 计算表单值
*/
formsData() {
let data = {};
this.forms.forEach(form => {
this.forms.forEach((form) => {
data[form.key] = form.value;
});
return data;
},
/**
* 获取需要监听变化的表单 keys
*/
watchKeys() {
let keys = [];
this.forms.forEach(form => {
if (
["select", "amount", "number"].includes(form.type) &&
form.attributes.required == true
) {
keys.push(form.key);
}
});
return keys;
}
},
methods: {
validate() {
return this.$refs.form_generator.validate();
}
}
methods: {},
};
</script>

Expand Down
12 changes: 0 additions & 12 deletions src/widgets/amount/input.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<template>
<el-input
class="w100"
@input.native="inputhandle"
:placeholder="item.attributes.placeholder"
:readonly="'readonly' in item.attributes ? item.attributes.readonly : false"
v-model="item.value"
Expand All @@ -14,16 +13,5 @@ import vModel from "../../vModel";
export default {
extends: vModel,
methods: {
inputhandle({ target }) {
target.value = target.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
target.value = target.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
target.value = target.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
target.value = target.value
.replace(".", "$#$")
.replace(/\./g, "")
.replace("$#$", ".");
},
},
};
</script>

0 comments on commit 498f149

Please sign in to comment.