Skip to content

Commit

Permalink
perf: 优化表单必填校验提醒@0.9.4-3
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed Apr 22, 2021
1 parent 8dee9b7 commit 83b9ba5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "element-pro-crud",
"version": "0.9.4-2",
"version": "0.9.4-3",
"author": "BoBo<[email protected]>",
"main": "lib/pro-crud.js",
"files": [
Expand Down
20 changes: 16 additions & 4 deletions src/component/crud-table/src/ProFormDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
<script lang="ts">
import guid from '@/utils/generator';
import { DML, fn } from '@/types/common';
import { defineComponent, PropType, ref, Ref, reactive, computed, set, watch, watchEffect } from '@vue/composition-api';
import { defineComponent, PropType, ref, Ref, reactive, computed, set, watch, watchEffect, h } from '@vue/composition-api';
import ProForm from '@/component/pro-form/src/ProForm.vue';
import { VNode } from 'vue';

const STATUS = {
CREATE: 0,
Expand Down Expand Up @@ -122,7 +123,7 @@ export default defineComponent({
},
},
setup(props, { root, emit }) {
const { $PROCRUD, $message } = root;
const { $PROCRUD, $message, $notify } = root;

const generateDialogForm: Ref<any> = ref(null);

Expand Down Expand Up @@ -255,10 +256,21 @@ export default defineComponent({
});
});
})
.catch(() => {
.catch((e) => {
// 数据校验失败
// 数据校验失败
const messages = Object.values(e)
.flat(1)
.map((_) => _.message);
const newDatas: VNode[] = [];
for (const i of messages) {
newDatas.push(h('p', {}, i));
}
$notify.error({
title: '表单校验失败,请检查',
message: h('div', { style: 'color: teal' }, newDatas),
});
btnSaveIsLoading.value = false;
$message.warning('表单校验失败,请检查');
});
};

Expand Down
11 changes: 8 additions & 3 deletions src/component/form-designer/src/FormDesigner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,14 @@ export default {
},
// 预览点确定获取表单数据
handleTest() {
this.$refs.proForm.getData().then((data) => {
this.$alert(data, '').catch(() => {});
});
this.$refs.proForm
.getData()
.then((data) => {
this.$alert(data, '').catch(() => {});
})
.catch((err) => {
this.$alert(JSON.stringify(err), '').catch(() => {});
});
},
// 生成json
handleGenerateJson() {
Expand Down
10 changes: 9 additions & 1 deletion src/component/pro-form/src/ProForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,22 @@ export default class ProForm extends Vue {
}

// ----全局api----

// 先验证再获取表单内容
getData(original: boolean) {
return new Promise((resolve, reject) => {
this.$refs.proForm.validate((valid, obj) => {
if (valid) {
resolve(original ? JSON.parse(JSON.stringify(this.models)) : this.filterFormData());
} else {
// 校验失败时focus到文本框
setTimeout(() => {
const isError: any = document.getElementsByClassName('is-error');
if (isError[0].querySelector('input')) {
isError[0].querySelector('input').focus();
} else if (isError[0].querySelector('textarea')) {
isError[0].querySelector('textarea').focus();
}
}, 100);
reject(obj);
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/component/pro-form/src/styles/grid-table-form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
outline: none;
}
.el-form-item__error {
top: calc(100% - 14px);
top: calc(100% - 10px);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
right: 0;
left: 15px;
}
}
}

0 comments on commit 83b9ba5

Please sign in to comment.