From ebcfca0bef70ee14c1996a6543ffe9c6182a7f64 Mon Sep 17 00:00:00 2001 From: liwei Date: Fri, 17 Sep 2021 09:20:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=91=E5=B8=831.2.1=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/packages/components/el-checkbox/index.js | 5 +- src/packages/components/el-radio/index.js | 5 +- src/packages/components/el-select/index.js | 5 +- src/packages/components/index.js | 4 +- src/packages/components/object/index.js | 17 +-- src/packages/components/utils.js | 45 ++++--- src/packages/index.vue | 19 +-- .../components/example/base/demo1.vue | 17 ++- .../components/example/base/demo2.vue | 19 +-- .../components/example/base/demo3.vue | 57 +++++++++ .../components/example/dialog/demo.vue | 121 ------------------ .../components/example/dialog/demo1.vue | 11 +- .../components/example/dialog/demo3.vue | 77 +++++++++++ .../components/example/showcase/demo1.vue | 2 +- vuepressdocs/guide/base.md | 10 ++ vuepressdocs/guide/dialog.md | 14 +- vuepressdocs/guide/feature.md | 2 +- 17 files changed, 230 insertions(+), 200 deletions(-) create mode 100644 vuepressdocs/.vuepress/components/example/base/demo3.vue delete mode 100644 vuepressdocs/.vuepress/components/example/dialog/demo.vue create mode 100644 vuepressdocs/.vuepress/components/example/dialog/demo3.vue diff --git a/src/packages/components/el-checkbox/index.js b/src/packages/components/el-checkbox/index.js index 2df1674..07ea164 100644 --- a/src/packages/components/el-checkbox/index.js +++ b/src/packages/components/el-checkbox/index.js @@ -1,4 +1,4 @@ -import { createElementBySlot, createTipComponent } from '../utils' +import { createElementBySlot, createTipComponent, getOptionList } from '../utils' export default function(createElement, value, data) { const vm = this @@ -9,12 +9,13 @@ export default function(createElement, value, data) { attrs = {}, props = {}, style = {}, - items = [], name = '', keys = null, render = { before: null, after: null } } = data + const items = getOptionList(data) + data.checkAll = data.checkAll || [] data.checkAllDisabled = data.checkAllDisabled || false if (!this.inline) style.width = '' diff --git a/src/packages/components/el-radio/index.js b/src/packages/components/el-radio/index.js index ba7c929..5c27072 100644 --- a/src/packages/components/el-radio/index.js +++ b/src/packages/components/el-radio/index.js @@ -1,4 +1,4 @@ -import { createElementBySlot, createTipComponent } from '../utils' +import { createElementBySlot, createTipComponent, getOptionList } from '../utils' export default function(createElement, value, data) { const { @@ -6,10 +6,11 @@ export default function(createElement, value, data) { attrs = {}, props = {}, style = {}, - items = [], keys = null } = data + const items = getOptionList(data) + if (!this.inline) style.width = '' const nodes = [ diff --git a/src/packages/components/el-select/index.js b/src/packages/components/el-select/index.js index 371bd5f..22ee4f4 100644 --- a/src/packages/components/el-select/index.js +++ b/src/packages/components/el-select/index.js @@ -1,4 +1,4 @@ -import { createElementBySlot, createTipComponent } from '../utils' +import { createElementBySlot, createTipComponent, getOptionList } from '../utils' export default function(createElement, value, data) { const { @@ -6,11 +6,12 @@ export default function(createElement, value, data) { attrs = {}, props = {}, style = {}, - items = [], keys = null, scopedSlots = null } = data + const items = getOptionList(data) + const nodes = [ createElementBySlot.call(this, createElement, data, 'before'), createElement( diff --git a/src/packages/components/index.js b/src/packages/components/index.js index bce7c65..fccce31 100644 --- a/src/packages/components/index.js +++ b/src/packages/components/index.js @@ -183,8 +183,8 @@ export const Component = (createElement, vm, key, item) => { if (vifBool) { vm.validiteFieldSet.delete(name) } else if (!COMPFLEX_COMPONENTS.includes(item.tag)) { - // 修复联动 vif = false,清空数值 - eval(`formValues.${name} = item.default || ''`) + // 修复联动 vif = false,清空重置组件(注意:el-input-number 组件默认值0,需要单独处理); + eval(`formValues.${name} = item.default || (item.tag === 'el-input-number' ? item.props.min || 0 : '')`) vm.validiteFieldSet.add(name) vm.$refs[vm.refName] && vm.$refs[vm.refName].clearValidate(key) } else if (COMPFLEX_COMPONENTS.includes(item.tag)) { diff --git a/src/packages/components/object/index.js b/src/packages/components/object/index.js index c428e1b..96316b9 100644 --- a/src/packages/components/object/index.js +++ b/src/packages/components/object/index.js @@ -21,16 +21,13 @@ export default function(createElement, value, data) { } this.$set(data.components[key], '$item', eval(`model.${data.name}`)) const componentDataCopy = deepClone(data.components[key]) - // 联动清除子组件的验证,array/table 做数组清空 - if (!componentDataCopy.vif) { - // 复杂组件的情况 - if (componentDataCopy.isInput) { - componentDataCopy.vif = data.vifBool - } - // 子组件不包含验证表达式 - if (!componentDataCopy.requiredExpression && componentDataCopy.isInput && typeof data.vif === 'string') { - componentDataCopy.required = data.vifBool - } + // 复杂组件的情况 + if (typeof componentDataCopy.vif !== 'string') { + componentDataCopy.vif = data.vifBool + } + // 子组件不包含验证表达式 + if (!componentDataCopy.requiredExpression && componentDataCopy.hasOwnProperty('required') && componentDataCopy.isInput && typeof data.vif === 'string') { + componentDataCopy.required = data.vifBool } return Component(createElement, this, `${data.name}.${key}`, componentDataCopy) }) diff --git a/src/packages/components/utils.js b/src/packages/components/utils.js index 09657d7..20cbbb1 100644 --- a/src/packages/components/utils.js +++ b/src/packages/components/utils.js @@ -199,8 +199,8 @@ const genUnique = function() { // 创建操作按钮 const createActionButtons = function({ h, action, component, scope }) { - const { formValues } = this; - const $model = formValues; + const { formValues } = this + const $model = formValues return action.buttons.map(item => { const ifBool = item.if ? eval(item.if) : true const disabledBool = item.disabled ? eval(item.disabled) : false @@ -231,16 +231,16 @@ const createActionButtons = function({ h, action, component, scope }) { // array/table 新增操作 const addRow = function(data, formValues) { - if(data.on && data.on.add) { + if (data.on && data.on.add) { eval( `formValues.${data.name}.push(JSON.parse(JSON.stringify(data.keys)))` ) - const list = eval(`formValues.${data.name}`); + const list = eval(`formValues.${data.name}`) data.on.add({ - row: list[list.length-1], - $index: list.length-1, + row: list[list.length - 1], + $index: list.length - 1, $length: list.length - }); + }) } else { eval( `formValues.${data.name}.push(JSON.parse(JSON.stringify(data.keys)))` @@ -250,44 +250,54 @@ const addRow = function(data, formValues) { // array 删除操作 const delRowForArray = function(data, formValues, item, index) { - if(data.on && typeof data.on.delete === 'function') { + if (data.on && typeof data.on.delete === 'function') { data.on.delete({ row: item, $index: index, $length: formValues.length }, done => { - if(done) { - eval(`(formValues.${data.name}).splice(${index}, 1)`); + if (done) { + eval(`(formValues.${data.name}).splice(${index}, 1)`) } }) } else { - eval(`(formValues.${data.name}).splice(${index}, 1)`); + eval(`(formValues.${data.name}).splice(${index}, 1)`) } } // table 删除行操作 const delRowForTable = function(data, scope, formValues) { - if(data.on && typeof data.on.delete === 'function') { + if (data.on && typeof data.on.delete === 'function') { data.on.delete(scope, done => { - if(done) { + if (done) { eval(`formValues.${data.name}.splice(scope.$index, 1)`) } - }); + }) } else { eval(`formValues.${data.name}.splice(scope.$index, 1)`) } } // data transform base64 -const utoa = function (data) { +const utoa = function(data) { return btoa(unescape(encodeURIComponent(data))) } // base64 transform data -const atou = function (base64) { +const atou = function(base64) { return decodeURIComponent(escape(atob(base64))) } +// 获取(el-select/el-radio/el-checkbox)数据 +const getOptionList = function(data) { + return Array.isArray(data.items) ? data.items.map(item => { + return (typeof item === 'string') ? { label: item, value: item } : item + }) : typeof data.items === 'object' ? Object.keys(data.items).reduce((prev, next) => { + prev.push({ label: data.items[next], value: next }) + return prev + }, []) : [] +} + export { isEqual, isEmpty, @@ -305,5 +315,6 @@ export { CUSTOM_TAGS, addRow, delRowForArray, - delRowForTable + delRowForTable, + getOptionList } diff --git a/src/packages/index.vue b/src/packages/index.vue index 2dee8b2..f5cd2e1 100644 --- a/src/packages/index.vue +++ b/src/packages/index.vue @@ -216,9 +216,7 @@ export default { }, {}) this.schemaValues = JSON.parse(JSON.stringify(values)) this.formValues = { ...values, ...customData } - this.$nextTick(() => { - this.$emit('input', { ...this.formValues, ...this.model }) - }) + this.$emit('input', { ...this.formValues, ...this.model }) }, /** * label/title/slot 模版字符串 @@ -311,17 +309,12 @@ export default { component.label = component.label || '' component.style = component.style || {} component.slot = component.slot || {} + component.items = component.items || [] component.props = component.props || {} component.attrs = component.attrs || {} component.rules = component.rules || (component.required ? { required: true, message: '必填' } : null) // 监听config表达式的情况 this.watchConfigExp(component, watchConfigExp) - if (component.items) { - const list = JSON.parse(JSON.stringify(component.items)) - component.items = list.map(item => { - return (typeof item === 'string') ? { label: item, value: item } : item - }) - } switch (component.tag) { case 'p': case 'a': @@ -499,9 +492,7 @@ export default { * @description: 设置默认值 */ setDefaultValue(item) { - if (!isEmpty(item.initValue)) { - return item.initValue - } else if (!isEmpty(item.default)) { + if (!isEmpty(item.default)) { return item.default } else { return '' @@ -591,10 +582,6 @@ export default { this.validiteFieldSet.clear() // 表单重置 this.$refs[this.refName].resetFields() - // 解决 array/table 设置minLimit后 无法重置的bug - this.$nextTick(() => { - Object.assign(this.formValues, this.schemaValues) - }) } catch (ex) { // 重置数组复杂对象会报以下的一个错误,暂时可以忽略,目前发现并不影响操作 // Error: please transfer a valid prop path to form item! diff --git a/vuepressdocs/.vuepress/components/example/base/demo1.vue b/vuepressdocs/.vuepress/components/example/base/demo1.vue index 54468bb..2888a3e 100644 --- a/vuepressdocs/.vuepress/components/example/base/demo1.vue +++ b/vuepressdocs/.vuepress/components/example/base/demo1.vue @@ -1,4 +1,4 @@ -