說(shuō)明:
- 該組件可以參考如何通過(guò)普通組件做一個(gè)表單組件的能力
- 注意:該組件僅供學(xué)習(xí)使用,并不是官方提供的標(biāo)準(zhǔn)組件。
1. 使用場(chǎng)景
- 宜搭組件發(fā)布了一個(gè)自定義組件的功能,支持用低代碼的方式做一個(gè)組件,但并不支持表單組件,只能做些展示型的組件。
- 但文檔上提示可以用已有的表單做存儲(chǔ),利用這種方式實(shí)現(xiàn)了個(gè)圖片剪裁上傳的組件,通過(guò)生成一個(gè)新的表單來(lái)存儲(chǔ)數(shù)據(jù),從而模擬出一個(gè)偽表單組件。
2.效果演示
此處為語(yǔ)雀視頻卡片,點(diǎn)擊鏈接查看:圖片剪裁中級(jí).mov
3. 操作步驟
1. 新建一個(gè)自定義組件;
2. 開(kāi)啟 schame 導(dǎo)入功能,和宜搭表單設(shè)計(jì)器的方式一樣,具體可以參考:
- 打開(kāi)組件設(shè)計(jì)器,打開(kāi)控制臺(tái),執(zhí)行下面代碼
document.querySelector('.lc-left-area-bottom div:last-child').style.display = 'block'
3. 下面的 schema ,粘貼到組件設(shè)計(jì)器的 schema 控制臺(tái)中,點(diǎn)擊導(dǎo)入 schema
{ "schemaType": "superform", "schemaVersion": "5.0", "utils": [], "componentsMap": [ { "package": "@ali/vc-deep-yida", "version": "1.5.112", "componentName": "ImageField" }, { "package": "@ali/vc-deep-yida", "version": "1.5.112", "componentName": "Jsx" }, { "package": "@ali/vc-deep-yida", "version": "1.5.112", "componentName": "Dialog" }, { "package": "@ali/vc-deep-yida", "version": "1.5.112", "componentName": "Text" }, { "package": "@ali/vc-deep-yida", "version": "1.5.112", "componentName": "Component" } ], "componentsTree": [ { "componentName": "Component", "id": "node_k8bnubvz", "props": { "mobileSlot": { "type": "JSBlock", "value": { "componentName": "Slot", "children": [ { "componentName": "Text", "id": "node_ockzmfyu4f2", "props": { "maxLine": 0, "showTitle": false, "contentPaddingMobile": "0", "behavior": "NORMAL", "content": { "en_US": "Title", "zh_CN": "頁(yè)面標(biāo)題", "type": "i18n" }, "__style__": {}, "fieldId": "text_kzmfyv1m" }, "condition": true, "hidden": false, "isLocked": false, "conditionGroup": "", "title": "" } ], "props": { "slotTitle": "mobile容器", "slotName": "mobileSlot" } } }, "className": "component_k8e4naln", "useDevice": false, "fieldId": "symbol_k8bnubw4" }, "css": ".imagefield_kzmrm1kd {\n\n}\n", "defaultProps": { "buttonTitle": { "en_US": "上傳圖片", "use": "zh_CN", "zh_CN": "上傳圖片", "type": "i18n" }, "getValue": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).getValue) {\n return this.$(fieldId).getValue();\n }\n return [];\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).getValue) {\n return this.$(fieldId).getValue();\n }\n return [];\n}", "type": "js", "error": {} }, "setValue": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function func(fieldId, value) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setValue) {\n this.$(fieldId).setValue(value);\n }\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId, value) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setValue) {\n this.$(fieldId).setValue(value);\n }\n}", "type": "js", "error": {} }, "setValidation": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function func(fieldId) {\n if (fieldId && this.$(fieldId)) {\n this.$(fieldId).set('validation', [{\n \"type\": \"required\"\n }]);\n }\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId) {\n if (fieldId && this.$(fieldId)) {\n this.$(fieldId).set('validation', [\n {\n \"type\": \"required\"\n }\n ]);\n }\n}", "type": "js", "error": {} }, "setBehavior": { "compiled": "function main(){\n \n 'use strict';\n\nvar __compiledFunc__ = function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setBehavior) {\n this.$(fieldId).setBehavior(this.utils.isViewPage() ? 'READONLY' : 'HIDDEN');\n }\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setBehavior) {\n this.$(fieldId).setBehavior(this.utils.isViewPage() ? 'READONLY' : 'HIDDEN');\n }\n}", "type": "js", "error": {} } }, "methods": { "__initMethods__": { "type": "js", "source": "function (exports, module) { /*set actions code here*/ }", "compiled": "function (exports, module) { /*set actions code here*/ }" } }, "loopArgs": [ "item", "index" ], "lifeCycles": { "constructor": { "type": "js", "compiled": "function constructor() {\nvar module = { exports: {} };\nvar _this = this;\nthis.__initMethods__(module.exports, module);\nObject.keys(module.exports).forEach(function(item) {\n if(typeof module.exports[item] === 'function'){\n _this[item] = module.exports[item];\n }\n});\n\n}", "source": "function constructor() {\nvar module = { exports: {} };\nvar _this = this;\nthis.__initMethods__(module.exports, module);\nObject.keys(module.exports).forEach(function(item) {\n if(typeof module.exports[item] === 'function'){\n _this[item] = module.exports[item];\n }\n});\n\n}" }, "componentDidMount": { "name": "componentDidMount", "id": "componentDidMount", "type": "actionRef", "params": {} } }, "condition": true, "propTypes": [ { "title": "按鈕內(nèi)容", "name": "buttonTitle", "type": "string", "setter": "I18nSetter", "defaultValue": { "en_US": "上傳圖片", "use": "zh_CN", "zh_CN": "上傳圖片", "type": "i18n" }, "setterProps": {}, "display": "block" }, { "title": "是否必填", "name": "required", "type": "boolean", "setter": "BoolSetter", "defaultValue": false, "setterProps": {}, "display": "block" }, { "title": "associatedFieldId", "name": "associatedFieldId", "type": "string", "setter": "TextSetter", "display": "block" }, { "title": "getValue", "name": "getValue", "type": "function", "setter": "CodeSetter", "defaultValue": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).getValue) {\n return this.$(fieldId).getValue();\n }\n return [];\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).getValue) {\n return this.$(fieldId).getValue();\n }\n return [];\n}", "type": "js", "error": {} }, "setterProps": {}, "hidden": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function hidden() {\n return true;\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function hidden() {\n return true; \n}", "type": "js", "error": {}, "params": {} }, "display": "block" }, { "title": "setValue", "name": "setValue", "type": "function", "setter": "CodeSetter", "defaultValue": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function func(fieldId, value) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setValue) {\n this.$(fieldId).setValue(value);\n }\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId, value) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setValue) {\n this.$(fieldId).setValue(value);\n }\n}", "type": "js", "error": {} }, "setterProps": {}, "hidden": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function hidden() {\n return true;\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function hidden() {\n return true; \n}", "type": "js", "error": {}, "params": {} }, "display": "block" }, { "title": "setValidation", "name": "setValidation", "type": "function", "setter": "CodeSetter", "defaultValue": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function func(fieldId) {\n if (fieldId && this.$(fieldId)) {\n this.$(fieldId).set('validation', [{\n \"type\": \"required\"\n }]);\n }\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId) {\n if (fieldId && this.$(fieldId)) {\n this.$(fieldId).set('validation', [\n {\n \"type\": \"required\"\n }\n ]);\n }\n}", "type": "js", "error": {} }, "setterProps": {}, "hidden": { "compiled": "function main(){\n \"use strict\";\n\nvar __compiledFunc__ = function hidden() {\n return true;\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function hidden() {\n return true; \n}", "type": "js", "error": {}, "params": {} }, "display": "block" }, { "title": "setBehavior", "name": "setBehavior", "type": "function", "setter": "CodeSetter", "defaultValue": { "compiled": "function main(){\n \n 'use strict';\n\nvar __compiledFunc__ = function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setBehavior) {\n this.$(fieldId).setBehavior(this.utils.isViewPage() ? 'READONLY' : 'HIDDEN');\n }\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function func(fieldId) {\n if (fieldId && this.$(fieldId) && this.$(fieldId).setBehavior) {\n this.$(fieldId).setBehavior(this.utils.isViewPage() ? 'READONLY' : 'HIDDEN');\n }\n}", "type": "js", "error": {} }, "setterProps": {}, "hidden": { "type": "js", "source": "function hidden() {\n return true; \n}", "compiled": "function main(){\n \n \"use strict\";\n\nvar __compiledFunc__ = function hidden() {\n return true;\n};\n return __compiledFunc__.apply(this, arguments);\n }", "error": {}, "params": {} }, "display": "block" } ], "state": {}, "dataSource": { "offline": [], "globalConfig": {}, "online": [ { "isReadonly": true, "formUuid": "OJYJ0B3W-2WJEBXWE4PQFV556VLL52-8RM24E8K-0", "name": "urlParams", "description": "URL 上 querystring 解析后的對(duì)象", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-GH1T4ZZK-75", "protocal": "URI" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "src", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-LH1T4ZZK-85", "protocal": "VALUE" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "cropperrRef", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-NH1T4ZZK-95", "protocal": "VALUE" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "file", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-PH1T4ZZK-A5", "protocal": "VALUE" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "visible", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-RH1T4ZZK-B5", "protocal": "VALUE" } ], "list": [ { "isReadonly": true, "formUuid": "OJYJ0B3W-2WJEBXWE4PQFV556VLL52-8RM24E8K-0", "name": "urlParams", "description": "URL 上 querystring 解析后的對(duì)象", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-GH1T4ZZK-75", "protocal": "URI" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "src", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-LH1T4ZZK-85", "protocal": "VALUE" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "cropperrRef", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-NH1T4ZZK-95", "protocal": "VALUE" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "file", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-PH1T4ZZK-A5", "protocal": "VALUE" }, { "initialData": "", "formUuid": "LCC-6QA1-TPUXXOONWRWL53K9B2YN3-IF8WCMZK-2", "name": "visible", "description": "", "id": "PGA66QA1-AV0YP95IWFUQI14T1EP92-RH1T4ZZK-B5", "protocal": "VALUE" } ], "sync": true }, "hidden": false, "title": "", "isLocked": false, "conditionGroup": "", "children": [ { "componentName": "ImageField", "id": "node_ockzmfyu4f3", "props": { "labelTipsTypes": "none", "__useMediator": "value", "fieldName": "upload", "labelTipsIcon": "", "className": "imagefield_kzmrm1kd", "type": "card", "listType": "card", "buttonSize": "medium", "tips": { "en_US": "", "zh_CN": "", "type": "i18n" }, "onlyCameraUpload": false, "autoUpload": false, "onSelect": { "type": "JSExpression", "value": "this.utils.legaoBuiltin.execEventFlow.bind(this, [this.onFileSelect])", "events": [ { "name": "onFileSelect", "id": "onFileSelect", "params": {}, "type": "actionRef", "uuid": "1644828348266_0" } ] }, "buttonType": "primary", "limit": 9, "labelTextAlign": "right", "behavior": { "type": "variable", "value": "NORMAL", "variable": "utils.isViewPage() ? 'HIDDEN' : 'NORMAL'" }, "value": [], "validation": [], "__style__": ":root {\n\n}\n", "fieldId": "imageField_kzmg0dhs", "buttonText": { "type": "variable", "value": { "use": "zh_CN", "en_US": "Upload", "zh_CN": "上傳圖片", "type": "i18n" }, "variable": "props.buttonTitle.zh_CN || props.buttonTitle" }, "method": "post", "dataEntryMode": false, "normalListType": "image", "multiple": true, "labelColOffset": 0, "label": { "type": "variable", "value": { "use": "zh_CN", "en_US": "Upload image", "zh_CN": "上傳圖片", "type": "i18n" }, "variable": "props.buttonTitle.zh_CN || props.buttonTitle" }, "maxFileSize": 50, "__category__": "form", "labelColSpan": 4, "wrapperColSpan": 0, "accept": "image/*", "wrapperColOffset": 0, "cardListType": "card", "size": "medium", "labelAlign": "top", "withCredentials": false, "labelTipsText": { "use": "zh_CN", "en_US": "", "zh_CN": "", "type": "i18n" }, "onSuccess": { "type": "JSExpression", "value": "this.utils.legaoBuiltin.execEventFlow.bind(this, [this.onSuccess])", "events": [ { "name": "onSuccess", "id": "onSuccess", "params": {}, "type": "actionRef", "uuid": "1644830760389_13" } ] } }, "condition": true, "loopArgs": [ "", "" ], "hidden": false, "title": "", "isLocked": false, "conditionGroup": "" }, { "componentName": "Dialog", "id": "node_ockzmfyu4f4", "props": { "hasMask": true, "visible": { "type": "variable", "value": false, "variable": "state.visible" }, "footer": true, "footerActions": "cancel,ok", "confirmStyle": "primary", "confirmState": "確定", "confirmText": { "use": "zh_CN", "en_US": "Confirm", "zh_CN": "確定", "type": "i18n" }, "autoFocus": true, "title": { "use": "zh_CN", "en_US": "Dialog Title", "zh_CN": "Dialog標(biāo)題", "type": "i18n" }, "onOk": { "type": "JSExpression", "value": "this.utils.legaoBuiltin.execEventFlow.bind(this, [this.onOk])", "events": [ { "name": "onOk", "id": "onOk", "params": {}, "type": "actionRef", "uuid": "1624614775084_4" } ] }, "closeable": "esc", "cancelText": { "use": "zh_CN", "en_US": "Cancel", "zh_CN": "取消", "type": "i18n" }, "footerAlign": "right", "popupOutDialog": true, "__style__": {}, "fieldId": "dialog_kqby9wqe" }, "condition": true, "loopArgs": [ "", "" ], "hidden": true, "title": "", "isLocked": false, "conditionGroup": "", "children": [ { "componentName": "Jsx", "id": "node_ockzmfyu4f5", "props": { "render": { "type": "js", "compiled": "function main(){\n \n \"use strict\";\n\nvar __compiledFunc__ = function render() {\n return React.createElement(window.ReactCropper, {\n ref: this.saveCropperrRef.bind(this),\n src: this.state.src,\n style: { height: 300, width: 400 }\n });\n};\n return __compiledFunc__.apply(this, arguments);\n }", "source": "function render() {\n return(\n <window.ReactCropper\n ref={this.saveCropperrRef.bind(this)}\n src={this.state.src}\n style={{ height: 300, width: 400 }}\n />\n );\n}", "error": {} }, "__style__": {}, "fieldId": "JSX_kqc4v31m" }, "condition": true, "hidden": false, "title": "", "isLocked": false, "conditionGroup": "" } ] } ] } ], "actions": { "module": { "compiled": "'use strict';\n\nexports.__esModule = true;\nexports.saveCropperrRef = saveCropperrRef;\nexports.onFileSelect = onFileSelect;\nexports.onOk = onOk;\nexports.componentDidMount = componentDidMount;\nexports.onSuccess = onSuccess;\n\n\n/**\n* 尊敬的用戶,你好:頁(yè)面 JS 面板是高階用法,一般不建議普通用戶使用,如需使用,請(qǐng)確定你具備研發(fā)背景,能夠自我排查問(wèn)題。當(dāng)然,你也可以咨詢身邊的技術(shù)顧問(wèn)或者聯(lián)系宜搭平臺(tái)的技術(shù)支持獲得服務(wù)(可能收費(fèi))。\n* 我們可以用 JS 面板來(lái)開(kāi)發(fā)一些定制度高功能,比如:調(diào)用阿里云接口用來(lái)做圖像識(shí)別、上報(bào)用戶使用數(shù)據(jù)(如加載完成打點(diǎn))等等。\n* 你可以點(diǎn)擊面板上方的 「使用幫助」了解。\n*/\n\n// 當(dāng)頁(yè)面渲染完畢后馬上調(diào)用下面的函數(shù),這個(gè)函數(shù)是在當(dāng)前頁(yè)面 - 設(shè)置 - 生命周期 - 頁(yè)面加載完成時(shí)中被關(guān)聯(lián)的。\nfunction saveCropperrRef(ref) {\n // window.cropperRef = ref\n this.setState({\n cropperRef: ref\n });\n}\n\nfunction onFileSelect(files) {\n var _this = this;\n\n var reader = new FileReader();\n reader.onload = function () {\n _this.setState({\n src: reader.result,\n visible: true,\n file: files[0]\n });\n _this.$('dialog_kqby9wqe').show();\n };\n reader.readAsDataURL(files[0].originFileObj);\n}\n\nfunction loadScript(src, callback) {\n if (!src) {\n return;\n }\n var node = document.createElement('script');\n // node.crossOrigin = 'crossOrigin';\n node.src = src;\n node.addEventListener('load', callback, false);\n document.head.appendChild(node);\n}\n\nfunction loadCss(url) {\n var linkElement = document.createElement('link');\n\n linkElement.rel = 'stylesheet';\n linkElement.href = url;\n document.body.appendChild(linkElement);\n}\n\n(function loadAssets() {\n loadCss('https://g.alicdn.com/yida-platform/react-cropper/1.0.0/css/react-cropper.css');\n loadScript('https://g.alicdn.com/yida-platform/react-cropper/1.0.0/js/react-cropper.js', function () {\n // your code \n });\n})();\n\nfunction dataURL2Blob2File(dataURL, file) {\n var arr = dataURL.split(\",\"),\n mime = arr[0].match(/:(.*?);/)[1],\n bstr = atob(arr[1]),\n u8arr = new Uint8Array(bstr.length);\n var n = bstr.length;\n while (n--) {\n u8arr[n] = bstr.charCodeAt(n);\n }\n var blob = new Blob([u8arr], { type: mime });\n // Blob to File\n // set lastModifiedDate and name\n blob.lastModifiedDate = new Date();\n blob.name = file.name;\n blob.uid = file.uid;\n return blob;\n}\n\n/**\n* dialog onOk\n*/\nfunction onOk() {\n var data = this.state.cropperRef.cropper.getCroppedCanvas().toDataURL();\n var file = dataURL2Blob2File(data, this.state.file);\n console.log('file', file);\n this.$('imageField_kzmg0dhs').uploaderRef.uploaderRef.uploaderRef.startUpload([file]);\n this.$('dialog_kqby9wqe').hide();\n}\n\nfunction componentDidMount() {}\n\n/**\n* imageField onSuccess\n* @param file: {Object} 文件\n* @param value: {Array} 值\n*/\nfunction onSuccess(file, value) {\n if (this.props.associatedFieldId) {\n this.props.setValue(this.props.associatedFieldId, value);\n }\n}", "source": "\n\n/**\n* 尊敬的用戶,你好:頁(yè)面 JS 面板是高階用法,一般不建議普通用戶使用,如需使用,請(qǐng)確定你具備研發(fā)背景,能夠自我排查問(wèn)題。當(dāng)然,你也可以咨詢身邊的技術(shù)顧問(wèn)或者聯(lián)系宜搭平臺(tái)的技術(shù)支持獲得服務(wù)(可能收費(fèi))。\n* 我們可以用 JS 面板來(lái)開(kāi)發(fā)一些定制度高功能,比如:調(diào)用阿里云接口用來(lái)做圖像識(shí)別、上報(bào)用戶使用數(shù)據(jù)(如加載完成打點(diǎn))等等。\n* 你可以點(diǎn)擊面板上方的 「使用幫助」了解。\n*/\n\n// 當(dāng)頁(yè)面渲染完畢后馬上調(diào)用下面的函數(shù),這個(gè)函數(shù)是在當(dāng)前頁(yè)面 - 設(shè)置 - 生命周期 - 頁(yè)面加載完成時(shí)中被關(guān)聯(lián)的。\nexport function saveCropperrRef(ref) {\n // window.cropperRef = ref\n this.setState({\n cropperRef: ref,\n });\n}\n\nexport function onFileSelect(files) {\n const reader = new FileReader();\n reader.onload = () => {\n this.setState({\n src: reader.result,\n visible: true,\n file: files[0],\n });\n this.$('dialog_kqby9wqe').show();\n };\n reader.readAsDataURL(files[0].originFileObj);\n}\n\nfunction loadScript(src, callback) {\n if (!src) {\n return;\n }\n const node = document.createElement('script');\n // node.crossOrigin = 'crossOrigin';\n node.src = src;\n node.addEventListener('load', callback, false);\n document.head.appendChild(node);\n}\n\nfunction loadCss(url) {\n const linkElement = document.createElement('link');\n\n linkElement.rel = 'stylesheet';\n linkElement.href = url;\n document.body.appendChild(linkElement);\n}\n\n(function loadAssets() {\n loadCss('https://g.alicdn.com/yida-platform/react-cropper/1.0.0/css/react-cropper.css');\n loadScript('https://g.alicdn.com/yida-platform/react-cropper/1.0.0/js/react-cropper.js', () => {\n // your code \n });\n})();\n\nfunction dataURL2Blob2File(dataURL, file) {\n const arr = dataURL.split(\",\"),\n mime = arr[0].match(/:(.*?);/)[1],\n bstr = atob(arr[1]),\n u8arr = new Uint8Array(bstr.length);\n let n = bstr.length;\n while (n--) {\n u8arr[n] = bstr.charCodeAt(n);\n }\n const blob = new Blob([u8arr], { type: mime });\n // Blob to File\n // set lastModifiedDate and name\n blob.lastModifiedDate = new Date();\n blob.name = file.name;\n blob.uid = file.uid;\n return blob;\n}\n\n/**\n* dialog onOk\n*/\nexport function onOk() {\n const data = this.state.cropperRef.cropper.getCroppedCanvas().toDataURL();\n const file = dataURL2Blob2File(data, this.state.file);\n console.log('file', file);\n this.$('imageField_kzmg0dhs').uploaderRef.uploaderRef.uploaderRef.startUpload([file]);\n this.$('dialog_kqby9wqe').hide();\n}\n\n\nexport function componentDidMount() {\n\n}\n\n/**\n* imageField onSuccess\n* @param file: {Object} 文件\n* @param value: {Array} 值\n*/\nexport function onSuccess(file, value) {\n if (this.props.associatedFieldId) {\n this.props.setValue(this.props.associatedFieldId, value);\n }\n}" }, "type": "FUNCTION", "list": [ { "id": "saveCropperrRef", "title": "saveCropperrRef" }, { "id": "onFileSelect", "title": "onFileSelect" }, { "id": "onOk", "title": "onOk" }, { "id": "componentDidMount", "title": "componentDidMount" }, { "id": "onSuccess", "title": "onSuccess" } ] } }
4. 案例涉及原理
- 主要是利用了綁定已有的表單組件做數(shù)據(jù)存儲(chǔ),從而實(shí)現(xiàn)數(shù)據(jù)提交。
- 缺點(diǎn)是在使用上有些成本,需要自己控制被綁定的組件的顯隱。
- 圖片裁剪使用的是開(kāi)源的 react-cropper,https://www.npmjs.com/package/react-cropper ,因?yàn)檫€不支持 NPM 安裝依賴,所以只能采用 CDN 動(dòng)態(tài)加載的方案。
?
若有收獲,就點(diǎn)個(gè)贊吧
更多建議: