diff --git a/src/behavior.js b/src/behavior.js index 592c79d9e0eae248f870501c6718d6d79a7518d4..788691425d3d1af4cfa8fa809e5954039284a177 100644 --- a/src/behavior.js +++ b/src/behavior.js @@ -26,7 +26,9 @@ export const HandleClass = { }, 'changeParams': { name: '修改business,请求参数', - fun: ({ behavior = {}, formData, componentData, value }, resertValue) => { + fun: ({ behavior = {}, formData, componentData, value, handleType }, resertValue) => { + // handleType 为init时不处理值的问题 + const newFormData = formData.slice(); const targets = (behavior.targets || '').split(','); targets.forEach(ls => { @@ -40,7 +42,10 @@ export const HandleClass = { paramsJson, } resertValue && resertValue({ key: ls }); - obj.value = void 0; //将目标组件清空 + if (handleType !== 'init') { + obj.value = void 0; //将目标组件清空 + } + }); return newFormData diff --git a/src/index.js b/src/index.js index f46e8f86ab61aa5d714d22593a3fdcf502d11aac..e41c70784f65d450f12e0b28d0b8c60d3ebb9646 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ class HandleBehavior { this.formData.map(item => { if (!item.behaviors || !Array.isArray(item.behaviors) || item.behaviors.length === 0) return; const value = this.handleFormaterValue(item.value, item); - this.handleBehavior({ currentValue: value, componentData: item }) + this.handleBehavior({ currentValue: value, componentData: item, handleType: 'init' }) }) return this; } @@ -33,28 +33,28 @@ class HandleBehavior { /* * 处理行为方法 */ - handleBehavior = ({ currentValue, componentData = {}, eventType, formData }) => { + handleBehavior = ({ currentValue, componentData = {}, eventType, formData, handleType }) => { this.formData = formData || this.formData; const value = this.handleFormaterValue((currentValue || componentData.value), componentData); const events = componentData.events || [] const behaviors = componentData.behaviors || []; const changeEvents = eventType ? events.filter(ls => ls.eventValue === 'onChange') : events; changeEvents.forEach(event => { - this.onChangeBehavior({ event, value, componentData, behaviors }) + this.onChangeBehavior({ event, value, componentData, behaviors, handleType }) }); } - onChangeBehavior = ({ event, value, componentData, behaviors }) => { + onChangeBehavior = ({ event, value, componentData, behaviors, handleType }) => { if (!event.behaviors || !Array.isArray(event.behaviors)) return; event.behaviors.forEach(item => { - this.handleBehaviorType({ value, behavior: item, componentData, behaviors }); + this.handleBehaviorType({ value, behavior: item, componentData, behaviors, handleType }); }); } - handleBehaviorType = ({ value, behavior, behaviors, componentData }) => { + handleBehaviorType = ({ value, behavior, behaviors, componentData, handleType }) => { const behaviorInstance = HandleClass[behavior.behaviorValue]; if (!behaviorInstance || !behaviorInstance.fun) return; let formData = this.formData; - this.formData = behaviorInstance.fun({ value, behavior, behaviors, formData, componentData }, this.resertValueCallback); + this.formData = behaviorInstance.fun({ value, behavior, behaviors, formData, componentData, handleType }, this.resertValueCallback); } resertValueCallback = ({ key, value }) => { const componentData = this.formData.find(ls => ls.name === key) || {}; diff --git a/src/index.mdx b/src/index.mdx index ae54b080b4b9622f019c31b6c5960a5e958c28e1..72bc34c65c06449aaedff7b5c72183e5bf70a7ff 100644 --- a/src/index.mdx +++ b/src/index.mdx @@ -6,11 +6,22 @@ sidebar: true --- import HandleBehavior from './'; +import {data} from './test/json.js' # Button Component ## new HandleBehavior({formData}) ## instance{initBehavior(formData)} + + +
ddd + +
+ + + diff --git a/src/test/index.html b/src/test/index.html new file mode 100644 index 0000000000000000000000000000000000000000..86ed3360fe3836f11ac8b3d2dd9167eca86dc5b3 --- /dev/null +++ b/src/test/index.html @@ -0,0 +1,15 @@ + + + + + + + Document + + + + + + + \ No newline at end of file diff --git a/src/test/json.js b/src/test/json.js new file mode 100644 index 0000000000000000000000000000000000000000..90cfcf1908c0f169610b77e189c95147e63045a2 --- /dev/null +++ b/src/test/json.js @@ -0,0 +1,711 @@ +export const data = [ + { + "id": 569, + "name": "KDLX", + "parentId": 0, + "type": "select", + "value": [ + { + "name": "DHL国际快递", + "value": 1 + } + ], + "formId": 76, + "conditonField": 1, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请选择快递类型", + "fieldSort": 1, + "createBy": 533, + "createAt": "2019-10-10 00:36:48", + "updateBy": 533, + "updateAt": "2019-10-22 17:51:53", + "title": "快递类型", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": [ + { + "id": 9, + "formId": 76, + "fieldId": 569, + "fieldName": "KDLX", + "eventName": "值变化", + "eventValue": "onChange", + "behaviors": [ + { + "id": 9, + "formId": 76, + "fieldId": 569, + "eventId": 9, + "behaviorValue": "showComponent", + "targets": "talentName,customerName", + "params": "1" + } + ] + } + ], + "behaviors": [ + { + "id": 9, + "formId": 76, + "fieldId": 569, + "fieldName": "KDLX", + "behaviorName": "显示控制", + "behaviorValue": "showComponent", + "targets": "talentName,customerName" + } + ] + }, + { + "id": 571, + "name": "goodsType", + "parentId": 0, + "type": "select", + "value": [ + { + "name": "合同/协议", + "value": 1 + } + ], + "formId": 76, + "conditonField": 1, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请选择物品类型", + "fieldSort": 2, + "createBy": 298, + "createAt": "2019-10-26 11:28:39", + "updateBy": 298, + "updateAt": "2019-10-29 19:26:34", + "title": "物品类型", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 550, + "name": "SSGS", + "parentId": 0, + "type": "select", + "value": [ + { + "name": "北京春雨听雷网络科技有限公司", + "value": 2 + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请选择所属公司", + "fieldSort": 3, + "createBy": 533, + "createAt": "2019-08-21 14:02:59", + "updateBy": 533, + "updateAt": "2019-10-26 11:28:43", + "title": "所属公司", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 555, + "name": "JJRBM", + "parentId": 0, + "type": "department", + "value": [ + { + "name": "运营管理部", + "value": 14 + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请选择寄件人部门", + "fieldSort": 4, + "createBy": 533, + "createAt": "2019-08-21 14:30:29", + "updateBy": 533, + "updateAt": "2019-10-26 11:28:45", + "title": "寄件人部门", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 556, + "name": "projectType", + "parentId": 0, + "type": "select", + "value": [ + { + "name": "立项项目", + "value": 1 + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请选择项目类型", + "fieldSort": 5, + "createBy": 533, + "createAt": "2019-08-21 14:31:40", + "updateBy": 533, + "updateAt": "2019-10-26 15:10:17", + "title": "项目类型", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": [ + { + "id": 3, + "formId": 76, + "fieldId": 556, + "fieldName": "projectType", + "eventName": "值变化", + "eventValue": "onChange", + "behaviors": [ + { + "id": 3, + "formId": 76, + "fieldId": 556, + "eventId": 3, + "behaviorValue": "changeParams", + "targets": "projectName", + "params": null + }, + { + "id": 8, + "formId": 76, + "fieldId": 556, + "eventId": 3, + "behaviorValue": "showComponent", + "targets": "contractName", + "params": "1" + } + ] + } + ], + "behaviors": [ + { + "id": 3, + "formId": 76, + "fieldId": 556, + "fieldName": "projectType", + "behaviorName": "显示控制", + "behaviorValue": "showComponent", + "targets": "contractName" + }, + { + "id": 8, + "formId": 76, + "fieldId": 556, + "fieldName": "projectType", + "behaviorName": "更新请求参数", + "behaviorValue": "changeParams", + "targets": "projectName" + } + ] + }, + { + "id": 557, + "name": "projectName", + "parentId": 0, + "type": "business", + "value": [ + { + "name": " 广发信用卡授权 *papi酱*1905", + "value": "1388" + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请输入项目名称", + "fieldSort": 6, + "createBy": 533, + "createAt": "2019-08-21 14:36:48", + "updateBy": 533, + "updateAt": "2019-10-26 15:09:28", + "title": "项目名称", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": [ + { + "id": 4, + "formId": 76, + "fieldId": 557, + "fieldName": "projectName", + "eventName": "值变化", + "eventValue": "onChange", + "behaviors": [ + { + "id": 4, + "formId": 76, + "fieldId": 557, + "eventId": 4, + "behaviorValue": "changeParams", + "targets": "contractName", + "params": null + } + ] + } + ], + "behaviors": [ + { + "id": 4, + "formId": 76, + "fieldId": 557, + "fieldName": "projectName", + "behaviorName": "更新请求参数", + "behaviorValue": "changeParams", + "targets": "contractName" + } + ] + }, + { + "id": 572, + "name": "contractName", + "parentId": 0, + "type": "business", + "value": "", + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请选择合同", + "fieldSort": 7, + "createBy": 298, + "createAt": "2019-10-26 11:30:24", + "updateBy": 298, + "updateAt": "2019-10-30 17:29:57", + "title": "合同", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 558, + "name": "talentName", + "parentId": 0, + "type": "business", + "value": [ + { + "name": "经理人1", + "value": "233_0" + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请输入涉及艺人/博主", + "fieldSort": 8, + "createBy": 533, + "createAt": "2019-08-21 14:49:55", + "updateBy": 533, + "updateAt": "2019-10-26 15:09:38", + "title": "涉及艺人/博主", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 570, + "name": "customerName", + "parentId": 0, + "type": "business", + "value": [ + { + "name": "青青河边草123", + "value": "972" + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请输入客户名称", + "fieldSort": 9, + "createBy": 533, + "createAt": "2019-10-10 00:38:48", + "updateBy": 533, + "updateAt": "2019-10-26 15:09:44", + "title": "客户名称", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 559, + "name": "FYCDGS", + "parentId": 0, + "type": "select", + "value": [ + { + "name": "个人承担", + "value": 3 + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请选择费用承担归属", + "fieldSort": 10, + "createBy": 533, + "createAt": "2019-08-21 14:50:58", + "updateBy": 533, + "updateAt": "2019-10-26 11:30:35", + "title": "费用承担归属", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 551, + "name": "JJR", + "parentId": 0, + "type": "input", + "value": "风格", + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请输入寄件人姓名", + "fieldSort": 11, + "createBy": 533, + "createAt": "2019-08-21 14:03:36", + "updateBy": 533, + "updateAt": "2019-10-26 11:28:53", + "title": "寄件人", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 560, + "name": "JTCDBM", + "parentId": 0, + "type": "department", + "value": [ + { + "name": "市场部", + "value": 60 + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 0, + "fieldMessage": "请输入具体承担部门", + "fieldSort": 11, + "createBy": 533, + "createAt": "2019-08-21 14:51:40", + "updateBy": 533, + "updateAt": "2019-10-30 16:49:59", + "title": "具体承担部门", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 552, + "name": "JJRLXFS", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入寄件人联系方式", + "fieldSort": 12, + "createBy": 533, + "createAt": "2019-08-21 14:04:11", + "updateBy": 533, + "updateAt": "2019-10-26 11:28:54", + "title": "寄件人联系方式", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 553, + "name": "SJR", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入收件人", + "fieldSort": 13, + "createBy": 533, + "createAt": "2019-08-21 14:05:01", + "updateBy": 533, + "updateAt": "2019-10-26 11:27:37", + "title": "收件人", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 562, + "name": "FJDZ", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请选择发件地址", + "fieldSort": 14, + "createBy": 533, + "createAt": "2019-08-21 14:53:22", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:05", + "title": "发件地址", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 554, + "name": "SJRLXFS", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入收件人联系方式", + "fieldSort": 15, + "createBy": 533, + "createAt": "2019-08-21 14:25:43", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:06", + "title": "收件人联系方式", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 563, + "name": "SJDZ", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入收件地址", + "fieldSort": 16, + "createBy": 533, + "createAt": "2019-08-21 14:54:08", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:08", + "title": "收件地址", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 561, + "name": "JJRQ", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请选择寄件日期", + "fieldSort": 17, + "createBy": 533, + "createAt": "2019-08-21 14:52:31", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:09", + "title": "寄件日期", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 564, + "name": "WPMC", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入物品名称", + "fieldSort": 18, + "createBy": 533, + "createAt": "2019-08-21 14:54:37", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:11", + "title": "物品名称", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 565, + "name": "WPSL", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入物品数量", + "fieldSort": 19, + "createBy": 533, + "createAt": "2019-08-21 14:55:26", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:13", + "title": "物品数量", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 566, + "name": "WPJZ", + "parentId": 0, + "type": "hidden", + "value": null, + "formId": 76, + "conditonField": 0, + "purposeType": -1, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "请输入物品价值(元)", + "fieldSort": 20, + "createBy": 533, + "createAt": "2019-08-21 14:56:23", + "updateBy": 533, + "updateAt": "2019-10-26 11:29:15", + "title": "物品价值", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 567, + "name": "SXMS", + "parentId": 0, + "type": "textarea", + "value": "时间啥代理费控件水电费为广东人复合弓", + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "", + "fieldSort": 21, + "createBy": 533, + "createAt": "2019-08-21 14:57:31", + "updateBy": 533, + "updateAt": "2019-10-31 14:12:25", + "title": "事项描述", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + }, + { + "id": 568, + "name": "FJ", + "parentId": 0, + "type": "upload", + "value": [ + { + "type": "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "percent": 100, + "url": "https://attachment-test.mttop.cn/20191031193956gFN55Q.pptx", + "originFileObj": { + "uid": "rc-upload-1572517878886-4" + }, + "uid": "rc-upload-1572517878886-4", + "size": 57517255, + "response": { + "hash": "lgtajVcjFEzl7f6a60UOieQpMpKO", + "key": "20191031193956gFN55Q.pptx" + }, + "domain": "attachment-test.mttop.cn", + "name": "演示文稿1.pptx", + "attachmentOrigin": 1, + "lastModified": 1572320913000, + "thumbUrl": "https://static.mttop.cn/admin/pptIcon.png", + "value": "20191031193956gFN55Q.pptx", + "status": "done" + } + ], + "formId": 76, + "conditonField": 0, + "purposeType": 0, + "inspectRules": null, + "fieldEmpty": 1, + "fieldMessage": "", + "fieldSort": 22, + "createBy": 533, + "createAt": "2019-08-21 14:58:08", + "updateBy": 533, + "updateAt": "2019-10-28 20:16:56", + "title": "附件", + "approvalFormFields": null, + "approvalFormFieldValues": null, + "events": null, + "behaviors": null + } +] \ No newline at end of file