From 3b03f87a02458f719e2eb4bf112a13441b427d14 Mon Sep 17 00:00:00 2001 From: ‘chensiAb’ <‘chenchenco03@163.com’> Date: Tue, 25 Mar 2025 13:54:34 +0800 Subject: [PATCH] Merge branch 'master' of ssh://dev.zhiheiot.com:29418/mob-components --- forms/form/CForm.vue | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 109 insertions(+), 8 deletions(-) diff --git a/forms/form/CForm.vue b/forms/form/CForm.vue index 8e42097..7245951 100644 --- a/forms/form/CForm.vue +++ b/forms/form/CForm.vue @@ -1,19 +1,44 @@ /** * CForm - 表单套装组件,套件的主体 + * 表单容器组件,用于管理表单数据、处理表单验证和提交 + * 支持自动滚动到错误位置,提供表单项变化回调和表单完成回调 + * 提供手动提交、预验证和错误设置等功能 * @author Tevin */ <template> - <form - class="c-form" - @submit="evt=>handleSubmit()" + <view + :class="'c-form-outer ' + (autoScrollToError==='on'?'auto-scroll':'')" + ref="formOuter" > - <slot :formRes="formRes" /> - </form> + <form + class="c-form" + v-if="autoScrollToError==='off'" + @submit="evt=>handleSubmit()" + > + <slot :formRes="formRes" /> + </form> + <scroll-view + class="c-form-scroller" + v-else-if="autoScrollToError==='on'" + :scrollY="true" + :scroll-top="scrollTop" + :scroll-with-animation="true" + @scroll="evt => handleScroll(evt)" + > + <form + class="c-form" + @submit="evt=>handleSubmit()" + > + <slot :formRes="formRes" /> + </form> + </scroll-view> + </view> </template> <script> import Taro from '@tarojs/taro'; +import { $ } from '@tarojs/extend'; import './cForm.scss'; export default { @@ -21,6 +46,11 @@ props: { // 表单数据主体 formData: Object, + // 是否自动滚动到错误位置,on/off + autoScrollToError: { + type: String, + default: 'off', + }, // 表单项变化的回调 // 回调传参只有单项数据 onChange: Function, @@ -35,9 +65,14 @@ formData: this.formData, }, validators: {}, + scrollTop: 0, + offsetTop: 0, }; }, methods: { + handleScroll(evt) { + this.scrollTop = evt.target.scrollTop; + }, handleSubmit() { const checklist = []; // 所有项验证 @@ -51,11 +86,14 @@ for (let validation of validations) { // 第一个不通过项提示 if (!validation.passed) { + if (this.autoScrollToError === 'on') { + this.scrollTop = validation.offset.top - this.offsetTop; + } Taro.showToast({ title: validation.msg, icon: 'none', mask: false, - duration: 2000, + duration: validation.msg.length < 16 ? 2000 : 3000, }); return; } @@ -64,7 +102,12 @@ this.onFinish && this.onFinish(); }); }, - preCheckValidators(keys, callback) { + // 手工提交 + $submit() { + this.handleSubmit(); + }, + // 提前验证指定项 + $preVerify(keys, callback) { const checklist = []; // 从选定的项中验证 keys.forEach(key => { @@ -77,11 +120,14 @@ for (let validation of validations) { // 第一个不通过项提示 if (!validation.passed) { + if (this.autoScrollToError === 'on') { + this.scrollTop = validation.offset.top - this.offsetTop; + } Taro.showToast({ title: validation.msg, icon: 'none', mask: false, - duration: 2000, + duration: validation.msg.length < 16 ? 2000 : 3000, }); // 检查失败 callback && callback(false); @@ -91,6 +137,51 @@ // 所有检查通过 callback && callback(true); }); + }, + // 直接设定错误 + $setErrors(errors) { + const checklist = []; + const unchecks = []; + Object.keys(errors).forEach(errorKey => { + if (typeof this.validators[errorKey] !== 'undefined') { + checklist.push( + this.validators[errorKey]('setError', errors[errorKey]), + ); + } else { + unchecks.push(errors[errorKey]); + } + }); + if (checklist.length > 0) { + Promise.all(checklist).then(validations => { + for (let validation of validations) { + // 第一个不通过项提示 + if (!validation.passed) { + if (this.autoScrollToError === 'on') { + this.scrollTop = validation.offset.top - this.offsetTop; + } + Taro.showToast({ + title: validation.msg, + icon: 'none', + mask: false, + duration: validation.msg.length < 16 ? 2000 : 3000, + }); + return; + } + } + }); + } else if (unchecks.length > 0) { + Taro.showToast({ + title: unchecks[0], + icon: 'none', + mask: false, + duration: unchecks[0] < 16 ? 2000 : 3000, + }); + } + }, + $setScrollTop(top) { + if (this.autoScrollToError === 'on') { + this.scrollTop = top; + } }, }, mounted() { @@ -110,6 +201,16 @@ this.formRes.$regItemValidator = (name, cb) => { this.validators[name] = cb; }; + // 表单滚动偏移 + if (this.autoScrollToError === 'on') { + setTimeout(() => { + $(this.$refs.formOuter) + .offset() + .then(offset => { + this.offsetTop = offset.top + 10; + }); + }, 10); + } }, beforeDestroy() { this.validators = {}; -- Gitblit v1.9.1