From e1f70a62b31f326a6627d7975b17dd8304af7d90 Mon Sep 17 00:00:00 2001 From: whycq <10027870+whycq@user.noreply.gitee.com> Date: 星期五, 06 十月 2023 16:23:24 +0800 Subject: [PATCH] # --- uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue | 509 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 509 insertions(+), 0 deletions(-) diff --git a/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue b/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue new file mode 100644 index 0000000..a5ae8ab --- /dev/null +++ b/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue @@ -0,0 +1,509 @@ +<template> + <view class="uni-forms-item" :class="{ 'uni-forms-item--border': border, 'is-first-border': border && isFirstBorder, 'uni-forms-item-error': msg }"> + <view class="uni-forms-item__box"> + <view class="uni-forms-item__inner" :class="['is-direction-' + labelPos]"> + <view class="uni-forms-item__label" :style="{ width: labelWid , justifyContent: justifyContent }"> + <slot name="label"> + <text v-if="required" class="is-required">*</text> + <uni-icons v-if="leftIcon" class="label-icon" size="16" :type="leftIcon" :color="iconColor" /> + <text class="label-text">{{ label }}</text> + + <view v-if="label" class="label-seat"></view> + </slot> + </view> + <view class="uni-forms-item__content" :class="{ 'is-input-error-border': msg }"><slot></slot></view> + </view> + <view + v-if="msg" + class="uni-error-message" + :class="{ 'uni-error-msg--boeder': border }" + :style="{ + paddingLeft: labelLeft + }" + > + <text class="uni-error-message-text">{{ showMsg === 'undertext' ? msg : '' }}</text> + </view> + </view> + </view> +</template> + +<script> +/** + * Field 杈撳叆妗� + * @description 姝ょ粍浠跺彲浠ュ疄鐜拌〃鍗曠殑杈撳叆涓庢牎楠岋紝鍖呮嫭 "text" 鍜� "textarea" 绫诲瀷銆� + * @tutorial https://ext.dcloud.net.cn/plugin?id=21001 + * @property {Boolean} required 鏄惁蹇呭~锛屽乏杈规樉绀虹孩鑹�"*"鍙凤紙榛樿false锛� + * @property {String} validateTrigger = [bind|submit] 鏍¢獙瑙﹀彂鍣ㄦ柟寮� 榛樿 submit 鍙�� + * @value bind 鍙戠敓鍙樺寲鏃惰Е鍙� + * @value submit 鎻愪氦鏃惰Е鍙� + * @property {String } leftIcon label宸﹁竟鐨勫浘鏍囷紝闄� uni-ui 鐨勫浘鏍囧悕绉� + * @property {String } iconColor 宸﹁竟閫氳繃icon閰嶇疆鐨勫浘鏍囩殑棰滆壊锛堥粯璁�#606266锛� + * @property {String } label 杈撳叆妗嗗乏杈圭殑鏂囧瓧鎻愮ず + * @property {Number } labelWidth label鐨勫搴︼紝鍗曚綅px锛堥粯璁�65锛� + * @property {String } labelAlign = [left|center|right] label鐨勬枃瀛楀榻愭柟寮忥紙榛樿left锛� + * @value left label 宸︿晶鏄剧ず + * @value center label 灞呬腑 + * @value right label 鍙充晶瀵归綈 + * @property {String } labelPosition = [top|left] label鐨勬枃瀛楃殑浣嶇疆锛堥粯璁eft锛� + * @value top 椤堕儴鏄剧ず label + * @value left 宸︿晶鏄剧ず label + * @property {String } errorMessage 鏄剧ず鐨勯敊璇彁绀哄唴瀹癸紝濡傛灉涓虹┖瀛楃涓叉垨鑰協alse锛屽垯涓嶆樉绀洪敊璇俊鎭� + * @property {String } name 琛ㄥ崟鍩熺殑灞炴�у悕锛屽湪浣跨敤鏍¢獙瑙勫垯鏃跺繀濉� + */ + +export default { + name: 'uniFormsItem', + props: { + // 鑷畾涔夊唴瀹� + custom: { + type: Boolean, + default: false + }, + // 鏄惁鏄剧ず鎶ラ敊淇℃伅 + showMessage: { + type: Boolean, + default: true + }, + name: String, + required: Boolean, + validateTrigger: { + type: String, + default: '' + }, + leftIcon: String, + iconColor: { + type: String, + default: '#606266' + }, + label: String, + // 宸﹁竟鏍囬鐨勫搴﹀崟浣峱x + labelWidth: { + type: [Number, String], + default: '' + }, + // 瀵归綈鏂瑰紡锛宭eft|center|right + labelAlign: { + type: String, + default: '' + }, + // lable鐨勪綅缃紝鍙�変负 left-宸﹁竟锛宼op-涓婅竟 + labelPosition: { + type: String, + default: '' + }, + errorMessage: { + type: [String, Boolean], + default: '' + }, + // 琛ㄥ崟鏍¢獙瑙勫垯 + rules: { + type: Array, + default() { + return []; + } + } + }, + data() { + return { + errorTop: false, + errorBottom: false, + labelMarginBottom: '', + errorWidth: '', + errMsg: '', + val: '', + labelPos: '', + labelWid: '', + labelAli: '', + showMsg: 'undertext', + border: false, + isFirstBorder: false, + isArray: false, + arrayField: '' + }; + }, + computed: { + msg() { + return this.errorMessage || this.errMsg; + }, + fieldStyle() { + let style = {}; + if (this.labelPos == 'top') { + style.padding = '0 0'; + this.labelMarginBottom = '6px'; + } + if (this.labelPos == 'left' && this.msg !== false && this.msg != '') { + style.paddingBottom = '0px'; + this.errorBottom = true; + this.errorTop = false; + } else if (this.labelPos == 'top' && this.msg !== false && this.msg != '') { + this.errorBottom = false; + this.errorTop = true; + } else { + // style.paddingBottom = '' + this.errorTop = false; + this.errorBottom = false; + } + return style; + }, + + // uni涓嶆敮鎸佸湪computed涓啓style.justifyContent = 'center'鐨勫舰寮忥紝鏁呯敤姝ゆ柟娉� + justifyContent() { + if (this.labelAli === 'left') return 'flex-start'; + if (this.labelAli === 'center') return 'center'; + if (this.labelAli === 'right') return 'flex-end'; + }, + labelLeft(){ + return (this.labelPos === 'left' ? parseInt(this.labelWid) : 0) + 'px' + } + }, + watch: { + validateTrigger(trigger) { + this.formTrigger = trigger; + } + }, + created() { + this.form = this.getForm(); + this.group = this.getForm('uniGroup'); + this.formRules = []; + this.formTrigger = this.validateTrigger; + // 澶勭悊 name锛屾槸鍚︽暟缁� + if (this.name && this.name.indexOf('[') !== -1 && this.name.indexOf(']') !== -1) { + this.isArray = true; + this.arrayField = this.name + // fix by mehaotian 淇敼涓嶄慨鏀圭殑鎯呭喌锛屽姩鎬佸�间笉妫�楠岀殑闂 + this.form.formData[this.name] = this.form._getValue(this.name, '') + } + }, + mounted() { + if (this.form) { + this.form.childrens.push(this); + } + this.init(); + }, + // #ifndef VUE3 + destroyed() { + if(this.__isUnmounted) return + this.unInit() + }, + // #endif + // #ifdef VUE3 + unmounted(){ + this.__isUnmounted = true + this.unInit() + }, + // #endif + methods: { + init() { + if (this.form) { + let { formRules, validator, formData, value, labelPosition, labelWidth, labelAlign, errShowType } = this.form; + this.labelPos = this.labelPosition ? this.labelPosition : labelPosition; + + if(this.label){ + this.labelWid = (this.labelWidth ? this.labelWidth : (labelWidth||70)) + }else{ + this.labelWid =( this.labelWidth ? this.labelWidth : (labelWidth||'auto')) + } + if(this.labelWid && this.labelWid !=='auto') { + this.labelWid +='px' + } + this.labelAli = this.labelAlign ? this.labelAlign : labelAlign; + + // 鍒ゆ柇绗竴涓� item + if (!this.form.isFirstBorder) { + this.form.isFirstBorder = true; + this.isFirstBorder = true; + } + + // 鍒ゆ柇 group 閲岀殑绗竴涓� item + if (this.group) { + if (!this.group.isFirstBorder) { + this.group.isFirstBorder = true; + this.isFirstBorder = true; + } + } + + this.border = this.form.border; + this.showMsg = errShowType; + let name = this.isArray ? this.arrayField : this.name; + if(!name) return + if (formRules && this.rules.length > 0) { + if (!formRules[name]) { + formRules[name] = { + rules: this.rules + } + } + validator.updateSchema(formRules); + } + this.formRules = formRules[name] || {}; + this.validator = validator; + } else { + this.labelPos = this.labelPosition || 'left'; + this.labelWid = this.labelWidth || 65; + this.labelAli = this.labelAlign || 'left'; + } + }, + unInit(){ + if (this.form) { + this.form.childrens.forEach((item, index) => { + if (item === this) { + this.form.childrens.splice(index, 1) + delete this.form.formData[item.name] + } + }) + } + }, + /** + * 鑾峰彇鐖跺厓绱犲疄渚� + */ + getForm(name = 'uniForms') { + let parent = this.$parent; + let parentName = parent.$options.name; + while (parentName !== name) { + parent = parent.$parent; + if (!parent) return false; + parentName = parent.$options.name; + } + return parent; + }, + + /** + * 绉婚櫎璇ヨ〃鍗曢」鐨勬牎楠岀粨鏋� + */ + clearValidate() { + this.errMsg = ''; + }, + /** + * 瀛愮粍浠惰皟鐢紝濡� easyinput + * @param {Object} value + */ + setValue(value) { + let name = this.isArray ? this.arrayField : this.name; + if (name) { + if (this.errMsg) this.errMsg = ''; + // 缁欑粍浠惰祴鍊� + this.form.formData[name] = this.form._getValue(name, value); + if (!this.formRules || (typeof this.formRules && JSON.stringify(this.formRules) === '{}')) return; + this.triggerCheck(this.form._getValue(this.name, value)); + } + }, + + /** + * 鏍¢獙瑙勫垯 + * @param {Object} value + */ + async triggerCheck(value,formTrigger) { + let promise = null; + this.errMsg = ''; + // fix by mehaotian 瑙e喅娌℃湁妫�楠岃鍒欑殑鎯呭喌涓嬶紝鎶涘嚭閿欒鐨勯棶棰� + if (!this.validator || Object.keys(this.formRules).length === 0) return; + const isNoField = this.isRequired(this.formRules.rules || []); + let isTrigger = this.isTrigger(this.formRules.validateTrigger, this.validateTrigger, this.form.validateTrigger); + let result = null; + if (!!isTrigger || formTrigger) { + let name = this.isArray ? this.arrayField : this.name; + result = await this.validator.validateUpdate( + { + [name]: value + }, + this.form.formData + ); + } + // 鍒ゆ柇鏄惁蹇呭~,闈炲繀濉紝涓嶅~涓嶆牎楠岋紝濉啓鎵嶆牎楠� + if (!isNoField && (value === undefined || value === '')) { + result = null; + } + const inputComp = this.form.inputChildrens.find(child => child.rename === this.name); + if ((isTrigger ||銆�formTrigger) && result && result.errorMessage) { + if (inputComp) { + inputComp.errMsg = result.errorMessage; + } + if (this.form.errShowType === 'toast') { + uni.showToast({ + title: result.errorMessage || '鏍¢獙閿欒', + icon: 'none' + }); + } + if (this.form.errShowType === 'modal') { + uni.showModal({ + title: '鎻愮ず', + content: result.errorMessage || '鏍¢獙閿欒' + }); + } + } else { + if (inputComp) { + inputComp.errMsg = ''; + } + } + + this.errMsg = !result ? '' : result.errorMessage; + // 瑙﹀彂validate浜嬩欢 + this.form.validateCheck(result ? result : null); + // typeof callback === 'function' && callback(result ? result : null); + // if (promise) return promise + return result ? result : null; + }, + /** + * 瑙﹀彂鏃舵満 + * @param {Object} event + */ + isTrigger(rule, itemRlue, parentRule) { + let rl = true; + // bind submit + if (rule === 'submit' || !rule) { + if (rule === undefined) { + if (itemRlue !== 'bind') { + if (!itemRlue) { + return parentRule === 'bind' ? true : false; + } + return false; + } + return true; + } + return false; + } + return true; + }, + // 鏄惁鏈夊繀濉瓧娈� + isRequired(rules) { + let isNoField = false; + for (let i = 0; i < rules.length; i++) { + const ruleData = rules[i]; + if (ruleData.required) { + isNoField = true; + break; + } + } + return isNoField; + } + } +}; +</script> + +<style lang="scss" > +.uni-forms-item { + position: relative; + padding: 0px; + text-align: left; + color: #333; + font-size: 14px; + // margin-bottom: 22px; +} + +.uni-forms-item__box { + position: relative; +} + +.uni-forms-item__inner { + /* #ifndef APP-NVUE */ + display: flex; + /* #endif */ + // flex-direction: row; + // align-items: center; + padding-bottom: 22px; + // margin-bottom: 22px; +} + +.is-direction-left { + flex-direction: row; +} + +.is-direction-top { + flex-direction: column; +} + +.uni-forms-item__label { + /* #ifndef APP-NVUE */ + display: flex; + flex-shrink: 0; + box-sizing: border-box; + /* #endif */ + flex-direction: row; + align-items: center; + width: 65px; + // line-height: 2; + // margin-top: 3px; + padding: 5px 0; + height: 36px; + // margin-right: 5px; + + .label-text { + font-size: 13px; + color: #666666; + } + .label-seat { + margin-right: 5px; + } +} + +.uni-forms-item__content { + /* #ifndef APP-NVUE */ + width: 100%; + box-sizing: border-box; + min-height: 36px; + /* #endif */ + flex: 1; +} + +.label-icon { + margin-right: 5px; + margin-top: -1px; +} + +// 蹇呭~ +.is-required { + // color: $uni-color-error; + color: #dd524d; + font-weight: bold; +} + +.uni-error-message { + position: absolute; + bottom: 0px; + left: 0; + text-align: left; +} + +.uni-error-message-text { + line-height: 22px; + color: #dd524d; + font-size: 12px; +} + +.uni-error-msg--boeder { + position: relative; + bottom: 0; + line-height: 22px; +} + +.is-input-error-border { + border-color: #dd524d; +} + +.uni-forms-item--border { + margin-bottom: 0; + padding: 10px 0; + // padding-bottom: 0; + border-top: 1px #eee solid; + + .uni-forms-item__inner { + padding: 0; + } +} + +.uni-forms-item-error { + // padding-bottom: 0; +} + +.is-first-border { + /* #ifndef APP-NVUE */ + border: none; + /* #endif */ + /* #ifdef APP-NVUE */ + border-width: 0; + /* #endif */ +} + +.uni-forms--no-padding { + padding: 0; +} +</style> -- Gitblit v1.9.1