Showing
95 changed files
with
4449 additions
and
8165 deletions
es/atom.css
renamed from
es/form-render-core/src/atom.css
es/connectForm.js
renamed from
es/form-render-core/src/connectForm.js
es/core/RenderChildren/RenderList/CardList.js
renamed from
es/form-render-core/src/core/RenderChildren/RenderList/CardList.js
| 1 | 1 | import "antd/es/button/style"; |
| 2 | 2 | import _Button from "antd/es/button"; |
| 3 | -import "antd/es/popconfirm/style"; | |
| 4 | -import _Popconfirm from "antd/es/popconfirm"; | |
| 5 | -import _CloseOutlined from "@ant-design/icons/lib/icons/CloseOutlined"; | |
| 6 | -import _CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined"; | |
| 3 | +import "antd/es/space/style"; | |
| 4 | +import _Space from "antd/es/space"; | |
| 7 | 5 | import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; |
| 8 | 6 | import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; |
| 7 | +import _CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined"; | |
| 8 | +import "antd/es/popconfirm/style"; | |
| 9 | +import _Popconfirm from "antd/es/popconfirm"; | |
| 10 | +import _DeleteOutlined from "@ant-design/icons/lib/icons/DeleteOutlined"; | |
| 9 | 11 | |
| 10 | 12 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } |
| 11 | 13 | |
| ... | ... | @@ -56,44 +58,44 @@ var CardList = function CardList(_ref) { |
| 56 | 58 | key: idx |
| 57 | 59 | }, /*#__PURE__*/React.createElement("div", { |
| 58 | 60 | className: "fr-card-index" |
| 59 | - }, idx + 1), /*#__PURE__*/React.createElement(Core, fieldsProps), /*#__PURE__*/React.createElement("div", { | |
| 61 | + }, idx + 1), /*#__PURE__*/React.createElement(Core, fieldsProps), /*#__PURE__*/React.createElement(_Space, { | |
| 60 | 62 | direction: "horizontal", |
| 61 | 63 | className: "fr-card-toolbar" |
| 62 | - }, !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 64 | + }, !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 65 | + title: "\u786E\u5B9A\u5220\u9664?", | |
| 66 | + onConfirm: function onConfirm() { | |
| 67 | + return deleteItem(idx); | |
| 68 | + }, | |
| 69 | + okText: "\u786E\u5B9A", | |
| 70 | + cancelText: "\u53D6\u6D88" | |
| 71 | + }, /*#__PURE__*/React.createElement(_DeleteOutlined, { | |
| 72 | + style: { | |
| 73 | + fontSize: 17, | |
| 74 | + marginLeft: 8 | |
| 75 | + } | |
| 76 | + })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement(_CopyOutlined, { | |
| 63 | 77 | style: { |
| 64 | 78 | fontSize: 16, |
| 65 | - marginLeft: 4 | |
| 79 | + marginLeft: 8 | |
| 66 | 80 | }, |
| 67 | 81 | onClick: function onClick() { |
| 68 | - return moveItemUp(idx); | |
| 82 | + return copyItem(idx); | |
| 69 | 83 | } |
| 70 | - }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 84 | + }), !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 71 | 85 | style: { |
| 72 | 86 | fontSize: 16, |
| 73 | 87 | marginLeft: 4 |
| 74 | 88 | }, |
| 75 | 89 | onClick: function onClick() { |
| 76 | - return moveItemDown(idx); | |
| 90 | + return moveItemUp(idx); | |
| 77 | 91 | } |
| 78 | - })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement(_CopyOutlined, { | |
| 92 | + }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 79 | 93 | style: { |
| 80 | 94 | fontSize: 16, |
| 81 | - marginLeft: 8 | |
| 95 | + marginLeft: 4 | |
| 82 | 96 | }, |
| 83 | 97 | onClick: function onClick() { |
| 84 | - return copyItem(idx); | |
| 85 | - } | |
| 86 | - }), !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 87 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 88 | - onConfirm: function onConfirm() { | |
| 89 | - return deleteItem(idx); | |
| 90 | - }, | |
| 91 | - okText: "\u786E\u5B9A", | |
| 92 | - cancelText: "\u53D6\u6D88" | |
| 93 | - }, /*#__PURE__*/React.createElement(_CloseOutlined, { | |
| 94 | - style: { | |
| 95 | - fontSize: 16, | |
| 96 | - marginLeft: 8 | |
| 98 | + return moveItemDown(idx); | |
| 97 | 99 | } |
| 98 | 100 | })))); |
| 99 | 101 | })), /*#__PURE__*/React.createElement("div", { | ... | ... |
es/core/RenderChildren/RenderList/DrawerList.js
renamed from
es/widgets/antd/listDrawer.js
| ... | ... | @@ -4,6 +4,8 @@ import "antd/es/drawer/style"; |
| 4 | 4 | import _Drawer from "antd/es/drawer"; |
| 5 | 5 | import "antd/es/button/style"; |
| 6 | 6 | import _Button from "antd/es/button"; |
| 7 | +import "antd/es/space/style"; | |
| 8 | +import _Space from "antd/es/space"; | |
| 7 | 9 | import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; |
| 8 | 10 | import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; |
| 9 | 11 | import "antd/es/popconfirm/style"; |
| ... | ... | @@ -124,7 +126,7 @@ var DrawerList = function DrawerList(_ref) { |
| 124 | 126 | width: 120, |
| 125 | 127 | render: function render(value, record, idx) { |
| 126 | 128 | var index = value && value.$idx || 0; |
| 127 | - return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("a", { | |
| 129 | + return /*#__PURE__*/React.createElement(_Space, null, /*#__PURE__*/React.createElement("a", { | |
| 128 | 130 | onClick: function onClick() { |
| 129 | 131 | return openDrawer(index); |
| 130 | 132 | } | ... | ... |
es/core/RenderChildren/RenderList/SimpleList.js
renamed from
es/form-render-core/src/core/RenderChildren/RenderList/SimpleList.js
es/core/RenderChildren/RenderList/TableList.js
renamed from
es/widgets/antd/listTable.js
| ... | ... | @@ -2,6 +2,8 @@ import "antd/es/table/style"; |
| 2 | 2 | import _Table from "antd/es/table"; |
| 3 | 3 | import "antd/es/button/style"; |
| 4 | 4 | import _Button from "antd/es/button"; |
| 5 | +import "antd/es/space/style"; | |
| 6 | +import _Space from "antd/es/space"; | |
| 5 | 7 | import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; |
| 6 | 8 | import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; |
| 7 | 9 | import "antd/es/popconfirm/style"; |
| ... | ... | @@ -105,7 +107,7 @@ var TableList = function TableList(_ref) { |
| 105 | 107 | fixed: 'right', |
| 106 | 108 | width: 120, |
| 107 | 109 | render: function render(value, record, idx) { |
| 108 | - return /*#__PURE__*/React.createElement("div", null, !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement("a", { | |
| 110 | + return /*#__PURE__*/React.createElement(_Space, null, !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement("a", { | |
| 109 | 111 | onClick: function onClick() { |
| 110 | 112 | return copyItem(idx); |
| 111 | 113 | } | ... | ... |
es/core/RenderChildren/RenderList/VirtualList.js
renamed from
es/form-render-core/src/core/RenderChildren/RenderList/VirtualList.js
| ... | ... | @@ -123,7 +123,7 @@ var VirtualList = function VirtualList(_ref) { |
| 123 | 123 | style: { |
| 124 | 124 | color: '#1890ff', |
| 125 | 125 | fontSize: 16, |
| 126 | - marginLeft: 8 | |
| 126 | + marginLeft: 4 | |
| 127 | 127 | }, |
| 128 | 128 | onClick: function onClick() { |
| 129 | 129 | return moveItemUp(idx); |
| ... | ... | @@ -132,7 +132,7 @@ var VirtualList = function VirtualList(_ref) { |
| 132 | 132 | style: { |
| 133 | 133 | color: '#1890ff', |
| 134 | 134 | fontSize: 16, |
| 135 | - marginLeft: 8 | |
| 135 | + marginLeft: 4 | |
| 136 | 136 | }, |
| 137 | 137 | onClick: function onClick() { |
| 138 | 138 | return moveItemDown(idx); | ... | ... |
es/core/RenderChildren/RenderList/index.js
renamed from
es/form-render-core/src/core/RenderChildren/RenderList/index.js
| ... | ... | @@ -28,7 +28,6 @@ import CardList from './CardList'; |
| 28 | 28 | import TableList from './TableList'; |
| 29 | 29 | import DrawerList from './DrawerList'; |
| 30 | 30 | import VirtualList from './VirtualList'; |
| 31 | -import TabList from './TabList'; | |
| 32 | 31 | |
| 33 | 32 | var RenderList = function RenderList(_ref) { |
| 34 | 33 | var parentId = _ref.parentId, |
| ... | ... | @@ -177,9 +176,6 @@ var RenderList = function RenderList(_ref) { |
| 177 | 176 | case 'virtualList': |
| 178 | 177 | return /*#__PURE__*/React.createElement(VirtualList, displayProps); |
| 179 | 178 | |
| 180 | - case 'tabList': | |
| 181 | - return /*#__PURE__*/React.createElement(TabList, displayProps); | |
| 182 | - | |
| 183 | 179 | default: |
| 184 | 180 | return /*#__PURE__*/React.createElement(CardList, displayProps); |
| 185 | 181 | } | ... | ... |
es/core/RenderChildren/RenderList/list.css
renamed from
es/form-render-core/src/core/RenderChildren/RenderList/list.css
es/core/RenderChildren/RenderObject.js
renamed from
es/form-render-core/src/core/RenderChildren/RenderObject.js
es/core/RenderField/ErrorMessage.css
renamed from
es/form-render-core/src/core/RenderField/ErrorMessage.css
es/core/RenderField/ErrorMessage.js
renamed from
es/form-render-core/src/core/RenderField/ErrorMessage.js
es/core/RenderField/ExtendedWidget.js
renamed from
es/form-render-core/src/core/RenderField/ExtendedWidget.js
| ... | ... | @@ -152,14 +152,8 @@ var ExtendedWidget = function ExtendedWidget(_ref) { |
| 152 | 152 | }; |
| 153 | 153 | |
| 154 | 154 | var areEqual = function areEqual(prev, current) { |
| 155 | - if (prev.schema && current.schema) { | |
| 156 | - if (prev.schema.$id === '#') { | |
| 157 | - return false; | |
| 158 | - } | |
| 159 | - | |
| 160 | - if (prev.schema.hidden && current.schema.hidden) { | |
| 161 | - return true; | |
| 162 | - } | |
| 155 | + if (prev.schema && prev.schema.$id === '#') { | |
| 156 | + return false; | |
| 163 | 157 | } |
| 164 | 158 | |
| 165 | 159 | if (prev.readOnly !== current.readOnly) { | ... | ... |
es/core/RenderField/Extra.css
renamed from
es/form-render-core/src/core/RenderField/Extra.css
es/core/RenderField/Extra.js
renamed from
es/form-render-core/src/core/RenderField/Extra.js
es/core/RenderField/Title.js
renamed from
es/form-render-core/src/core/RenderField/Title.js
es/core/RenderField/index.js
renamed from
es/form-render-core/src/core/RenderField/index.js
| ... | ... | @@ -2,18 +2,6 @@ function _extends() { _extends = Object.assign || function (target) { for (var i |
| 2 | 2 | |
| 3 | 3 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 4 | 4 | |
| 5 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 6 | - | |
| 7 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 8 | - | |
| 9 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 10 | - | |
| 11 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 12 | - | |
| 13 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 14 | - | |
| 15 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 16 | - | |
| 17 | 5 | import React, { useEffect, useRef } from 'react'; |
| 18 | 6 | import { useStore, useStore2, useTools } from '../../hooks'; |
| 19 | 7 | import useDebouncedCallback from '../../useDebounce'; |
| ... | ... | @@ -21,7 +9,6 @@ import { getValueByPath, isCheckBoxType, isObjType } from '../../utils'; |
| 21 | 9 | import ErrorMessage from './ErrorMessage'; |
| 22 | 10 | import Extra from './Extra'; |
| 23 | 11 | import FieldTitle from './Title'; |
| 24 | -import { validateField } from '../../validator'; | |
| 25 | 12 | import ExtendedWidget from './ExtendedWidget'; // TODO: 之后不要直接用get,收口到一个内部方法getValue,便于全局 ctrl + f 查找 |
| 26 | 13 | |
| 27 | 14 | var RenderField = function RenderField(props) { |
| ... | ... | @@ -41,23 +28,19 @@ var RenderField = function RenderField(props) { |
| 41 | 28 | displayType = props.displayType; |
| 42 | 29 | |
| 43 | 30 | var _useStore = useStore(), |
| 44 | - formData = _useStore.formData, | |
| 45 | - flatten = _useStore.flatten; | |
| 31 | + formData = _useStore.formData; | |
| 46 | 32 | |
| 47 | 33 | var _useStore2 = useStore2(), |
| 48 | 34 | debounceInput = _useStore2.debounceInput, |
| 49 | 35 | readOnly = _useStore2.readOnly, |
| 50 | 36 | disabled = _useStore2.disabled, |
| 51 | - showValidate = _useStore2.showValidate, | |
| 52 | - validateMessages = _useStore2.validateMessages, | |
| 53 | - locale = _useStore2.locale; | |
| 37 | + showValidate = _useStore2.showValidate; | |
| 54 | 38 | |
| 55 | 39 | var _useTools = useTools(), |
| 56 | 40 | onValuesChange = _useTools.onValuesChange, |
| 57 | 41 | onItemChange = _useTools.onItemChange, |
| 58 | 42 | setEditing = _useTools.setEditing, |
| 59 | - touchKey = _useTools.touchKey, | |
| 60 | - _setErrors = _useTools._setErrors; | |
| 43 | + touchKey = _useTools.touchKey; | |
| 61 | 44 | |
| 62 | 45 | var formDataRef = useRef(); |
| 63 | 46 | formDataRef.current = formData; // console.log('<renderField>', $id); |
| ... | ... | @@ -75,38 +58,7 @@ var RenderField = function RenderField(props) { |
| 75 | 58 | |
| 76 | 59 | var _readOnly = readOnly !== undefined ? readOnly : _schema.readOnly; |
| 77 | 60 | |
| 78 | - var _disabled = disabled !== undefined ? disabled : _schema.disabled; | |
| 79 | - | |
| 80 | - var removeDupErrors = function removeDupErrors(arr) { | |
| 81 | - if (!Array.isArray(arr)) { | |
| 82 | - console.log('in removeDups: param is not an array'); | |
| 83 | - return; | |
| 84 | - } | |
| 85 | - | |
| 86 | - var array = []; | |
| 87 | - | |
| 88 | - for (var i = 0; i < arr.length; i++) { | |
| 89 | - var sameNameIndex = array.findIndex(function (item) { | |
| 90 | - return item.name === arr[i].name; | |
| 91 | - }); | |
| 92 | - | |
| 93 | - if (sameNameIndex > -1) { | |
| 94 | - var sameNameItem = array[sameNameIndex]; | |
| 95 | - var error1 = sameNameItem.error; | |
| 96 | - var error2 = arr[i].error; | |
| 97 | - array[sameNameIndex] = { | |
| 98 | - name: sameNameItem.name, | |
| 99 | - error: error1.length > 0 && error2.length > 0 ? [].concat(_toConsumableArray(error1), _toConsumableArray(error2)) : [] | |
| 100 | - }; | |
| 101 | - } else { | |
| 102 | - array.push(arr[i]); | |
| 103 | - } | |
| 104 | - } | |
| 105 | - | |
| 106 | - return array.filter(function (item) { | |
| 107 | - return Array.isArray(item.error) && item.error.length > 0; | |
| 108 | - }); | |
| 109 | - }; // TODO: 优化一下,只有touch还是false的时候,setTouched | |
| 61 | + var _disabled = disabled !== undefined ? disabled : _schema.disabled; // TODO: 优化一下,只有touch还是false的时候,setTouched | |
| 110 | 62 | |
| 111 | 63 | |
| 112 | 64 | var onChange = function onChange(value) { |
| ... | ... | @@ -126,20 +78,6 @@ var RenderField = function RenderField(props) { |
| 126 | 78 | if (typeof onValuesChange === 'function') { |
| 127 | 79 | onValuesChange(_defineProperty({}, dataPath, value), formDataRef.current); |
| 128 | 80 | } |
| 129 | - | |
| 130 | - validateField({ | |
| 131 | - path: dataPath, | |
| 132 | - formData: formDataRef.current, | |
| 133 | - flatten: flatten, | |
| 134 | - options: { | |
| 135 | - locale: locale, | |
| 136 | - validateMessages: validateMessages | |
| 137 | - } | |
| 138 | - }).then(function (res) { | |
| 139 | - _setErrors(function (errors) { | |
| 140 | - return removeDupErrors([].concat(_toConsumableArray(errors), _toConsumableArray(res))); | |
| 141 | - }); | |
| 142 | - }); | |
| 143 | 81 | }; |
| 144 | 82 | |
| 145 | 83 | var titleProps = { | ... | ... |
es/core/index.js
renamed from
es/form-render-core/src/core/index.js
| ... | ... | @@ -133,12 +133,11 @@ var CoreRender = function CoreRender(_ref2) { |
| 133 | 133 | effectiveLabelWidth = _ref2.effectiveLabelWidth, |
| 134 | 134 | rest = _objectWithoutProperties(_ref2, _excluded2); |
| 135 | 135 | |
| 136 | - if (schema.hidden) { | |
| 137 | - return null; | |
| 138 | - } // 样式的逻辑全放在这层 | |
| 136 | + // if (schema.hidden) { | |
| 137 | + // return null; | |
| 138 | + // } | |
| 139 | + // 样式的逻辑全放在这层 | |
| 139 | 140 | // displayType 一层层网上找值 |
| 140 | - | |
| 141 | - | |
| 142 | 141 | var _displayType = schema.displayType || rest.displayType || displayType || 'column'; |
| 143 | 142 | |
| 144 | 143 | var isList = isListType(schema); |
| ... | ... | @@ -321,8 +320,7 @@ var CoreRender = function CoreRender(_ref2) { |
| 321 | 320 | style: columnStyle, |
| 322 | 321 | className: "".concat(containerClass, " ").concat(debugCss ? 'debug' : '') |
| 323 | 322 | }, /*#__PURE__*/React.createElement(RenderField, fieldProps, _children)); |
| 324 | -}; // haven't used | |
| 325 | - | |
| 323 | +}; | |
| 326 | 324 | |
| 327 | 325 | var areEqual = function areEqual(prev, current) { |
| 328 | 326 | if (prev.allTouched !== current.allTouched) { |
| ... | ... | @@ -341,24 +339,6 @@ var areEqual = function areEqual(prev, current) { |
| 341 | 339 | return false; |
| 342 | 340 | } |
| 343 | 341 | |
| 344 | - if (prev.readOnly !== current.readOnly) { | |
| 345 | - return false; | |
| 346 | - } | |
| 347 | - | |
| 348 | - if (prev.disabled !== current.disabled) { | |
| 349 | - return false; | |
| 350 | - } | |
| 351 | - | |
| 352 | - if (prev.schema && current.schema) { | |
| 353 | - if (prev.schema.$id === '#') { | |
| 354 | - return false; | |
| 355 | - } | |
| 356 | - } | |
| 357 | - | |
| 358 | - if (isObjType(prev.schema) && isObjType(current.schema)) { | |
| 359 | - return false; | |
| 360 | - } | |
| 361 | - | |
| 362 | 342 | if (JSON.stringify(prev.dependValues) !== JSON.stringify(current.dependValues)) { |
| 363 | 343 | return false; |
| 364 | 344 | } |
| ... | ... | @@ -370,4 +350,5 @@ var areEqual = function areEqual(prev, current) { |
| 370 | 350 | return false; |
| 371 | 351 | }; |
| 372 | 352 | |
| 353 | +var MCore = /*#__PURE__*/React.memo(CoreRender, areEqual); | |
| 373 | 354 | export default Core; |
| \ No newline at end of file | ... | ... |
es/createWidget.js
renamed from
es/form-render-core/src/createWidget.js
es/form-render-core/src/Watcher.js
deleted
100644 → 0
| 1 | -/* eslint-disable react-hooks/exhaustive-deps */ | |
| 2 | -import { useEffect } from 'react'; | |
| 3 | -import { getValueByPath } from './utils'; | |
| 4 | - | |
| 5 | -var Watcher = function Watcher(_ref) { | |
| 6 | - var watchKey = _ref.watchKey, | |
| 7 | - watch = _ref.watch, | |
| 8 | - formData = _ref.formData, | |
| 9 | - firstMount = _ref.firstMount; | |
| 10 | - var value = getValueByPath(formData, watchKey); | |
| 11 | - var watchObj = watch[watchKey]; | |
| 12 | - useEffect(function () { | |
| 13 | - var runWatcher = function runWatcher() { | |
| 14 | - if (typeof watchObj === 'function') { | |
| 15 | - try { | |
| 16 | - watchObj(value); | |
| 17 | - } catch (error) { | |
| 18 | - console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 19 | - } | |
| 20 | - } else if (watchObj && typeof watchObj.handler === 'function') { | |
| 21 | - try { | |
| 22 | - watchObj.handler(value); | |
| 23 | - } catch (error) { | |
| 24 | - console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 25 | - } | |
| 26 | - } | |
| 27 | - }; | |
| 28 | - | |
| 29 | - if (firstMount) { | |
| 30 | - var immediate = watchObj && watchObj.immediate; | |
| 31 | - | |
| 32 | - if (immediate) { | |
| 33 | - runWatcher(); | |
| 34 | - } | |
| 35 | - } else { | |
| 36 | - runWatcher(); | |
| 37 | - } | |
| 38 | - }, [JSON.stringify(value), firstMount]); | |
| 39 | - return null; | |
| 40 | -}; | |
| 41 | - | |
| 42 | -export default Watcher; | |
| \ No newline at end of file |
es/form-render-core/src/core/RenderChildren/RenderList/DrawerList.js
deleted
100644 → 0
| 1 | -import "antd/es/table/style"; | |
| 2 | -import _Table from "antd/es/table"; | |
| 3 | -import "antd/es/drawer/style"; | |
| 4 | -import _Drawer from "antd/es/drawer"; | |
| 5 | -import "antd/es/button/style"; | |
| 6 | -import _Button from "antd/es/button"; | |
| 7 | -import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; | |
| 8 | -import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; | |
| 9 | -import "antd/es/popconfirm/style"; | |
| 10 | -import _Popconfirm from "antd/es/popconfirm"; | |
| 11 | -var _excluded = ["buttons"], | |
| 12 | - _excluded2 = ["pagination"]; | |
| 13 | - | |
| 14 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 15 | - | |
| 16 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 17 | - | |
| 18 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 19 | - | |
| 20 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 21 | - | |
| 22 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 23 | - | |
| 24 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 25 | - | |
| 26 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 27 | - | |
| 28 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 29 | - | |
| 30 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 31 | - | |
| 32 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 33 | - | |
| 34 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 35 | - | |
| 36 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 37 | - | |
| 38 | -/* eslint-disable jsx-a11y/anchor-is-valid */ | |
| 39 | -import React, { useRef } from 'react'; | |
| 40 | -import Core from '../../index'; | |
| 41 | -import { useSet } from '../../../hooks'; | |
| 42 | -import { getDataPath, getKeyFromPath, getDisplayValue } from '../../../utils'; | |
| 43 | -import ErrorMessage from '../../RenderField/ErrorMessage'; | |
| 44 | -var FIELD_LENGTH = 170; | |
| 45 | - | |
| 46 | -var DrawerList = function DrawerList(_ref) { | |
| 47 | - var _extends2; | |
| 48 | - | |
| 49 | - var _ref$displayList = _ref.displayList, | |
| 50 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 51 | - dataPath = _ref.dataPath, | |
| 52 | - children = _ref.children, | |
| 53 | - deleteItem = _ref.deleteItem, | |
| 54 | - addItem = _ref.addItem, | |
| 55 | - moveItemDown = _ref.moveItemDown, | |
| 56 | - moveItemUp = _ref.moveItemUp, | |
| 57 | - flatten = _ref.flatten, | |
| 58 | - errorFields = _ref.errorFields, | |
| 59 | - getFieldsProps = _ref.getFieldsProps, | |
| 60 | - schema = _ref.schema, | |
| 61 | - changeList = _ref.changeList, | |
| 62 | - listData = _ref.listData; | |
| 63 | - var _schema$props = schema.props, | |
| 64 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 65 | - _schema$itemProps = schema.itemProps, | |
| 66 | - itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps; | |
| 67 | - | |
| 68 | - var buttons = itemProps.buttons, | |
| 69 | - columnProps = _objectWithoutProperties(itemProps, _excluded); | |
| 70 | - | |
| 71 | - var _props$pagination = props.pagination, | |
| 72 | - pagination = _props$pagination === void 0 ? {} : _props$pagination, | |
| 73 | - rest = _objectWithoutProperties(props, _excluded2); | |
| 74 | - | |
| 75 | - var paginationConfig = pagination && _objectSpread({ | |
| 76 | - size: 'small', | |
| 77 | - hideOnSinglePage: true | |
| 78 | - }, pagination); | |
| 79 | - | |
| 80 | - var currentIndex = useRef(-1); | |
| 81 | - | |
| 82 | - var _useSet = useSet({ | |
| 83 | - showDrawer: false | |
| 84 | - }), | |
| 85 | - _useSet2 = _slicedToArray(_useSet, 2), | |
| 86 | - state = _useSet2[0], | |
| 87 | - setState = _useSet2[1]; | |
| 88 | - | |
| 89 | - var showDrawer = state.showDrawer; | |
| 90 | - var dataSource = displayList.map(function (item, index) { | |
| 91 | - return _objectSpread(_objectSpread({}, item), {}, { | |
| 92 | - $idx: index | |
| 93 | - }); | |
| 94 | - }); | |
| 95 | - var columns = children.map(function (child) { | |
| 96 | - var item = flatten[child]; | |
| 97 | - var schema = item && item.schema || {}; | |
| 98 | - | |
| 99 | - var _dataIndex = getKeyFromPath(child); | |
| 100 | - | |
| 101 | - return _objectSpread({ | |
| 102 | - dataIndex: _dataIndex, | |
| 103 | - title: schema.required ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", { | |
| 104 | - className: "fr-label-required" | |
| 105 | - }, " *"), /*#__PURE__*/React.createElement("span", null, schema.title)) : schema.title, | |
| 106 | - width: FIELD_LENGTH, | |
| 107 | - render: function render(value, record) { | |
| 108 | - var childPath = getDataPath(child, [record.$idx]); | |
| 109 | - var errorObj = errorFields.find(function (item) { | |
| 110 | - return item.name == childPath; | |
| 111 | - }) || {}; //TODO: 万一error在更深的层,这个办法是find不到的,会展示那一行没有提示。可以整一行加一个红线的方式处理 | |
| 112 | - | |
| 113 | - return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", null, getDisplayValue(value, schema)), errorObj.error && /*#__PURE__*/React.createElement(ErrorMessage, { | |
| 114 | - message: errorObj.error, | |
| 115 | - schema: schema | |
| 116 | - })); | |
| 117 | - } | |
| 118 | - }, columnProps); | |
| 119 | - }); | |
| 120 | - columns.push({ | |
| 121 | - title: '操作', | |
| 122 | - key: '$action', | |
| 123 | - fixed: 'right', | |
| 124 | - width: 120, | |
| 125 | - render: function render(value, record, idx) { | |
| 126 | - var index = value && value.$idx || 0; | |
| 127 | - return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("a", { | |
| 128 | - onClick: function onClick() { | |
| 129 | - return openDrawer(index); | |
| 130 | - } | |
| 131 | - }, "\u7F16\u8F91"), !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 132 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 133 | - onConfirm: function onConfirm() { | |
| 134 | - return deleteItem(index); | |
| 135 | - }, | |
| 136 | - okText: "\u786E\u5B9A", | |
| 137 | - cancelText: "\u53D6\u6D88" | |
| 138 | - }, /*#__PURE__*/React.createElement("a", { | |
| 139 | - style: { | |
| 140 | - marginLeft: 8 | |
| 141 | - } | |
| 142 | - }, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 143 | - style: { | |
| 144 | - color: '#1890ff', | |
| 145 | - fontSize: 16, | |
| 146 | - marginLeft: 8 | |
| 147 | - }, | |
| 148 | - onClick: function onClick() { | |
| 149 | - return moveItemUp(idx); | |
| 150 | - } | |
| 151 | - }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 152 | - style: { | |
| 153 | - color: '#1890ff', | |
| 154 | - fontSize: 16, | |
| 155 | - marginLeft: 8 | |
| 156 | - }, | |
| 157 | - onClick: function onClick() { | |
| 158 | - return moveItemDown(idx); | |
| 159 | - } | |
| 160 | - }))); | |
| 161 | - } | |
| 162 | - }); | |
| 163 | - var fieldsProps = getFieldsProps(currentIndex.current); | |
| 164 | - | |
| 165 | - var openDrawer = function openDrawer(index) { | |
| 166 | - currentIndex.current = index; | |
| 167 | - setState({ | |
| 168 | - showDrawer: true | |
| 169 | - }); | |
| 170 | - }; | |
| 171 | - | |
| 172 | - var closeDrawer = function closeDrawer() { | |
| 173 | - currentIndex.current = -1; | |
| 174 | - setState({ | |
| 175 | - showDrawer: false | |
| 176 | - }); | |
| 177 | - }; | |
| 178 | - | |
| 179 | - var handleAdd = function handleAdd() { | |
| 180 | - var newIndex = addItem(); | |
| 181 | - openDrawer(newIndex); | |
| 182 | - }; | |
| 183 | - | |
| 184 | - return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", { | |
| 185 | - className: "w-100 mb2 tr" | |
| 186 | - }, !props.hideAdd && /*#__PURE__*/React.createElement(_Button, { | |
| 187 | - type: "primary", | |
| 188 | - size: "small", | |
| 189 | - onClick: handleAdd | |
| 190 | - }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 191 | - var callback = item.callback, | |
| 192 | - text = item.text, | |
| 193 | - html = item.html; | |
| 194 | - | |
| 195 | - var onClick = function onClick() { | |
| 196 | - console.log({ | |
| 197 | - value: listData, | |
| 198 | - onChange: changeList, | |
| 199 | - schema: schema | |
| 200 | - }); | |
| 201 | - }; | |
| 202 | - | |
| 203 | - if (typeof window[callback] === 'function') { | |
| 204 | - onClick = function onClick() { | |
| 205 | - window[callback]({ | |
| 206 | - value: listData, | |
| 207 | - onChange: changeList, | |
| 208 | - schema: schema | |
| 209 | - }); | |
| 210 | - }; | |
| 211 | - } | |
| 212 | - | |
| 213 | - return /*#__PURE__*/React.createElement(_Button, { | |
| 214 | - key: idx.toString(), | |
| 215 | - style: { | |
| 216 | - marginLeft: 8 | |
| 217 | - }, | |
| 218 | - size: "small", | |
| 219 | - onClick: onClick | |
| 220 | - }, /*#__PURE__*/React.createElement("span", { | |
| 221 | - dangerouslySetInnerHTML: { | |
| 222 | - __html: html || text | |
| 223 | - } | |
| 224 | - })); | |
| 225 | - }) : null), /*#__PURE__*/React.createElement(_Drawer, { | |
| 226 | - width: "600", | |
| 227 | - title: "\u7F16\u8F91", | |
| 228 | - placement: "right", | |
| 229 | - onClose: closeDrawer, | |
| 230 | - visible: showDrawer, | |
| 231 | - destroyOnClose: true // 必须要加,currentIndex不是一个state,Core不会重新渲染就跪了 | |
| 232 | - | |
| 233 | - }, /*#__PURE__*/React.createElement("div", { | |
| 234 | - className: "fr-container" | |
| 235 | - }, /*#__PURE__*/React.createElement(Core, fieldsProps))), /*#__PURE__*/React.createElement(_Table, _extends((_extends2 = { | |
| 236 | - size: "small", | |
| 237 | - scroll: { | |
| 238 | - x: 'max-content' | |
| 239 | - }, | |
| 240 | - columns: columns, | |
| 241 | - dataSource: dataSource, | |
| 242 | - rowClassName: function rowClassName(record, idx) { | |
| 243 | - var index = record && record.$idx; | |
| 244 | - var hasError = errorFields.find(function (item) { | |
| 245 | - return item.name.indexOf("".concat(dataPath, "[").concat(index, "]")) > -1; | |
| 246 | - }); | |
| 247 | - return hasError ? 'fr-row-error' : ''; | |
| 248 | - }, | |
| 249 | - rowKey: "$idx" | |
| 250 | - }, _defineProperty(_extends2, "size", "small"), _defineProperty(_extends2, "pagination", paginationConfig), _extends2), rest))); | |
| 251 | -}; | |
| 252 | - | |
| 253 | -export default DrawerList; | |
| \ No newline at end of file |
es/form-render-core/src/core/RenderChildren/RenderList/TabList.js
deleted
100644 → 0
| 1 | -import "antd/es/tabs/style"; | |
| 2 | -import _Tabs from "antd/es/tabs"; | |
| 3 | -var _excluded = ["tabName", "type"]; | |
| 4 | - | |
| 5 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 6 | - | |
| 7 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 8 | - | |
| 9 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 10 | - | |
| 11 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 12 | - | |
| 13 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 14 | - | |
| 15 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 16 | - | |
| 17 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 18 | - | |
| 19 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 20 | - | |
| 21 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 22 | - | |
| 23 | -/* eslint-disable jsx-a11y/anchor-is-valid */ | |
| 24 | -import React, { useState } from 'react'; | |
| 25 | -import Core from '../../index'; | |
| 26 | -var TabPane = _Tabs.TabPane; | |
| 27 | - | |
| 28 | -var TabList = function TabList(_ref) { | |
| 29 | - var _ref$displayList = _ref.displayList, | |
| 30 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 31 | - listData = _ref.listData, | |
| 32 | - changeList = _ref.changeList, | |
| 33 | - schema = _ref.schema, | |
| 34 | - deleteItem = _ref.deleteItem, | |
| 35 | - copyItem = _ref.copyItem, | |
| 36 | - addItem = _ref.addItem, | |
| 37 | - moveItemUp = _ref.moveItemUp, | |
| 38 | - moveItemDown = _ref.moveItemDown, | |
| 39 | - displayType = _ref.displayType, | |
| 40 | - getFieldsProps = _ref.getFieldsProps; | |
| 41 | - | |
| 42 | - var _useState = useState('0'), | |
| 43 | - _useState2 = _slicedToArray(_useState, 2), | |
| 44 | - activeKey = _useState2[0], | |
| 45 | - setActiveKey = _useState2[1]; | |
| 46 | - | |
| 47 | - var _schema$props = schema.props, | |
| 48 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 49 | - itemProps = schema.itemProps; | |
| 50 | - | |
| 51 | - var tabName = props.tabName, | |
| 52 | - type = props.type, | |
| 53 | - restProps = _objectWithoutProperties(props, _excluded); | |
| 54 | - | |
| 55 | - var onEdit = function onEdit(targetKey, action) { | |
| 56 | - if (action === 'add') { | |
| 57 | - var currentKey = addItem(); | |
| 58 | - setActiveKey("".concat(currentKey)); | |
| 59 | - } else if (action === 'remove') { | |
| 60 | - deleteItem(Number(targetKey)); | |
| 61 | - setActiveKey("".concat(targetKey > 1 ? targetKey - 1 : 0)); | |
| 62 | - } else { | |
| 63 | - return null; | |
| 64 | - } | |
| 65 | - }; | |
| 66 | - | |
| 67 | - var getCurrentTabPaneName = function getCurrentTabPaneName(idx) { | |
| 68 | - return tabName instanceof Array ? tabName[idx] || idx + 1 : "".concat(tabName || '项目', " ").concat(idx + 1); | |
| 69 | - }; | |
| 70 | - | |
| 71 | - return /*#__PURE__*/React.createElement(_Tabs, _extends({ | |
| 72 | - type: type || 'line', | |
| 73 | - onChange: setActiveKey, | |
| 74 | - activeKey: activeKey, | |
| 75 | - onEdit: onEdit | |
| 76 | - }, restProps), displayList.map(function (item, idx) { | |
| 77 | - var fieldsProps = getFieldsProps(idx); | |
| 78 | - fieldsProps.displayType = displayType; | |
| 79 | - return /*#__PURE__*/React.createElement(TabPane, { | |
| 80 | - tab: getCurrentTabPaneName(idx), | |
| 81 | - key: "".concat(idx) | |
| 82 | - }, /*#__PURE__*/React.createElement(Core, fieldsProps)); | |
| 83 | - })); | |
| 84 | -}; | |
| 85 | - | |
| 86 | -export default TabList; | |
| \ No newline at end of file |
es/form-render-core/src/core/RenderChildren/RenderList/TableList.js
deleted
100644 → 0
| 1 | -import "antd/es/table/style"; | |
| 2 | -import _Table from "antd/es/table"; | |
| 3 | -import "antd/es/button/style"; | |
| 4 | -import _Button from "antd/es/button"; | |
| 5 | -import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; | |
| 6 | -import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; | |
| 7 | -import "antd/es/popconfirm/style"; | |
| 8 | -import _Popconfirm from "antd/es/popconfirm"; | |
| 9 | -var _excluded = ["buttons"], | |
| 10 | - _excluded2 = ["pagination"]; | |
| 11 | - | |
| 12 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 13 | - | |
| 14 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 15 | - | |
| 16 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 17 | - | |
| 18 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 19 | - | |
| 20 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 21 | - | |
| 22 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 23 | - | |
| 24 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 25 | - | |
| 26 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 27 | - | |
| 28 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 29 | - | |
| 30 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 31 | - | |
| 32 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 33 | - | |
| 34 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 35 | - | |
| 36 | -/* eslint-disable jsx-a11y/anchor-is-valid */ | |
| 37 | -import React from 'react'; | |
| 38 | -import Core from '../../index'; | |
| 39 | -// import ArrowDown from '../../../components/ArrowDown'; | |
| 40 | -var FIELD_LENGTH = 170; | |
| 41 | - | |
| 42 | -var TableList = function TableList(_ref) { | |
| 43 | - var _ref$displayList = _ref.displayList, | |
| 44 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 45 | - dataIndex = _ref.dataIndex, | |
| 46 | - children = _ref.children, | |
| 47 | - deleteItem = _ref.deleteItem, | |
| 48 | - copyItem = _ref.copyItem, | |
| 49 | - addItem = _ref.addItem, | |
| 50 | - moveItemUp = _ref.moveItemUp, | |
| 51 | - moveItemDown = _ref.moveItemDown, | |
| 52 | - flatten = _ref.flatten, | |
| 53 | - schema = _ref.schema, | |
| 54 | - listData = _ref.listData, | |
| 55 | - changeList = _ref.changeList; | |
| 56 | - var _schema$props = schema.props, | |
| 57 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 58 | - _schema$itemProps = schema.itemProps, | |
| 59 | - itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps; | |
| 60 | - | |
| 61 | - var buttons = itemProps.buttons, | |
| 62 | - columnProps = _objectWithoutProperties(itemProps, _excluded); | |
| 63 | - | |
| 64 | - var _props$pagination = props.pagination, | |
| 65 | - pagination = _props$pagination === void 0 ? {} : _props$pagination, | |
| 66 | - rest = _objectWithoutProperties(props, _excluded2); | |
| 67 | - | |
| 68 | - var paginationConfig = pagination && _objectSpread({ | |
| 69 | - size: 'small', | |
| 70 | - hideOnSinglePage: true | |
| 71 | - }, pagination); | |
| 72 | - | |
| 73 | - var dataSource = displayList.map(function (item, idx) { | |
| 74 | - return { | |
| 75 | - index: idx | |
| 76 | - }; | |
| 77 | - }); | |
| 78 | - var columns = children.map(function (child) { | |
| 79 | - var item = flatten[child]; | |
| 80 | - var schema = item && item.schema || {}; | |
| 81 | - return _objectSpread({ | |
| 82 | - dataIndex: child, | |
| 83 | - title: schema.required ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", { | |
| 84 | - className: "fr-label-required" | |
| 85 | - }, " *"), /*#__PURE__*/React.createElement("span", null, schema.title)) : schema.title, | |
| 86 | - width: FIELD_LENGTH, | |
| 87 | - render: function render(value, record, index) { | |
| 88 | - // Check: record.index 似乎是antd自己会给的,不错哦 | |
| 89 | - var childIndex = [].concat(_toConsumableArray(dataIndex), [record.index]); | |
| 90 | - return /*#__PURE__*/React.createElement(Core, { | |
| 91 | - hideTitle: true, | |
| 92 | - displayType: "inline", | |
| 93 | - key: index.toString(), | |
| 94 | - id: child, | |
| 95 | - dataIndex: childIndex | |
| 96 | - }); | |
| 97 | - } | |
| 98 | - }, columnProps); | |
| 99 | - }); | |
| 100 | - | |
| 101 | - if (!props.hideDelete || !props.hideAdd || !props.hideCopy || !props.hideMove) { | |
| 102 | - columns.push({ | |
| 103 | - title: '操作', | |
| 104 | - key: '$action', | |
| 105 | - fixed: 'right', | |
| 106 | - width: 120, | |
| 107 | - render: function render(value, record, idx) { | |
| 108 | - return /*#__PURE__*/React.createElement("div", null, !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement("a", { | |
| 109 | - onClick: function onClick() { | |
| 110 | - return copyItem(idx); | |
| 111 | - } | |
| 112 | - }, "\u590D\u5236"), !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 113 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 114 | - onConfirm: function onConfirm() { | |
| 115 | - return deleteItem(idx); | |
| 116 | - }, | |
| 117 | - okText: "\u786E\u5B9A", | |
| 118 | - cancelText: "\u53D6\u6D88" | |
| 119 | - }, /*#__PURE__*/React.createElement("a", { | |
| 120 | - style: { | |
| 121 | - marginLeft: 8 | |
| 122 | - } | |
| 123 | - }, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 124 | - style: { | |
| 125 | - color: '#1890ff', | |
| 126 | - fontSize: 16, | |
| 127 | - marginLeft: 8 | |
| 128 | - }, | |
| 129 | - onClick: function onClick() { | |
| 130 | - return moveItemUp(idx); | |
| 131 | - } | |
| 132 | - }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 133 | - style: { | |
| 134 | - color: '#1890ff', | |
| 135 | - fontSize: 16, | |
| 136 | - marginLeft: 8 | |
| 137 | - }, | |
| 138 | - onClick: function onClick() { | |
| 139 | - return moveItemDown(idx); | |
| 140 | - } | |
| 141 | - }))); | |
| 142 | - } | |
| 143 | - }); | |
| 144 | - } | |
| 145 | - | |
| 146 | - return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", { | |
| 147 | - className: "w-100 mb2 tr" | |
| 148 | - }, !props.hideAdd && /*#__PURE__*/React.createElement(_Button, { | |
| 149 | - type: "primary", | |
| 150 | - size: "small", | |
| 151 | - onClick: addItem | |
| 152 | - }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 153 | - var callback = item.callback, | |
| 154 | - text = item.text, | |
| 155 | - html = item.html; | |
| 156 | - | |
| 157 | - var onClick = function onClick() { | |
| 158 | - console.log({ | |
| 159 | - value: listData, | |
| 160 | - onChange: changeList, | |
| 161 | - schema: schema | |
| 162 | - }); | |
| 163 | - }; | |
| 164 | - | |
| 165 | - if (typeof window[callback] === 'function') { | |
| 166 | - onClick = function onClick() { | |
| 167 | - window[callback]({ | |
| 168 | - value: listData, | |
| 169 | - onChange: changeList, | |
| 170 | - schema: schema | |
| 171 | - }); | |
| 172 | - }; | |
| 173 | - } | |
| 174 | - | |
| 175 | - return /*#__PURE__*/React.createElement(_Button, { | |
| 176 | - key: idx.toString(), | |
| 177 | - style: { | |
| 178 | - marginLeft: 8 | |
| 179 | - }, | |
| 180 | - size: "small", | |
| 181 | - onClick: onClick | |
| 182 | - }, /*#__PURE__*/React.createElement("span", { | |
| 183 | - dangerouslySetInnerHTML: { | |
| 184 | - __html: html || text | |
| 185 | - } | |
| 186 | - })); | |
| 187 | - }) : null), /*#__PURE__*/React.createElement(_Table, _extends({ | |
| 188 | - scroll: { | |
| 189 | - x: 'max-content' | |
| 190 | - }, | |
| 191 | - columns: columns, | |
| 192 | - dataSource: dataSource, | |
| 193 | - rowKey: "index", | |
| 194 | - size: "small", | |
| 195 | - pagination: paginationConfig | |
| 196 | - }, rest))); | |
| 197 | -}; | |
| 198 | - | |
| 199 | -export default TableList; | |
| \ No newline at end of file |
es/form-render-core/src/index.d.ts
deleted
100644 → 0
| 1 | -import * as React from 'react'; | |
| 2 | -export interface Error { | |
| 3 | - /** 错误的数据路径 */ | |
| 4 | - name: string; | |
| 5 | - /** 错误的内容 */ | |
| 6 | - error: string[]; | |
| 7 | -} | |
| 8 | -export interface FormParams { | |
| 9 | - formData?: any; | |
| 10 | - onChange?: (data: any) => void; | |
| 11 | - onValidate?: (valid: any) => void; | |
| 12 | - showValidate?: boolean; | |
| 13 | - /** 数据分析接口,表单展示完成渲染时触发 */ | |
| 14 | - logOnMount?: (stats: any) => void; | |
| 15 | - /** 数据分析接口,表单提交成功时触发,获得本次表单填写的总时长 */ | |
| 16 | - logOnSubmit?: (stats: any) => void; | |
| 17 | -} | |
| 18 | - | |
| 19 | -export interface ValidateParams { | |
| 20 | - formData: any; | |
| 21 | - schema: any; | |
| 22 | - error: Error[]; | |
| 23 | - [k: string]: any; | |
| 24 | -} | |
| 25 | -export interface FormInstance { | |
| 26 | - formData: any; | |
| 27 | - schema: any; | |
| 28 | - flatten: any; | |
| 29 | - touchedKeys: string[]; | |
| 30 | - touchKey: (key: string) => void; | |
| 31 | - onItemChange: (path: string, value: any) => void; | |
| 32 | - setValueByPath: (path: string, value: any) => void; | |
| 33 | - getSchemaByPath: (path: string) => object; | |
| 34 | - setSchemaByPath: (path: string, value: any) => void; | |
| 35 | - setSchema: (settings: any) => void; | |
| 36 | - setValues: (formData: any) => void; | |
| 37 | - getValues: () => any; | |
| 38 | - resetFields: () => void; | |
| 39 | - submit: () => Promise<void> | Promise<any[]>; | |
| 40 | - submitData: any; | |
| 41 | - errorFields: Error[]; | |
| 42 | - isValidating: boolean; | |
| 43 | - outsideValidating: boolean; | |
| 44 | - isSubmitting: boolean; | |
| 45 | - endValidating: () => void; | |
| 46 | - endSubmitting: () => void; | |
| 47 | - setErrorFields: (error: Error[]) => void; | |
| 48 | - removeErrorField: (path: string) => void; | |
| 49 | - removeTouched: (path: string) => void; | |
| 50 | - changeTouchedKeys: (pathArray: string[]) => void; | |
| 51 | - isEditing: boolean; | |
| 52 | - setEditing: (status: boolean) => void; | |
| 53 | - syncStuff: (args: any) => void; | |
| 54 | - /** 折中升级方案中使用到,正常用不到 */ | |
| 55 | - init: () => void; | |
| 56 | - /** 数据分析接口,表单展示完成渲染时触发 */ | |
| 57 | - logOnMount: (args: any) => void; | |
| 58 | - /** 数据分析接口,表单提交成功时触发,获得本次表单填写的总时长 */ | |
| 59 | - logOnSubmit: (args: any) => void; | |
| 60 | - _setErrors: (args: any) => void; | |
| 61 | -} | |
| 62 | - | |
| 63 | -export type WatchProperties = { | |
| 64 | - [path: string]: | |
| 65 | - | { | |
| 66 | - handler: (value: any) => void; | |
| 67 | - immediate?: boolean; | |
| 68 | - } | |
| 69 | - | ((value: any) => void); | |
| 70 | -}; | |
| 71 | - | |
| 72 | -export interface FRProps { | |
| 73 | - /** 表单 id */ | |
| 74 | - id?: string | number; | |
| 75 | - /** 表单顶层的className */ | |
| 76 | - className?: string; | |
| 77 | - /** 表单顶层的样式 */ | |
| 78 | - style?: any; | |
| 79 | - /** 表单 schema */ | |
| 80 | - schema: any; | |
| 81 | - /** form单例 */ | |
| 82 | - form: FormInstance; | |
| 83 | - /** 组件和schema的映射规则 */ | |
| 84 | - mapping?: any; | |
| 85 | - /** 自定义组件 */ | |
| 86 | - widgets?: any; | |
| 87 | - /** 表单提交前钩子 */ | |
| 88 | - displayType?: string; | |
| 89 | - /** 只读模式 */ | |
| 90 | - readOnly?: boolean; | |
| 91 | - /** 禁用模式 */ | |
| 92 | - disabled?: boolean; | |
| 93 | - /** 标签宽度 */ | |
| 94 | - labelWidth?: string | number; | |
| 95 | - /** antd的全局config */ | |
| 96 | - configProvider?: any; | |
| 97 | - theme?: string | number; | |
| 98 | - /** 覆盖默认的校验信息 */ | |
| 99 | - validateMessages?: any; | |
| 100 | - /** 显示当前表单内部状态 */ | |
| 101 | - debug?: boolean; | |
| 102 | - /** 显示css布局提示线 */ | |
| 103 | - debugCss?: boolean; | |
| 104 | - locale?: string; | |
| 105 | - column?: number; | |
| 106 | - debounceInput?: boolean; | |
| 107 | - size?: string; | |
| 108 | - // 数据会作为 beforeFinish 的第四个参数传入 | |
| 109 | - config?: any; | |
| 110 | - // 类似于 vuejs 的 watch 的用法,监控值的变化,触发 callback | |
| 111 | - watch?: WatchProperties; | |
| 112 | - /** 对象组件是否折叠(全局的控制) */ | |
| 113 | - allCollapsed?: boolean; | |
| 114 | - /** 表单的全局共享属性 */ | |
| 115 | - globalProps?: any; | |
| 116 | - /** 表单首次加载钩子 */ | |
| 117 | - onMount?: () => void; | |
| 118 | - /** 表单提交前钩子 */ | |
| 119 | - beforeFinish?: (params: ValidateParams) => Error[] | Promise<Error[]>; | |
| 120 | - /** 表单提交后钩子 */ | |
| 121 | - onFinish?: (formData: any, error: Error[]) => void; | |
| 122 | - /** 时时与外部更新同步的钩子 */ | |
| 123 | - onValuesChange?: (changedValues: any, formData: any) => void; | |
| 124 | - /** 隐藏的数据是否去掉,默认不去掉(false) */ | |
| 125 | - removeHiddenData?: boolean; | |
| 126 | -} | |
| 127 | - | |
| 128 | -declare const FR: React.FC<FRProps>; | |
| 129 | - | |
| 130 | -export declare function useForm(params?: FormParams): FormInstance; | |
| 131 | - | |
| 132 | -export type ConnectedForm<T> = T & { | |
| 133 | - form: FormInstance; | |
| 134 | -}; | |
| 135 | - | |
| 136 | -export declare function connectForm<T extends {} = any>( | |
| 137 | - component: React.ComponentType<ConnectedForm<T>> | |
| 138 | -): React.ComponentType<T>; | |
| 139 | - | |
| 140 | -export default FR; |
es/form-render-core/src/index.js
deleted
100644 → 0
| 1 | -var _excluded = ["id", "widgets", "mapping", "form", "className", "style", "beforeFinish", "onFinish", "displayType", "schema", "debug", "debugCss", "locale", "debounceInput", "size", "configProvider", "theme", "validateMessages", "watch", "config", "onMount", "labelWidth", "readOnly", "disabled", "allCollapsed", "onValuesChange", "column", "removeHiddenData", "globalProps"], | |
| 2 | - _excluded2 = ["onItemChange", "setEditing", "touchKey", "setValueByPath", "getSchemaByPath", "setSchemaByPath", "setSchema", "setValues", "getValues", "resetFields", "submit", "endValidating", "endSubmitting", "setErrorFields", "removeErrorField", "removeTouched", "changeTouchedKeys", "syncStuff", "logOnMount", "logOnSubmit", "setFirstMount", "_setErrors"], | |
| 3 | - _excluded3 = ["isOldVersion", "schema"]; | |
| 4 | - | |
| 5 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 6 | - | |
| 7 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 8 | - | |
| 9 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 10 | - | |
| 11 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 12 | - | |
| 13 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 14 | - | |
| 15 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 16 | - | |
| 17 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 18 | - | |
| 19 | -/* eslint-disable react-hooks/exhaustive-deps */ | |
| 20 | -import React, { useEffect, useMemo, useRef } from 'react'; | |
| 21 | -import { updateSchemaToNewVersion, msToTime, yymmdd, getParamByName } from './utils'; | |
| 22 | -import Core from './core'; | |
| 23 | -import Watcher from './Watcher'; | |
| 24 | -import { Ctx, StoreCtx, Store2Ctx } from './hooks'; | |
| 25 | -import "./atom.css"; | |
| 26 | -import "./index.css"; | |
| 27 | -import { mapping as defaultMapping } from './mapping'; | |
| 28 | - | |
| 29 | -var defaultFinish = function defaultFinish(data, errors) { | |
| 30 | - console.log('onFinish:', { | |
| 31 | - data: data, | |
| 32 | - errors: errors | |
| 33 | - }); | |
| 34 | -}; | |
| 35 | - | |
| 36 | -export { default as useForm } from './useForm'; | |
| 37 | -export { defaultMapping as mapping }; | |
| 38 | -export { default as connectForm } from './connectForm'; | |
| 39 | - | |
| 40 | -function App(_ref) { | |
| 41 | - var id = _ref.id, | |
| 42 | - widgets = _ref.widgets, | |
| 43 | - mapping = _ref.mapping, | |
| 44 | - form = _ref.form, | |
| 45 | - className = _ref.className, | |
| 46 | - style = _ref.style, | |
| 47 | - beforeFinish = _ref.beforeFinish, | |
| 48 | - _ref$onFinish = _ref.onFinish, | |
| 49 | - onFinish = _ref$onFinish === void 0 ? defaultFinish : _ref$onFinish, | |
| 50 | - _ref$displayType = _ref.displayType, | |
| 51 | - displayType = _ref$displayType === void 0 ? 'column' : _ref$displayType, | |
| 52 | - schema = _ref.schema, | |
| 53 | - debug = _ref.debug, | |
| 54 | - debugCss = _ref.debugCss, | |
| 55 | - _ref$locale = _ref.locale, | |
| 56 | - locale = _ref$locale === void 0 ? 'cn' : _ref$locale, | |
| 57 | - _ref$debounceInput = _ref.debounceInput, | |
| 58 | - debounceInput = _ref$debounceInput === void 0 ? false : _ref$debounceInput, | |
| 59 | - size = _ref.size, | |
| 60 | - configProvider = _ref.configProvider, | |
| 61 | - theme = _ref.theme, | |
| 62 | - validateMessages = _ref.validateMessages, | |
| 63 | - _ref$watch = _ref.watch, | |
| 64 | - watch = _ref$watch === void 0 ? {} : _ref$watch, | |
| 65 | - config = _ref.config, | |
| 66 | - onMount = _ref.onMount, | |
| 67 | - labelWidth = _ref.labelWidth, | |
| 68 | - readOnly = _ref.readOnly, | |
| 69 | - disabled = _ref.disabled, | |
| 70 | - _ref$allCollapsed = _ref.allCollapsed, | |
| 71 | - allCollapsed = _ref$allCollapsed === void 0 ? false : _ref$allCollapsed, | |
| 72 | - onValuesChange = _ref.onValuesChange, | |
| 73 | - column = _ref.column, | |
| 74 | - _ref$removeHiddenData = _ref.removeHiddenData, | |
| 75 | - removeHiddenData = _ref$removeHiddenData === void 0 ? false : _ref$removeHiddenData, | |
| 76 | - _ref$globalProps = _ref.globalProps, | |
| 77 | - globalProps = _ref$globalProps === void 0 ? {} : _ref$globalProps, | |
| 78 | - rest = _objectWithoutProperties(_ref, _excluded); | |
| 79 | - | |
| 80 | - try { | |
| 81 | - var _ = form.submit; | |
| 82 | - } catch (error) { | |
| 83 | - console.error('form 为必填 props,<FormRender /> 没有接收到 form 属性!'); | |
| 84 | - } | |
| 85 | - | |
| 86 | - var _column = schema && schema.column || column; | |
| 87 | - | |
| 88 | - var onItemChange = form.onItemChange, | |
| 89 | - setEditing = form.setEditing, | |
| 90 | - touchKey = form.touchKey, | |
| 91 | - setValueByPath = form.setValueByPath, | |
| 92 | - getSchemaByPath = form.getSchemaByPath, | |
| 93 | - setSchemaByPath = form.setSchemaByPath, | |
| 94 | - setSchema = form.setSchema, | |
| 95 | - setValues = form.setValues, | |
| 96 | - getValues = form.getValues, | |
| 97 | - resetFields = form.resetFields, | |
| 98 | - submit = form.submit, | |
| 99 | - endValidating = form.endValidating, | |
| 100 | - endSubmitting = form.endSubmitting, | |
| 101 | - setErrorFields = form.setErrorFields, | |
| 102 | - removeErrorField = form.removeErrorField, | |
| 103 | - removeTouched = form.removeTouched, | |
| 104 | - changeTouchedKeys = form.changeTouchedKeys, | |
| 105 | - syncStuff = form.syncStuff, | |
| 106 | - logOnMount = form.logOnMount, | |
| 107 | - logOnSubmit = form.logOnSubmit, | |
| 108 | - setFirstMount = form.setFirstMount, | |
| 109 | - _setErrors = form._setErrors, | |
| 110 | - valuesThatWillChange = _objectWithoutProperties(form, _excluded2); | |
| 111 | - | |
| 112 | - var submitData = valuesThatWillChange.submitData, | |
| 113 | - errorFields = valuesThatWillChange.errorFields, | |
| 114 | - isValidating = valuesThatWillChange.isValidating, | |
| 115 | - outsideValidating = valuesThatWillChange.outsideValidating, | |
| 116 | - isSubmitting = valuesThatWillChange.isSubmitting, | |
| 117 | - formData = valuesThatWillChange.formData, | |
| 118 | - flatten = valuesThatWillChange.flatten, | |
| 119 | - showValidate = valuesThatWillChange.showValidate, | |
| 120 | - firstMount = valuesThatWillChange.firstMount; | |
| 121 | - useEffect(function () { | |
| 122 | - // Schema最外层的type是object来判断,没有的话,认为schema没有传 | |
| 123 | - if (schema && schema.type) { | |
| 124 | - setFirstMount(true); | |
| 125 | - syncStuff({ | |
| 126 | - schema: schema, | |
| 127 | - locale: locale, | |
| 128 | - validateMessages: validateMessages, | |
| 129 | - beforeFinish: beforeFinish, | |
| 130 | - onMount: onMount, | |
| 131 | - removeHiddenData: removeHiddenData | |
| 132 | - }); | |
| 133 | - } else {} | |
| 134 | - }, [JSON.stringify(schema)]); | |
| 135 | - useEffect(function () { | |
| 136 | - if (!firstMount && schema && schema.type) { | |
| 137 | - if (typeof onMount === 'function') { | |
| 138 | - // 等一下 useForm 里接到第一份schema时,计算第一份data的骨架 | |
| 139 | - setTimeout(function () { | |
| 140 | - onMount(); | |
| 141 | - }, 0); | |
| 142 | - } | |
| 143 | - | |
| 144 | - setTimeout(onMountLogger, 0); | |
| 145 | - } | |
| 146 | - }, [JSON.stringify(schema), firstMount]); | |
| 147 | - | |
| 148 | - var onMountLogger = function onMountLogger() { | |
| 149 | - var start = new Date().getTime(); | |
| 150 | - | |
| 151 | - if (typeof logOnMount === 'function' || typeof logOnSubmit === 'function') { | |
| 152 | - sessionStorage.setItem('FORM_MOUNT_TIME', start); | |
| 153 | - sessionStorage.setItem('FORM_START', start); | |
| 154 | - } | |
| 155 | - | |
| 156 | - if (typeof logOnMount === 'function') { | |
| 157 | - var logParams = { | |
| 158 | - schema: schema, | |
| 159 | - url: location.href, | |
| 160 | - formData: JSON.stringify(form.getValues()), | |
| 161 | - formMount: yymmdd(start) | |
| 162 | - }; | |
| 163 | - | |
| 164 | - if (id) { | |
| 165 | - logParams.id = id; | |
| 166 | - } | |
| 167 | - | |
| 168 | - logOnMount(logParams); | |
| 169 | - } // 如果是要计算时间,在 onMount 时存一个时间戳 | |
| 170 | - | |
| 171 | - | |
| 172 | - if (typeof logOnSubmit === 'function') { | |
| 173 | - sessionStorage.setItem('NUMBER_OF_SUBMITS', 0); | |
| 174 | - sessionStorage.setItem('FAILED_ATTEMPTS', 0); | |
| 175 | - } | |
| 176 | - }; // 组件destroy的时候,destroy form,因为useForm可能在上层,所以不一定会跟着destroy | |
| 177 | - | |
| 178 | - | |
| 179 | - useEffect(function () { | |
| 180 | - return function () { | |
| 181 | - form.resetFields(); | |
| 182 | - }; | |
| 183 | - }, []); | |
| 184 | - var store = useMemo(function () { | |
| 185 | - return _objectSpread(_objectSpread({}, valuesThatWillChange), {}, { | |
| 186 | - globalProps: globalProps | |
| 187 | - }, rest); | |
| 188 | - }, [JSON.stringify(flatten), JSON.stringify(formData), JSON.stringify(errorFields), JSON.stringify(globalProps)]); // 不常用的context单独放一个地方 | |
| 189 | - | |
| 190 | - var store2 = useMemo(function () { | |
| 191 | - return { | |
| 192 | - displayType: displayType, | |
| 193 | - theme: theme, | |
| 194 | - column: _column, | |
| 195 | - debounceInput: debounceInput, | |
| 196 | - debug: debug, | |
| 197 | - labelWidth: labelWidth, | |
| 198 | - locale: locale, | |
| 199 | - validateMessages: validateMessages, | |
| 200 | - readOnly: readOnly, | |
| 201 | - disabled: disabled, | |
| 202 | - allCollapsed: allCollapsed, | |
| 203 | - showValidate: showValidate | |
| 204 | - }; | |
| 205 | - }, [displayType, theme, _column, debounceInput, debug, labelWidth, locale, validateMessages, readOnly, disabled, allCollapsed, showValidate]); | |
| 206 | - var tools = useMemo(function () { | |
| 207 | - return _objectSpread({ | |
| 208 | - widgets: widgets, | |
| 209 | - mapping: _objectSpread(_objectSpread({}, defaultMapping), mapping), | |
| 210 | - onValuesChange: onValuesChange | |
| 211 | - }, form); | |
| 212 | - }, []); | |
| 213 | - useEffect(function () { | |
| 214 | - // 需要外部校验的情况,此时 submitting 还是 false | |
| 215 | - if (outsideValidating === true) { | |
| 216 | - Promise.resolve(beforeFinish(_objectSpread({ | |
| 217 | - data: submitData, | |
| 218 | - schema: schema, | |
| 219 | - errors: errorFields | |
| 220 | - }, config))).then(function (error) { | |
| 221 | - if (error) { | |
| 222 | - setErrorFields(error); | |
| 223 | - } | |
| 224 | - | |
| 225 | - endValidating(); | |
| 226 | - }); | |
| 227 | - return; | |
| 228 | - } // 如果validation结束,submitting开始 | |
| 229 | - | |
| 230 | - | |
| 231 | - if (isValidating === false && isSubmitting === true) { | |
| 232 | - endSubmitting(); | |
| 233 | - onFinish(submitData, errorFields); | |
| 234 | - | |
| 235 | - if (typeof logOnSubmit === 'function') { | |
| 236 | - var start = sessionStorage.getItem('FORM_START'); | |
| 237 | - var mount = sessionStorage.getItem('FORM_MOUNT_TIME'); | |
| 238 | - var numberOfSubmits = Number(sessionStorage.getItem('NUMBER_OF_SUBMITS')) + 1; | |
| 239 | - var end = new Date().getTime(); | |
| 240 | - var failedAttempts = Number(sessionStorage.getItem('FAILED_ATTEMPTS')); | |
| 241 | - | |
| 242 | - if (errorFields.length > 0) { | |
| 243 | - failedAttempts = failedAttempts + 1; | |
| 244 | - } | |
| 245 | - | |
| 246 | - var logParams = { | |
| 247 | - formMount: yymmdd(mount), | |
| 248 | - ms: end - start, | |
| 249 | - duration: msToTime(end - start), | |
| 250 | - numberOfSubmits: numberOfSubmits, | |
| 251 | - failedAttempts: failedAttempts, | |
| 252 | - url: location.href, | |
| 253 | - formData: JSON.stringify(submitData), | |
| 254 | - errors: JSON.stringify(errorFields), | |
| 255 | - schema: JSON.stringify(schema) | |
| 256 | - }; | |
| 257 | - | |
| 258 | - if (id) { | |
| 259 | - logParams.id = id; | |
| 260 | - } | |
| 261 | - | |
| 262 | - logOnSubmit(logParams); | |
| 263 | - sessionStorage.setItem('FORM_START', end); | |
| 264 | - sessionStorage.setItem('NUMBER_OF_SUBMITS', numberOfSubmits); | |
| 265 | - sessionStorage.setItem('FAILED_ATTEMPTS', failedAttempts); | |
| 266 | - } | |
| 267 | - } | |
| 268 | - }, [isValidating, isSubmitting, outsideValidating]); // TODO: fk doesn't work | |
| 269 | - | |
| 270 | - var sizeCls = ''; | |
| 271 | - | |
| 272 | - if (size === 'small') { | |
| 273 | - sizeCls = 'fr-form-small'; | |
| 274 | - } else if (size === 'large') { | |
| 275 | - sizeCls = 'fr-form-large'; | |
| 276 | - } | |
| 277 | - | |
| 278 | - var rootProps = { | |
| 279 | - className: "fr-container ".concat(sizeCls, " ").concat(className || '') | |
| 280 | - }; | |
| 281 | - | |
| 282 | - if (style && _typeof(style) === 'object') { | |
| 283 | - rootProps.style = style; | |
| 284 | - } | |
| 285 | - | |
| 286 | - if (id && ['number', 'string'].indexOf(_typeof(id)) > -1) { | |
| 287 | - rootProps.id = id; | |
| 288 | - } | |
| 289 | - | |
| 290 | - var debugForm = getParamByName('_debug_form'); | |
| 291 | - var debugFormCss = getParamByName('_debug_form_css'); | |
| 292 | - var isPre = location.href.indexOf('pre') > -1; | |
| 293 | - var watchList = Object.keys(watch); | |
| 294 | - return /*#__PURE__*/React.createElement(StoreCtx.Provider, { | |
| 295 | - value: store | |
| 296 | - }, /*#__PURE__*/React.createElement(Store2Ctx.Provider, { | |
| 297 | - value: store2 | |
| 298 | - }, /*#__PURE__*/React.createElement(Ctx.Provider, { | |
| 299 | - value: tools | |
| 300 | - }, /*#__PURE__*/React.createElement("div", rootProps, isPre && debugForm || debug ? /*#__PURE__*/React.createElement("div", { | |
| 301 | - className: "mv2 bg-black-05 pa2 br2" | |
| 302 | - }, /*#__PURE__*/React.createElement("div", { | |
| 303 | - style: { | |
| 304 | - display: 'flex' | |
| 305 | - } | |
| 306 | - }, /*#__PURE__*/React.createElement("span", null, "formData:"), /*#__PURE__*/React.createElement("span", { | |
| 307 | - style: { | |
| 308 | - display: 'inline-block', | |
| 309 | - wordBreak: 'break-all', | |
| 310 | - maxWidth: 600 | |
| 311 | - } | |
| 312 | - }, JSON.stringify(form.formData))), /*#__PURE__*/React.createElement("div", null, 'errorFields:' + JSON.stringify(form.errorFields)), /*#__PURE__*/React.createElement("div", null, 'touchedKeys:' + JSON.stringify(form.touchedKeys)), /*#__PURE__*/React.createElement("div", null, 'allTouched:' + JSON.stringify(form.allTouched)), /*#__PURE__*/React.createElement("div", null, 'descriptor:' + JSON.stringify(window.descriptor))) : null, watchList.length > 0 ? watchList.map(function (item, idx) { | |
| 313 | - return /*#__PURE__*/React.createElement(Watcher, { | |
| 314 | - key: idx.toString(), | |
| 315 | - watchKey: item, | |
| 316 | - watch: watch, | |
| 317 | - formData: formData, | |
| 318 | - firstMount: firstMount | |
| 319 | - }); | |
| 320 | - }) : null, /*#__PURE__*/React.createElement(Core, { | |
| 321 | - debugCss: isPre && debugFormCss || debugCss | |
| 322 | - }))))); | |
| 323 | -} | |
| 324 | - | |
| 325 | -export { createWidget } from './createWidget'; | |
| 326 | - | |
| 327 | -var Wrapper = function Wrapper(props) { | |
| 328 | - var _ref2 = props || {}, | |
| 329 | - _ref2$isOldVersion = _ref2.isOldVersion, | |
| 330 | - isOldVersion = _ref2$isOldVersion === void 0 ? true : _ref2$isOldVersion, | |
| 331 | - schema = _ref2.schema, | |
| 332 | - rest = _objectWithoutProperties(_ref2, _excluded3); | |
| 333 | - | |
| 334 | - var _schema = useRef(schema); | |
| 335 | - | |
| 336 | - if (isOldVersion) { | |
| 337 | - _schema.current = updateSchemaToNewVersion(schema); | |
| 338 | - } | |
| 339 | - | |
| 340 | - return /*#__PURE__*/React.createElement(App, _extends({ | |
| 341 | - schema: _schema.current | |
| 342 | - }, rest)); | |
| 343 | -}; | |
| 344 | - | |
| 345 | -export default Wrapper; | |
| \ No newline at end of file |
es/form-render-core/src/mapping.js
deleted
100644 → 0
| 1 | -export var mapping = { | |
| 2 | - default: 'input', | |
| 3 | - string: 'input', | |
| 4 | - array: 'list', | |
| 5 | - boolean: 'checkbox', | |
| 6 | - integer: 'number', | |
| 7 | - number: 'number', | |
| 8 | - object: 'map', | |
| 9 | - html: 'html', | |
| 10 | - 'string:upload': 'upload', | |
| 11 | - 'string:url': 'url', | |
| 12 | - 'string:dateTime': 'date', | |
| 13 | - 'string:date': 'date', | |
| 14 | - 'string:year': 'date', | |
| 15 | - 'string:month': 'date', | |
| 16 | - 'string:week': 'date', | |
| 17 | - 'string:quarter': 'date', | |
| 18 | - 'string:time': 'time', | |
| 19 | - 'string:textarea': 'textarea', | |
| 20 | - 'string:color': 'color', | |
| 21 | - 'string:image': 'imageInput', | |
| 22 | - 'range:time': 'timeRange', | |
| 23 | - 'range:dateTime': 'dateRange', | |
| 24 | - 'range:date': 'dateRange', | |
| 25 | - 'range:year': 'dateRange', | |
| 26 | - 'range:month': 'dateRange', | |
| 27 | - 'range:week': 'dateRange', | |
| 28 | - 'range:quarter': 'dateRange', | |
| 29 | - '*?enum': 'radio', | |
| 30 | - '*?enum_long': 'select', | |
| 31 | - 'array?enum': 'checkboxes', | |
| 32 | - 'array?enum_long': 'multiSelect', | |
| 33 | - '*?readOnly': 'html' // TODO: html widgets for list / object | |
| 34 | - | |
| 35 | -}; | |
| 36 | -export function getWidgetName(schema) { | |
| 37 | - var _mapping = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : mapping; | |
| 38 | - | |
| 39 | - var type = schema.type, | |
| 40 | - format = schema.format, | |
| 41 | - enums = schema.enum, | |
| 42 | - readOnly = schema.readOnly, | |
| 43 | - widget = schema.widget; // 如果已经注明了渲染widget,那最好 | |
| 44 | - // if (schema['ui:widget']) { | |
| 45 | - // return schema['ui:widget']; | |
| 46 | - // } | |
| 47 | - | |
| 48 | - var list = []; | |
| 49 | - | |
| 50 | - if (readOnly) { | |
| 51 | - list.push("".concat(type, "?readOnly")); | |
| 52 | - list.push('*?readOnly'); | |
| 53 | - } | |
| 54 | - | |
| 55 | - if (enums) { | |
| 56 | - // 根据enum长度来智能选择控件 | |
| 57 | - if (Array.isArray(enums) && (type === 'array' && enums.length > 6 || type !== 'array' && enums.length > 2)) { | |
| 58 | - list.push("".concat(type, "?enum_long")); | |
| 59 | - list.push('*?enum_long'); | |
| 60 | - } else { | |
| 61 | - list.push("".concat(type, "?enum")); // array 默认使用list,array?enum 默认使用checkboxes,*?enum 默认使用select | |
| 62 | - | |
| 63 | - list.push('*?enum'); | |
| 64 | - } | |
| 65 | - } | |
| 66 | - | |
| 67 | - var _widget = widget || format; | |
| 68 | - | |
| 69 | - if (_widget) { | |
| 70 | - list.push("".concat(type, ":").concat(_widget)); | |
| 71 | - } | |
| 72 | - | |
| 73 | - list.push(type); // 放在最后兜底,其他都不match时使用type默认的组件 | |
| 74 | - | |
| 75 | - var found = ''; | |
| 76 | - list.some(function (item) { | |
| 77 | - found = _mapping[item]; | |
| 78 | - return !!found; | |
| 79 | - }); | |
| 80 | - return found; | |
| 81 | -} | |
| 82 | -export var extraSchemaList = { | |
| 83 | - checkbox: { | |
| 84 | - valuePropName: 'checked' | |
| 85 | - }, | |
| 86 | - switch: { | |
| 87 | - valuePropName: 'checked' | |
| 88 | - } | |
| 89 | -}; | |
| \ No newline at end of file |
es/form-render-core/src/utils.js
deleted
100644 → 0
| 1 | -var _excluded = ["propsSchema"], | |
| 2 | - _excluded2 = ["schema"]; | |
| 3 | - | |
| 4 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 5 | - | |
| 6 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 7 | - | |
| 8 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 9 | - | |
| 10 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 11 | - | |
| 12 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 13 | - | |
| 14 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 15 | - | |
| 16 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 17 | - | |
| 18 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 19 | - | |
| 20 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 21 | - | |
| 22 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 23 | - | |
| 24 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 25 | - | |
| 26 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 27 | - | |
| 28 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 29 | - | |
| 30 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 31 | - | |
| 32 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 33 | - | |
| 34 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 35 | - | |
| 36 | -import { get, set, cloneDeep } from 'lodash-es'; // window.log1 = value => { | |
| 37 | -// console.log('%ccommon:', 'color: #00A7F7; font-weight: 500;', value); | |
| 38 | -// }; | |
| 39 | -// window.log2 = value => { | |
| 40 | -// console.log('%cwarning:', 'color: #f50; font-weight: 500;', value); | |
| 41 | -// }; | |
| 42 | -// window.log3 = value => { | |
| 43 | -// console.log('%csuccess:', 'color: #87d068; font-weight: 500;', value); | |
| 44 | -// }; | |
| 45 | -// window.log4 = value => { | |
| 46 | -// console.log('%cspecial:', 'color: #722ed1; font-weight: 500;', value); | |
| 47 | -// }; | |
| 48 | - | |
| 49 | -export function getParamByName(name) { | |
| 50 | - var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href; | |
| 51 | - name = name.replace(/[\[\]]/g, '\\$&'); | |
| 52 | - var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), | |
| 53 | - results = regex.exec(url); | |
| 54 | - if (!results) return null; | |
| 55 | - if (!results[2]) return ''; | |
| 56 | - return decodeURIComponent(results[2].replace(/\+/g, ' ')); | |
| 57 | -} | |
| 58 | -export function isUrl(string) { | |
| 59 | - var protocolRE = /^(?:\w+:)?\/\/(\S+)$/; // const domainRE = /^[^\s\.]+\.\S{2,}$/; | |
| 60 | - | |
| 61 | - if (typeof string !== 'string') return false; | |
| 62 | - return protocolRE.test(string); | |
| 63 | -} | |
| 64 | -export function isCheckBoxType(schema, readOnly) { | |
| 65 | - if (readOnly) return false; | |
| 66 | - if (schema.widget === 'checkbox') return true; | |
| 67 | - | |
| 68 | - if (schema && schema.type === 'boolean') { | |
| 69 | - if (schema.enum) return false; | |
| 70 | - if (schema.widget === undefined) return true; | |
| 71 | - return false; | |
| 72 | - } | |
| 73 | -} // a[].b.c => a.b.c | |
| 74 | - | |
| 75 | -function removeBrackets(string) { | |
| 76 | - if (typeof string === 'string') { | |
| 77 | - return string.replace(/\[\]/g, ''); | |
| 78 | - } else { | |
| 79 | - return string; | |
| 80 | - } | |
| 81 | -} | |
| 82 | - | |
| 83 | -export function getParentPath(path) { | |
| 84 | - if (typeof path === 'string') { | |
| 85 | - var pathArr = path.split('.'); | |
| 86 | - | |
| 87 | - if (pathArr.length === 1) { | |
| 88 | - return '#'; | |
| 89 | - } | |
| 90 | - | |
| 91 | - pathArr.pop(); | |
| 92 | - return pathArr.join('.'); | |
| 93 | - } | |
| 94 | - | |
| 95 | - return '#'; | |
| 96 | -} | |
| 97 | -export function getValueByPath(formData, path) { | |
| 98 | - if (path === '#' || !path) { | |
| 99 | - return formData || {}; | |
| 100 | - } else if (typeof path === 'string') { | |
| 101 | - return get(formData, path); | |
| 102 | - } else { | |
| 103 | - console.error('path has to be a string'); | |
| 104 | - } | |
| 105 | -} // path: 'a.b[1].c[0]' => { id: 'a.b[].c[]' dataIndex: [1,0] } | |
| 106 | - | |
| 107 | -export function destructDataPath(path) { | |
| 108 | - var id; | |
| 109 | - var dataIndex; | |
| 110 | - | |
| 111 | - if (path === '#') { | |
| 112 | - return { | |
| 113 | - id: '#', | |
| 114 | - dataIndex: [] | |
| 115 | - }; | |
| 116 | - } | |
| 117 | - | |
| 118 | - if (typeof path !== 'string') { | |
| 119 | - throw Error("path ".concat(path, " is not a string!!! Something wrong here")); | |
| 120 | - } | |
| 121 | - | |
| 122 | - var pattern = /\[[0-9]+\]/g; | |
| 123 | - var matchList = path.match(pattern); | |
| 124 | - | |
| 125 | - if (!matchList) { | |
| 126 | - id = path; | |
| 127 | - } else { | |
| 128 | - id = path.replace(pattern, '[]'); // 这个是match下来的结果,可安全处理 | |
| 129 | - | |
| 130 | - dataIndex = matchList.map(function (item) { | |
| 131 | - return Number(item.substring(1, item.length - 1)); | |
| 132 | - }); | |
| 133 | - } | |
| 134 | - | |
| 135 | - return { | |
| 136 | - id: id, | |
| 137 | - dataIndex: dataIndex | |
| 138 | - }; | |
| 139 | -} // id: 'a.b[].c[]' dataIndex: [1,0] => 'a.b[1].c[0]' | |
| 140 | - | |
| 141 | -export function getDataPath(id, dataIndex) { | |
| 142 | - if (id === '#') { | |
| 143 | - return id; | |
| 144 | - } | |
| 145 | - | |
| 146 | - if (typeof id !== 'string') { | |
| 147 | - throw Error("id ".concat(id, " is not a string!!! Something wrong here")); | |
| 148 | - } | |
| 149 | - | |
| 150 | - var _id = id; | |
| 151 | - | |
| 152 | - if (Array.isArray(dataIndex)) { | |
| 153 | - // const matches = id.match(/\[\]/g) || []; | |
| 154 | - // const count = matches.length; | |
| 155 | - dataIndex.forEach(function (item) { | |
| 156 | - _id = _id.replace(/\[\]/, "[".concat(item, "]")); | |
| 157 | - }); | |
| 158 | - } | |
| 159 | - | |
| 160 | - return removeBrackets(_id); | |
| 161 | -} | |
| 162 | -export function isObjType(schema) { | |
| 163 | - return schema && schema.type === 'object' && schema.properties && !schema.widget; | |
| 164 | -} // TODO: to support case that item is not an object | |
| 165 | - | |
| 166 | -export function isListType(schema) { | |
| 167 | - return schema && schema.type === 'array' && isObjType(schema.items) && schema.enum === undefined; | |
| 168 | -} // TODO: more tests to make sure weird & wrong schema won't crush | |
| 169 | - | |
| 170 | -export function flattenSchema() { | |
| 171 | - var _schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 172 | - | |
| 173 | - var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 174 | - var parent = arguments.length > 2 ? arguments[2] : undefined; | |
| 175 | - var result = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | |
| 176 | - var schema = clone(_schema); | |
| 177 | - var _name = name; | |
| 178 | - | |
| 179 | - if (!schema.$id) { | |
| 180 | - schema.$id = _name; // path as $id, for easy access to path in schema | |
| 181 | - } | |
| 182 | - | |
| 183 | - var children = []; | |
| 184 | - | |
| 185 | - if (isObjType(schema)) { | |
| 186 | - Object.entries(schema.properties).forEach(function (_ref) { | |
| 187 | - var _ref2 = _slicedToArray(_ref, 2), | |
| 188 | - key = _ref2[0], | |
| 189 | - value = _ref2[1]; | |
| 190 | - | |
| 191 | - var _key = isListType(value) ? key + '[]' : key; | |
| 192 | - | |
| 193 | - var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 194 | - children.push(uniqueName); | |
| 195 | - flattenSchema(value, uniqueName, _name, result); | |
| 196 | - }); | |
| 197 | - schema.properties = {}; | |
| 198 | - } | |
| 199 | - | |
| 200 | - if (isListType(schema)) { | |
| 201 | - Object.entries(schema.items.properties).forEach(function (_ref3) { | |
| 202 | - var _ref4 = _slicedToArray(_ref3, 2), | |
| 203 | - key = _ref4[0], | |
| 204 | - value = _ref4[1]; | |
| 205 | - | |
| 206 | - var _key = isListType(value) ? key + '[]' : key; | |
| 207 | - | |
| 208 | - var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 209 | - children.push(uniqueName); | |
| 210 | - flattenSchema(value, uniqueName, _name, result); | |
| 211 | - }); | |
| 212 | - schema.items.properties = {}; | |
| 213 | - } | |
| 214 | - | |
| 215 | - if (schema.type) { | |
| 216 | - result[_name] = { | |
| 217 | - parent: parent, | |
| 218 | - schema: schema, | |
| 219 | - children: children | |
| 220 | - }; | |
| 221 | - } | |
| 222 | - | |
| 223 | - return result; | |
| 224 | -} | |
| 225 | -export function getSchemaFromFlatten(flatten) { | |
| 226 | - var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 227 | - var schema = {}; | |
| 228 | - var item = clone(flatten[path]); | |
| 229 | - | |
| 230 | - if (item) { | |
| 231 | - schema = item.schema; // schema.$id && delete schema.$id; | |
| 232 | - | |
| 233 | - if (item.children.length > 0) { | |
| 234 | - item.children.forEach(function (child) { | |
| 235 | - if (!flatten[child]) return; | |
| 236 | - var key = getKeyFromPath(child); | |
| 237 | - | |
| 238 | - if (isObjType(schema)) { | |
| 239 | - schema.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 240 | - } | |
| 241 | - | |
| 242 | - if (isListType(schema)) { | |
| 243 | - schema.items.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 244 | - } | |
| 245 | - }); | |
| 246 | - } | |
| 247 | - } | |
| 248 | - | |
| 249 | - return schema; | |
| 250 | -} | |
| 251 | - | |
| 252 | -function stringContains(str, text) { | |
| 253 | - return str.indexOf(text) > -1; | |
| 254 | -} | |
| 255 | - | |
| 256 | -export var isObject = function isObject(a) { | |
| 257 | - return stringContains(Object.prototype.toString.call(a), 'Object'); | |
| 258 | -}; | |
| 259 | -export var clone = cloneDeep; // '3' => true, 3 => true, undefined => false | |
| 260 | - | |
| 261 | -export function isLooselyNumber(num) { | |
| 262 | - if (typeof num === 'number') return true; | |
| 263 | - | |
| 264 | - if (typeof num === 'string') { | |
| 265 | - return !Number.isNaN(Number(num)); | |
| 266 | - } | |
| 267 | - | |
| 268 | - return false; | |
| 269 | -} | |
| 270 | -export function isCssLength(str) { | |
| 271 | - if (typeof str !== 'string') return false; | |
| 272 | - return str.match(/^([0-9])*(%|px|rem|em)$/i); | |
| 273 | -} | |
| 274 | -export function isDeepEqual(param1, param2) { | |
| 275 | - if (param1 === undefined && param2 === undefined) return true;else if (param1 === undefined || param2 === undefined) return false; | |
| 276 | - if (param1 === null && param2 === null) return true;else if (param1 === null || param2 === null) return false;else if (param1.constructor !== param2.constructor) return false; | |
| 277 | - | |
| 278 | - if (param1.constructor === Array) { | |
| 279 | - if (param1.length !== param2.length) return false; | |
| 280 | - | |
| 281 | - for (var i = 0; i < param1.length; i++) { | |
| 282 | - if (param1[i].constructor === Array || param1[i].constructor === Object) { | |
| 283 | - if (!isDeepEqual(param1[i], param2[i])) return false; | |
| 284 | - } else if (param1[i] !== param2[i]) return false; | |
| 285 | - } | |
| 286 | - } else if (param1.constructor === Object) { | |
| 287 | - if (Object.keys(param1).length !== Object.keys(param2).length) return false; | |
| 288 | - | |
| 289 | - for (var _i2 = 0; _i2 < Object.keys(param1).length; _i2++) { | |
| 290 | - var key = Object.keys(param1)[_i2]; | |
| 291 | - | |
| 292 | - if (param1[key] && typeof param1[key] !== 'number' && (param1[key].constructor === Array || param1[key].constructor === Object)) { | |
| 293 | - if (!isDeepEqual(param1[key], param2[key])) return false; | |
| 294 | - } else if (param1[key] !== param2[key]) return false; | |
| 295 | - } | |
| 296 | - } else if (param1.constructor === String || param1.constructor === Number) { | |
| 297 | - return param1 === param2; | |
| 298 | - } | |
| 299 | - | |
| 300 | - return true; | |
| 301 | -} | |
| 302 | -export function getFormat(format) { | |
| 303 | - var dateFormat; | |
| 304 | - | |
| 305 | - switch (format) { | |
| 306 | - case 'date': | |
| 307 | - dateFormat = 'YYYY-MM-DD'; | |
| 308 | - break; | |
| 309 | - | |
| 310 | - case 'time': | |
| 311 | - dateFormat = 'HH:mm:ss'; | |
| 312 | - break; | |
| 313 | - | |
| 314 | - case 'dateTime': | |
| 315 | - dateFormat = 'YYYY-MM-DD HH:mm:ss'; | |
| 316 | - break; | |
| 317 | - | |
| 318 | - case 'week': | |
| 319 | - dateFormat = 'YYYY-w'; | |
| 320 | - break; | |
| 321 | - | |
| 322 | - case 'year': | |
| 323 | - dateFormat = 'YYYY'; | |
| 324 | - break; | |
| 325 | - | |
| 326 | - case 'quarter': | |
| 327 | - dateFormat = 'YYYY-Q'; | |
| 328 | - break; | |
| 329 | - | |
| 330 | - case 'month': | |
| 331 | - dateFormat = 'YYYY-MM'; | |
| 332 | - break; | |
| 333 | - | |
| 334 | - default: | |
| 335 | - // dateTime | |
| 336 | - if (typeof format === 'string') { | |
| 337 | - dateFormat = format; | |
| 338 | - } else { | |
| 339 | - dateFormat = 'YYYY-MM-DD'; | |
| 340 | - } | |
| 341 | - | |
| 342 | - } | |
| 343 | - | |
| 344 | - return dateFormat; | |
| 345 | -} | |
| 346 | -export function hasRepeat(list) { | |
| 347 | - return list.find(function (x, i, self) { | |
| 348 | - return i !== self.findIndex(function (y) { | |
| 349 | - return JSON.stringify(x) === JSON.stringify(y); | |
| 350 | - }); | |
| 351 | - }); | |
| 352 | -} | |
| 353 | -export function combineSchema() { | |
| 354 | - var propsSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 355 | - var uiSchema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 356 | - var propList = getChildren(propsSchema); | |
| 357 | - var newList = propList.map(function (p) { | |
| 358 | - var name = p.name; | |
| 359 | - var _p$schema = p.schema, | |
| 360 | - type = _p$schema.type, | |
| 361 | - options = _p$schema.enum, | |
| 362 | - properties = _p$schema.properties, | |
| 363 | - items = _p$schema.items; | |
| 364 | - var isObj = type === 'object' && properties; | |
| 365 | - var isArr = type === 'array' && items && !options; // enum + array 代表的多选框,没有sub | |
| 366 | - | |
| 367 | - var ui = name && uiSchema[p.name]; | |
| 368 | - | |
| 369 | - if (!ui) { | |
| 370 | - return p; | |
| 371 | - } // 如果是list,递归合并items | |
| 372 | - | |
| 373 | - | |
| 374 | - if (isArr) { | |
| 375 | - var newItems = combineSchema(items, ui.items || {}); | |
| 376 | - return _objectSpread(_objectSpread({}, p), {}, { | |
| 377 | - schema: _objectSpread(_objectSpread(_objectSpread({}, p.schema), ui), {}, { | |
| 378 | - items: newItems | |
| 379 | - }) | |
| 380 | - }); | |
| 381 | - } // object递归合并整个schema | |
| 382 | - | |
| 383 | - | |
| 384 | - if (isObj) { | |
| 385 | - var newSchema = combineSchema(p.schema, ui); | |
| 386 | - return _objectSpread(_objectSpread({}, p), {}, { | |
| 387 | - schema: newSchema | |
| 388 | - }); | |
| 389 | - } | |
| 390 | - | |
| 391 | - return _objectSpread(_objectSpread({}, p), {}, { | |
| 392 | - schema: _objectSpread(_objectSpread({}, p.schema), ui) | |
| 393 | - }); | |
| 394 | - }); | |
| 395 | - var newObj = {}; | |
| 396 | - newList.forEach(function (s) { | |
| 397 | - newObj[s.name] = s.schema; | |
| 398 | - }); | |
| 399 | - var topLevelUi = {}; | |
| 400 | - Object.keys(uiSchema).forEach(function (key) { | |
| 401 | - if (typeof key === 'string' && key.substring(0, 3) === 'ui:') { | |
| 402 | - topLevelUi[key] = uiSchema[key]; | |
| 403 | - } | |
| 404 | - }); | |
| 405 | - | |
| 406 | - if (isEmpty(newObj)) { | |
| 407 | - return _objectSpread(_objectSpread({}, propsSchema), topLevelUi); | |
| 408 | - } | |
| 409 | - | |
| 410 | - return _objectSpread(_objectSpread(_objectSpread({}, propsSchema), topLevelUi), {}, { | |
| 411 | - properties: newObj | |
| 412 | - }); | |
| 413 | -} | |
| 414 | - | |
| 415 | -function isEmpty(obj) { | |
| 416 | - return Object.keys(obj).length === 0; | |
| 417 | -} | |
| 418 | - | |
| 419 | -function getChildren(schema) { | |
| 420 | - if (!schema) return []; | |
| 421 | - var properties = schema.properties, | |
| 422 | - items = schema.items, | |
| 423 | - type = schema.type; | |
| 424 | - | |
| 425 | - if (!properties && !items) { | |
| 426 | - return []; | |
| 427 | - } | |
| 428 | - | |
| 429 | - var schemaSubs = {}; | |
| 430 | - | |
| 431 | - if (type === 'object') { | |
| 432 | - schemaSubs = properties; | |
| 433 | - } | |
| 434 | - | |
| 435 | - if (type === 'array') { | |
| 436 | - schemaSubs = items; | |
| 437 | - } | |
| 438 | - | |
| 439 | - return Object.keys(schemaSubs).map(function (name) { | |
| 440 | - return { | |
| 441 | - schema: schemaSubs[name], | |
| 442 | - name: name | |
| 443 | - }; | |
| 444 | - }); | |
| 445 | -} | |
| 446 | - | |
| 447 | -export var parseString = function parseString(string) { | |
| 448 | - return Function('"use strict";return (' + string + ')')(); | |
| 449 | -}; | |
| 450 | -export var evaluateString = function evaluateString(string, formData, rootValue) { | |
| 451 | - return Function("\"use strict\";\n const rootValue = ".concat(JSON.stringify(rootValue), ";\n const formData = ").concat(JSON.stringify(formData), ";\n return (").concat(string, ")"))(); | |
| 452 | -}; | |
| 453 | -export function isExpression(func) { | |
| 454 | - // if (typeof func === 'function') { | |
| 455 | - // const funcString = func.toString(); | |
| 456 | - // return ( | |
| 457 | - // funcString.indexOf('formData') > -1 || | |
| 458 | - // funcString.indexOf('rootValue') > -1 | |
| 459 | - // ); | |
| 460 | - // } | |
| 461 | - if (typeof func !== 'string') return false; | |
| 462 | - var pattern = /^{{(.+)}}$/; | |
| 463 | - var reg1 = /^{{function\(.+}}$/; // const reg2 = /^{{(.+=>.+)}}$/; | |
| 464 | - | |
| 465 | - if (typeof func === 'string' && func.match(pattern) && !func.match(reg1)) { | |
| 466 | - return true; | |
| 467 | - } | |
| 468 | - | |
| 469 | - return false; | |
| 470 | -} | |
| 471 | -export var parseRootValueInSchema = function parseRootValueInSchema(schema, rootValue) { | |
| 472 | - var result = clone(schema); | |
| 473 | - | |
| 474 | - if (isObject(schema)) { | |
| 475 | - Object.keys(schema).forEach(function (key) { | |
| 476 | - var item = schema[key]; | |
| 477 | - | |
| 478 | - if (isObject(item)) { | |
| 479 | - result[key] = parseRootValueInSchema(item, rootValue); | |
| 480 | - } else if (typeof item === 'string') { | |
| 481 | - result[key] = parseSingleRootValue(item, rootValue); | |
| 482 | - } | |
| 483 | - }); | |
| 484 | - } else { | |
| 485 | - console.error('schema is not an object:', schema); | |
| 486 | - } | |
| 487 | - | |
| 488 | - return result; | |
| 489 | -}; // handle rootValue inside List | |
| 490 | - | |
| 491 | -export var parseSingleRootValue = function parseSingleRootValue(expression) { | |
| 492 | - var rootValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 493 | - | |
| 494 | - if (typeof expression === 'string' && expression.indexOf('rootValue') > 0) { | |
| 495 | - var funcBody = expression.substring(2, expression.length - 2); | |
| 496 | - var str = "\n return ".concat(funcBody.replace(/rootValue/g, JSON.stringify(rootValue))); | |
| 497 | - | |
| 498 | - try { | |
| 499 | - return Function(str)(); | |
| 500 | - } catch (error) { | |
| 501 | - console.error(error, 'expression:', expression, 'rootValue:', rootValue); | |
| 502 | - return null; // 如果计算有错误,return null 最合适 | |
| 503 | - } | |
| 504 | - } else { | |
| 505 | - return expression; | |
| 506 | - } | |
| 507 | -}; | |
| 508 | -export function parseSingleExpression(func) { | |
| 509 | - var formData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 510 | - var dataPath = arguments.length > 2 ? arguments[2] : undefined; | |
| 511 | - var parentPath = getParentPath(dataPath); | |
| 512 | - var parent = getValueByPath(formData, parentPath) || {}; | |
| 513 | - | |
| 514 | - if (typeof func === 'string') { | |
| 515 | - var funcBody = func.substring(2, func.length - 2); | |
| 516 | - var str = "\n return ".concat(funcBody.replace(/formData/g, JSON.stringify(formData)).replace(/rootValue/g, JSON.stringify(parent))); | |
| 517 | - | |
| 518 | - try { | |
| 519 | - return Function(str)(); | |
| 520 | - } catch (error) { | |
| 521 | - console.log(error, func, dataPath); | |
| 522 | - return null; // 如果计算有错误,return null 最合适 | |
| 523 | - } // const funcBody = func.substring(2, func.length - 2); | |
| 524 | - // //TODO: 这样有问题,例如 a.b.indexOf(), 会把 a.b.indexOf 当做值 | |
| 525 | - // const match1 = /formData.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 526 | - // const match2 = /rootValue.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 527 | - // const str = ` | |
| 528 | - // return (${funcBody | |
| 529 | - // .replaceAll(match1, (v, m1) => | |
| 530 | - // JSON.stringify(getValueByPath(formData, m1)) | |
| 531 | - // ) | |
| 532 | - // .replaceAll(match2, (v, m1) => | |
| 533 | - // JSON.stringify(getValueByPath(parent, m1)) | |
| 534 | - // )})`; | |
| 535 | - // try { | |
| 536 | - // return Function(str)(); | |
| 537 | - // } catch (error) { | |
| 538 | - // console.log(error); | |
| 539 | - // return func; | |
| 540 | - // } | |
| 541 | - | |
| 542 | - } else return func; | |
| 543 | -} | |
| 544 | -export var schemaContainsExpression = function schemaContainsExpression(schema) { | |
| 545 | - if (isObject(schema)) { | |
| 546 | - return Object.keys(schema).some(function (key) { | |
| 547 | - var value = schema[key]; | |
| 548 | - | |
| 549 | - if (typeof value === 'string') { | |
| 550 | - return isExpression(value); | |
| 551 | - } else if (isObject(value)) { | |
| 552 | - return schemaContainsExpression(value); | |
| 553 | - } else { | |
| 554 | - return false; | |
| 555 | - } | |
| 556 | - }); | |
| 557 | - } | |
| 558 | - | |
| 559 | - return false; | |
| 560 | -}; | |
| 561 | -export var parseAllExpression = function parseAllExpression(_schema, formData, dataPath) { | |
| 562 | - var schema = clone(_schema); | |
| 563 | - Object.keys(schema).forEach(function (key) { | |
| 564 | - var value = schema[key]; | |
| 565 | - | |
| 566 | - if (isObject(value)) { | |
| 567 | - schema[key] = parseAllExpression(value, formData, dataPath); | |
| 568 | - } else if (isExpression(value)) { | |
| 569 | - schema[key] = parseSingleExpression(value, formData, dataPath); | |
| 570 | - } else if (typeof key === 'string' && key.toLowerCase().indexOf('props') > -1) { | |
| 571 | - // 有可能叫 xxxProps | |
| 572 | - var propsObj = schema[key]; | |
| 573 | - | |
| 574 | - if (isObject(propsObj)) { | |
| 575 | - Object.keys(propsObj).forEach(function (k) { | |
| 576 | - schema[key][k] = parseSingleExpression(propsObj[k], formData, dataPath); | |
| 577 | - }); | |
| 578 | - } | |
| 579 | - } | |
| 580 | - }); | |
| 581 | - return schema; | |
| 582 | -}; | |
| 583 | -export function isFunctionSchema(schema) { | |
| 584 | - return Object.keys(schema).some(function (key) { | |
| 585 | - if (typeof schema[key] === 'function') { | |
| 586 | - return true; | |
| 587 | - } else if (typeof schema[key] === 'string') { | |
| 588 | - return isExpression(schema[key]); | |
| 589 | - } else if (_typeof(schema[key]) === 'object') { | |
| 590 | - return isFunctionSchema(schema[key]); | |
| 591 | - } else { | |
| 592 | - return false; | |
| 593 | - } | |
| 594 | - }); | |
| 595 | -} | |
| 596 | -export var getParentProps = function getParentProps(propName, id, flatten) { | |
| 597 | - try { | |
| 598 | - var item = flatten[id]; | |
| 599 | - if (item.schema[propName] !== undefined) return item.schema[propName]; | |
| 600 | - | |
| 601 | - if (item && item.parent) { | |
| 602 | - var parentSchema = flatten[item.parent].schema; | |
| 603 | - | |
| 604 | - if (parentSchema[propName] !== undefined) { | |
| 605 | - return parentSchema[propName]; | |
| 606 | - } else { | |
| 607 | - return getParentProps(propName, item.parent, flatten); | |
| 608 | - } | |
| 609 | - } | |
| 610 | - } catch (error) { | |
| 611 | - return undefined; | |
| 612 | - } | |
| 613 | -}; | |
| 614 | -export var getSaveNumber = function getSaveNumber() { | |
| 615 | - var searchStr = localStorage.getItem('SAVES'); | |
| 616 | - | |
| 617 | - if (searchStr) { | |
| 618 | - try { | |
| 619 | - var saves = JSON.parse(searchStr); | |
| 620 | - var length = saves.length; | |
| 621 | - if (length) return length + 1; | |
| 622 | - } catch (error) { | |
| 623 | - return 1; | |
| 624 | - } | |
| 625 | - } else { | |
| 626 | - return 1; | |
| 627 | - } | |
| 628 | -}; | |
| 629 | -export function looseJsonParse(obj) { | |
| 630 | - return Function('"use strict";return (' + obj + ')')(); | |
| 631 | -} | |
| 632 | -export var isFunctionString = function isFunctionString(fString) { | |
| 633 | - return typeof fString === 'string' && fString.indexOf('function(') === 0; | |
| 634 | -}; | |
| 635 | -export function parseFunction(fString) { | |
| 636 | - if (isFunctionString(fString)) { | |
| 637 | - return Function('return ' + fString)(); | |
| 638 | - } | |
| 639 | - | |
| 640 | - return fString; | |
| 641 | -} // 获得propsSchema的children | |
| 642 | -// function getChildren2(schema) { | |
| 643 | -// if (!schema) return []; | |
| 644 | -// const { | |
| 645 | -// // object | |
| 646 | -// properties, | |
| 647 | -// // array | |
| 648 | -// items, | |
| 649 | -// type, | |
| 650 | -// } = schema; | |
| 651 | -// if (!properties && !items) { | |
| 652 | -// return []; | |
| 653 | -// } | |
| 654 | -// let schemaSubs = {}; | |
| 655 | -// if (type === 'object') { | |
| 656 | -// schemaSubs = properties; | |
| 657 | -// } | |
| 658 | -// if (type === 'array') { | |
| 659 | -// schemaSubs = items.properties; | |
| 660 | -// } | |
| 661 | -// return Object.keys(schemaSubs).map(name => ({ | |
| 662 | -// schema: schemaSubs[name], | |
| 663 | -// name, | |
| 664 | -// })); | |
| 665 | -// } | |
| 666 | - | |
| 667 | -export var oldSchemaToNew = function oldSchemaToNew(schema) { | |
| 668 | - if (schema && schema.propsSchema) { | |
| 669 | - var propsSchema = schema.propsSchema, | |
| 670 | - rest = _objectWithoutProperties(schema, _excluded); | |
| 671 | - | |
| 672 | - return _objectSpread({ | |
| 673 | - schema: propsSchema | |
| 674 | - }, rest); | |
| 675 | - } | |
| 676 | - | |
| 677 | - return schema; | |
| 678 | -}; | |
| 679 | -export var newSchemaToOld = function newSchemaToOld(setting) { | |
| 680 | - if (setting && setting.schema) { | |
| 681 | - var schema = setting.schema, | |
| 682 | - rest = _objectWithoutProperties(setting, _excluded2); | |
| 683 | - | |
| 684 | - return _objectSpread({ | |
| 685 | - propsSchema: schema | |
| 686 | - }, rest); | |
| 687 | - } | |
| 688 | - | |
| 689 | - return setting; | |
| 690 | -}; // from FR | |
| 691 | - | |
| 692 | -export var getEnum = function getEnum(schema) { | |
| 693 | - if (!schema) return undefined; | |
| 694 | - var itemEnum = schema && schema.items && schema.items.enum; | |
| 695 | - var schemaEnum = schema && schema.enum; | |
| 696 | - return itemEnum ? itemEnum : schemaEnum; | |
| 697 | -}; | |
| 698 | -export var getArray = function getArray(arr) { | |
| 699 | - var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| 700 | - if (Array.isArray(arr)) return arr; | |
| 701 | - return defaultValue; | |
| 702 | -}; | |
| 703 | -export var isEmail = function isEmail(value) { | |
| 704 | - var regex = '^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$'; | |
| 705 | - | |
| 706 | - if (value && new RegExp(regex).test(value)) { | |
| 707 | - return true; | |
| 708 | - } | |
| 709 | - | |
| 710 | - return false; | |
| 711 | -}; | |
| 712 | -export function defaultGetValueFromEvent(valuePropName) { | |
| 713 | - var event = arguments.length <= 1 ? undefined : arguments[1]; | |
| 714 | - | |
| 715 | - if (event && event.target && valuePropName in event.target) { | |
| 716 | - return event.target[valuePropName]; | |
| 717 | - } | |
| 718 | - | |
| 719 | - return event; | |
| 720 | -} | |
| 721 | -export var getKeyFromPath = function getKeyFromPath() { | |
| 722 | - var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '#'; | |
| 723 | - | |
| 724 | - try { | |
| 725 | - var arr = path.split('.'); | |
| 726 | - var last = arr.slice(-1)[0]; | |
| 727 | - var result = last.replace('[]', ''); | |
| 728 | - return result; | |
| 729 | - } catch (error) { | |
| 730 | - console.error(error, 'getKeyFromPath'); | |
| 731 | - return ''; | |
| 732 | - } | |
| 733 | -}; // 更多的值获取 | |
| 734 | - | |
| 735 | -export var getDisplayValue = function getDisplayValue(value, schema) { | |
| 736 | - if (typeof value === 'boolean') { | |
| 737 | - return value ? 'yes' : 'no'; | |
| 738 | - } | |
| 739 | - | |
| 740 | - if (isObjType(schema) || isListType(schema)) { | |
| 741 | - return '-'; | |
| 742 | - } | |
| 743 | - | |
| 744 | - if (Array.isArray(schema.enum) && Array.isArray(schema.enumNames)) { | |
| 745 | - try { | |
| 746 | - return schema.enumNames[schema.enum.indexOf(value)]; | |
| 747 | - } catch (error) { | |
| 748 | - return value; | |
| 749 | - } | |
| 750 | - } | |
| 751 | - | |
| 752 | - return value; | |
| 753 | -}; // 去掉数组里的空元素 {a: [null, {x:1}]} => {a: [{x:1}]} | |
| 754 | - | |
| 755 | -export var removeEmptyItemFromList = function removeEmptyItemFromList(formData) { | |
| 756 | - var result = {}; | |
| 757 | - | |
| 758 | - if (isObject(formData)) { | |
| 759 | - Object.keys(formData).forEach(function (key) { | |
| 760 | - result[key] = removeEmptyItemFromList(formData[key]); | |
| 761 | - }); | |
| 762 | - } else if (Array.isArray(formData)) { | |
| 763 | - result = formData.filter(function (item) { | |
| 764 | - if ([false, 0, ''].indexOf(item) > -1) return true; | |
| 765 | - | |
| 766 | - if (item && JSON.stringify(item) !== '{}') { | |
| 767 | - return true; | |
| 768 | - } | |
| 769 | - | |
| 770 | - return false; | |
| 771 | - }); | |
| 772 | - } else { | |
| 773 | - result = formData; | |
| 774 | - } | |
| 775 | - | |
| 776 | - return result; | |
| 777 | -}; | |
| 778 | -export var getDescriptorSimple = function getDescriptorSimple() { | |
| 779 | - var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 780 | - var path = arguments.length > 1 ? arguments[1] : undefined; | |
| 781 | - var result = {}; | |
| 782 | - | |
| 783 | - if (isObject(schema)) { | |
| 784 | - if (schema.type) { | |
| 785 | - switch (schema.type) { | |
| 786 | - case 'range': | |
| 787 | - result.type = 'array'; | |
| 788 | - break; | |
| 789 | - | |
| 790 | - case 'html': | |
| 791 | - result.type = 'string'; | |
| 792 | - break; | |
| 793 | - | |
| 794 | - default: | |
| 795 | - result.type = schema.type; | |
| 796 | - break; | |
| 797 | - } | |
| 798 | - } | |
| 799 | - | |
| 800 | - ['pattern', 'min', 'max', 'len', 'required'].forEach(function (key) { | |
| 801 | - if (Object.keys(schema).indexOf(key) > -1) { | |
| 802 | - result[key] = schema[key]; | |
| 803 | - } | |
| 804 | - }); | |
| 805 | - | |
| 806 | - switch (schema.format) { | |
| 807 | - case 'email': | |
| 808 | - case 'url': | |
| 809 | - result.type = schema.format; | |
| 810 | - break; | |
| 811 | - | |
| 812 | - default: | |
| 813 | - break; | |
| 814 | - } | |
| 815 | - | |
| 816 | - var handleRegx = function handleRegx(desc) { | |
| 817 | - if (desc.pattern && typeof desc.pattern === 'string') { | |
| 818 | - desc.pattern = new RegExp(desc.pattern); | |
| 819 | - } | |
| 820 | - | |
| 821 | - return desc; | |
| 822 | - }; // result be array | |
| 823 | - | |
| 824 | - | |
| 825 | - if (schema.rules) { | |
| 826 | - if (Array.isArray(schema.rules)) { | |
| 827 | - var requiredRule = schema.rules.find(function (rule) { | |
| 828 | - return rule.required === true; | |
| 829 | - }); | |
| 830 | - | |
| 831 | - if (requiredRule) { | |
| 832 | - result = _objectSpread(_objectSpread({}, result), requiredRule); | |
| 833 | - } | |
| 834 | - | |
| 835 | - result = [result].concat(_toConsumableArray(schema.rules)); | |
| 836 | - result = result.map(function (r) { | |
| 837 | - return handleRegx(r); | |
| 838 | - }); | |
| 839 | - } else if (isObject(schema.rules)) { | |
| 840 | - result = [result, schema.rules]; | |
| 841 | - result = result.map(function (r) { | |
| 842 | - return handleRegx(r); | |
| 843 | - }); | |
| 844 | - } | |
| 845 | - } else { | |
| 846 | - result = [result]; | |
| 847 | - } | |
| 848 | - } | |
| 849 | - | |
| 850 | - return _defineProperty({}, path, result); | |
| 851 | -}; // export const getDescriptorFromSchema = ({ schema, isRequired = true }) => { | |
| 852 | -// let result = {}; | |
| 853 | -// let singleResult = {}; | |
| 854 | -// if (schema.hidden === true) return { validator: () => true }; | |
| 855 | -// if (isObjType(schema)) { | |
| 856 | -// result.type = 'object'; | |
| 857 | -// if (isRequired && schema.required === true) { | |
| 858 | -// result.required = true; | |
| 859 | -// } | |
| 860 | -// result.fields = {}; | |
| 861 | -// Object.keys(schema.properties).forEach(key => { | |
| 862 | -// const item = schema.properties[key]; | |
| 863 | -// // 兼容旧的! | |
| 864 | -// if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 865 | -// item.required = true; | |
| 866 | -// } | |
| 867 | -// result.fields[key] = getDescriptorFromSchema({ | |
| 868 | -// schema: item, | |
| 869 | -// isRequired, | |
| 870 | -// }); | |
| 871 | -// }); | |
| 872 | -// } else if (isListType(schema)) { | |
| 873 | -// result.type = 'array'; | |
| 874 | -// if (isRequired && schema.required === true) { | |
| 875 | -// result.required = true; | |
| 876 | -// } | |
| 877 | -// if (typeof schema.min === 'number') { | |
| 878 | -// result.min = schema.min; | |
| 879 | -// } | |
| 880 | -// if (typeof schema.max === 'number') { | |
| 881 | -// result.max = schema.max; | |
| 882 | -// } | |
| 883 | -// result.defaultField = { type: 'object', fields: {} }; // 目前就默认只有object类型的 TODO: | |
| 884 | -// Object.keys(schema.items.properties).forEach(key => { | |
| 885 | -// const item = schema.items.properties[key]; | |
| 886 | -// // 兼容旧的! | |
| 887 | -// if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 888 | -// item.required = true; | |
| 889 | -// } | |
| 890 | -// result.defaultField.fields[key] = getDescriptorFromSchema({ | |
| 891 | -// schema: item, | |
| 892 | -// isRequired, | |
| 893 | -// }); | |
| 894 | -// }); | |
| 895 | -// } else { | |
| 896 | -// // 单个的逻辑 | |
| 897 | -// const processRule = item => { | |
| 898 | -// if (schema.type) return { ...item, type: schema.type }; | |
| 899 | -// if (item.pattern && typeof item.pattern === 'string') { | |
| 900 | -// return { ...item, pattern: new RegExp(item.pattern) }; | |
| 901 | -// } | |
| 902 | -// return item; | |
| 903 | -// }; | |
| 904 | -// const { required, ...rest } = schema; | |
| 905 | -// ['type', 'pattern', 'min', 'max', 'len'].forEach(key => { | |
| 906 | -// if (Object.keys(rest).indexOf(key) > -1) { | |
| 907 | -// singleResult[key] = rest[key]; | |
| 908 | -// } | |
| 909 | -// }); | |
| 910 | -// switch (schema.type) { | |
| 911 | -// case 'range': | |
| 912 | -// singleResult.type = 'array'; | |
| 913 | -// break; | |
| 914 | -// case 'html': | |
| 915 | -// singleResult.type = 'string'; | |
| 916 | -// break; | |
| 917 | -// default: | |
| 918 | -// break; | |
| 919 | -// } | |
| 920 | -// switch (schema.format) { | |
| 921 | -// case 'email': | |
| 922 | -// case 'url': | |
| 923 | -// singleResult.type = schema.format; | |
| 924 | -// break; | |
| 925 | -// default: | |
| 926 | -// break; | |
| 927 | -// } | |
| 928 | -// let requiredRule; | |
| 929 | -// if (isRequired && schema.required === true) { | |
| 930 | -// requiredRule = { required: true, type: singleResult.type || 'string' }; | |
| 931 | -// } | |
| 932 | -// if (schema.rules) { | |
| 933 | -// if (Array.isArray(schema.rules)) { | |
| 934 | -// const _rules = []; | |
| 935 | -// schema.rules.forEach(item => { | |
| 936 | -// if (item.required === true) { | |
| 937 | -// if (isRequired) { | |
| 938 | -// requiredRule = item; | |
| 939 | -// } | |
| 940 | -// } else { | |
| 941 | -// _rules.push(processRule(item)); | |
| 942 | -// } | |
| 943 | -// }); | |
| 944 | -// result = [singleResult, ..._rules]; | |
| 945 | -// } else if (isObject(schema.rules)) { | |
| 946 | -// // TODO: 规范上不允许rules是object,省一点事儿 | |
| 947 | -// result = [singleResult, processRule(schema.rules)]; | |
| 948 | -// } else { | |
| 949 | -// result = singleResult; | |
| 950 | -// } | |
| 951 | -// } else { | |
| 952 | -// result = singleResult; | |
| 953 | -// } | |
| 954 | -// if (requiredRule) { | |
| 955 | -// if (Array.isArray(result)) { | |
| 956 | -// result.push(requiredRule); | |
| 957 | -// } else if (isObject(result)) { | |
| 958 | -// result = [result, requiredRule]; | |
| 959 | -// } | |
| 960 | -// } | |
| 961 | -// if (schema.format === 'image') { | |
| 962 | -// const imgValidator = { | |
| 963 | -// validator: (rule, value) => { | |
| 964 | -// const pattern = /([/|.|w|s|-])*.(jpg|gif|png|bmp|apng|webp|jpeg|json)/; | |
| 965 | -// if (!value) return true; // 这里判断宽一点,undefined、null、'' 都当做没有填写 | |
| 966 | -// return !!pattern.exec(value) || isUrl(value); | |
| 967 | -// }, | |
| 968 | -// message: '${title}的类型不是image', | |
| 969 | -// }; | |
| 970 | -// if (Array.isArray(result)) { | |
| 971 | -// result.push(imgValidator); | |
| 972 | -// } else if (isObject(result)) { | |
| 973 | -// result = [result, imgValidator]; | |
| 974 | -// } | |
| 975 | -// } | |
| 976 | -// } | |
| 977 | -// return result; | |
| 978 | -// }; | |
| 979 | -// schema = { | |
| 980 | -// type: 'object', | |
| 981 | -// properties: { | |
| 982 | -// x: { | |
| 983 | -// type: 'object', | |
| 984 | -// properties: { | |
| 985 | -// y: { | |
| 986 | -// type: 'string', | |
| 987 | -// required: true, | |
| 988 | -// }, | |
| 989 | -// }, | |
| 990 | -// }, | |
| 991 | -// }, | |
| 992 | -// }; | |
| 993 | -// path = 'x.y' | |
| 994 | -// return {required: true, message?: 'xxxx'} | |
| 995 | -// export const isPathRequired = (path, schema) => { | |
| 996 | -// let pathArr = path.split('.'); | |
| 997 | -// while (pathArr.length > 0) { | |
| 998 | -// let [_path, ...rest] = pathArr; | |
| 999 | -// _path = _path.split('[')[0]; | |
| 1000 | -// let childSchema; | |
| 1001 | -// if (isObjType(schema)) { | |
| 1002 | -// childSchema = schema.properties[_path]; | |
| 1003 | -// } else if (isListType(schema)) { | |
| 1004 | -// childSchema = schema.items.properties[_path]; | |
| 1005 | -// } | |
| 1006 | -// pathArr = rest; | |
| 1007 | -// if (childSchema) { | |
| 1008 | -// return isPathRequired(rest.join('.'), childSchema); | |
| 1009 | -// } | |
| 1010 | -// // 单个的逻辑 | |
| 1011 | -// let result = { required: false }; | |
| 1012 | -// if (schema.required === true) { | |
| 1013 | -// result.required = true; | |
| 1014 | -// } | |
| 1015 | -// if (schema.rules) { | |
| 1016 | -// const requiredItem = schema.rules.find(item => item.required); | |
| 1017 | -// if (requiredItem) { | |
| 1018 | -// result = requiredItem; | |
| 1019 | -// } | |
| 1020 | -// } | |
| 1021 | -// return result; | |
| 1022 | -// } | |
| 1023 | -// }; | |
| 1024 | -// _path 只供内部递归使用 | |
| 1025 | - | |
| 1026 | -export var generateDataSkeleton = function generateDataSkeleton(schema, formData) { | |
| 1027 | - var _formData = clone(formData); | |
| 1028 | - | |
| 1029 | - var result = _formData; | |
| 1030 | - | |
| 1031 | - if (isObjType(schema)) { | |
| 1032 | - if (_formData === undefined || _typeof(_formData) !== 'object') { | |
| 1033 | - _formData = {}; | |
| 1034 | - result = {}; | |
| 1035 | - } | |
| 1036 | - | |
| 1037 | - Object.keys(schema.properties).forEach(function (key) { | |
| 1038 | - var childSchema = schema.properties[key]; | |
| 1039 | - var childData = _formData[key]; | |
| 1040 | - var childResult = generateDataSkeleton(childSchema, childData); | |
| 1041 | - result[key] = childResult; | |
| 1042 | - }); | |
| 1043 | - } else if (_formData !== undefined) {// result = _formData; | |
| 1044 | - } else { | |
| 1045 | - if (schema.default !== undefined) { | |
| 1046 | - result = clone(schema.default); | |
| 1047 | - } else if (schema.type === 'boolean' && !schema.widget) { | |
| 1048 | - // result = false; | |
| 1049 | - result = undefined; | |
| 1050 | - } else { | |
| 1051 | - result = undefined; | |
| 1052 | - } | |
| 1053 | - } | |
| 1054 | - | |
| 1055 | - return result; | |
| 1056 | -}; | |
| 1057 | -export var translateMessage = function translateMessage(msg, schema) { | |
| 1058 | - if (typeof msg !== 'string') { | |
| 1059 | - return ''; | |
| 1060 | - } | |
| 1061 | - | |
| 1062 | - if (!schema) return msg; | |
| 1063 | - msg = msg.replace('${title}', schema.title); | |
| 1064 | - msg = msg.replace('${type}', schema.format || schema.type); // 兼容代码 | |
| 1065 | - | |
| 1066 | - if (typeof schema.min === 'number') { | |
| 1067 | - msg = msg.replace('${min}', schema.min); | |
| 1068 | - } | |
| 1069 | - | |
| 1070 | - if (typeof schema.max === 'number') { | |
| 1071 | - msg = msg.replace('${max}', schema.max); | |
| 1072 | - } | |
| 1073 | - | |
| 1074 | - if (schema.rules) { | |
| 1075 | - var minRule = schema.rules.find(function (r) { | |
| 1076 | - return r.min !== undefined; | |
| 1077 | - }); | |
| 1078 | - | |
| 1079 | - if (minRule) { | |
| 1080 | - msg = msg.replace('${min}', minRule.min); | |
| 1081 | - } | |
| 1082 | - | |
| 1083 | - var maxRule = schema.rules.find(function (r) { | |
| 1084 | - return r.max !== undefined; | |
| 1085 | - }); | |
| 1086 | - | |
| 1087 | - if (maxRule) { | |
| 1088 | - msg = msg.replace('${max}', maxRule.max); | |
| 1089 | - } | |
| 1090 | - | |
| 1091 | - var lenRule = schema.rules.find(function (r) { | |
| 1092 | - return r.len !== undefined; | |
| 1093 | - }); | |
| 1094 | - | |
| 1095 | - if (lenRule) { | |
| 1096 | - msg = msg.replace('${len}', lenRule.len); | |
| 1097 | - } | |
| 1098 | - | |
| 1099 | - var patternRule = schema.rules.find(function (r) { | |
| 1100 | - return r.pattern !== undefined; | |
| 1101 | - }); | |
| 1102 | - | |
| 1103 | - if (patternRule) { | |
| 1104 | - msg = msg.replace('${pattern}', patternRule.pattern); | |
| 1105 | - } | |
| 1106 | - } | |
| 1107 | - | |
| 1108 | - return msg; | |
| 1109 | -}; // "objectName": { | |
| 1110 | -// "title": "对象", | |
| 1111 | -// "description": "这是一个对象类型", | |
| 1112 | -// "type": "object", | |
| 1113 | -// "properties": { | |
| 1114 | -// } | |
| 1115 | -// } | |
| 1116 | -// "listName": { | |
| 1117 | -// "title": "对象数组", | |
| 1118 | -// "description": "对象数组嵌套功能", | |
| 1119 | -// "type": "array", | |
| 1120 | -// "items": { | |
| 1121 | -// "type": "object", | |
| 1122 | -// "properties": { | |
| 1123 | -// } | |
| 1124 | -// } | |
| 1125 | -// } | |
| 1126 | - | |
| 1127 | -var changeSchema = function changeSchema(_schema, singleChange) { | |
| 1128 | - var schema = clone(_schema); | |
| 1129 | - schema = singleChange(schema); | |
| 1130 | - | |
| 1131 | - if (isObjType(schema)) { | |
| 1132 | - var requiredKeys = []; | |
| 1133 | - | |
| 1134 | - if (Array.isArray(schema.required)) { | |
| 1135 | - requiredKeys = schema.required; | |
| 1136 | - delete schema.required; | |
| 1137 | - } | |
| 1138 | - | |
| 1139 | - Object.keys(schema.properties).forEach(function (key) { | |
| 1140 | - var item = schema.properties[key]; | |
| 1141 | - | |
| 1142 | - if (requiredKeys.indexOf(key) > -1) { | |
| 1143 | - item.required = true; | |
| 1144 | - } | |
| 1145 | - | |
| 1146 | - schema.properties[key] = changeSchema(item, singleChange); | |
| 1147 | - }); | |
| 1148 | - } else if (isListType(schema)) { | |
| 1149 | - Object.keys(schema.items.properties).forEach(function (key) { | |
| 1150 | - var item = schema.items.properties[key]; | |
| 1151 | - schema.items.properties[key] = changeSchema(item, singleChange); | |
| 1152 | - }); | |
| 1153 | - } | |
| 1154 | - | |
| 1155 | - return schema; | |
| 1156 | -}; | |
| 1157 | - | |
| 1158 | -export var updateSchemaToNewVersion = function updateSchemaToNewVersion(schema) { | |
| 1159 | - return changeSchema(schema, updateSingleSchema); | |
| 1160 | -}; | |
| 1161 | - | |
| 1162 | -var updateSingleSchema = function updateSingleSchema(schema) { | |
| 1163 | - try { | |
| 1164 | - schema.rules = schema.rules || []; | |
| 1165 | - schema.props = schema.props || {}; | |
| 1166 | - | |
| 1167 | - if (schema['ui:options']) { | |
| 1168 | - schema.props = schema['ui:options']; | |
| 1169 | - delete schema['ui:options']; | |
| 1170 | - } | |
| 1171 | - | |
| 1172 | - if (schema.pattern) { | |
| 1173 | - var validItem = { | |
| 1174 | - pattern: schema.pattern | |
| 1175 | - }; | |
| 1176 | - | |
| 1177 | - if (schema.message && schema.message.pattern) { | |
| 1178 | - validItem.message = schema.message.pattern; | |
| 1179 | - } | |
| 1180 | - | |
| 1181 | - schema.rules.push(validItem); | |
| 1182 | - delete schema.pattern; | |
| 1183 | - delete schema.message; | |
| 1184 | - } // min / max | |
| 1185 | - | |
| 1186 | - | |
| 1187 | - if (schema.minLength) { | |
| 1188 | - schema.min = schema.minLength; | |
| 1189 | - delete schema.minLength; | |
| 1190 | - } | |
| 1191 | - | |
| 1192 | - if (schema.maxLength) { | |
| 1193 | - schema.max = schema.maxLength; | |
| 1194 | - delete schema.maxLength; | |
| 1195 | - } | |
| 1196 | - | |
| 1197 | - if (schema.minItems) { | |
| 1198 | - schema.min = schema.minItems; | |
| 1199 | - delete schema.minItems; | |
| 1200 | - } | |
| 1201 | - | |
| 1202 | - if (schema.maxItems) { | |
| 1203 | - schema.max = schema.maxItems; | |
| 1204 | - delete schema.maxItems; | |
| 1205 | - } | |
| 1206 | - | |
| 1207 | - if (schema.step) { | |
| 1208 | - schema.props.step = schema.step; | |
| 1209 | - delete schema.step; | |
| 1210 | - } // ui:xxx | |
| 1211 | - | |
| 1212 | - | |
| 1213 | - if (schema['ui:className']) { | |
| 1214 | - schema.className = schema['ui:className']; | |
| 1215 | - delete schema['ui:className']; | |
| 1216 | - } | |
| 1217 | - | |
| 1218 | - if (schema['ui:hidden']) { | |
| 1219 | - schema.hidden = schema['ui:hidden']; | |
| 1220 | - delete schema['ui:hidden']; | |
| 1221 | - } | |
| 1222 | - | |
| 1223 | - if (schema['ui:readonly']) { | |
| 1224 | - schema.readOnly = schema['ui:readonly']; // 改成驼峰了 | |
| 1225 | - | |
| 1226 | - delete schema['ui:readonly']; | |
| 1227 | - } | |
| 1228 | - | |
| 1229 | - if (schema['ui:disabled']) { | |
| 1230 | - schema.disabled = schema['ui:disabled']; | |
| 1231 | - delete schema['ui:disabled']; | |
| 1232 | - } | |
| 1233 | - | |
| 1234 | - if (schema['ui:width']) { | |
| 1235 | - schema.width = schema['ui:width']; | |
| 1236 | - delete schema['ui:width']; | |
| 1237 | - } | |
| 1238 | - | |
| 1239 | - if (schema['ui:displayType']) { | |
| 1240 | - schema.displayType = schema['ui:displayType']; | |
| 1241 | - delete schema['ui:displayType']; | |
| 1242 | - } | |
| 1243 | - | |
| 1244 | - if (schema['ui:column']) { | |
| 1245 | - schema.column = schema['ui:column']; | |
| 1246 | - delete schema['ui:column']; | |
| 1247 | - } | |
| 1248 | - | |
| 1249 | - if (schema['ui:widget']) { | |
| 1250 | - schema.widget = schema['ui:widget']; | |
| 1251 | - delete schema['ui:widget']; | |
| 1252 | - } | |
| 1253 | - | |
| 1254 | - if (schema['ui:labelWidth']) { | |
| 1255 | - schema.labelWidth = schema['ui:labelWidth']; | |
| 1256 | - delete schema['ui:labelWidth']; | |
| 1257 | - } | |
| 1258 | - | |
| 1259 | - if (schema.rules && schema.rules.length === 0) { | |
| 1260 | - delete schema.rules; | |
| 1261 | - } | |
| 1262 | - | |
| 1263 | - if (typeof schema.props === 'function' || isObject(schema.props) && Object.keys(schema.props).length > 0) {} else { | |
| 1264 | - delete schema.props; | |
| 1265 | - } | |
| 1266 | - | |
| 1267 | - return schema; | |
| 1268 | - } catch (error) { | |
| 1269 | - console.error('schema转换失败!', error); | |
| 1270 | - return schema; | |
| 1271 | - } | |
| 1272 | -}; // 检验一个string是 function(传统活箭头函数) | |
| 1273 | - | |
| 1274 | - | |
| 1275 | -export var parseFunctionString = function parseFunctionString(string) { | |
| 1276 | - if (typeof string !== 'string') return false; | |
| 1277 | - var reg1 = /^{{(function.+)}}$/; | |
| 1278 | - var reg2 = /^{{(.+=>.+)}}$/; | |
| 1279 | - | |
| 1280 | - if (string.match(reg1)) { | |
| 1281 | - return string.match(reg1)[1]; | |
| 1282 | - } | |
| 1283 | - | |
| 1284 | - if (string.match(reg2)) { | |
| 1285 | - return string.match(reg2)[1]; | |
| 1286 | - } | |
| 1287 | - | |
| 1288 | - return false; | |
| 1289 | -}; | |
| 1290 | -export var completeSchemaWithTheme = function completeSchemaWithTheme() { | |
| 1291 | - var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 1292 | - var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 1293 | - var result = {}; | |
| 1294 | - | |
| 1295 | - if (isObject(schema)) { | |
| 1296 | - if (schema.theme && theme[schema.theme]) { | |
| 1297 | - result = _objectSpread(_objectSpread({}, schema), theme[schema.theme]); | |
| 1298 | - } | |
| 1299 | - | |
| 1300 | - Object.keys(schema).forEach(function (key) { | |
| 1301 | - result[key] = completeSchemaWithTheme(schema[key], theme); | |
| 1302 | - }); | |
| 1303 | - } else { | |
| 1304 | - result = schema; | |
| 1305 | - } | |
| 1306 | - | |
| 1307 | - return result; | |
| 1308 | -}; | |
| 1309 | -export var cleanEmpty = function cleanEmpty(obj) { | |
| 1310 | - if (Array.isArray(obj)) { | |
| 1311 | - return obj.map(function (v) { | |
| 1312 | - return v && isObject(v) ? cleanEmpty(v) : v; | |
| 1313 | - }).filter(function (v) { | |
| 1314 | - return !(v == undefined); | |
| 1315 | - }); | |
| 1316 | - } else if (isObject(obj)) { | |
| 1317 | - return Object.entries(obj).map(function (_ref6) { | |
| 1318 | - var _ref7 = _slicedToArray(_ref6, 2), | |
| 1319 | - k = _ref7[0], | |
| 1320 | - v = _ref7[1]; | |
| 1321 | - | |
| 1322 | - return [k, v && isObject(v) ? cleanEmpty(v) : v]; | |
| 1323 | - }).reduce(function (a, _ref8) { | |
| 1324 | - var _ref9 = _slicedToArray(_ref8, 2), | |
| 1325 | - k = _ref9[0], | |
| 1326 | - v = _ref9[1]; | |
| 1327 | - | |
| 1328 | - return v == undefined ? a : (a[k] = v, a); | |
| 1329 | - }, {}); | |
| 1330 | - } else { | |
| 1331 | - return obj; | |
| 1332 | - } | |
| 1333 | -}; // const x = { a: 1, b: { c: 2 }, d: [{ e: 3, f: [{ g: 5 }] }, { e: 4 }] }; | |
| 1334 | -// ['a', 'b.c', 'd[0].e', 'd[0].f[0].g', 'd[1].e'] | |
| 1335 | - | |
| 1336 | -export var dataToKeys = function dataToKeys(data) { | |
| 1337 | - var rootKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | |
| 1338 | - var result = []; | |
| 1339 | - | |
| 1340 | - if (rootKey && rootKey.slice(-1) !== ']') { | |
| 1341 | - result.push(rootKey); | |
| 1342 | - } | |
| 1343 | - | |
| 1344 | - var isComplex = function isComplex(data) { | |
| 1345 | - return isObject(data) || Array.isArray(data); | |
| 1346 | - }; | |
| 1347 | - | |
| 1348 | - if (isObject(data)) { | |
| 1349 | - Object.keys(data).forEach(function (key) { | |
| 1350 | - var item = data[key]; | |
| 1351 | - var itemRootKey = rootKey ? rootKey + '.' + key : key; | |
| 1352 | - | |
| 1353 | - if (isComplex(item)) { | |
| 1354 | - var itemKeys = dataToKeys(item, itemRootKey); | |
| 1355 | - result = [].concat(_toConsumableArray(result), _toConsumableArray(itemKeys)); | |
| 1356 | - } else { | |
| 1357 | - result.push(itemRootKey); | |
| 1358 | - } | |
| 1359 | - }); | |
| 1360 | - } else if (Array.isArray(data)) { | |
| 1361 | - data.forEach(function (item, idx) { | |
| 1362 | - var itemRootKey = rootKey ? "".concat(rootKey, "[").concat(idx, "]") : "[".concat(idx, "]"); | |
| 1363 | - | |
| 1364 | - if (isComplex(item)) { | |
| 1365 | - var itemKeys = dataToKeys(item, itemRootKey); | |
| 1366 | - result = [].concat(_toConsumableArray(result), _toConsumableArray(itemKeys)); | |
| 1367 | - } else { | |
| 1368 | - result.push(itemRootKey); | |
| 1369 | - } | |
| 1370 | - }); | |
| 1371 | - } else {} | |
| 1372 | - | |
| 1373 | - return result; | |
| 1374 | -}; | |
| 1375 | -export var removeHiddenFromResult = function removeHiddenFromResult(data, flatten) { | |
| 1376 | - var result = clone(data); | |
| 1377 | - var keys = dataToKeys(result); | |
| 1378 | - keys.forEach(function (key) { | |
| 1379 | - var _destructDataPath = destructDataPath(key), | |
| 1380 | - id = _destructDataPath.id, | |
| 1381 | - dataIndex = _destructDataPath.dataIndex; | |
| 1382 | - | |
| 1383 | - if (flatten[id]) { | |
| 1384 | - var _ref10 = flatten[id].schema || {}, | |
| 1385 | - hidden = _ref10.hidden; | |
| 1386 | - | |
| 1387 | - if (isExpression(hidden)) { | |
| 1388 | - hidden = parseSingleExpression(hidden, result, key); | |
| 1389 | - } | |
| 1390 | - | |
| 1391 | - if (get(result, key) !== undefined && hidden) { | |
| 1392 | - set(result, key, undefined); | |
| 1393 | - } | |
| 1394 | - } | |
| 1395 | - }); | |
| 1396 | - return result; | |
| 1397 | -}; | |
| 1398 | -export function msToTime(duration) { | |
| 1399 | - var seconds = Math.floor(duration / 1000 % 60); | |
| 1400 | - var minutes = Math.floor(duration / (1000 * 60) % 60); | |
| 1401 | - var hours = Math.floor(duration / (1000 * 60 * 60) % 24); | |
| 1402 | - hours = hours < 10 ? '0' + hours : hours; | |
| 1403 | - minutes = minutes < 10 ? '0' + minutes : minutes; | |
| 1404 | - seconds = seconds < 10 ? '0' + seconds : seconds; | |
| 1405 | - return hours + ':' + minutes + ':' + seconds; | |
| 1406 | -} | |
| 1407 | -export function yymmdd(timeStamp) { | |
| 1408 | - var date_ob = new Date(Number(timeStamp)); | |
| 1409 | - | |
| 1410 | - var adjustZero = function adjustZero(num) { | |
| 1411 | - return ('0' + num).slice(-2); | |
| 1412 | - }; | |
| 1413 | - | |
| 1414 | - var day = adjustZero(date_ob.getDate()); | |
| 1415 | - var month = adjustZero(date_ob.getMonth()); | |
| 1416 | - var year = date_ob.getFullYear(); | |
| 1417 | - var hours = adjustZero(date_ob.getHours()); | |
| 1418 | - var minutes = adjustZero(date_ob.getMinutes()); | |
| 1419 | - var seconds = adjustZero(date_ob.getSeconds()); | |
| 1420 | - return "".concat(year, "-").concat(month, "-").concat(day, " ").concat(hours, ":").concat(minutes, ":").concat(seconds); | |
| 1421 | -} | |
| 1422 | -export function allPromiseFinish(promiseList) { | |
| 1423 | - var hasError = false; | |
| 1424 | - var count = promiseList.length; | |
| 1425 | - var results = []; | |
| 1426 | - | |
| 1427 | - if (!promiseList.length) { | |
| 1428 | - return Promise.resolve([]); | |
| 1429 | - } | |
| 1430 | - | |
| 1431 | - return new Promise(function (resolve, reject) { | |
| 1432 | - promiseList.forEach(function (promise, index) { | |
| 1433 | - promise.catch(function (e) { | |
| 1434 | - hasError = true; | |
| 1435 | - return e; | |
| 1436 | - }).then(function (result) { | |
| 1437 | - count -= 1; | |
| 1438 | - results[index] = result; | |
| 1439 | - | |
| 1440 | - if (count > 0) { | |
| 1441 | - return; | |
| 1442 | - } | |
| 1443 | - | |
| 1444 | - if (hasError) { | |
| 1445 | - reject(results); | |
| 1446 | - } | |
| 1447 | - | |
| 1448 | - resolve(results); | |
| 1449 | - }); | |
| 1450 | - }); | |
| 1451 | - }); | |
| 1452 | -} | |
| 1453 | -export var removeDups = function removeDups(arr) { | |
| 1454 | - if (!Array.isArray(arr)) { | |
| 1455 | - console.log('in removeDups: param is not an array'); | |
| 1456 | - return; | |
| 1457 | - } | |
| 1458 | - | |
| 1459 | - var array = []; | |
| 1460 | - | |
| 1461 | - for (var i = 0; i < arr.length; i++) { | |
| 1462 | - if (array.indexOf(arr[i]) === -1) { | |
| 1463 | - array.push(arr[i]); | |
| 1464 | - } | |
| 1465 | - } | |
| 1466 | - | |
| 1467 | - return array; | |
| 1468 | -}; | |
| \ No newline at end of file |
es/form-render-core/src/validator.js
deleted
100644 → 0
| 1 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 2 | - | |
| 3 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 4 | - | |
| 5 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 6 | - | |
| 7 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 8 | - | |
| 9 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 10 | - | |
| 11 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 12 | - | |
| 13 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 14 | - | |
| 15 | -/* eslint-disable react-hooks/exhaustive-deps */ | |
| 16 | -import { getDescriptorSimple, dataToKeys, destructDataPath, getDataPath, isExpression, parseSingleExpression, isObject, allPromiseFinish, removeDups } from './utils'; | |
| 17 | -import { defaultValidateMessagesCN } from './validateMessageCN'; | |
| 18 | -import { defaultValidateMessages } from './validateMessage'; | |
| 19 | -import Validator from 'async-validator'; | |
| 20 | -import { get, merge } from 'lodash-es'; | |
| 21 | -export var parseSchemaExpression = function parseSchemaExpression(schema, formData, path) { | |
| 22 | - if (!isObject(schema)) return schema; | |
| 23 | - var result = {}; | |
| 24 | - Object.keys(schema).forEach(function (key) { | |
| 25 | - var item = schema[key]; | |
| 26 | - | |
| 27 | - if (isObject(item)) { | |
| 28 | - result[key] = parseSchemaExpression(item, formData, path); | |
| 29 | - } else if (isExpression(item)) { | |
| 30 | - result[key] = parseSingleExpression(item, formData, path); | |
| 31 | - } else { | |
| 32 | - result[key] = item; | |
| 33 | - } | |
| 34 | - }); | |
| 35 | - return result; | |
| 36 | -}; | |
| 37 | - | |
| 38 | -var getRelatedPaths = function getRelatedPaths(path, flatten) { | |
| 39 | - var parentPaths = []; | |
| 40 | - var pathArr = path.split('.'); | |
| 41 | - | |
| 42 | - while (pathArr.length > 0) { | |
| 43 | - parentPaths.push(pathArr.join('.')); | |
| 44 | - pathArr.pop(); | |
| 45 | - } | |
| 46 | - | |
| 47 | - var result = [].concat(parentPaths); | |
| 48 | - parentPaths.forEach(function (path) { | |
| 49 | - var _destructDataPath = destructDataPath(path), | |
| 50 | - id = _destructDataPath.id, | |
| 51 | - dataIndex = _destructDataPath.dataIndex; | |
| 52 | - | |
| 53 | - if (flatten[id] && flatten[id].schema && Array.isArray(flatten[id].schema.dependecies)) { | |
| 54 | - var deps = flatten[id].schema.dependecies; | |
| 55 | - var fullPathDeps = deps.map(function (dep) { | |
| 56 | - return getDataPath(dep, dataIndex); | |
| 57 | - }); | |
| 58 | - result = [].concat(_toConsumableArray(result), _toConsumableArray(fullPathDeps)); | |
| 59 | - } | |
| 60 | - }); | |
| 61 | - return removeDups(result).map(function (path) { | |
| 62 | - if (path.slice(-1) === ']') { | |
| 63 | - var pattern = /\[[0-9]+\]$/; | |
| 64 | - return path.replace(pattern, ''); | |
| 65 | - } else { | |
| 66 | - return path; | |
| 67 | - } | |
| 68 | - }); | |
| 69 | -}; | |
| 70 | - | |
| 71 | -export var validateField = function validateField(_ref) { | |
| 72 | - var path = _ref.path, | |
| 73 | - formData = _ref.formData, | |
| 74 | - flatten = _ref.flatten, | |
| 75 | - options = _ref.options; | |
| 76 | - var paths = getRelatedPaths(path, flatten); // console.log('all relevant paths:', paths); | |
| 77 | - | |
| 78 | - var promiseArray = paths.map(function (path) { | |
| 79 | - var _destructDataPath2 = destructDataPath(path), | |
| 80 | - id = _destructDataPath2.id, | |
| 81 | - dataIndex = _destructDataPath2.dataIndex; | |
| 82 | - | |
| 83 | - if (flatten[id] || flatten["".concat(id, "[]")]) { | |
| 84 | - var item = flatten[id] || flatten["".concat(id, "[]")]; | |
| 85 | - var singleData = get(formData, path); | |
| 86 | - var schema = item.schema || {}; | |
| 87 | - var finalSchema = parseSchemaExpression(schema, formData, path); | |
| 88 | - return validateSingle(singleData, finalSchema, path, options); // is a promise | |
| 89 | - } else { | |
| 90 | - return Promise.resolve(); | |
| 91 | - } | |
| 92 | - }); | |
| 93 | - return allPromiseFinish(promiseArray).then(function (res) { | |
| 94 | - var errorFields = res.filter(function (item) { | |
| 95 | - return Array.isArray(item) && item.length > 0; | |
| 96 | - }).map(function (item) { | |
| 97 | - var name = item[0].field; | |
| 98 | - var error = item.map(function (m) { | |
| 99 | - return m.message; | |
| 100 | - }).filter(function (m) { | |
| 101 | - return !!m; | |
| 102 | - }); | |
| 103 | - return { | |
| 104 | - name: name, | |
| 105 | - error: error | |
| 106 | - }; | |
| 107 | - }); | |
| 108 | - return errorFields; | |
| 109 | - }).catch(function (e) { | |
| 110 | - console.log(e); | |
| 111 | - }); | |
| 112 | -}; // pathFromData => allPath | |
| 113 | - | |
| 114 | -var getAllPaths = function getAllPaths(paths, flatten) { | |
| 115 | - if (!Array.isArray(paths)) return []; | |
| 116 | - | |
| 117 | - var result = _toConsumableArray(paths).filter(function (p) { | |
| 118 | - return p.indexOf(']') > -1; | |
| 119 | - }).map(function (p1) { | |
| 120 | - var last = p1.lastIndexOf(']'); | |
| 121 | - return p1.substring(0, last + 1); | |
| 122 | - }); | |
| 123 | - | |
| 124 | - var uniqueResult = removeDups(result); | |
| 125 | - var allFlattenPath = Object.keys(flatten); | |
| 126 | - | |
| 127 | - var res = _toConsumableArray(paths); | |
| 128 | - | |
| 129 | - uniqueResult.forEach(function (result) { | |
| 130 | - var _destructDataPath3 = destructDataPath(result), | |
| 131 | - id = _destructDataPath3.id, | |
| 132 | - dataIndex = _destructDataPath3.dataIndex; | |
| 133 | - | |
| 134 | - if (flatten[id]) { | |
| 135 | - var children = allFlattenPath.filter(function (f) { | |
| 136 | - return f.indexOf(id) === 0 && f !== id; | |
| 137 | - }); | |
| 138 | - var childrenWithIndex = children.map(function (child) { | |
| 139 | - var p = getDataPath(child, dataIndex); | |
| 140 | - return p.split('[]')[0]; | |
| 141 | - }).filter(function (i) { | |
| 142 | - return !!i; | |
| 143 | - }); | |
| 144 | - res = [].concat(_toConsumableArray(res), _toConsumableArray(removeDups(childrenWithIndex))); | |
| 145 | - } | |
| 146 | - }); | |
| 147 | - return removeDups(res); | |
| 148 | -}; | |
| 149 | - | |
| 150 | -export var validateAll = function validateAll(_ref2) { | |
| 151 | - var formData = _ref2.formData, | |
| 152 | - flatten = _ref2.flatten, | |
| 153 | - options = _ref2.options; | |
| 154 | - var paths = dataToKeys(formData); | |
| 155 | - var allPaths = getAllPaths(paths, flatten); // console.log(formData, dataToKeys(formData), 'dataToKeysdataToKeys'); | |
| 156 | - // console.log('allPaths', allPaths); | |
| 157 | - | |
| 158 | - var promiseArray = allPaths.map(function (path) { | |
| 159 | - var _destructDataPath4 = destructDataPath(path), | |
| 160 | - id = _destructDataPath4.id, | |
| 161 | - dataIndex = _destructDataPath4.dataIndex; | |
| 162 | - | |
| 163 | - if (flatten[id] || flatten["".concat(id, "[]")]) { | |
| 164 | - var item = flatten[id] || flatten["".concat(id, "[]")]; | |
| 165 | - var singleData = get(formData, path); | |
| 166 | - var schema = item.schema || {}; | |
| 167 | - var finalSchema = parseSchemaExpression(schema, formData, path); | |
| 168 | - return validateSingle(singleData, finalSchema, path, options); // is a promise | |
| 169 | - } else { | |
| 170 | - return Promise.resolve(); | |
| 171 | - } | |
| 172 | - }); | |
| 173 | - return allPromiseFinish(promiseArray).then(function (res) { | |
| 174 | - var errorFields = res.filter(function (item) { | |
| 175 | - return Array.isArray(item) && item.length > 0 && item[0].message !== null; | |
| 176 | - }) // NOTICE: different from validateField | |
| 177 | - .map(function (item) { | |
| 178 | - var name = item[0].field; | |
| 179 | - var error = item.map(function (m) { | |
| 180 | - return m.message; | |
| 181 | - }).filter(function (m) { | |
| 182 | - return !!m; | |
| 183 | - }); | |
| 184 | - return { | |
| 185 | - name: name, | |
| 186 | - error: error | |
| 187 | - }; | |
| 188 | - }); | |
| 189 | - return errorFields; | |
| 190 | - }).catch(function (e) { | |
| 191 | - console.log(e); | |
| 192 | - }); | |
| 193 | -}; | |
| 194 | - | |
| 195 | -var validateSingle = function validateSingle(data) { | |
| 196 | - var schema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 197 | - var path = arguments.length > 2 ? arguments[2] : undefined; | |
| 198 | - var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | |
| 199 | - | |
| 200 | - if (schema.hidden) { | |
| 201 | - return Promise.resolve(); | |
| 202 | - } | |
| 203 | - | |
| 204 | - var _options$validateMess = options.validateMessages, | |
| 205 | - validateMessages = _options$validateMess === void 0 ? {} : _options$validateMess, | |
| 206 | - _options$locale = options.locale, | |
| 207 | - locale = _options$locale === void 0 ? 'cn' : _options$locale; | |
| 208 | - var cn = defaultValidateMessagesCN; | |
| 209 | - var en = defaultValidateMessages; | |
| 210 | - var descriptor = getDescriptorSimple(schema, path); // console.log('descriptor, schema, path', descriptor, schema, path, data); | |
| 211 | - // TODO: 有些情况会出现没有rules,需要看一下,先兜底 | |
| 212 | - | |
| 213 | - var validator; | |
| 214 | - | |
| 215 | - try { | |
| 216 | - validator = new Validator(descriptor); | |
| 217 | - } catch (error) { | |
| 218 | - return Promise.resolve(); | |
| 219 | - } | |
| 220 | - | |
| 221 | - var messageFeed = locale === 'en' ? en : cn; | |
| 222 | - merge(messageFeed, validateMessages); | |
| 223 | - validator.messages(messageFeed); | |
| 224 | - return validator.validate(_defineProperty({}, path, data)).then(function (res) { | |
| 225 | - return [{ | |
| 226 | - field: path, | |
| 227 | - message: null | |
| 228 | - }]; | |
| 229 | - }).catch(function (_ref3) { | |
| 230 | - var errors = _ref3.errors, | |
| 231 | - fields = _ref3.fields; | |
| 232 | - return errors; | |
| 233 | - }); | |
| 234 | -}; // 无用 | |
| 235 | -// const theRest = () => { | |
| 236 | -// let _schema = getSchemaFromFlatten(flatten); | |
| 237 | -// if (Object.keys(_schema).length === 0) return Promise.resolve(); | |
| 238 | -// const descriptor = getDescriptorFromSchema({ | |
| 239 | -// schema: _schema, | |
| 240 | -// isRequired, | |
| 241 | -// }).fields; | |
| 242 | -// window.descriptor = descriptor; | |
| 243 | -// let touchVerifyList = []; | |
| 244 | -// // 如果是最后的校验,所有key都touch了,就不用算这个了 | |
| 245 | -// // 因为要整个构建validator在list的情况太复杂了,所以required单独拿出来处理,但是这边有不少单独处理逻辑,例如message | |
| 246 | -// if (!isRequired) { | |
| 247 | -// touchedKeys.forEach(key => { | |
| 248 | -// const keyRequired = isPathRequired(key, _schema); | |
| 249 | -// const val = get(formData, key); | |
| 250 | -// const nullValue = [undefined, null, ''].indexOf(val) > -1; // 注意 0 不是 | |
| 251 | -// const isEmptyMultiSelect = Array.isArray(val) && val.length === 0; | |
| 252 | -// if ((nullValue || isEmptyMultiSelect) && keyRequired.required) { | |
| 253 | -// const _message = | |
| 254 | -// keyRequired.message || validateMessages.required || '${title}必填'; | |
| 255 | -// touchVerifyList.push({ name: key, error: [_message] }); | |
| 256 | -// } | |
| 257 | -// }); | |
| 258 | -// } | |
| 259 | -// const cn = defaultValidateMessagesCN; | |
| 260 | -// const en = defaultValidateMessages; | |
| 261 | -// // TODO: 有些情况会出现没有rules,需要看一下,先兜底 | |
| 262 | -// let validator; | |
| 263 | -// try { | |
| 264 | -// validator = new Validator(descriptor); | |
| 265 | -// } catch (error) { | |
| 266 | -// return Promise.resolve([]); | |
| 267 | -// } | |
| 268 | -// let messageFeed = locale === 'en' ? en : cn; | |
| 269 | -// merge(messageFeed, validateMessages); | |
| 270 | -// validator.messages(messageFeed); | |
| 271 | -// return validator | |
| 272 | -// .validate(formData || {}) | |
| 273 | -// .then(res => { | |
| 274 | -// if (touchVerifyList.length > 0) return touchVerifyList; | |
| 275 | -// return []; | |
| 276 | -// }) | |
| 277 | -// .catch(({ errors, fields }) => { | |
| 278 | -// // error的name改成正常的path | |
| 279 | -// let normalizedErrors = getArray(errors).map(err => { | |
| 280 | -// const _path = formatPathFromValidator(err.field); | |
| 281 | -// return { name: _path, error: [err.message] }; | |
| 282 | -// }); | |
| 283 | -// // 添加touched的required | |
| 284 | -// normalizedErrors = [...normalizedErrors, ...touchVerifyList]; | |
| 285 | -// // 合并同名的error | |
| 286 | -// let _errorFields = []; | |
| 287 | -// normalizedErrors.forEach(item => { | |
| 288 | -// const matchIndex = _errorFields.findIndex( | |
| 289 | -// ele => ele.name === item.name | |
| 290 | -// ); | |
| 291 | -// if (matchIndex === -1) { | |
| 292 | -// _errorFields.push(item); | |
| 293 | -// } else { | |
| 294 | -// _errorFields[matchIndex].error = [ | |
| 295 | -// ..._errorFields[matchIndex].error, | |
| 296 | -// ...item.error, | |
| 297 | -// ]; | |
| 298 | -// } | |
| 299 | -// }); | |
| 300 | -// return _errorFields; | |
| 301 | -// }); | |
| 302 | -// }; | |
| 303 | -// var d3 = { | |
| 304 | -// list: { | |
| 305 | -// type: 'array', | |
| 306 | -// defaultField: { | |
| 307 | -// type: 'object', | |
| 308 | -// fields: { | |
| 309 | -// userName: [ | |
| 310 | -// { | |
| 311 | -// bind: false, | |
| 312 | -// title: 'User Name', | |
| 313 | -// type: 'string', | |
| 314 | -// rules: [ | |
| 315 | -// { | |
| 316 | -// min: 5, | |
| 317 | -// message: '长度需要大于5', | |
| 318 | -// }, | |
| 319 | -// { | |
| 320 | -// max: 12, | |
| 321 | -// }, | |
| 322 | -// ], | |
| 323 | -// }, | |
| 324 | -// { | |
| 325 | -// min: 5, | |
| 326 | -// message: '长度需要大于5', | |
| 327 | -// }, | |
| 328 | -// { | |
| 329 | -// max: 12, | |
| 330 | -// }, | |
| 331 | -// ], | |
| 332 | -// selectName: { | |
| 333 | -// title: '单选', | |
| 334 | -// type: 'string', | |
| 335 | -// enum: ['a', 'b', 'c'], | |
| 336 | -// enumNames: ['早', '中', '晚'], | |
| 337 | -// required: true, | |
| 338 | -// }, | |
| 339 | -// checkboxName: { | |
| 340 | -// title: '是否判断', | |
| 341 | -// type: 'boolean', | |
| 342 | -// valuePropName: 'checked', | |
| 343 | -// }, | |
| 344 | -// }, | |
| 345 | -// }, | |
| 346 | -// }, | |
| 347 | -// }; | |
| 348 | -// var d2 = { | |
| 349 | -// list: { | |
| 350 | -// type: 'array', | |
| 351 | -// defaultField: { | |
| 352 | -// type: 'object', | |
| 353 | -// fields: { | |
| 354 | -// userName: [ | |
| 355 | -// { | |
| 356 | -// bind: false, | |
| 357 | -// title: 'User Name', | |
| 358 | -// type: 'string', | |
| 359 | -// rules: [ | |
| 360 | -// { | |
| 361 | -// min: 5, | |
| 362 | -// message: '长度需要大于5', | |
| 363 | -// }, | |
| 364 | -// { | |
| 365 | -// max: 12, | |
| 366 | -// }, | |
| 367 | -// ], | |
| 368 | -// }, | |
| 369 | -// { | |
| 370 | -// min: 5, | |
| 371 | -// message: '长度需要大于5', | |
| 372 | -// }, | |
| 373 | -// { | |
| 374 | -// max: 12, | |
| 375 | -// }, | |
| 376 | -// ], | |
| 377 | -// selectName: { | |
| 378 | -// title: '单选', | |
| 379 | -// type: 'string', | |
| 380 | -// enum: ['a', 'b', 'c'], | |
| 381 | -// enumNames: ['早', '中', '晚'], | |
| 382 | -// required: true, | |
| 383 | -// }, | |
| 384 | -// checkboxName: { | |
| 385 | -// title: '是否判断', | |
| 386 | -// type: 'boolean', | |
| 387 | -// valuePropName: 'checked', | |
| 388 | -// }, | |
| 389 | -// }, | |
| 390 | -// }, | |
| 391 | -// }, | |
| 392 | -// }; | |
| \ No newline at end of file |
es/hooks.js
renamed from
es/form-render-core/src/hooks.js
es/index.css
renamed from
es/form-render-core/src/index.css
| ... | ... | @@ -54,10 +54,9 @@ export interface FormInstance { |
| 54 | 54 | /** 折中升级方案中使用到,正常用不到 */ |
| 55 | 55 | init: () => void; |
| 56 | 56 | /** 数据分析接口,表单展示完成渲染时触发 */ |
| 57 | - logOnMount: (args: any) => void; | |
| 57 | + logOnMount?: (any) => void; | |
| 58 | 58 | /** 数据分析接口,表单提交成功时触发,获得本次表单填写的总时长 */ |
| 59 | - logOnSubmit: (args: any) => void; | |
| 60 | - _setErrors: (args: any) => void; | |
| 59 | + logOnSubmit?: (any) => void; | |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | 62 | export type WatchProperties = { | ... | ... |
| 1 | 1 | import "antd/es/config-provider/style"; |
| 2 | 2 | import _ConfigProvider from "antd/es/config-provider"; |
| 3 | -var _excluded = ["widgets", "configProvider"]; | |
| 3 | +var _excluded = ["id", "widgets", "mapping", "form", "className", "style", "beforeFinish", "onFinish", "displayType", "schema", "debug", "debugCss", "locale", "debounceInput", "size", "configProvider", "theme", "validateMessages", "watch", "config", "onMount", "labelWidth", "readOnly", "disabled", "allCollapsed", "onValuesChange", "column", "removeHiddenData", "globalProps"], | |
| 4 | + _excluded2 = ["onItemChange", "setEditing", "touchKey", "setValueByPath", "getSchemaByPath", "setSchemaByPath", "setSchema", "setValues", "getValues", "resetFields", "submit", "endValidating", "endSubmitting", "setErrorFields", "removeErrorField", "removeTouched", "changeTouchedKeys", "syncStuff", "logOnMount", "logOnSubmit", "setFirstMount"], | |
| 5 | + _excluded3 = ["isOldVersion", "schema"]; | |
| 4 | 6 | |
| 5 | 7 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } |
| 6 | 8 | |
| 9 | +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 10 | + | |
| 7 | 11 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } |
| 8 | 12 | |
| 9 | 13 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } |
| ... | ... | @@ -14,23 +18,364 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return |
| 14 | 18 | |
| 15 | 19 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } |
| 16 | 20 | |
| 17 | -import React from 'react'; | |
| 18 | -import zhCN from 'antd/lib/locale/zh_CN'; | |
| 19 | -import FRCore from './form-render-core/src'; | |
| 21 | +/* eslint-disable react-hooks/exhaustive-deps */ | |
| 22 | +import React, { useEffect, useMemo, useRef } from 'react'; | |
| 23 | +import { updateSchemaToNewVersion, getValueByPath, msToTime, yymmdd } from './utils'; | |
| 24 | +import Core from './core'; | |
| 25 | +import { Ctx, StoreCtx, Store2Ctx } from './hooks'; | |
| 20 | 26 | import { widgets as defaultWidgets } from './widgets/antd'; |
| 21 | -export { defaultWidgets as widgets }; | |
| 22 | -export { useForm, connectForm, createWidget, mapping } from './form-render-core/src'; | |
| 27 | +import { mapping as defaultMapping } from './mapping'; | |
| 28 | +import zhCN from 'antd/lib/locale/zh_CN'; | |
| 29 | +import "./atom.css"; | |
| 30 | +import "./index.css"; | |
| 31 | + | |
| 32 | +var defaultFinish = function defaultFinish(data, errors) { | |
| 33 | + console.log('onFinish:', { | |
| 34 | + data: data, | |
| 35 | + errors: errors | |
| 36 | + }); | |
| 37 | +}; | |
| 38 | + | |
| 39 | +export { defaultWidgets as widgets, defaultMapping as mapping }; | |
| 40 | +export { default as useForm } from './useForm'; | |
| 41 | +export { default as connectForm } from './connectForm'; | |
| 23 | 42 | |
| 24 | -var FR = function FR(_ref) { | |
| 25 | - var widgets = _ref.widgets, | |
| 43 | +function App(_ref) { | |
| 44 | + var id = _ref.id, | |
| 45 | + widgets = _ref.widgets, | |
| 46 | + mapping = _ref.mapping, | |
| 47 | + form = _ref.form, | |
| 48 | + className = _ref.className, | |
| 49 | + style = _ref.style, | |
| 50 | + beforeFinish = _ref.beforeFinish, | |
| 51 | + _ref$onFinish = _ref.onFinish, | |
| 52 | + onFinish = _ref$onFinish === void 0 ? defaultFinish : _ref$onFinish, | |
| 53 | + _ref$displayType = _ref.displayType, | |
| 54 | + displayType = _ref$displayType === void 0 ? 'column' : _ref$displayType, | |
| 55 | + schema = _ref.schema, | |
| 56 | + debug = _ref.debug, | |
| 57 | + debugCss = _ref.debugCss, | |
| 58 | + _ref$locale = _ref.locale, | |
| 59 | + locale = _ref$locale === void 0 ? 'cn' : _ref$locale, | |
| 60 | + _ref$debounceInput = _ref.debounceInput, | |
| 61 | + debounceInput = _ref$debounceInput === void 0 ? false : _ref$debounceInput, | |
| 62 | + size = _ref.size, | |
| 26 | 63 | configProvider = _ref.configProvider, |
| 64 | + theme = _ref.theme, | |
| 65 | + validateMessages = _ref.validateMessages, | |
| 66 | + _ref$watch = _ref.watch, | |
| 67 | + watch = _ref$watch === void 0 ? {} : _ref$watch, | |
| 68 | + config = _ref.config, | |
| 69 | + onMount = _ref.onMount, | |
| 70 | + labelWidth = _ref.labelWidth, | |
| 71 | + readOnly = _ref.readOnly, | |
| 72 | + disabled = _ref.disabled, | |
| 73 | + _ref$allCollapsed = _ref.allCollapsed, | |
| 74 | + allCollapsed = _ref$allCollapsed === void 0 ? false : _ref$allCollapsed, | |
| 75 | + onValuesChange = _ref.onValuesChange, | |
| 76 | + column = _ref.column, | |
| 77 | + _ref$removeHiddenData = _ref.removeHiddenData, | |
| 78 | + removeHiddenData = _ref$removeHiddenData === void 0 ? false : _ref$removeHiddenData, | |
| 79 | + _ref$globalProps = _ref.globalProps, | |
| 80 | + globalProps = _ref$globalProps === void 0 ? {} : _ref$globalProps, | |
| 27 | 81 | rest = _objectWithoutProperties(_ref, _excluded); |
| 28 | 82 | |
| 83 | + try { | |
| 84 | + var _ = form.submit; | |
| 85 | + } catch (error) { | |
| 86 | + console.error('form 为必填 props,<FormRender /> 没有接收到 form 属性!'); | |
| 87 | + } | |
| 88 | + | |
| 89 | + var _column = schema && schema.column || column; | |
| 90 | + | |
| 91 | + var onItemChange = form.onItemChange, | |
| 92 | + setEditing = form.setEditing, | |
| 93 | + touchKey = form.touchKey, | |
| 94 | + setValueByPath = form.setValueByPath, | |
| 95 | + getSchemaByPath = form.getSchemaByPath, | |
| 96 | + setSchemaByPath = form.setSchemaByPath, | |
| 97 | + setSchema = form.setSchema, | |
| 98 | + setValues = form.setValues, | |
| 99 | + getValues = form.getValues, | |
| 100 | + resetFields = form.resetFields, | |
| 101 | + submit = form.submit, | |
| 102 | + endValidating = form.endValidating, | |
| 103 | + endSubmitting = form.endSubmitting, | |
| 104 | + setErrorFields = form.setErrorFields, | |
| 105 | + removeErrorField = form.removeErrorField, | |
| 106 | + removeTouched = form.removeTouched, | |
| 107 | + changeTouchedKeys = form.changeTouchedKeys, | |
| 108 | + syncStuff = form.syncStuff, | |
| 109 | + logOnMount = form.logOnMount, | |
| 110 | + logOnSubmit = form.logOnSubmit, | |
| 111 | + setFirstMount = form.setFirstMount, | |
| 112 | + valuesThatWillChange = _objectWithoutProperties(form, _excluded2); | |
| 113 | + | |
| 114 | + var submitData = valuesThatWillChange.submitData, | |
| 115 | + errorFields = valuesThatWillChange.errorFields, | |
| 116 | + isValidating = valuesThatWillChange.isValidating, | |
| 117 | + outsideValidating = valuesThatWillChange.outsideValidating, | |
| 118 | + isSubmitting = valuesThatWillChange.isSubmitting, | |
| 119 | + formData = valuesThatWillChange.formData, | |
| 120 | + flatten = valuesThatWillChange.flatten, | |
| 121 | + showValidate = valuesThatWillChange.showValidate, | |
| 122 | + firstMount = valuesThatWillChange.firstMount; | |
| 123 | + useEffect(function () { | |
| 124 | + // Schema最外层的type是object来判断,没有的话,认为schema没有传 | |
| 125 | + if (schema && schema.type) { | |
| 126 | + setFirstMount(true); | |
| 127 | + syncStuff({ | |
| 128 | + schema: schema, | |
| 129 | + locale: locale, | |
| 130 | + validateMessages: validateMessages, | |
| 131 | + beforeFinish: beforeFinish, | |
| 132 | + onMount: onMount, | |
| 133 | + removeHiddenData: removeHiddenData | |
| 134 | + }); | |
| 135 | + } else {} | |
| 136 | + }, [JSON.stringify(schema)]); | |
| 137 | + useEffect(function () { | |
| 138 | + if (!firstMount && schema && schema.type) { | |
| 139 | + if (typeof onMount === 'function') { | |
| 140 | + // 等一下 useForm 里接到第一份schema时,计算第一份data的骨架 | |
| 141 | + setTimeout(function () { | |
| 142 | + onMount(); | |
| 143 | + }, 0); | |
| 144 | + } | |
| 145 | + | |
| 146 | + setTimeout(onMountLogger, 0); | |
| 147 | + } | |
| 148 | + }, [JSON.stringify(schema), firstMount]); | |
| 149 | + | |
| 150 | + var onMountLogger = function onMountLogger() { | |
| 151 | + var start = new Date().getTime(); | |
| 152 | + | |
| 153 | + if (typeof logOnMount === 'function' || typeof logOnSubmit === 'function') { | |
| 154 | + sessionStorage.setItem('FORM_MOUNT_TIME', start); | |
| 155 | + sessionStorage.setItem('FORM_START', start); | |
| 156 | + } | |
| 157 | + | |
| 158 | + if (typeof logOnMount === 'function') { | |
| 159 | + var logParams = { | |
| 160 | + schema: schema, | |
| 161 | + url: location.href, | |
| 162 | + formData: JSON.stringify(form.getValues()), | |
| 163 | + formMount: yymmdd(start) | |
| 164 | + }; | |
| 165 | + | |
| 166 | + if (id) { | |
| 167 | + logParams.id = id; | |
| 168 | + } | |
| 169 | + | |
| 170 | + logOnMount(logParams); | |
| 171 | + } // 如果是要计算时间,在 onMount 时存一个时间戳 | |
| 172 | + | |
| 173 | + | |
| 174 | + if (typeof logOnSubmit === 'function') { | |
| 175 | + sessionStorage.setItem('NUMBER_OF_SUBMITS', 0); | |
| 176 | + sessionStorage.setItem('FAILED_ATTEMPTS', 0); | |
| 177 | + } | |
| 178 | + }; // 组件destroy的时候,destroy form,因为useForm可能在上层,所以不一定会跟着destroy | |
| 179 | + | |
| 180 | + | |
| 181 | + useEffect(function () { | |
| 182 | + return function () { | |
| 183 | + form.resetFields(); | |
| 184 | + }; | |
| 185 | + }, []); | |
| 186 | + var store = useMemo(function () { | |
| 187 | + return _objectSpread(_objectSpread({}, valuesThatWillChange), {}, { | |
| 188 | + globalProps: globalProps | |
| 189 | + }, rest); | |
| 190 | + }, [JSON.stringify(flatten), JSON.stringify(formData), JSON.stringify(errorFields), JSON.stringify(globalProps)]); // 不常用的context单独放一个地方 | |
| 191 | + | |
| 192 | + var store2 = useMemo(function () { | |
| 193 | + return { | |
| 194 | + displayType: displayType, | |
| 195 | + theme: theme, | |
| 196 | + column: _column, | |
| 197 | + debounceInput: debounceInput, | |
| 198 | + debug: debug, | |
| 199 | + labelWidth: labelWidth, | |
| 200 | + locale: locale, | |
| 201 | + readOnly: readOnly, | |
| 202 | + disabled: disabled, | |
| 203 | + allCollapsed: allCollapsed, | |
| 204 | + showValidate: showValidate | |
| 205 | + }; | |
| 206 | + }, [displayType, theme, _column, debounceInput, debug, labelWidth, locale, readOnly, disabled, allCollapsed, showValidate]); | |
| 207 | + var tools = useMemo(function () { | |
| 208 | + return _objectSpread({ | |
| 209 | + widgets: _objectSpread(_objectSpread({}, defaultWidgets), widgets), | |
| 210 | + mapping: _objectSpread(_objectSpread({}, defaultMapping), mapping), | |
| 211 | + onValuesChange: onValuesChange | |
| 212 | + }, form); | |
| 213 | + }, []); | |
| 214 | + useEffect(function () { | |
| 215 | + // 需要外部校验的情况,此时 submitting 还是 false | |
| 216 | + if (outsideValidating === true) { | |
| 217 | + Promise.resolve(beforeFinish(_objectSpread({ | |
| 218 | + data: submitData, | |
| 219 | + schema: schema, | |
| 220 | + errors: errorFields | |
| 221 | + }, config))).then(function (error) { | |
| 222 | + if (error) { | |
| 223 | + setErrorFields(error); | |
| 224 | + } | |
| 225 | + | |
| 226 | + endValidating(); | |
| 227 | + }); | |
| 228 | + return; | |
| 229 | + } // 如果validation结束,submitting开始 | |
| 230 | + | |
| 231 | + | |
| 232 | + if (isValidating === false && isSubmitting === true) { | |
| 233 | + endSubmitting(); | |
| 234 | + onFinish(submitData, errorFields); | |
| 235 | + | |
| 236 | + if (typeof logOnSubmit === 'function') { | |
| 237 | + var start = sessionStorage.getItem('FORM_START'); | |
| 238 | + var mount = sessionStorage.getItem('FORM_MOUNT_TIME'); | |
| 239 | + var numberOfSubmits = Number(sessionStorage.getItem('NUMBER_OF_SUBMITS')) + 1; | |
| 240 | + var end = new Date().getTime(); | |
| 241 | + var failedAttempts = Number(sessionStorage.getItem('FAILED_ATTEMPTS')); | |
| 242 | + | |
| 243 | + if (errorFields.length > 0) { | |
| 244 | + failedAttempts = failedAttempts + 1; | |
| 245 | + } | |
| 246 | + | |
| 247 | + var logParams = { | |
| 248 | + formMount: yymmdd(mount), | |
| 249 | + ms: end - start, | |
| 250 | + duration: msToTime(end - start), | |
| 251 | + numberOfSubmits: numberOfSubmits, | |
| 252 | + failedAttempts: failedAttempts, | |
| 253 | + url: location.href, | |
| 254 | + formData: JSON.stringify(submitData), | |
| 255 | + errors: JSON.stringify(errorFields), | |
| 256 | + schema: JSON.stringify(schema) | |
| 257 | + }; | |
| 258 | + | |
| 259 | + if (id) { | |
| 260 | + logParams.id = id; | |
| 261 | + } | |
| 262 | + | |
| 263 | + logOnSubmit(logParams); | |
| 264 | + sessionStorage.setItem('FORM_START', end); | |
| 265 | + sessionStorage.setItem('NUMBER_OF_SUBMITS', numberOfSubmits); | |
| 266 | + sessionStorage.setItem('FAILED_ATTEMPTS', failedAttempts); | |
| 267 | + } | |
| 268 | + } | |
| 269 | + }, [isValidating, isSubmitting, outsideValidating]); // TODO: fk doesn't work | |
| 270 | + | |
| 271 | + var sizeCls = ''; | |
| 272 | + | |
| 273 | + if (size === 'small') { | |
| 274 | + sizeCls = 'fr-form-small'; | |
| 275 | + } else if (size === 'large') { | |
| 276 | + sizeCls = 'fr-form-large'; | |
| 277 | + } | |
| 278 | + | |
| 279 | + var rootProps = { | |
| 280 | + className: "fr-container ".concat(sizeCls, " ").concat(className || '') | |
| 281 | + }; | |
| 282 | + | |
| 283 | + if (style && _typeof(style) === 'object') { | |
| 284 | + rootProps.style = style; | |
| 285 | + } | |
| 286 | + | |
| 287 | + if (id && ['number', 'string'].indexOf(_typeof(id)) > -1) { | |
| 288 | + rootProps.id = id; | |
| 289 | + } | |
| 290 | + | |
| 291 | + var watchList = Object.keys(watch); | |
| 29 | 292 | return /*#__PURE__*/React.createElement(_ConfigProvider, _extends({ |
| 30 | 293 | locale: zhCN |
| 31 | - }, configProvider), /*#__PURE__*/React.createElement(FRCore, _extends({ | |
| 32 | - widgets: _objectSpread(_objectSpread({}, defaultWidgets), widgets) | |
| 33 | - }, rest))); | |
| 294 | + }, configProvider), /*#__PURE__*/React.createElement(StoreCtx.Provider, { | |
| 295 | + value: store | |
| 296 | + }, /*#__PURE__*/React.createElement(Store2Ctx.Provider, { | |
| 297 | + value: store2 | |
| 298 | + }, /*#__PURE__*/React.createElement(Ctx.Provider, { | |
| 299 | + value: tools | |
| 300 | + }, /*#__PURE__*/React.createElement("div", rootProps, debug ? /*#__PURE__*/React.createElement("div", { | |
| 301 | + className: "mv2 bg-black-05 pa2 br2" | |
| 302 | + }, /*#__PURE__*/React.createElement("div", { | |
| 303 | + style: { | |
| 304 | + display: 'flex' | |
| 305 | + } | |
| 306 | + }, /*#__PURE__*/React.createElement("span", null, "formData:"), /*#__PURE__*/React.createElement("span", { | |
| 307 | + style: { | |
| 308 | + display: 'inline-block', | |
| 309 | + wordBreak: 'break-all', | |
| 310 | + maxWidth: 600 | |
| 311 | + } | |
| 312 | + }, JSON.stringify(form.formData))), /*#__PURE__*/React.createElement("div", null, 'errorFields:' + JSON.stringify(form.errorFields)), /*#__PURE__*/React.createElement("div", null, 'touchedKeys:' + JSON.stringify(form.touchedKeys)), /*#__PURE__*/React.createElement("div", null, 'allTouched:' + JSON.stringify(form.allTouched)), /*#__PURE__*/React.createElement("div", null, 'descriptor:' + JSON.stringify(window.descriptor))) : null, watchList.length > 0 ? watchList.map(function (item, idx) { | |
| 313 | + return /*#__PURE__*/React.createElement(Watcher, { | |
| 314 | + key: idx.toString(), | |
| 315 | + watchKey: item, | |
| 316 | + watch: watch, | |
| 317 | + formData: formData, | |
| 318 | + firstMount: firstMount | |
| 319 | + }); | |
| 320 | + }) : null, /*#__PURE__*/React.createElement(Core, { | |
| 321 | + debugCss: debugCss | |
| 322 | + })))))); | |
| 323 | +} | |
| 324 | + | |
| 325 | +export { createWidget } from './createWidget'; | |
| 326 | + | |
| 327 | +var Wrapper = function Wrapper(props) { | |
| 328 | + var _props$isOldVersion = props.isOldVersion, | |
| 329 | + isOldVersion = _props$isOldVersion === void 0 ? true : _props$isOldVersion, | |
| 330 | + schema = props.schema, | |
| 331 | + rest = _objectWithoutProperties(props, _excluded3); | |
| 332 | + | |
| 333 | + var _schema = useRef(schema); | |
| 334 | + | |
| 335 | + if (isOldVersion) { | |
| 336 | + _schema.current = updateSchemaToNewVersion(schema); | |
| 337 | + } | |
| 338 | + | |
| 339 | + return /*#__PURE__*/React.createElement(App, _extends({ | |
| 340 | + schema: _schema.current | |
| 341 | + }, rest)); | |
| 34 | 342 | }; |
| 35 | 343 | |
| 36 | -export default FR; | |
| \ No newline at end of file | ||
| 344 | +export default Wrapper; | |
| 345 | + | |
| 346 | +var Watcher = function Watcher(_ref2) { | |
| 347 | + var watchKey = _ref2.watchKey, | |
| 348 | + watch = _ref2.watch, | |
| 349 | + formData = _ref2.formData, | |
| 350 | + firstMount = _ref2.firstMount; | |
| 351 | + var value = getValueByPath(formData, watchKey); | |
| 352 | + var watchObj = watch[watchKey]; | |
| 353 | + useEffect(function () { | |
| 354 | + var runWatcher = function runWatcher() { | |
| 355 | + if (typeof watchObj === 'function') { | |
| 356 | + try { | |
| 357 | + watchObj(value); | |
| 358 | + } catch (error) { | |
| 359 | + console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 360 | + } | |
| 361 | + } else if (watchObj && typeof watchObj.handler === 'function') { | |
| 362 | + try { | |
| 363 | + watchObj.handler(value); | |
| 364 | + } catch (error) { | |
| 365 | + console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 366 | + } | |
| 367 | + } | |
| 368 | + }; | |
| 369 | + | |
| 370 | + if (firstMount) { | |
| 371 | + var immediate = watchObj && watchObj.immediate; | |
| 372 | + | |
| 373 | + if (immediate) { | |
| 374 | + runWatcher(); | |
| 375 | + } | |
| 376 | + } else { | |
| 377 | + runWatcher(); | |
| 378 | + } | |
| 379 | + }, [JSON.stringify(value), firstMount]); | |
| 380 | + return null; | |
| 381 | +}; | |
| \ No newline at end of file | ... | ... |
es/processData.js
renamed from
es/form-render-core/src/processData.js
es/useDebounce.js
renamed from
es/form-render-core/src/useDebounce.js
es/useForm.js
renamed from
es/form-render-core/src/useForm.js
| ... | ... | @@ -65,6 +65,8 @@ var useForm = function useForm(props) { |
| 65 | 65 | // 是否所有表单元素都被碰过了(一键开关,用于提交的时候,默认所有都被touch了) |
| 66 | 66 | touchedKeys: [], |
| 67 | 67 | // 碰过的key(用于submit之前,判断哪些被碰过了) |
| 68 | + flatten: {}, | |
| 69 | + // schema 的转换结构,便于处理 | |
| 68 | 70 | finalFlatten: {}, |
| 69 | 71 | // 表达式等被处理过的flatten,用于渲染 |
| 70 | 72 | firstMount: true |
| ... | ... | @@ -110,11 +112,13 @@ var useForm = function useForm(props) { |
| 110 | 112 | isEditing = state.isEditing, |
| 111 | 113 | allTouched = state.allTouched, |
| 112 | 114 | touchedKeys = state.touchedKeys, |
| 115 | + flatten = state.flatten, | |
| 113 | 116 | finalFlatten = state.finalFlatten, |
| 114 | 117 | firstMount = state.firstMount; |
| 115 | 118 | _errorFields.current = errorFields; |
| 116 | 119 | _outErrorFields.current = outErrorFields; |
| 117 | 120 | _touchedKeys.current = touchedKeys; |
| 121 | + _flatten.current = flatten; | |
| 118 | 122 | _finalFlatten.current = finalFlatten; |
| 119 | 123 | var dataFromOutside = props && props.hasOwnProperty('formData'); |
| 120 | 124 | var formData = dataFromOutside ? _formData : innerData; // 生成一个基础结构,确保对象内的必填元素也被校验 |
| ... | ... | @@ -137,39 +141,55 @@ var useForm = function useForm(props) { |
| 137 | 141 | } |
| 138 | 142 | }, [JSON.stringify(_errorFields.current), JSON.stringify(_outErrorFields.current)]); |
| 139 | 143 | useEffect(function () { |
| 140 | - if (schemaRef.current) { | |
| 141 | - var newFlatten = clone(_flatten.current); | |
| 142 | - | |
| 143 | - if (firstMount) { | |
| 144 | - _flatten.current = flattenSchema(schemaRef.current); | |
| 145 | - setState({ | |
| 146 | - firstMount: false | |
| 147 | - }); | |
| 148 | - } else { | |
| 149 | - // 统一的处理expression | |
| 150 | - Object.entries(_flatten.current).forEach(function (_ref2) { | |
| 151 | - var _ref3 = _slicedToArray(_ref2, 2), | |
| 152 | - path = _ref3[0], | |
| 153 | - info = _ref3[1]; | |
| 154 | - | |
| 155 | - if (schemaContainsExpression(info.schema)) { | |
| 156 | - var arrayLikeIndex = path.indexOf(']'); | |
| 157 | - var isArrayItem = arrayLikeIndex > -1 && arrayLikeIndex < path.length - 1; | |
| 158 | - var hasRootValue = JSON.stringify(info.schema).indexOf('rootValue') > -1; | |
| 159 | - | |
| 160 | - if (isArrayItem && hasRootValue) {// do nothing | |
| 161 | - } else { | |
| 162 | - newFlatten[path].schema = parseAllExpression(info.schema, _data.current, path); | |
| 163 | - } | |
| 164 | - } | |
| 165 | - }); | |
| 166 | - } | |
| 144 | + if (schemaRef.current && firstMount) { | |
| 145 | + var _flatten2 = flattenSchema(schemaRef.current); | |
| 167 | 146 | |
| 168 | 147 | setState({ |
| 169 | - finalFlatten: newFlatten | |
| 148 | + flatten: _flatten2, | |
| 149 | + firstMount: false | |
| 170 | 150 | }); |
| 171 | 151 | } |
| 172 | - }, [JSON.stringify(schemaRef.current), JSON.stringify(_data.current), firstMount]); // All form methods are down here ---------------------------------------------------------------- | |
| 152 | + }, [JSON.stringify(schemaRef.current), firstMount]); // 统一的处理expression | |
| 153 | + | |
| 154 | + useEffect(function () { | |
| 155 | + if (firstMount) { | |
| 156 | + return; | |
| 157 | + } | |
| 158 | + | |
| 159 | + var newFlatten = clone(_flatten.current); | |
| 160 | + Object.entries(_flatten.current).forEach(function (_ref2) { | |
| 161 | + var _ref3 = _slicedToArray(_ref2, 2), | |
| 162 | + path = _ref3[0], | |
| 163 | + info = _ref3[1]; | |
| 164 | + | |
| 165 | + if (schemaContainsExpression(info.schema)) { | |
| 166 | + var arrayLikeIndex = path.indexOf(']'); | |
| 167 | + var isArrayItem = arrayLikeIndex > -1 && arrayLikeIndex < path.length - 1; | |
| 168 | + var hasRootValue = JSON.stringify(info.schema).indexOf('rootValue') > -1; | |
| 169 | + | |
| 170 | + if (isArrayItem && hasRootValue) {// do nothing | |
| 171 | + } else { | |
| 172 | + newFlatten[path].schema = parseAllExpression(info.schema, _data.current, path); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + }); | |
| 176 | + setState({ | |
| 177 | + finalFlatten: newFlatten | |
| 178 | + }); | |
| 179 | + }, [JSON.stringify(_flatten.current), JSON.stringify(_data.current), firstMount]); | |
| 180 | + useEffect(function () { | |
| 181 | + if (firstMount) return; | |
| 182 | + validateAll({ | |
| 183 | + formData: _data.current, | |
| 184 | + flatten: _finalFlatten.current, | |
| 185 | + isRequired: allTouched, | |
| 186 | + touchedKeys: _touchedKeys.current, | |
| 187 | + locale: localeRef.current, | |
| 188 | + validateMessages: validateMessagesRef.current | |
| 189 | + }).then(function (res) { | |
| 190 | + _setErrors(res); | |
| 191 | + }); | |
| 192 | + }, [JSON.stringify(_data.current)]); // All form methods are down here ---------------------------------------------------------------- | |
| 173 | 193 | // 两个兼容 0.x 的函数 |
| 174 | 194 | |
| 175 | 195 | var _setData = function _setData(data) { |
| ... | ... | @@ -180,8 +200,7 @@ var useForm = function useForm(props) { |
| 180 | 200 | formData: data |
| 181 | 201 | }); |
| 182 | 202 | } |
| 183 | - }; // Allow function to get the old value | |
| 184 | - | |
| 203 | + }; | |
| 185 | 204 | |
| 186 | 205 | var _setErrors = function _setErrors(errors) { |
| 187 | 206 | if (typeof _onValidate === 'function') { |
| ... | ... | @@ -192,18 +211,9 @@ var useForm = function useForm(props) { |
| 192 | 211 | _onValidate(oldFormatErrors); |
| 193 | 212 | } |
| 194 | 213 | |
| 195 | - if (typeof errors === 'function') { | |
| 196 | - setState(function (_ref4) { | |
| 197 | - var errorFields = _ref4.errorFields; | |
| 198 | - return { | |
| 199 | - errorFields: errors(errorFields) | |
| 200 | - }; | |
| 201 | - }); | |
| 202 | - } else { | |
| 203 | - setState({ | |
| 204 | - errorFields: errors | |
| 205 | - }); | |
| 206 | - } | |
| 214 | + setState({ | |
| 215 | + errorFields: errors | |
| 216 | + }); | |
| 207 | 217 | }; |
| 208 | 218 | |
| 209 | 219 | var setFirstMount = function setFirstMount(value) { |
| ... | ... | @@ -262,12 +272,12 @@ var useForm = function useForm(props) { |
| 262 | 272 | // ] |
| 263 | 273 | |
| 264 | 274 | |
| 265 | - var syncStuff = function syncStuff(_ref5) { | |
| 266 | - var schema = _ref5.schema, | |
| 267 | - locale = _ref5.locale, | |
| 268 | - validateMessages = _ref5.validateMessages, | |
| 269 | - beforeFinish = _ref5.beforeFinish, | |
| 270 | - removeHiddenData = _ref5.removeHiddenData; | |
| 275 | + var syncStuff = function syncStuff(_ref4) { | |
| 276 | + var schema = _ref4.schema, | |
| 277 | + locale = _ref4.locale, | |
| 278 | + validateMessages = _ref4.validateMessages, | |
| 279 | + beforeFinish = _ref4.beforeFinish, | |
| 280 | + removeHiddenData = _ref4.removeHiddenData; | |
| 271 | 281 | schemaRef.current = schema; |
| 272 | 282 | localeRef.current = locale; |
| 273 | 283 | validateMessagesRef.current = validateMessages; |
| ... | ... | @@ -277,11 +287,11 @@ var useForm = function useForm(props) { |
| 277 | 287 | }; |
| 278 | 288 | |
| 279 | 289 | var setSchema = function setSchema(settings) { |
| 280 | - var newFlatten = clone(_finalFlatten.current); | |
| 290 | + var newFlatten = clone(_flatten.current); | |
| 281 | 291 | |
| 282 | 292 | try { |
| 283 | 293 | Object.keys(settings).forEach(function (path) { |
| 284 | - if (!_finalFlatten.current[path]) { | |
| 294 | + if (!_flatten.current[path]) { | |
| 285 | 295 | console.error("path\uFF1A'".concat(path, "' \u4E0D\u5B58\u5728(form.setSchemaByPath)")); |
| 286 | 296 | } else { |
| 287 | 297 | var newSchema = settings[path]; |
| ... | ... | @@ -292,30 +302,30 @@ var useForm = function useForm(props) { |
| 292 | 302 | } |
| 293 | 303 | }); |
| 294 | 304 | setState({ |
| 295 | - finalFlatten: newFlatten | |
| 305 | + flatten: newFlatten | |
| 296 | 306 | }); |
| 297 | - _finalFlatten.current = newFlatten; | |
| 307 | + _flatten.current = newFlatten; | |
| 298 | 308 | } catch (error) { |
| 299 | 309 | console.error(error, 'setSchema'); |
| 300 | 310 | } |
| 301 | 311 | }; |
| 302 | 312 | |
| 303 | 313 | var setSchemaByPath = function setSchemaByPath(path, newSchema) { |
| 304 | - if (!_finalFlatten.current[path]) { | |
| 314 | + if (!_flatten.current[path]) { | |
| 305 | 315 | console.error("path\uFF1A'".concat(path, "' \u4E0D\u5B58\u5728(form.setSchemaByPath)")); |
| 306 | 316 | return; |
| 307 | 317 | } |
| 308 | 318 | |
| 309 | - var newFlatten = clone(_finalFlatten.current); | |
| 319 | + var newFlatten = clone(_flatten.current); | |
| 310 | 320 | |
| 311 | 321 | try { |
| 312 | 322 | var _newSchema = typeof newSchema === 'function' ? newSchema(newFlatten[path].schema) : newSchema; |
| 313 | 323 | |
| 314 | 324 | newFlatten[path].schema = _objectSpread(_objectSpread({}, newFlatten[path].schema), _newSchema); |
| 315 | 325 | setState({ |
| 316 | - finalFlatten: newFlatten | |
| 326 | + flatten: newFlatten | |
| 317 | 327 | }); |
| 318 | - _finalFlatten.current = newFlatten; | |
| 328 | + _flatten.current = newFlatten; | |
| 319 | 329 | } catch (error) { |
| 320 | 330 | console.error(error, 'setSchemaByPath'); |
| 321 | 331 | } |
| ... | ... | @@ -323,7 +333,7 @@ var useForm = function useForm(props) { |
| 323 | 333 | |
| 324 | 334 | var getSchemaByPath = function getSchemaByPath(path) { |
| 325 | 335 | try { |
| 326 | - return _finalFlatten.current[path].schema; | |
| 336 | + return _flatten.current[path].schema; | |
| 327 | 337 | } catch (error) { |
| 328 | 338 | console.log(error, 'getSchemaByPath'); |
| 329 | 339 | return {}; |
| ... | ... | @@ -365,7 +375,7 @@ var useForm = function useForm(props) { |
| 365 | 375 | }; |
| 366 | 376 | |
| 367 | 377 | var setValues = function setValues(newFormData) { |
| 368 | - var newData = transformDataWithBind2(newFormData, _finalFlatten.current); | |
| 378 | + var newData = transformDataWithBind2(newFormData, _flatten.current); | |
| 369 | 379 | |
| 370 | 380 | _setData(newData); |
| 371 | 381 | }; |
| ... | ... | @@ -380,10 +390,10 @@ var useForm = function useForm(props) { |
| 380 | 390 | return validateAll({ |
| 381 | 391 | formData: _data.current, |
| 382 | 392 | flatten: _finalFlatten.current, |
| 383 | - options: { | |
| 384 | - locale: localeRef.current, | |
| 385 | - validateMessages: validateMessagesRef.current | |
| 386 | - } | |
| 393 | + touchedKeys: [], | |
| 394 | + isRequired: true, | |
| 395 | + locale: localeRef.current, | |
| 396 | + validateMessages: validateMessagesRef.current | |
| 387 | 397 | }).then(function (errors) { |
| 388 | 398 | setState({ |
| 389 | 399 | errorFields: errors |
| ... | ... | @@ -426,13 +436,13 @@ var useForm = function useForm(props) { |
| 426 | 436 | }); |
| 427 | 437 | }; |
| 428 | 438 | |
| 429 | - var resetFields = function resetFields(options) { | |
| 439 | + var resetFields = function resetFields() { | |
| 430 | 440 | setState({ |
| 431 | - formData: (options === null || options === void 0 ? void 0 : options.formData) || {}, | |
| 432 | - submitData: (options === null || options === void 0 ? void 0 : options.submitData) || {}, | |
| 433 | - errorFields: (options === null || options === void 0 ? void 0 : options.errorFields) || [], | |
| 434 | - touchedKeys: (options === null || options === void 0 ? void 0 : options.touchedKeys) || [], | |
| 435 | - allTouched: (options === null || options === void 0 ? void 0 : options.allTouched) || false | |
| 441 | + formData: {}, | |
| 442 | + submitData: {}, | |
| 443 | + errorFields: [], | |
| 444 | + touchedKeys: [], | |
| 445 | + allTouched: false | |
| 436 | 446 | }); |
| 437 | 447 | }; |
| 438 | 448 | |
| ... | ... | @@ -492,9 +502,7 @@ var useForm = function useForm(props) { |
| 492 | 502 | setFirstMount: setFirstMount, |
| 493 | 503 | // logs |
| 494 | 504 | logOnMount: logOnMount, |
| 495 | - logOnSubmit: logOnSubmit, | |
| 496 | - // inner api, DON'T USE | |
| 497 | - _setErrors: _setErrors | |
| 505 | + logOnSubmit: logOnSubmit | |
| 498 | 506 | }; |
| 499 | 507 | return form; |
| 500 | 508 | }; | ... | ... |
| 1 | +var _excluded = ["propsSchema"], | |
| 2 | + _excluded2 = ["schema"], | |
| 3 | + _excluded3 = ["required"]; | |
| 4 | + | |
| 5 | +function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); } | |
| 6 | + | |
| 7 | +function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 8 | + | |
| 9 | +function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 10 | + | |
| 11 | +function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 12 | + | |
| 13 | +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 14 | + | |
| 15 | +function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 16 | + | |
| 17 | +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 18 | + | |
| 19 | +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 20 | + | |
| 21 | +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 22 | + | |
| 23 | +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 24 | + | |
| 25 | +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 26 | + | |
| 27 | +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 28 | + | |
| 29 | +function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 30 | + | |
| 31 | +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 32 | + | |
| 33 | +import { get, set, cloneDeep } from 'lodash-es'; // window.log1 = value => { | |
| 34 | +// console.log('%ccommon:', 'color: #00A7F7; font-weight: 500;', value); | |
| 35 | +// }; | |
| 36 | +// window.log2 = value => { | |
| 37 | +// console.log('%cwarning:', 'color: #f50; font-weight: 500;', value); | |
| 38 | +// }; | |
| 39 | +// window.log3 = value => { | |
| 40 | +// console.log('%csuccess:', 'color: #87d068; font-weight: 500;', value); | |
| 41 | +// }; | |
| 42 | +// window.log4 = value => { | |
| 43 | +// console.log('%cspecial:', 'color: #722ed1; font-weight: 500;', value); | |
| 44 | +// }; | |
| 45 | + | |
| 1 | 46 | export function isUrl(string) { |
| 2 | 47 | var protocolRE = /^(?:\w+:)?\/\/(\S+)$/; // const domainRE = /^[^\s\.]+\.\S{2,}$/; |
| 3 | 48 | |
| 4 | 49 | if (typeof string !== 'string') return false; |
| 5 | 50 | return protocolRE.test(string); |
| 6 | 51 | } |
| 7 | -export var getArray = function getArray(arr) { | |
| 8 | - var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| 9 | - if (Array.isArray(arr)) return arr; | |
| 10 | - return defaultValue; | |
| 52 | +export function isCheckBoxType(schema, readOnly) { | |
| 53 | + if (readOnly) return false; | |
| 54 | + if (schema.widget === 'checkbox') return true; | |
| 55 | + | |
| 56 | + if (schema && schema.type === 'boolean') { | |
| 57 | + if (schema.enum) return false; | |
| 58 | + if (schema.widget === undefined) return true; | |
| 59 | + return false; | |
| 60 | + } | |
| 61 | +} // a[].b.c => a.b.c | |
| 62 | + | |
| 63 | +function removeBrackets(string) { | |
| 64 | + if (typeof string === 'string') { | |
| 65 | + return string.replace(/\[\]/g, ''); | |
| 66 | + } else { | |
| 67 | + return string; | |
| 68 | + } | |
| 69 | +} | |
| 70 | + | |
| 71 | +export function getParentPath(path) { | |
| 72 | + if (typeof path === 'string') { | |
| 73 | + var pathArr = path.split('.'); | |
| 74 | + | |
| 75 | + if (pathArr.length === 1) { | |
| 76 | + return '#'; | |
| 77 | + } | |
| 78 | + | |
| 79 | + pathArr.pop(); | |
| 80 | + return pathArr.join('.'); | |
| 81 | + } | |
| 82 | + | |
| 83 | + return '#'; | |
| 84 | +} | |
| 85 | +export function getValueByPath(formData, path) { | |
| 86 | + if (path === '#' || !path) { | |
| 87 | + return formData || {}; | |
| 88 | + } else if (typeof path === 'string') { | |
| 89 | + return get(formData, path); | |
| 90 | + } else { | |
| 91 | + console.error('path has to be a string'); | |
| 92 | + } | |
| 93 | +} // path: 'a.b[1].c[0]' => { id: 'a.b[].c[]' dataIndex: [1,0] } | |
| 94 | + | |
| 95 | +export function destructDataPath(path) { | |
| 96 | + var id; | |
| 97 | + var dataIndex; | |
| 98 | + | |
| 99 | + if (path === '#') { | |
| 100 | + return { | |
| 101 | + id: '#', | |
| 102 | + dataIndex: [] | |
| 103 | + }; | |
| 104 | + } | |
| 105 | + | |
| 106 | + if (typeof path !== 'string') { | |
| 107 | + throw Error("path ".concat(path, " is not a string!!! Something wrong here")); | |
| 108 | + } | |
| 109 | + | |
| 110 | + var pattern = /\[[0-9]+\]/g; | |
| 111 | + var matchList = path.match(pattern); | |
| 112 | + | |
| 113 | + if (!matchList) { | |
| 114 | + id = path; | |
| 115 | + } else { | |
| 116 | + id = path.replace(pattern, '[]'); // 这个是match下来的结果,可安全处理 | |
| 117 | + | |
| 118 | + dataIndex = matchList.map(function (item) { | |
| 119 | + return Number(item.substring(1, item.length - 1)); | |
| 120 | + }); | |
| 121 | + } | |
| 122 | + | |
| 123 | + return { | |
| 124 | + id: id, | |
| 125 | + dataIndex: dataIndex | |
| 126 | + }; | |
| 127 | +} // id: 'a.b[].c[]' dataIndex: [1,0] => 'a.b[1].c[0]' | |
| 128 | + | |
| 129 | +export function getDataPath(id, dataIndex) { | |
| 130 | + if (id === '#') { | |
| 131 | + return id; | |
| 132 | + } | |
| 133 | + | |
| 134 | + if (typeof id !== 'string') { | |
| 135 | + throw Error("id ".concat(id, " is not a string!!! Something wrong here")); | |
| 136 | + } | |
| 137 | + | |
| 138 | + var _id = id; | |
| 139 | + | |
| 140 | + if (Array.isArray(dataIndex)) { | |
| 141 | + // const matches = id.match(/\[\]/g) || []; | |
| 142 | + // const count = matches.length; | |
| 143 | + dataIndex.forEach(function (item) { | |
| 144 | + _id = _id.replace(/\[\]/, "[".concat(item, "]")); | |
| 145 | + }); | |
| 146 | + } | |
| 147 | + | |
| 148 | + return removeBrackets(_id); | |
| 149 | +} | |
| 150 | +export function isObjType(schema) { | |
| 151 | + return schema && schema.type === 'object' && schema.properties && !schema.widget; | |
| 152 | +} // TODO: to support case that item is not an object | |
| 153 | + | |
| 154 | +export function isListType(schema) { | |
| 155 | + return schema && schema.type === 'array' && isObjType(schema.items) && schema.enum === undefined; | |
| 156 | +} // TODO: more tests to make sure weird & wrong schema won't crush | |
| 157 | + | |
| 158 | +export function flattenSchema() { | |
| 159 | + var _schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 160 | + | |
| 161 | + var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 162 | + var parent = arguments.length > 2 ? arguments[2] : undefined; | |
| 163 | + var result = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | |
| 164 | + var schema = clone(_schema); | |
| 165 | + var _name = name; | |
| 166 | + | |
| 167 | + if (!schema.$id) { | |
| 168 | + schema.$id = _name; // path as $id, for easy access to path in schema | |
| 169 | + } | |
| 170 | + | |
| 171 | + var children = []; | |
| 172 | + | |
| 173 | + if (isObjType(schema)) { | |
| 174 | + Object.entries(schema.properties).forEach(function (_ref) { | |
| 175 | + var _ref2 = _slicedToArray(_ref, 2), | |
| 176 | + key = _ref2[0], | |
| 177 | + value = _ref2[1]; | |
| 178 | + | |
| 179 | + var _key = isListType(value) ? key + '[]' : key; | |
| 180 | + | |
| 181 | + var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 182 | + children.push(uniqueName); | |
| 183 | + flattenSchema(value, uniqueName, _name, result); | |
| 184 | + }); | |
| 185 | + schema.properties = {}; | |
| 186 | + } | |
| 187 | + | |
| 188 | + if (isListType(schema)) { | |
| 189 | + Object.entries(schema.items.properties).forEach(function (_ref3) { | |
| 190 | + var _ref4 = _slicedToArray(_ref3, 2), | |
| 191 | + key = _ref4[0], | |
| 192 | + value = _ref4[1]; | |
| 193 | + | |
| 194 | + var _key = isListType(value) ? key + '[]' : key; | |
| 195 | + | |
| 196 | + var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 197 | + children.push(uniqueName); | |
| 198 | + flattenSchema(value, uniqueName, _name, result); | |
| 199 | + }); | |
| 200 | + schema.items.properties = {}; | |
| 201 | + } | |
| 202 | + | |
| 203 | + if (schema.type) { | |
| 204 | + result[_name] = { | |
| 205 | + parent: parent, | |
| 206 | + schema: schema, | |
| 207 | + children: children | |
| 208 | + }; | |
| 209 | + } | |
| 210 | + | |
| 211 | + return result; | |
| 212 | +} | |
| 213 | +export function getSchemaFromFlatten(flatten) { | |
| 214 | + var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 215 | + var schema = {}; | |
| 216 | + var item = clone(flatten[path]); | |
| 217 | + | |
| 218 | + if (item) { | |
| 219 | + schema = item.schema; // schema.$id && delete schema.$id; | |
| 220 | + | |
| 221 | + if (item.children.length > 0) { | |
| 222 | + item.children.forEach(function (child) { | |
| 223 | + if (!flatten[child]) return; | |
| 224 | + var key = getKeyFromPath(child); | |
| 225 | + | |
| 226 | + if (isObjType(schema)) { | |
| 227 | + schema.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 228 | + } | |
| 229 | + | |
| 230 | + if (isListType(schema)) { | |
| 231 | + schema.items.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 232 | + } | |
| 233 | + }); | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + return schema; | |
| 238 | +} | |
| 239 | + | |
| 240 | +function stringContains(str, text) { | |
| 241 | + return str.indexOf(text) > -1; | |
| 242 | +} | |
| 243 | + | |
| 244 | +export var isObject = function isObject(a) { | |
| 245 | + return stringContains(Object.prototype.toString.call(a), 'Object'); | |
| 11 | 246 | }; |
| 247 | +export var clone = cloneDeep; // '3' => true, 3 => true, undefined => false | |
| 248 | + | |
| 249 | +export function isLooselyNumber(num) { | |
| 250 | + if (typeof num === 'number') return true; | |
| 251 | + | |
| 252 | + if (typeof num === 'string') { | |
| 253 | + return !Number.isNaN(Number(num)); | |
| 254 | + } | |
| 255 | + | |
| 256 | + return false; | |
| 257 | +} | |
| 258 | +export function isCssLength(str) { | |
| 259 | + if (typeof str !== 'string') return false; | |
| 260 | + return str.match(/^([0-9])*(%|px|rem|em)$/i); | |
| 261 | +} | |
| 262 | +export function isDeepEqual(param1, param2) { | |
| 263 | + if (param1 === undefined && param2 === undefined) return true;else if (param1 === undefined || param2 === undefined) return false; | |
| 264 | + if (param1 === null && param2 === null) return true;else if (param1 === null || param2 === null) return false;else if (param1.constructor !== param2.constructor) return false; | |
| 265 | + | |
| 266 | + if (param1.constructor === Array) { | |
| 267 | + if (param1.length !== param2.length) return false; | |
| 268 | + | |
| 269 | + for (var i = 0; i < param1.length; i++) { | |
| 270 | + if (param1[i].constructor === Array || param1[i].constructor === Object) { | |
| 271 | + if (!isDeepEqual(param1[i], param2[i])) return false; | |
| 272 | + } else if (param1[i] !== param2[i]) return false; | |
| 273 | + } | |
| 274 | + } else if (param1.constructor === Object) { | |
| 275 | + if (Object.keys(param1).length !== Object.keys(param2).length) return false; | |
| 276 | + | |
| 277 | + for (var _i2 = 0; _i2 < Object.keys(param1).length; _i2++) { | |
| 278 | + var key = Object.keys(param1)[_i2]; | |
| 279 | + | |
| 280 | + if (param1[key] && typeof param1[key] !== 'number' && (param1[key].constructor === Array || param1[key].constructor === Object)) { | |
| 281 | + if (!isDeepEqual(param1[key], param2[key])) return false; | |
| 282 | + } else if (param1[key] !== param2[key]) return false; | |
| 283 | + } | |
| 284 | + } else if (param1.constructor === String || param1.constructor === Number) { | |
| 285 | + return param1 === param2; | |
| 286 | + } | |
| 287 | + | |
| 288 | + return true; | |
| 289 | +} | |
| 12 | 290 | export function getFormat(format) { |
| 13 | 291 | var dateFormat; |
| 14 | 292 | |
| ... | ... | @@ -52,4 +330,1057 @@ export function getFormat(format) { |
| 52 | 330 | } |
| 53 | 331 | |
| 54 | 332 | return dateFormat; |
| 333 | +} | |
| 334 | +export function hasRepeat(list) { | |
| 335 | + return list.find(function (x, i, self) { | |
| 336 | + return i !== self.findIndex(function (y) { | |
| 337 | + return JSON.stringify(x) === JSON.stringify(y); | |
| 338 | + }); | |
| 339 | + }); | |
| 340 | +} | |
| 341 | +export function combineSchema() { | |
| 342 | + var propsSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 343 | + var uiSchema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 344 | + var propList = getChildren(propsSchema); | |
| 345 | + var newList = propList.map(function (p) { | |
| 346 | + var name = p.name; | |
| 347 | + var _p$schema = p.schema, | |
| 348 | + type = _p$schema.type, | |
| 349 | + options = _p$schema.enum, | |
| 350 | + properties = _p$schema.properties, | |
| 351 | + items = _p$schema.items; | |
| 352 | + var isObj = type === 'object' && properties; | |
| 353 | + var isArr = type === 'array' && items && !options; // enum + array 代表的多选框,没有sub | |
| 354 | + | |
| 355 | + var ui = name && uiSchema[p.name]; | |
| 356 | + | |
| 357 | + if (!ui) { | |
| 358 | + return p; | |
| 359 | + } // 如果是list,递归合并items | |
| 360 | + | |
| 361 | + | |
| 362 | + if (isArr) { | |
| 363 | + var newItems = combineSchema(items, ui.items || {}); | |
| 364 | + return _objectSpread(_objectSpread({}, p), {}, { | |
| 365 | + schema: _objectSpread(_objectSpread(_objectSpread({}, p.schema), ui), {}, { | |
| 366 | + items: newItems | |
| 367 | + }) | |
| 368 | + }); | |
| 369 | + } // object递归合并整个schema | |
| 370 | + | |
| 371 | + | |
| 372 | + if (isObj) { | |
| 373 | + var newSchema = combineSchema(p.schema, ui); | |
| 374 | + return _objectSpread(_objectSpread({}, p), {}, { | |
| 375 | + schema: newSchema | |
| 376 | + }); | |
| 377 | + } | |
| 378 | + | |
| 379 | + return _objectSpread(_objectSpread({}, p), {}, { | |
| 380 | + schema: _objectSpread(_objectSpread({}, p.schema), ui) | |
| 381 | + }); | |
| 382 | + }); | |
| 383 | + var newObj = {}; | |
| 384 | + newList.forEach(function (s) { | |
| 385 | + newObj[s.name] = s.schema; | |
| 386 | + }); | |
| 387 | + var topLevelUi = {}; | |
| 388 | + Object.keys(uiSchema).forEach(function (key) { | |
| 389 | + if (typeof key === 'string' && key.substring(0, 3) === 'ui:') { | |
| 390 | + topLevelUi[key] = uiSchema[key]; | |
| 391 | + } | |
| 392 | + }); | |
| 393 | + | |
| 394 | + if (isEmpty(newObj)) { | |
| 395 | + return _objectSpread(_objectSpread({}, propsSchema), topLevelUi); | |
| 396 | + } | |
| 397 | + | |
| 398 | + return _objectSpread(_objectSpread(_objectSpread({}, propsSchema), topLevelUi), {}, { | |
| 399 | + properties: newObj | |
| 400 | + }); | |
| 401 | +} | |
| 402 | + | |
| 403 | +function isEmpty(obj) { | |
| 404 | + return Object.keys(obj).length === 0; | |
| 405 | +} | |
| 406 | + | |
| 407 | +function getChildren(schema) { | |
| 408 | + if (!schema) return []; | |
| 409 | + var properties = schema.properties, | |
| 410 | + items = schema.items, | |
| 411 | + type = schema.type; | |
| 412 | + | |
| 413 | + if (!properties && !items) { | |
| 414 | + return []; | |
| 415 | + } | |
| 416 | + | |
| 417 | + var schemaSubs = {}; | |
| 418 | + | |
| 419 | + if (type === 'object') { | |
| 420 | + schemaSubs = properties; | |
| 421 | + } | |
| 422 | + | |
| 423 | + if (type === 'array') { | |
| 424 | + schemaSubs = items; | |
| 425 | + } | |
| 426 | + | |
| 427 | + return Object.keys(schemaSubs).map(function (name) { | |
| 428 | + return { | |
| 429 | + schema: schemaSubs[name], | |
| 430 | + name: name | |
| 431 | + }; | |
| 432 | + }); | |
| 433 | +} | |
| 434 | + | |
| 435 | +export var parseString = function parseString(string) { | |
| 436 | + return Function('"use strict";return (' + string + ')')(); | |
| 437 | +}; | |
| 438 | +export var evaluateString = function evaluateString(string, formData, rootValue) { | |
| 439 | + return Function("\"use strict\";\n const rootValue = ".concat(JSON.stringify(rootValue), ";\n const formData = ").concat(JSON.stringify(formData), ";\n return (").concat(string, ")"))(); | |
| 440 | +}; | |
| 441 | +export function isExpression(func) { | |
| 442 | + // if (typeof func === 'function') { | |
| 443 | + // const funcString = func.toString(); | |
| 444 | + // return ( | |
| 445 | + // funcString.indexOf('formData') > -1 || | |
| 446 | + // funcString.indexOf('rootValue') > -1 | |
| 447 | + // ); | |
| 448 | + // } | |
| 449 | + if (typeof func !== 'string') return false; | |
| 450 | + var pattern = /^{{(.+)}}$/; | |
| 451 | + var reg1 = /^{{function\(.+}}$/; // const reg2 = /^{{(.+=>.+)}}$/; | |
| 452 | + | |
| 453 | + if (typeof func === 'string' && func.match(pattern) && !func.match(reg1)) { | |
| 454 | + return true; | |
| 455 | + } | |
| 456 | + | |
| 457 | + return false; | |
| 458 | +} | |
| 459 | +export var parseRootValueInSchema = function parseRootValueInSchema(schema, rootValue) { | |
| 460 | + var result = clone(schema); | |
| 461 | + | |
| 462 | + if (isObject(schema)) { | |
| 463 | + Object.keys(schema).forEach(function (key) { | |
| 464 | + var item = schema[key]; | |
| 465 | + | |
| 466 | + if (isObject(item)) { | |
| 467 | + result[key] = parseRootValueInSchema(item, rootValue); | |
| 468 | + } else if (typeof item === 'string') { | |
| 469 | + result[key] = parseSingleRootValue(item, rootValue); | |
| 470 | + } | |
| 471 | + }); | |
| 472 | + } else { | |
| 473 | + console.error('schema is not an object:', schema); | |
| 474 | + } | |
| 475 | + | |
| 476 | + return result; | |
| 477 | +}; // handle rootValue inside List | |
| 478 | + | |
| 479 | +export var parseSingleRootValue = function parseSingleRootValue(expression) { | |
| 480 | + var rootValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 481 | + | |
| 482 | + if (typeof expression === 'string' && expression.indexOf('rootValue') > 0) { | |
| 483 | + var funcBody = expression.substring(2, expression.length - 2); | |
| 484 | + var str = "\n return ".concat(funcBody.replace(/rootValue/g, JSON.stringify(rootValue))); | |
| 485 | + | |
| 486 | + try { | |
| 487 | + return Function(str)(); | |
| 488 | + } catch (error) { | |
| 489 | + console.error(error, 'expression:', expression, 'rootValue:', rootValue); | |
| 490 | + return null; // 如果计算有错误,return null 最合适 | |
| 491 | + } | |
| 492 | + } else { | |
| 493 | + return expression; | |
| 494 | + } | |
| 495 | +}; | |
| 496 | +export function parseSingleExpression(func) { | |
| 497 | + var formData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 498 | + var dataPath = arguments.length > 2 ? arguments[2] : undefined; | |
| 499 | + var parentPath = getParentPath(dataPath); | |
| 500 | + var parent = getValueByPath(formData, parentPath) || {}; | |
| 501 | + | |
| 502 | + if (typeof func === 'string') { | |
| 503 | + var funcBody = func.substring(2, func.length - 2); | |
| 504 | + var str = "\n return ".concat(funcBody.replace(/formData/g, JSON.stringify(formData)).replace(/rootValue/g, JSON.stringify(parent))); | |
| 505 | + | |
| 506 | + try { | |
| 507 | + return Function(str)(); | |
| 508 | + } catch (error) { | |
| 509 | + console.log(error, func, dataPath); | |
| 510 | + return null; // 如果计算有错误,return null 最合适 | |
| 511 | + } // const funcBody = func.substring(2, func.length - 2); | |
| 512 | + // //TODO: 这样有问题,例如 a.b.indexOf(), 会把 a.b.indexOf 当做值 | |
| 513 | + // const match1 = /formData.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 514 | + // const match2 = /rootValue.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 515 | + // const str = ` | |
| 516 | + // return (${funcBody | |
| 517 | + // .replaceAll(match1, (v, m1) => | |
| 518 | + // JSON.stringify(getValueByPath(formData, m1)) | |
| 519 | + // ) | |
| 520 | + // .replaceAll(match2, (v, m1) => | |
| 521 | + // JSON.stringify(getValueByPath(parent, m1)) | |
| 522 | + // )})`; | |
| 523 | + // try { | |
| 524 | + // return Function(str)(); | |
| 525 | + // } catch (error) { | |
| 526 | + // console.log(error); | |
| 527 | + // return func; | |
| 528 | + // } | |
| 529 | + | |
| 530 | + } else return func; | |
| 531 | +} | |
| 532 | +export var schemaContainsExpression = function schemaContainsExpression(schema) { | |
| 533 | + if (isObject(schema)) { | |
| 534 | + return Object.keys(schema).some(function (key) { | |
| 535 | + var value = schema[key]; | |
| 536 | + | |
| 537 | + if (typeof value === 'string') { | |
| 538 | + return isExpression(value); | |
| 539 | + } else if (isObject(value)) { | |
| 540 | + return schemaContainsExpression(value); | |
| 541 | + } else { | |
| 542 | + return false; | |
| 543 | + } | |
| 544 | + }); | |
| 545 | + } | |
| 546 | + | |
| 547 | + return false; | |
| 548 | +}; | |
| 549 | +export var parseAllExpression = function parseAllExpression(_schema, formData, dataPath) { | |
| 550 | + var schema = clone(_schema); | |
| 551 | + Object.keys(schema).forEach(function (key) { | |
| 552 | + var value = schema[key]; | |
| 553 | + | |
| 554 | + if (isObject(value)) { | |
| 555 | + schema[key] = parseAllExpression(value, formData, dataPath); | |
| 556 | + } else if (isExpression(value)) { | |
| 557 | + schema[key] = parseSingleExpression(value, formData, dataPath); | |
| 558 | + } else if (typeof key === 'string' && key.toLowerCase().indexOf('props') > -1) { | |
| 559 | + // 有可能叫 xxxProps | |
| 560 | + var propsObj = schema[key]; | |
| 561 | + | |
| 562 | + if (isObject(propsObj)) { | |
| 563 | + Object.keys(propsObj).forEach(function (k) { | |
| 564 | + schema[key][k] = parseSingleExpression(propsObj[k], formData, dataPath); | |
| 565 | + }); | |
| 566 | + } | |
| 567 | + } | |
| 568 | + }); | |
| 569 | + return schema; | |
| 570 | +}; | |
| 571 | +export function isFunctionSchema(schema) { | |
| 572 | + return Object.keys(schema).some(function (key) { | |
| 573 | + if (typeof schema[key] === 'function') { | |
| 574 | + return true; | |
| 575 | + } else if (typeof schema[key] === 'string') { | |
| 576 | + return isExpression(schema[key]); | |
| 577 | + } else if (_typeof(schema[key]) === 'object') { | |
| 578 | + return isFunctionSchema(schema[key]); | |
| 579 | + } else { | |
| 580 | + return false; | |
| 581 | + } | |
| 582 | + }); | |
| 583 | +} | |
| 584 | +export var getParentProps = function getParentProps(propName, id, flatten) { | |
| 585 | + try { | |
| 586 | + var item = flatten[id]; | |
| 587 | + if (item.schema[propName] !== undefined) return item.schema[propName]; | |
| 588 | + | |
| 589 | + if (item && item.parent) { | |
| 590 | + var parentSchema = flatten[item.parent].schema; | |
| 591 | + | |
| 592 | + if (parentSchema[propName] !== undefined) { | |
| 593 | + return parentSchema[propName]; | |
| 594 | + } else { | |
| 595 | + return getParentProps(propName, item.parent, flatten); | |
| 596 | + } | |
| 597 | + } | |
| 598 | + } catch (error) { | |
| 599 | + return undefined; | |
| 600 | + } | |
| 601 | +}; | |
| 602 | +export var getSaveNumber = function getSaveNumber() { | |
| 603 | + var searchStr = localStorage.getItem('SAVES'); | |
| 604 | + | |
| 605 | + if (searchStr) { | |
| 606 | + try { | |
| 607 | + var saves = JSON.parse(searchStr); | |
| 608 | + var length = saves.length; | |
| 609 | + if (length) return length + 1; | |
| 610 | + } catch (error) { | |
| 611 | + return 1; | |
| 612 | + } | |
| 613 | + } else { | |
| 614 | + return 1; | |
| 615 | + } | |
| 616 | +}; | |
| 617 | +export function looseJsonParse(obj) { | |
| 618 | + return Function('"use strict";return (' + obj + ')')(); | |
| 619 | +} | |
| 620 | +export var isFunctionString = function isFunctionString(fString) { | |
| 621 | + return typeof fString === 'string' && fString.indexOf('function(') === 0; | |
| 622 | +}; | |
| 623 | +export function parseFunction(fString) { | |
| 624 | + if (isFunctionString(fString)) { | |
| 625 | + return Function('return ' + fString)(); | |
| 626 | + } | |
| 627 | + | |
| 628 | + return fString; | |
| 629 | +} // 获得propsSchema的children | |
| 630 | +// function getChildren2(schema) { | |
| 631 | +// if (!schema) return []; | |
| 632 | +// const { | |
| 633 | +// // object | |
| 634 | +// properties, | |
| 635 | +// // array | |
| 636 | +// items, | |
| 637 | +// type, | |
| 638 | +// } = schema; | |
| 639 | +// if (!properties && !items) { | |
| 640 | +// return []; | |
| 641 | +// } | |
| 642 | +// let schemaSubs = {}; | |
| 643 | +// if (type === 'object') { | |
| 644 | +// schemaSubs = properties; | |
| 645 | +// } | |
| 646 | +// if (type === 'array') { | |
| 647 | +// schemaSubs = items.properties; | |
| 648 | +// } | |
| 649 | +// return Object.keys(schemaSubs).map(name => ({ | |
| 650 | +// schema: schemaSubs[name], | |
| 651 | +// name, | |
| 652 | +// })); | |
| 653 | +// } | |
| 654 | + | |
| 655 | +export var oldSchemaToNew = function oldSchemaToNew(schema) { | |
| 656 | + if (schema && schema.propsSchema) { | |
| 657 | + var propsSchema = schema.propsSchema, | |
| 658 | + rest = _objectWithoutProperties(schema, _excluded); | |
| 659 | + | |
| 660 | + return _objectSpread({ | |
| 661 | + schema: propsSchema | |
| 662 | + }, rest); | |
| 663 | + } | |
| 664 | + | |
| 665 | + return schema; | |
| 666 | +}; | |
| 667 | +export var newSchemaToOld = function newSchemaToOld(setting) { | |
| 668 | + if (setting && setting.schema) { | |
| 669 | + var schema = setting.schema, | |
| 670 | + rest = _objectWithoutProperties(setting, _excluded2); | |
| 671 | + | |
| 672 | + return _objectSpread({ | |
| 673 | + propsSchema: schema | |
| 674 | + }, rest); | |
| 675 | + } | |
| 676 | + | |
| 677 | + return setting; | |
| 678 | +}; // from FR | |
| 679 | + | |
| 680 | +export var getEnum = function getEnum(schema) { | |
| 681 | + if (!schema) return undefined; | |
| 682 | + var itemEnum = schema && schema.items && schema.items.enum; | |
| 683 | + var schemaEnum = schema && schema.enum; | |
| 684 | + return itemEnum ? itemEnum : schemaEnum; | |
| 685 | +}; | |
| 686 | +export var getArray = function getArray(arr) { | |
| 687 | + var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| 688 | + if (Array.isArray(arr)) return arr; | |
| 689 | + return defaultValue; | |
| 690 | +}; | |
| 691 | +export var isEmail = function isEmail(value) { | |
| 692 | + var regex = '^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$'; | |
| 693 | + | |
| 694 | + if (value && new RegExp(regex).test(value)) { | |
| 695 | + return true; | |
| 696 | + } | |
| 697 | + | |
| 698 | + return false; | |
| 699 | +}; | |
| 700 | +export function defaultGetValueFromEvent(valuePropName) { | |
| 701 | + var event = arguments.length <= 1 ? undefined : arguments[1]; | |
| 702 | + | |
| 703 | + if (event && event.target && valuePropName in event.target) { | |
| 704 | + return event.target[valuePropName]; | |
| 705 | + } | |
| 706 | + | |
| 707 | + return event; | |
| 708 | +} | |
| 709 | +export var getKeyFromPath = function getKeyFromPath() { | |
| 710 | + var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '#'; | |
| 711 | + | |
| 712 | + try { | |
| 713 | + var arr = path.split('.'); | |
| 714 | + var last = arr.slice(-1)[0]; | |
| 715 | + var result = last.replace('[]', ''); | |
| 716 | + return result; | |
| 717 | + } catch (error) { | |
| 718 | + console.error(error, 'getKeyFromPath'); | |
| 719 | + return ''; | |
| 720 | + } | |
| 721 | +}; // 更多的值获取 | |
| 722 | + | |
| 723 | +export var getDisplayValue = function getDisplayValue(value, schema) { | |
| 724 | + if (typeof value === 'boolean') { | |
| 725 | + return value ? 'yes' : 'no'; | |
| 726 | + } | |
| 727 | + | |
| 728 | + if (isObjType(schema) || isListType(schema)) { | |
| 729 | + return '-'; | |
| 730 | + } | |
| 731 | + | |
| 732 | + if (Array.isArray(schema.enum) && Array.isArray(schema.enumNames)) { | |
| 733 | + try { | |
| 734 | + return schema.enumNames[schema.enum.indexOf(value)]; | |
| 735 | + } catch (error) { | |
| 736 | + return value; | |
| 737 | + } | |
| 738 | + } | |
| 739 | + | |
| 740 | + return value; | |
| 741 | +}; // 去掉数组里的空元素 {a: [null, {x:1}]} => {a: [{x:1}]} | |
| 742 | + | |
| 743 | +export var removeEmptyItemFromList = function removeEmptyItemFromList(formData) { | |
| 744 | + var result = {}; | |
| 745 | + | |
| 746 | + if (isObject(formData)) { | |
| 747 | + Object.keys(formData).forEach(function (key) { | |
| 748 | + result[key] = removeEmptyItemFromList(formData[key]); | |
| 749 | + }); | |
| 750 | + } else if (Array.isArray(formData)) { | |
| 751 | + result = formData.filter(function (item) { | |
| 752 | + if ([false, 0, ''].indexOf(item) > -1) return true; | |
| 753 | + | |
| 754 | + if (item && JSON.stringify(item) !== '{}') { | |
| 755 | + return true; | |
| 756 | + } | |
| 757 | + | |
| 758 | + return false; | |
| 759 | + }); | |
| 760 | + } else { | |
| 761 | + result = formData; | |
| 762 | + } | |
| 763 | + | |
| 764 | + return result; | |
| 765 | +}; | |
| 766 | +export var getDescriptorFromSchema = function getDescriptorFromSchema(_ref5) { | |
| 767 | + var schema = _ref5.schema, | |
| 768 | + _ref5$isRequired = _ref5.isRequired, | |
| 769 | + isRequired = _ref5$isRequired === void 0 ? true : _ref5$isRequired; | |
| 770 | + var result = {}; | |
| 771 | + var singleResult = {}; | |
| 772 | + if (schema.hidden === true) return { | |
| 773 | + validator: function validator() { | |
| 774 | + return true; | |
| 775 | + } | |
| 776 | + }; | |
| 777 | + | |
| 778 | + if (isObjType(schema)) { | |
| 779 | + result.type = 'object'; | |
| 780 | + | |
| 781 | + if (isRequired && schema.required === true) { | |
| 782 | + result.required = true; | |
| 783 | + } | |
| 784 | + | |
| 785 | + result.fields = {}; | |
| 786 | + Object.keys(schema.properties).forEach(function (key) { | |
| 787 | + var item = schema.properties[key]; // 兼容旧的! | |
| 788 | + | |
| 789 | + if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 790 | + item.required = true; | |
| 791 | + } | |
| 792 | + | |
| 793 | + result.fields[key] = getDescriptorFromSchema({ | |
| 794 | + schema: item, | |
| 795 | + isRequired: isRequired | |
| 796 | + }); | |
| 797 | + }); | |
| 798 | + } else if (isListType(schema)) { | |
| 799 | + result.type = 'array'; | |
| 800 | + | |
| 801 | + if (isRequired && schema.required === true) { | |
| 802 | + result.required = true; | |
| 803 | + } | |
| 804 | + | |
| 805 | + if (typeof schema.min === 'number') { | |
| 806 | + result.min = schema.min; | |
| 807 | + } | |
| 808 | + | |
| 809 | + if (typeof schema.max === 'number') { | |
| 810 | + result.max = schema.max; | |
| 811 | + } | |
| 812 | + | |
| 813 | + result.defaultField = { | |
| 814 | + type: 'object', | |
| 815 | + fields: {} | |
| 816 | + }; // 目前就默认只有object类型的 TODO: | |
| 817 | + | |
| 818 | + Object.keys(schema.items.properties).forEach(function (key) { | |
| 819 | + var item = schema.items.properties[key]; // 兼容旧的! | |
| 820 | + | |
| 821 | + if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 822 | + item.required = true; | |
| 823 | + } | |
| 824 | + | |
| 825 | + result.defaultField.fields[key] = getDescriptorFromSchema({ | |
| 826 | + schema: item, | |
| 827 | + isRequired: isRequired | |
| 828 | + }); | |
| 829 | + }); | |
| 830 | + } else { | |
| 831 | + // 单个的逻辑 | |
| 832 | + var processRule = function processRule(item) { | |
| 833 | + if (schema.type) return _objectSpread(_objectSpread({}, item), {}, { | |
| 834 | + type: schema.type | |
| 835 | + }); | |
| 836 | + | |
| 837 | + if (item.pattern && typeof item.pattern === 'string') { | |
| 838 | + return _objectSpread(_objectSpread({}, item), {}, { | |
| 839 | + pattern: new RegExp(item.pattern) | |
| 840 | + }); | |
| 841 | + } | |
| 842 | + | |
| 843 | + return item; | |
| 844 | + }; | |
| 845 | + | |
| 846 | + var required = schema.required, | |
| 847 | + rest = _objectWithoutProperties(schema, _excluded3); | |
| 848 | + | |
| 849 | + ['type', 'pattern', 'min', 'max', 'len'].forEach(function (key) { | |
| 850 | + if (Object.keys(rest).indexOf(key) > -1) { | |
| 851 | + singleResult[key] = rest[key]; | |
| 852 | + } | |
| 853 | + }); | |
| 854 | + | |
| 855 | + switch (schema.type) { | |
| 856 | + case 'range': | |
| 857 | + // 暂时先去掉,应该没事了,在range组件的源头上把奇怪的case干掉了 | |
| 858 | + // const rangeValidator = { | |
| 859 | + // validator: (rule, value) => { | |
| 860 | + // const rangeRequired = schema.required === true && isRequired; | |
| 861 | + // // range组件点击clear,会变成 ['',''] | |
| 862 | + // // range组件对应的值bind的时候,会变成 [undefined,undefined] | |
| 863 | + // const notValid1 = value[0] === '' && value[1] === ''; | |
| 864 | + // const notValid2 = value[0] === undefined && value[1] === undefined; | |
| 865 | + // if (rangeRequired) { | |
| 866 | + // if (!value) return false; | |
| 867 | + // if (Array.isArray(value)) { | |
| 868 | + // if (notValid1 || notValid2) { | |
| 869 | + // return false; | |
| 870 | + // } | |
| 871 | + // return true; | |
| 872 | + // } | |
| 873 | + // return true; | |
| 874 | + // } else { | |
| 875 | + // return true; | |
| 876 | + // } | |
| 877 | + // }, | |
| 878 | + // type: 'array', | |
| 879 | + // message: '${title}必填', | |
| 880 | + // }; | |
| 881 | + // singleResult = rangeValidator; | |
| 882 | + singleResult.type = 'array'; | |
| 883 | + break; | |
| 884 | + | |
| 885 | + case 'html': | |
| 886 | + singleResult.type = 'string'; | |
| 887 | + break; | |
| 888 | + | |
| 889 | + default: | |
| 890 | + break; | |
| 891 | + } | |
| 892 | + | |
| 893 | + switch (schema.format) { | |
| 894 | + case 'email': | |
| 895 | + case 'url': | |
| 896 | + singleResult.type = schema.format; | |
| 897 | + break; | |
| 898 | + | |
| 899 | + default: | |
| 900 | + break; | |
| 901 | + } | |
| 902 | + | |
| 903 | + var requiredRule; | |
| 904 | + | |
| 905 | + if (isRequired && schema.required === true) { | |
| 906 | + requiredRule = { | |
| 907 | + required: true, | |
| 908 | + type: singleResult.type || 'string' | |
| 909 | + }; | |
| 910 | + } | |
| 911 | + | |
| 912 | + if (schema.rules) { | |
| 913 | + if (Array.isArray(schema.rules)) { | |
| 914 | + var _rules = []; | |
| 915 | + schema.rules.forEach(function (item) { | |
| 916 | + if (item.required === true) { | |
| 917 | + if (isRequired) { | |
| 918 | + requiredRule = item; | |
| 919 | + } | |
| 920 | + } else { | |
| 921 | + _rules.push(processRule(item)); | |
| 922 | + } | |
| 923 | + }); | |
| 924 | + result = [singleResult].concat(_rules); | |
| 925 | + } else if (isObject(schema.rules)) { | |
| 926 | + // TODO: 规范上不允许rules是object,省一点事儿 | |
| 927 | + result = [singleResult, processRule(schema.rules)]; | |
| 928 | + } else { | |
| 929 | + result = singleResult; | |
| 930 | + } | |
| 931 | + } else { | |
| 932 | + result = singleResult; | |
| 933 | + } | |
| 934 | + | |
| 935 | + if (requiredRule) { | |
| 936 | + if (Array.isArray(result)) { | |
| 937 | + result.push(requiredRule); | |
| 938 | + } else if (isObject(result)) { | |
| 939 | + result = [result, requiredRule]; | |
| 940 | + } | |
| 941 | + } | |
| 942 | + | |
| 943 | + if (schema.format === 'image') { | |
| 944 | + var imgValidator = { | |
| 945 | + validator: function validator(rule, value) { | |
| 946 | + var pattern = /([/|.|w|s|-])*.(jpg|gif|png|bmp|apng|webp|jpeg|json)/; | |
| 947 | + if (!value) return true; // 这里判断宽一点,undefined、null、'' 都当做没有填写 | |
| 948 | + | |
| 949 | + return !!pattern.exec(value) || isUrl(value); | |
| 950 | + }, | |
| 951 | + message: '${title}的类型不是image' | |
| 952 | + }; | |
| 953 | + | |
| 954 | + if (Array.isArray(result)) { | |
| 955 | + result.push(imgValidator); | |
| 956 | + } else if (isObject(result)) { | |
| 957 | + result = [result, imgValidator]; | |
| 958 | + } | |
| 959 | + } | |
| 960 | + } | |
| 961 | + | |
| 962 | + return result; | |
| 963 | +}; // async-validator 产出的path没法用,转一下 | |
| 964 | +// "list.1.userName" => "list[1].userName" | |
| 965 | + | |
| 966 | +export var formatPathFromValidator = function formatPathFromValidator(err) { | |
| 967 | + var errArr = err.split('.'); | |
| 968 | + return errArr.map(function (item) { | |
| 969 | + if (isNaN(Number(item))) { | |
| 970 | + return item; | |
| 971 | + } else { | |
| 972 | + return "[".concat(item, "]"); | |
| 973 | + } | |
| 974 | + }).reduce(function (a, b) { | |
| 975 | + if (b[0] === '[' || a === '') { | |
| 976 | + return a + b; | |
| 977 | + } else { | |
| 978 | + return a + '.' + b; | |
| 979 | + } | |
| 980 | + }, ''); | |
| 981 | +}; // schema = { | |
| 982 | +// type: 'object', | |
| 983 | +// properties: { | |
| 984 | +// x: { | |
| 985 | +// type: 'object', | |
| 986 | +// properties: { | |
| 987 | +// y: { | |
| 988 | +// type: 'string', | |
| 989 | +// required: true, | |
| 990 | +// }, | |
| 991 | +// }, | |
| 992 | +// }, | |
| 993 | +// }, | |
| 994 | +// }; | |
| 995 | +// path = 'x.y' | |
| 996 | +// return {required: true, message?: 'xxxx'} | |
| 997 | + | |
| 998 | +export var isPathRequired = function isPathRequired(path, schema) { | |
| 999 | + var pathArr = path.split('.'); | |
| 1000 | + | |
| 1001 | + while (pathArr.length > 0) { | |
| 1002 | + var _pathArr = pathArr, | |
| 1003 | + _pathArr2 = _toArray(_pathArr), | |
| 1004 | + _path = _pathArr2[0], | |
| 1005 | + rest = _pathArr2.slice(1); | |
| 1006 | + | |
| 1007 | + _path = _path.split('[')[0]; | |
| 1008 | + var childSchema = void 0; | |
| 1009 | + | |
| 1010 | + if (isObjType(schema)) { | |
| 1011 | + childSchema = schema.properties[_path]; | |
| 1012 | + } else if (isListType(schema)) { | |
| 1013 | + childSchema = schema.items.properties[_path]; | |
| 1014 | + } | |
| 1015 | + | |
| 1016 | + pathArr = rest; | |
| 1017 | + | |
| 1018 | + if (childSchema) { | |
| 1019 | + return isPathRequired(rest.join('.'), childSchema); | |
| 1020 | + } // 单个的逻辑 | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + var result = { | |
| 1024 | + required: false | |
| 1025 | + }; | |
| 1026 | + | |
| 1027 | + if (schema.required === true) { | |
| 1028 | + result.required = true; | |
| 1029 | + } | |
| 1030 | + | |
| 1031 | + if (schema.rules) { | |
| 1032 | + var requiredItem = schema.rules.find(function (item) { | |
| 1033 | + return item.required; | |
| 1034 | + }); | |
| 1035 | + | |
| 1036 | + if (requiredItem) { | |
| 1037 | + result = requiredItem; | |
| 1038 | + } | |
| 1039 | + } | |
| 1040 | + | |
| 1041 | + return result; | |
| 1042 | + } | |
| 1043 | +}; // _path 只供内部递归使用 | |
| 1044 | + | |
| 1045 | +export var generateDataSkeleton = function generateDataSkeleton(schema, formData) { | |
| 1046 | + var _formData = clone(formData); | |
| 1047 | + | |
| 1048 | + var result = _formData; | |
| 1049 | + | |
| 1050 | + if (isObjType(schema)) { | |
| 1051 | + if (_formData === undefined || _typeof(_formData) !== 'object') { | |
| 1052 | + _formData = {}; | |
| 1053 | + result = {}; | |
| 1054 | + } | |
| 1055 | + | |
| 1056 | + Object.keys(schema.properties).forEach(function (key) { | |
| 1057 | + var childSchema = schema.properties[key]; | |
| 1058 | + var childData = _formData[key]; | |
| 1059 | + var childResult = generateDataSkeleton(childSchema, childData); | |
| 1060 | + result[key] = childResult; | |
| 1061 | + }); | |
| 1062 | + } else if (_formData !== undefined) {// result = _formData; | |
| 1063 | + } else { | |
| 1064 | + if (schema.default !== undefined) { | |
| 1065 | + result = clone(schema.default); | |
| 1066 | + } else if (schema.type === 'boolean' && !schema.widget) { | |
| 1067 | + // result = false; | |
| 1068 | + result = undefined; | |
| 1069 | + } else { | |
| 1070 | + result = undefined; | |
| 1071 | + } | |
| 1072 | + } | |
| 1073 | + | |
| 1074 | + return result; | |
| 1075 | +}; | |
| 1076 | +export var translateMessage = function translateMessage(msg, schema) { | |
| 1077 | + if (typeof msg !== 'string') { | |
| 1078 | + return ''; | |
| 1079 | + } | |
| 1080 | + | |
| 1081 | + if (!schema) return msg; | |
| 1082 | + msg = msg.replace('${title}', schema.title); | |
| 1083 | + msg = msg.replace('${type}', schema.format || schema.type); // 兼容代码 | |
| 1084 | + | |
| 1085 | + if (typeof schema.min === 'number') { | |
| 1086 | + msg = msg.replace('${min}', schema.min); | |
| 1087 | + } | |
| 1088 | + | |
| 1089 | + if (typeof schema.max === 'number') { | |
| 1090 | + msg = msg.replace('${max}', schema.max); | |
| 1091 | + } | |
| 1092 | + | |
| 1093 | + if (schema.rules) { | |
| 1094 | + var minRule = schema.rules.find(function (r) { | |
| 1095 | + return r.min !== undefined; | |
| 1096 | + }); | |
| 1097 | + | |
| 1098 | + if (minRule) { | |
| 1099 | + msg = msg.replace('${min}', minRule.min); | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + var maxRule = schema.rules.find(function (r) { | |
| 1103 | + return r.max !== undefined; | |
| 1104 | + }); | |
| 1105 | + | |
| 1106 | + if (maxRule) { | |
| 1107 | + msg = msg.replace('${max}', maxRule.max); | |
| 1108 | + } | |
| 1109 | + | |
| 1110 | + var lenRule = schema.rules.find(function (r) { | |
| 1111 | + return r.len !== undefined; | |
| 1112 | + }); | |
| 1113 | + | |
| 1114 | + if (lenRule) { | |
| 1115 | + msg = msg.replace('${len}', lenRule.len); | |
| 1116 | + } | |
| 1117 | + | |
| 1118 | + var patternRule = schema.rules.find(function (r) { | |
| 1119 | + return r.pattern !== undefined; | |
| 1120 | + }); | |
| 1121 | + | |
| 1122 | + if (patternRule) { | |
| 1123 | + msg = msg.replace('${pattern}', patternRule.pattern); | |
| 1124 | + } | |
| 1125 | + } | |
| 1126 | + | |
| 1127 | + return msg; | |
| 1128 | +}; // "objectName": { | |
| 1129 | +// "title": "对象", | |
| 1130 | +// "description": "这是一个对象类型", | |
| 1131 | +// "type": "object", | |
| 1132 | +// "properties": { | |
| 1133 | +// } | |
| 1134 | +// } | |
| 1135 | +// "listName": { | |
| 1136 | +// "title": "对象数组", | |
| 1137 | +// "description": "对象数组嵌套功能", | |
| 1138 | +// "type": "array", | |
| 1139 | +// "items": { | |
| 1140 | +// "type": "object", | |
| 1141 | +// "properties": { | |
| 1142 | +// } | |
| 1143 | +// } | |
| 1144 | +// } | |
| 1145 | + | |
| 1146 | +var changeSchema = function changeSchema(_schema, singleChange) { | |
| 1147 | + var schema = clone(_schema); | |
| 1148 | + schema = singleChange(schema); | |
| 1149 | + | |
| 1150 | + if (isObjType(schema)) { | |
| 1151 | + var requiredKeys = []; | |
| 1152 | + | |
| 1153 | + if (Array.isArray(schema.required)) { | |
| 1154 | + requiredKeys = schema.required; | |
| 1155 | + delete schema.required; | |
| 1156 | + } | |
| 1157 | + | |
| 1158 | + Object.keys(schema.properties).forEach(function (key) { | |
| 1159 | + var item = schema.properties[key]; | |
| 1160 | + | |
| 1161 | + if (requiredKeys.indexOf(key) > -1) { | |
| 1162 | + item.required = true; | |
| 1163 | + } | |
| 1164 | + | |
| 1165 | + schema.properties[key] = changeSchema(item, singleChange); | |
| 1166 | + }); | |
| 1167 | + } else if (isListType(schema)) { | |
| 1168 | + Object.keys(schema.items.properties).forEach(function (key) { | |
| 1169 | + var item = schema.items.properties[key]; | |
| 1170 | + schema.items.properties[key] = changeSchema(item, singleChange); | |
| 1171 | + }); | |
| 1172 | + } | |
| 1173 | + | |
| 1174 | + return schema; | |
| 1175 | +}; | |
| 1176 | + | |
| 1177 | +export var updateSchemaToNewVersion = function updateSchemaToNewVersion(schema) { | |
| 1178 | + return changeSchema(schema, updateSingleSchema); | |
| 1179 | +}; | |
| 1180 | + | |
| 1181 | +var updateSingleSchema = function updateSingleSchema(schema) { | |
| 1182 | + try { | |
| 1183 | + schema.rules = schema.rules || []; | |
| 1184 | + schema.props = schema.props || {}; | |
| 1185 | + | |
| 1186 | + if (schema['ui:options']) { | |
| 1187 | + schema.props = schema['ui:options']; | |
| 1188 | + delete schema['ui:options']; | |
| 1189 | + } | |
| 1190 | + | |
| 1191 | + if (schema.pattern) { | |
| 1192 | + var validItem = { | |
| 1193 | + pattern: schema.pattern | |
| 1194 | + }; | |
| 1195 | + | |
| 1196 | + if (schema.message && schema.message.pattern) { | |
| 1197 | + validItem.message = schema.message.pattern; | |
| 1198 | + } | |
| 1199 | + | |
| 1200 | + schema.rules.push(validItem); | |
| 1201 | + delete schema.pattern; | |
| 1202 | + delete schema.message; | |
| 1203 | + } // min / max | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + if (schema.minLength) { | |
| 1207 | + schema.min = schema.minLength; | |
| 1208 | + delete schema.minLength; | |
| 1209 | + } | |
| 1210 | + | |
| 1211 | + if (schema.maxLength) { | |
| 1212 | + schema.max = schema.maxLength; | |
| 1213 | + delete schema.maxLength; | |
| 1214 | + } | |
| 1215 | + | |
| 1216 | + if (schema.minItems) { | |
| 1217 | + schema.min = schema.minItems; | |
| 1218 | + delete schema.minItems; | |
| 1219 | + } | |
| 1220 | + | |
| 1221 | + if (schema.maxItems) { | |
| 1222 | + schema.max = schema.maxItems; | |
| 1223 | + delete schema.maxItems; | |
| 1224 | + } | |
| 1225 | + | |
| 1226 | + if (schema.step) { | |
| 1227 | + schema.props.step = schema.step; | |
| 1228 | + delete schema.step; | |
| 1229 | + } // ui:xxx | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + if (schema['ui:className']) { | |
| 1233 | + schema.className = schema['ui:className']; | |
| 1234 | + delete schema['ui:className']; | |
| 1235 | + } | |
| 1236 | + | |
| 1237 | + if (schema['ui:hidden']) { | |
| 1238 | + schema.hidden = schema['ui:hidden']; | |
| 1239 | + delete schema['ui:hidden']; | |
| 1240 | + } | |
| 1241 | + | |
| 1242 | + if (schema['ui:readonly']) { | |
| 1243 | + schema.readOnly = schema['ui:readonly']; // 改成驼峰了 | |
| 1244 | + | |
| 1245 | + delete schema['ui:readonly']; | |
| 1246 | + } | |
| 1247 | + | |
| 1248 | + if (schema['ui:disabled']) { | |
| 1249 | + schema.disabled = schema['ui:disabled']; | |
| 1250 | + delete schema['ui:disabled']; | |
| 1251 | + } | |
| 1252 | + | |
| 1253 | + if (schema['ui:width']) { | |
| 1254 | + schema.width = schema['ui:width']; | |
| 1255 | + delete schema['ui:width']; | |
| 1256 | + } | |
| 1257 | + | |
| 1258 | + if (schema['ui:displayType']) { | |
| 1259 | + schema.displayType = schema['ui:displayType']; | |
| 1260 | + delete schema['ui:displayType']; | |
| 1261 | + } | |
| 1262 | + | |
| 1263 | + if (schema['ui:column']) { | |
| 1264 | + schema.column = schema['ui:column']; | |
| 1265 | + delete schema['ui:column']; | |
| 1266 | + } | |
| 1267 | + | |
| 1268 | + if (schema['ui:widget']) { | |
| 1269 | + schema.widget = schema['ui:widget']; | |
| 1270 | + delete schema['ui:widget']; | |
| 1271 | + } | |
| 1272 | + | |
| 1273 | + if (schema['ui:labelWidth']) { | |
| 1274 | + schema.labelWidth = schema['ui:labelWidth']; | |
| 1275 | + delete schema['ui:labelWidth']; | |
| 1276 | + } | |
| 1277 | + | |
| 1278 | + if (schema.rules && schema.rules.length === 0) { | |
| 1279 | + delete schema.rules; | |
| 1280 | + } | |
| 1281 | + | |
| 1282 | + if (typeof schema.props === 'function' || isObject(schema.props) && Object.keys(schema.props).length > 0) {} else { | |
| 1283 | + delete schema.props; | |
| 1284 | + } | |
| 1285 | + | |
| 1286 | + return schema; | |
| 1287 | + } catch (error) { | |
| 1288 | + console.error('schema转换失败!', error); | |
| 1289 | + return schema; | |
| 1290 | + } | |
| 1291 | +}; // 检验一个string是 function(传统活箭头函数) | |
| 1292 | + | |
| 1293 | + | |
| 1294 | +export var parseFunctionString = function parseFunctionString(string) { | |
| 1295 | + if (typeof string !== 'string') return false; | |
| 1296 | + var reg1 = /^{{(function.+)}}$/; | |
| 1297 | + var reg2 = /^{{(.+=>.+)}}$/; | |
| 1298 | + | |
| 1299 | + if (string.match(reg1)) { | |
| 1300 | + return string.match(reg1)[1]; | |
| 1301 | + } | |
| 1302 | + | |
| 1303 | + if (string.match(reg2)) { | |
| 1304 | + return string.match(reg2)[1]; | |
| 1305 | + } | |
| 1306 | + | |
| 1307 | + return false; | |
| 1308 | +}; | |
| 1309 | +export var completeSchemaWithTheme = function completeSchemaWithTheme() { | |
| 1310 | + var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 1311 | + var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 1312 | + var result = {}; | |
| 1313 | + | |
| 1314 | + if (isObject(schema)) { | |
| 1315 | + if (schema.theme && theme[schema.theme]) { | |
| 1316 | + result = _objectSpread(_objectSpread({}, schema), theme[schema.theme]); | |
| 1317 | + } | |
| 1318 | + | |
| 1319 | + Object.keys(schema).forEach(function (key) { | |
| 1320 | + result[key] = completeSchemaWithTheme(schema[key], theme); | |
| 1321 | + }); | |
| 1322 | + } else { | |
| 1323 | + result = schema; | |
| 1324 | + } | |
| 1325 | + | |
| 1326 | + return result; | |
| 1327 | +}; | |
| 1328 | +export var cleanEmpty = function cleanEmpty(obj) { | |
| 1329 | + if (Array.isArray(obj)) { | |
| 1330 | + return obj.map(function (v) { | |
| 1331 | + return v && isObject(v) ? cleanEmpty(v) : v; | |
| 1332 | + }).filter(function (v) { | |
| 1333 | + return !(v == undefined); | |
| 1334 | + }); | |
| 1335 | + } else if (isObject(obj)) { | |
| 1336 | + return Object.entries(obj).map(function (_ref6) { | |
| 1337 | + var _ref7 = _slicedToArray(_ref6, 2), | |
| 1338 | + k = _ref7[0], | |
| 1339 | + v = _ref7[1]; | |
| 1340 | + | |
| 1341 | + return [k, v && isObject(v) ? cleanEmpty(v) : v]; | |
| 1342 | + }).reduce(function (a, _ref8) { | |
| 1343 | + var _ref9 = _slicedToArray(_ref8, 2), | |
| 1344 | + k = _ref9[0], | |
| 1345 | + v = _ref9[1]; | |
| 1346 | + | |
| 1347 | + return v == undefined ? a : (a[k] = v, a); | |
| 1348 | + }, {}); | |
| 1349 | + } else { | |
| 1350 | + return obj; | |
| 1351 | + } | |
| 1352 | +}; | |
| 1353 | +export var removeHiddenFromResult = function removeHiddenFromResult(data, flatten) { | |
| 1354 | + Object.keys(flatten).forEach(function (key) { | |
| 1355 | + var hidden = flatten[key].schema && flatten[key].schema.hidden === true; // Remark: 有表达式的情况, 暂时不去掉了(有业务反而是希望留下的),就去掉 hidden = true 的 | |
| 1356 | + | |
| 1357 | + if (get(data, key) !== undefined && hidden) { | |
| 1358 | + set(data, key, undefined); | |
| 1359 | + } | |
| 1360 | + }); | |
| 1361 | + return data; | |
| 1362 | +}; | |
| 1363 | +export function msToTime(duration) { | |
| 1364 | + var seconds = Math.floor(duration / 1000 % 60); | |
| 1365 | + var minutes = Math.floor(duration / (1000 * 60) % 60); | |
| 1366 | + var hours = Math.floor(duration / (1000 * 60 * 60) % 24); | |
| 1367 | + hours = hours < 10 ? '0' + hours : hours; | |
| 1368 | + minutes = minutes < 10 ? '0' + minutes : minutes; | |
| 1369 | + seconds = seconds < 10 ? '0' + seconds : seconds; | |
| 1370 | + return hours + ':' + minutes + ':' + seconds; | |
| 1371 | +} | |
| 1372 | +export function yymmdd(timeStamp) { | |
| 1373 | + var date_ob = new Date(Number(timeStamp)); | |
| 1374 | + | |
| 1375 | + var adjustZero = function adjustZero(num) { | |
| 1376 | + return ('0' + num).slice(-2); | |
| 1377 | + }; | |
| 1378 | + | |
| 1379 | + var day = adjustZero(date_ob.getDate()); | |
| 1380 | + var month = adjustZero(date_ob.getMonth()); | |
| 1381 | + var year = date_ob.getFullYear(); | |
| 1382 | + var hours = adjustZero(date_ob.getHours()); | |
| 1383 | + var minutes = adjustZero(date_ob.getMinutes()); | |
| 1384 | + var seconds = adjustZero(date_ob.getSeconds()); | |
| 1385 | + return "".concat(year, "-").concat(month, "-").concat(day, " ").concat(hours, ":").concat(minutes, ":").concat(seconds); | |
| 55 | 1386 | } |
| \ No newline at end of file | ... | ... |
es/validateMessage.js
renamed from
es/form-render-core/src/validateMessage.js
es/validateMessageCN.js
renamed from
es/form-render-core/src/validateMessageCN.js
es/validator.js
0 → 100644
| 1 | +function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 2 | + | |
| 3 | +function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 4 | + | |
| 5 | +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 6 | + | |
| 7 | +function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 8 | + | |
| 9 | +function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 10 | + | |
| 11 | +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 12 | + | |
| 13 | +/* eslint-disable react-hooks/exhaustive-deps */ | |
| 14 | +import { getDescriptorFromSchema, formatPathFromValidator, isPathRequired, getArray, getSchemaFromFlatten } from './utils'; | |
| 15 | +import { defaultValidateMessagesCN } from './validateMessageCN'; | |
| 16 | +import { defaultValidateMessages } from './validateMessage'; | |
| 17 | +import Validator from 'async-validator'; | |
| 18 | +import { get, merge } from 'lodash-es'; | |
| 19 | +export var validateAll = function validateAll(_ref) { | |
| 20 | + var formData = _ref.formData, | |
| 21 | + flatten = _ref.flatten, | |
| 22 | + _ref$isRequired = _ref.isRequired, | |
| 23 | + isRequired = _ref$isRequired === void 0 ? true : _ref$isRequired, | |
| 24 | + _ref$touchedKeys = _ref.touchedKeys, | |
| 25 | + touchedKeys = _ref$touchedKeys === void 0 ? [] : _ref$touchedKeys, | |
| 26 | + _ref$locale = _ref.locale, | |
| 27 | + locale = _ref$locale === void 0 ? 'cn' : _ref$locale, | |
| 28 | + _ref$validateMessages = _ref.validateMessages, | |
| 29 | + validateMessages = _ref$validateMessages === void 0 ? {} : _ref$validateMessages; | |
| 30 | + | |
| 31 | + var _schema = getSchemaFromFlatten(flatten); | |
| 32 | + | |
| 33 | + if (Object.keys(_schema).length === 0) return Promise.resolve(); | |
| 34 | + var descriptor = getDescriptorFromSchema({ | |
| 35 | + schema: _schema, | |
| 36 | + isRequired: isRequired | |
| 37 | + }).fields; | |
| 38 | + window.descriptor = descriptor; // console.log(descriptor, '&&&& descriptor', formData); | |
| 39 | + | |
| 40 | + var touchVerifyList = []; // 如果是最后的校验,所有key都touch了,就不用算这个了 | |
| 41 | + // 因为要整个构建validator在list的情况太复杂了,所以required单独拿出来处理,但是这边有不少单独处理逻辑,例如message | |
| 42 | + | |
| 43 | + if (!isRequired) { | |
| 44 | + touchedKeys.forEach(function (key) { | |
| 45 | + var keyRequired = isPathRequired(key, _schema); | |
| 46 | + var val = get(formData, key); | |
| 47 | + var nullValue = [undefined, null, ''].indexOf(val) > -1; // 注意 0 不是 | |
| 48 | + | |
| 49 | + var isEmptyMultiSelect = Array.isArray(val) && val.length === 0; | |
| 50 | + | |
| 51 | + if ((nullValue || isEmptyMultiSelect) && keyRequired.required) { | |
| 52 | + var _message = keyRequired.message || validateMessages.required || '${title}必填'; | |
| 53 | + | |
| 54 | + touchVerifyList.push({ | |
| 55 | + name: key, | |
| 56 | + error: [_message] | |
| 57 | + }); | |
| 58 | + } | |
| 59 | + }); | |
| 60 | + } | |
| 61 | + | |
| 62 | + var cn = defaultValidateMessagesCN; | |
| 63 | + var en = defaultValidateMessages; // TODO: 有些情况会出现没有rules,需要看一下,先兜底 | |
| 64 | + | |
| 65 | + var validator; | |
| 66 | + | |
| 67 | + try { | |
| 68 | + validator = new Validator(descriptor); | |
| 69 | + } catch (error) { | |
| 70 | + return Promise.resolve([]); | |
| 71 | + } | |
| 72 | + | |
| 73 | + var messageFeed = locale === 'en' ? en : cn; | |
| 74 | + merge(messageFeed, validateMessages); | |
| 75 | + validator.messages(messageFeed); | |
| 76 | + return validator.validate(formData || {}).then(function (res) { | |
| 77 | + if (touchVerifyList.length > 0) return touchVerifyList; | |
| 78 | + return []; | |
| 79 | + }).catch(function (_ref2) { | |
| 80 | + var errors = _ref2.errors, | |
| 81 | + fields = _ref2.fields; | |
| 82 | + // error的name改成正常的path | |
| 83 | + var normalizedErrors = getArray(errors).map(function (err) { | |
| 84 | + var _path = formatPathFromValidator(err.field); | |
| 85 | + | |
| 86 | + return { | |
| 87 | + name: _path, | |
| 88 | + error: [err.message] | |
| 89 | + }; | |
| 90 | + }); // 添加touched的required | |
| 91 | + | |
| 92 | + normalizedErrors = [].concat(_toConsumableArray(normalizedErrors), touchVerifyList); // 合并同名的error | |
| 93 | + | |
| 94 | + var _errorFields = []; | |
| 95 | + normalizedErrors.forEach(function (item) { | |
| 96 | + var matchIndex = _errorFields.findIndex(function (ele) { | |
| 97 | + return ele.name === item.name; | |
| 98 | + }); | |
| 99 | + | |
| 100 | + if (matchIndex === -1) { | |
| 101 | + _errorFields.push(item); | |
| 102 | + } else { | |
| 103 | + _errorFields[matchIndex].error = [].concat(_toConsumableArray(_errorFields[matchIndex].error), _toConsumableArray(item.error)); | |
| 104 | + } | |
| 105 | + }); | |
| 106 | + return _errorFields; | |
| 107 | + }); | |
| 108 | +}; // useEffect(() => { | |
| 109 | +// const descriptor = { | |
| 110 | +// roles: { | |
| 111 | +// type: 'array', | |
| 112 | +// required: true, | |
| 113 | +// defaultField: { | |
| 114 | +// type: 'object', | |
| 115 | +// fields: { | |
| 116 | +// street: { type: 'string', required: true }, | |
| 117 | +// city: { type: 'string', required: true, message: 'invalid zip' }, | |
| 118 | +// zip: [ | |
| 119 | +// { | |
| 120 | +// type: 'string', | |
| 121 | +// required: true, | |
| 122 | +// message: 'invalid zip', | |
| 123 | +// }, | |
| 124 | +// { | |
| 125 | +// len: 8, | |
| 126 | +// message: 'xxx', | |
| 127 | +// }, | |
| 128 | +// ], | |
| 129 | +// res: { | |
| 130 | +// type: 'array', | |
| 131 | +// required: true, | |
| 132 | +// defaultField: { | |
| 133 | +// type: 'object', | |
| 134 | +// fields: { | |
| 135 | +// city: { type: 'string', required: true }, | |
| 136 | +// }, | |
| 137 | +// }, | |
| 138 | +// }, | |
| 139 | +// }, | |
| 140 | +// }, | |
| 141 | +// }, | |
| 142 | +// name: { | |
| 143 | +// type: 'string', | |
| 144 | +// required: true, | |
| 145 | +// validator: (rule, value) => value === 'muji', | |
| 146 | +// }, | |
| 147 | +// age: { | |
| 148 | +// type: 'number', | |
| 149 | +// min: 12, | |
| 150 | +// max: 13, | |
| 151 | +// message: 'asdfadsf', | |
| 152 | +// // asyncValidator: (rule, value) => { | |
| 153 | +// // return new Promise((resolve, reject) => { | |
| 154 | +// // if (value < 18) { | |
| 155 | +// // reject('too young'); // reject with error message | |
| 156 | +// // } else { | |
| 157 | +// // resolve(); | |
| 158 | +// // } | |
| 159 | +// // }); | |
| 160 | +// // }, | |
| 161 | +// }, | |
| 162 | +// }; | |
| 163 | +// const validator = new Validator(descriptor); | |
| 164 | +// // validator.validate({ name: 'muji' }, (errors, fields) => { | |
| 165 | +// // if (errors) { | |
| 166 | +// // // validation failed, errors is an array of all errors | |
| 167 | +// // // fields is an object keyed by field name with an array of | |
| 168 | +// // // errors per field | |
| 169 | +// // return handleErrors(errors, fields); | |
| 170 | +// // } | |
| 171 | +// // // validation passed | |
| 172 | +// // }); | |
| 173 | +// validator | |
| 174 | +// .validate({ name: 'muji', age: 16, roles: [{ zip: '123', res: [{}] }] }) | |
| 175 | +// .then(() => { | |
| 176 | +// // validation passed or without error message | |
| 177 | +// }) | |
| 178 | +// .catch(({ errors, fields }) => { | |
| 179 | +// return handleErrors(errors, fields); | |
| 180 | +// }); | |
| 181 | +// }, []); | |
| 182 | + | |
| 183 | +var d3 = { | |
| 184 | + list: { | |
| 185 | + type: 'array', | |
| 186 | + defaultField: { | |
| 187 | + type: 'object', | |
| 188 | + fields: { | |
| 189 | + userName: [{ | |
| 190 | + bind: false, | |
| 191 | + title: 'User Name', | |
| 192 | + type: 'string', | |
| 193 | + rules: [{ | |
| 194 | + min: 5, | |
| 195 | + message: '长度需要大于5' | |
| 196 | + }, { | |
| 197 | + max: 12 | |
| 198 | + }] | |
| 199 | + }, { | |
| 200 | + min: 5, | |
| 201 | + message: '长度需要大于5' | |
| 202 | + }, { | |
| 203 | + max: 12 | |
| 204 | + }], | |
| 205 | + selectName: { | |
| 206 | + title: '单选', | |
| 207 | + type: 'string', | |
| 208 | + enum: ['a', 'b', 'c'], | |
| 209 | + enumNames: ['早', '中', '晚'], | |
| 210 | + required: true | |
| 211 | + }, | |
| 212 | + checkboxName: { | |
| 213 | + title: '是否判断', | |
| 214 | + type: 'boolean', | |
| 215 | + valuePropName: 'checked' | |
| 216 | + } | |
| 217 | + } | |
| 218 | + } | |
| 219 | + } | |
| 220 | +}; | |
| 221 | +var d2 = { | |
| 222 | + list: { | |
| 223 | + type: 'array', | |
| 224 | + defaultField: { | |
| 225 | + type: 'object', | |
| 226 | + fields: { | |
| 227 | + userName: [{ | |
| 228 | + bind: false, | |
| 229 | + title: 'User Name', | |
| 230 | + type: 'string', | |
| 231 | + rules: [{ | |
| 232 | + min: 5, | |
| 233 | + message: '长度需要大于5' | |
| 234 | + }, { | |
| 235 | + max: 12 | |
| 236 | + }] | |
| 237 | + }, { | |
| 238 | + min: 5, | |
| 239 | + message: '长度需要大于5' | |
| 240 | + }, { | |
| 241 | + max: 12 | |
| 242 | + }], | |
| 243 | + selectName: { | |
| 244 | + title: '单选', | |
| 245 | + type: 'string', | |
| 246 | + enum: ['a', 'b', 'c'], | |
| 247 | + enumNames: ['早', '中', '晚'], | |
| 248 | + required: true | |
| 249 | + }, | |
| 250 | + checkboxName: { | |
| 251 | + title: '是否判断', | |
| 252 | + type: 'boolean', | |
| 253 | + valuePropName: 'checked' | |
| 254 | + } | |
| 255 | + } | |
| 256 | + } | |
| 257 | + } | |
| 258 | +}; | |
| \ No newline at end of file | ... | ... |
| ... | ... | @@ -71,7 +71,7 @@ var DateRange = function DateRange(_ref) { |
| 71 | 71 | var emptyList2 = stringList[0] === undefined || stringList[1] === undefined; |
| 72 | 72 | |
| 73 | 73 | if (emptyList1 || emptyList2) { |
| 74 | - onChange(null); | |
| 74 | + onChange(undefined); | |
| 75 | 75 | } else { |
| 76 | 76 | onChange(stringList); |
| 77 | 77 | } | ... | ... |
| ... | ... | @@ -4,31 +4,28 @@ import "antd/es/switch/style"; |
| 4 | 4 | import _Switch from "antd/es/switch"; |
| 5 | 5 | import "antd/es/checkbox/style"; |
| 6 | 6 | import _Checkbox from "antd/es/checkbox"; |
| 7 | -import "antd/es/tree-select/style"; | |
| 8 | -import _TreeSelect from "antd/es/tree-select"; | |
| 9 | 7 | import "antd/es/input-number/style"; |
| 10 | 8 | import _InputNumber from "antd/es/input-number"; |
| 11 | 9 | import "antd/es/input/style"; |
| 12 | 10 | import _Input from "antd/es/input"; |
| 13 | -var _excluded = ["style"], | |
| 14 | - _excluded2 = ["autoSize"], | |
| 15 | - _excluded3 = ["style"]; | |
| 11 | +var _excluded = ["style"]; | |
| 16 | 12 | |
| 17 | 13 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } |
| 18 | 14 | |
| 15 | +function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 16 | + | |
| 17 | +function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 18 | + | |
| 19 | 19 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } |
| 20 | 20 | |
| 21 | 21 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } |
| 22 | 22 | |
| 23 | 23 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 24 | 24 | |
| 25 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 26 | - | |
| 27 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 28 | - | |
| 29 | 25 | import React from "react"; |
| 30 | 26 | import list from './list'; |
| 31 | 27 | import map from './map'; |
| 28 | +import { createWidget } from '../../createWidget'; | |
| 32 | 29 | import ImageInput from './imageInput'; |
| 33 | 30 | import urlInput from './urlInput'; |
| 34 | 31 | import Html from './html'; |
| ... | ... | @@ -40,39 +37,42 @@ import time from './time'; |
| 40 | 37 | import date from './date'; |
| 41 | 38 | import dateRange from './dateRange'; |
| 42 | 39 | import timeRange from './timeRange'; |
| 43 | -import color from './color'; | |
| 44 | -import slider from './slider'; | |
| 45 | -import upload from './upload'; // const Cascader = React.lazy(() => import('antd/es/cascader')); | |
| 46 | - | |
| 40 | +var TreeSelect = React.lazy(function () { | |
| 41 | + return import('antd/es/tree-select'); | |
| 42 | +}); // const Cascader = React.lazy(() => import('antd/es/cascader')); | |
| 43 | + | |
| 44 | +var color = React.lazy(function () { | |
| 45 | + return import('./color'); | |
| 46 | +}); | |
| 47 | +var slider = React.lazy(function () { | |
| 48 | + return import('./slider'); | |
| 49 | +}); | |
| 50 | +var upload = React.lazy(function () { | |
| 51 | + return import('./upload'); | |
| 52 | +}); | |
| 47 | 53 | var TextArea = _Input.TextArea; |
| 48 | - | |
| 49 | -var FrNumber = function FrNumber(_ref) { | |
| 50 | - var style = _ref.style, | |
| 51 | - rest = _objectWithoutProperties(_ref, _excluded); | |
| 52 | - | |
| 53 | - return /*#__PURE__*/React.createElement(_InputNumber, _extends({ | |
| 54 | +var FrNumber = createWidget(function (_ref) { | |
| 55 | + var style = _ref.style; | |
| 56 | + return { | |
| 54 | 57 | style: _objectSpread({ |
| 55 | 58 | width: '100%' |
| 56 | 59 | }, style) |
| 57 | - }, rest)); | |
| 58 | -}; | |
| 59 | - | |
| 60 | -var FrTextArea = function FrTextArea(_ref2) { | |
| 61 | - var autoSize = _ref2.autoSize, | |
| 62 | - rest = _objectWithoutProperties(_ref2, _excluded2); | |
| 63 | - | |
| 64 | - return /*#__PURE__*/React.createElement(TextArea, _extends({ | |
| 60 | + }; | |
| 61 | +})(_InputNumber); | |
| 62 | +var FrTextArea = createWidget(function (_ref2) { | |
| 63 | + var autoSize = _ref2.autoSize; | |
| 64 | + return { | |
| 65 | 65 | autoSize: autoSize ? autoSize : { |
| 66 | 66 | minRows: 3 |
| 67 | 67 | } |
| 68 | - }, rest)); | |
| 69 | -}; | |
| 68 | + }; | |
| 69 | +})(TextArea); | |
| 70 | 70 | |
| 71 | 71 | var FrTreeSelect = function FrTreeSelect(_ref3) { |
| 72 | 72 | var style = _ref3.style, |
| 73 | - rest = _objectWithoutProperties(_ref3, _excluded3); | |
| 73 | + rest = _objectWithoutProperties(_ref3, _excluded); | |
| 74 | 74 | |
| 75 | - return /*#__PURE__*/React.createElement(_TreeSelect, _extends({ | |
| 75 | + return /*#__PURE__*/React.createElement(TreeSelect, _extends({ | |
| 76 | 76 | style: _objectSpread({ |
| 77 | 77 | width: '100%' |
| 78 | 78 | }, style) | ... | ... |
es/widgets/antd/listCard.js
deleted
100644 → 0
| 1 | -import "antd/es/button/style"; | |
| 2 | -import _Button from "antd/es/button"; | |
| 3 | -import "antd/es/popconfirm/style"; | |
| 4 | -import _Popconfirm from "antd/es/popconfirm"; | |
| 5 | -import _CloseOutlined from "@ant-design/icons/lib/icons/CloseOutlined"; | |
| 6 | -import _CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined"; | |
| 7 | -import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; | |
| 8 | -import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; | |
| 9 | - | |
| 10 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 11 | - | |
| 12 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 13 | - | |
| 14 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 15 | - | |
| 16 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 17 | - | |
| 18 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 19 | - | |
| 20 | -/* eslint-disable jsx-a11y/anchor-is-valid */ | |
| 21 | -import React from 'react'; | |
| 22 | -import Core from '../../index'; | |
| 23 | - | |
| 24 | -var CardList = function CardList(_ref) { | |
| 25 | - var _ref$displayList = _ref.displayList, | |
| 26 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 27 | - listData = _ref.listData, | |
| 28 | - changeList = _ref.changeList, | |
| 29 | - schema = _ref.schema, | |
| 30 | - deleteItem = _ref.deleteItem, | |
| 31 | - copyItem = _ref.copyItem, | |
| 32 | - addItem = _ref.addItem, | |
| 33 | - moveItemUp = _ref.moveItemUp, | |
| 34 | - moveItemDown = _ref.moveItemDown, | |
| 35 | - displayType = _ref.displayType, | |
| 36 | - getFieldsProps = _ref.getFieldsProps; | |
| 37 | - var _schema$props = schema.props, | |
| 38 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 39 | - itemProps = schema.itemProps; | |
| 40 | - var addBtnProps = { | |
| 41 | - type: 'dashed', | |
| 42 | - children: '新增一条' | |
| 43 | - }; | |
| 44 | - | |
| 45 | - if (props.addBtnProps && _typeof(props.addBtnProps) === 'object') { | |
| 46 | - addBtnProps = _objectSpread(_objectSpread({}, addBtnProps), props.addBtnProps); | |
| 47 | - } | |
| 48 | - | |
| 49 | - return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", { | |
| 50 | - className: "fr-card-list" | |
| 51 | - }, displayList.map(function (item, idx) { | |
| 52 | - var fieldsProps = getFieldsProps(idx); | |
| 53 | - fieldsProps.displayType = displayType; | |
| 54 | - return /*#__PURE__*/React.createElement("div", { | |
| 55 | - className: "fr-card-item ".concat(displayType === 'row' ? 'fr-card-item-row' : ''), | |
| 56 | - key: idx | |
| 57 | - }, /*#__PURE__*/React.createElement("div", { | |
| 58 | - className: "fr-card-index" | |
| 59 | - }, idx + 1), /*#__PURE__*/React.createElement(Core, fieldsProps), /*#__PURE__*/React.createElement("div", { | |
| 60 | - direction: "horizontal", | |
| 61 | - className: "fr-card-toolbar" | |
| 62 | - }, !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 63 | - style: { | |
| 64 | - fontSize: 16, | |
| 65 | - marginLeft: 4 | |
| 66 | - }, | |
| 67 | - onClick: function onClick() { | |
| 68 | - return moveItemUp(idx); | |
| 69 | - } | |
| 70 | - }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 71 | - style: { | |
| 72 | - fontSize: 16, | |
| 73 | - marginLeft: 4 | |
| 74 | - }, | |
| 75 | - onClick: function onClick() { | |
| 76 | - return moveItemDown(idx); | |
| 77 | - } | |
| 78 | - })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement(_CopyOutlined, { | |
| 79 | - style: { | |
| 80 | - fontSize: 16, | |
| 81 | - marginLeft: 8 | |
| 82 | - }, | |
| 83 | - onClick: function onClick() { | |
| 84 | - return copyItem(idx); | |
| 85 | - } | |
| 86 | - }), !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 87 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 88 | - onConfirm: function onConfirm() { | |
| 89 | - return deleteItem(idx); | |
| 90 | - }, | |
| 91 | - okText: "\u786E\u5B9A", | |
| 92 | - cancelText: "\u53D6\u6D88" | |
| 93 | - }, /*#__PURE__*/React.createElement(_CloseOutlined, { | |
| 94 | - style: { | |
| 95 | - fontSize: 16, | |
| 96 | - marginLeft: 8 | |
| 97 | - } | |
| 98 | - })))); | |
| 99 | - })), /*#__PURE__*/React.createElement("div", { | |
| 100 | - style: { | |
| 101 | - marginTop: displayList.length > 0 ? 0 : 8 | |
| 102 | - } | |
| 103 | - }, !props.hideAdd && /*#__PURE__*/React.createElement(_Button, _extends({ | |
| 104 | - onClick: addItem | |
| 105 | - }, addBtnProps)), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 106 | - var callback = item.callback, | |
| 107 | - text = item.text, | |
| 108 | - html = item.html; | |
| 109 | - | |
| 110 | - var onClick = function onClick() { | |
| 111 | - console.log({ | |
| 112 | - value: listData, | |
| 113 | - onChange: changeList, | |
| 114 | - schema: schema | |
| 115 | - }); | |
| 116 | - }; | |
| 117 | - | |
| 118 | - if (typeof window[callback] === 'function') { | |
| 119 | - onClick = function onClick() { | |
| 120 | - window[callback]({ | |
| 121 | - value: listData, | |
| 122 | - onChange: changeList, | |
| 123 | - schema: schema | |
| 124 | - }); | |
| 125 | - }; | |
| 126 | - } | |
| 127 | - | |
| 128 | - return /*#__PURE__*/React.createElement(_Button, { | |
| 129 | - key: idx.toString(), | |
| 130 | - style: { | |
| 131 | - marginLeft: 8 | |
| 132 | - }, | |
| 133 | - type: "dashed", | |
| 134 | - onClick: onClick | |
| 135 | - }, /*#__PURE__*/React.createElement("span", { | |
| 136 | - dangerouslySetInnerHTML: { | |
| 137 | - __html: html || text | |
| 138 | - } | |
| 139 | - })); | |
| 140 | - }) : null)); | |
| 141 | -}; | |
| 142 | - | |
| 143 | -export default CardList; | |
| \ No newline at end of file |
es/widgets/antd/listSimple.js
deleted
100644 → 0
| 1 | -import "antd/es/button/style"; | |
| 2 | -import _Button from "antd/es/button"; | |
| 3 | -import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; | |
| 4 | -import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; | |
| 5 | -import _CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined"; | |
| 6 | -import "antd/es/popconfirm/style"; | |
| 7 | -import _Popconfirm from "antd/es/popconfirm"; | |
| 8 | -import _DeleteOutlined from "@ant-design/icons/lib/icons/DeleteOutlined"; | |
| 9 | - | |
| 10 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 11 | - | |
| 12 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 13 | - | |
| 14 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 15 | - | |
| 16 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 17 | - | |
| 18 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 19 | - | |
| 20 | -import React from 'react'; | |
| 21 | -import Core from '../../index'; | |
| 22 | - | |
| 23 | -var SimpleList = function SimpleList(_ref) { | |
| 24 | - var schema = _ref.schema, | |
| 25 | - _ref$displayList = _ref.displayList, | |
| 26 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 27 | - listData = _ref.listData, | |
| 28 | - changeList = _ref.changeList, | |
| 29 | - deleteItem = _ref.deleteItem, | |
| 30 | - addItem = _ref.addItem, | |
| 31 | - copyItem = _ref.copyItem, | |
| 32 | - moveItemUp = _ref.moveItemUp, | |
| 33 | - moveItemDown = _ref.moveItemDown, | |
| 34 | - getFieldsProps = _ref.getFieldsProps; | |
| 35 | - var _schema$props = schema.props, | |
| 36 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 37 | - itemProps = schema.itemProps; | |
| 38 | - var addBtnProps = { | |
| 39 | - type: 'dashed', | |
| 40 | - children: '新增一条' | |
| 41 | - }; | |
| 42 | - | |
| 43 | - if (props.addBtnProps && _typeof(props.addBtnProps) === 'object') { | |
| 44 | - addBtnProps = _objectSpread(_objectSpread({}, addBtnProps), props.addBtnProps); | |
| 45 | - } | |
| 46 | - | |
| 47 | - return /*#__PURE__*/React.createElement("div", { | |
| 48 | - className: "fr-list-1" | |
| 49 | - }, displayList.map(function (item, idx) { | |
| 50 | - var fieldsProps = getFieldsProps(idx); | |
| 51 | - fieldsProps.displayType = 'inline'; | |
| 52 | - | |
| 53 | - if (props.hideTitle) { | |
| 54 | - fieldsProps.hideTitle = true; | |
| 55 | - } | |
| 56 | - | |
| 57 | - return /*#__PURE__*/React.createElement("div", { | |
| 58 | - key: idx, | |
| 59 | - style: { | |
| 60 | - display: 'flex' | |
| 61 | - } | |
| 62 | - }, /*#__PURE__*/React.createElement(Core, fieldsProps), /*#__PURE__*/React.createElement("div", { | |
| 63 | - style: { | |
| 64 | - marginTop: 6 | |
| 65 | - } | |
| 66 | - }, !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 67 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 68 | - onConfirm: function onConfirm() { | |
| 69 | - return deleteItem(idx); | |
| 70 | - }, | |
| 71 | - okText: "\u786E\u5B9A", | |
| 72 | - cancelText: "\u53D6\u6D88" | |
| 73 | - }, /*#__PURE__*/React.createElement(_DeleteOutlined, { | |
| 74 | - style: { | |
| 75 | - fontSize: 17, | |
| 76 | - marginLeft: 8 | |
| 77 | - } | |
| 78 | - })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/React.createElement(_CopyOutlined, { | |
| 79 | - style: { | |
| 80 | - fontSize: 15, | |
| 81 | - marginLeft: 8 | |
| 82 | - }, | |
| 83 | - onClick: function onClick() { | |
| 84 | - return copyItem(idx); | |
| 85 | - } | |
| 86 | - }), !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 87 | - style: { | |
| 88 | - fontSize: 16, | |
| 89 | - marginLeft: 8 | |
| 90 | - }, | |
| 91 | - onClick: function onClick() { | |
| 92 | - return moveItemUp(idx); | |
| 93 | - } | |
| 94 | - }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 95 | - style: { | |
| 96 | - fontSize: 16, | |
| 97 | - marginLeft: 8 | |
| 98 | - }, | |
| 99 | - onClick: function onClick() { | |
| 100 | - return moveItemDown(idx); | |
| 101 | - } | |
| 102 | - })))); | |
| 103 | - }), /*#__PURE__*/React.createElement("div", { | |
| 104 | - style: { | |
| 105 | - marginTop: displayList.length > 0 ? 0 : 8 | |
| 106 | - } | |
| 107 | - }, !props.hideAdd && /*#__PURE__*/React.createElement(_Button, _extends({ | |
| 108 | - onClick: addItem | |
| 109 | - }, addBtnProps)), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 110 | - var callback = item.callback, | |
| 111 | - text = item.text, | |
| 112 | - html = item.html; | |
| 113 | - | |
| 114 | - var onClick = function onClick() { | |
| 115 | - console.log({ | |
| 116 | - value: listData, | |
| 117 | - onChange: changeList, | |
| 118 | - schema: schema | |
| 119 | - }); | |
| 120 | - }; | |
| 121 | - | |
| 122 | - if (typeof window[callback] === 'function') { | |
| 123 | - onClick = function onClick() { | |
| 124 | - window[callback]({ | |
| 125 | - value: listData, | |
| 126 | - onChange: changeList, | |
| 127 | - schema: schema | |
| 128 | - }); | |
| 129 | - }; | |
| 130 | - } | |
| 131 | - | |
| 132 | - return /*#__PURE__*/React.createElement(_Button, { | |
| 133 | - key: idx.toString(), | |
| 134 | - style: { | |
| 135 | - marginLeft: 8 | |
| 136 | - }, | |
| 137 | - type: "dashed", | |
| 138 | - onClick: onClick | |
| 139 | - }, /*#__PURE__*/React.createElement("span", { | |
| 140 | - dangerouslySetInnerHTML: { | |
| 141 | - __html: html || text | |
| 142 | - } | |
| 143 | - })); | |
| 144 | - }) : null)); | |
| 145 | -}; | |
| 146 | - | |
| 147 | -export default SimpleList; | |
| \ No newline at end of file |
es/widgets/antd/listTab.js
deleted
100644 → 0
| 1 | -import "antd/es/tabs/style"; | |
| 2 | -import _Tabs from "antd/es/tabs"; | |
| 3 | -var _excluded = ["tabName", "type"]; | |
| 4 | - | |
| 5 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 6 | - | |
| 7 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 8 | - | |
| 9 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 10 | - | |
| 11 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 12 | - | |
| 13 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 14 | - | |
| 15 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 16 | - | |
| 17 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 18 | - | |
| 19 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 20 | - | |
| 21 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 22 | - | |
| 23 | -/* eslint-disable jsx-a11y/anchor-is-valid */ | |
| 24 | -import React, { useState } from 'react'; | |
| 25 | -import Core from '../../index'; | |
| 26 | -var TabPane = _Tabs.TabPane; | |
| 27 | - | |
| 28 | -var TabList = function TabList(_ref) { | |
| 29 | - var _ref$displayList = _ref.displayList, | |
| 30 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 31 | - listData = _ref.listData, | |
| 32 | - changeList = _ref.changeList, | |
| 33 | - schema = _ref.schema, | |
| 34 | - deleteItem = _ref.deleteItem, | |
| 35 | - copyItem = _ref.copyItem, | |
| 36 | - addItem = _ref.addItem, | |
| 37 | - moveItemUp = _ref.moveItemUp, | |
| 38 | - moveItemDown = _ref.moveItemDown, | |
| 39 | - displayType = _ref.displayType, | |
| 40 | - getFieldsProps = _ref.getFieldsProps; | |
| 41 | - | |
| 42 | - var _useState = useState('0'), | |
| 43 | - _useState2 = _slicedToArray(_useState, 2), | |
| 44 | - activeKey = _useState2[0], | |
| 45 | - setActiveKey = _useState2[1]; | |
| 46 | - | |
| 47 | - var _schema$props = schema.props, | |
| 48 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 49 | - itemProps = schema.itemProps; | |
| 50 | - | |
| 51 | - var tabName = props.tabName, | |
| 52 | - type = props.type, | |
| 53 | - restProps = _objectWithoutProperties(props, _excluded); | |
| 54 | - | |
| 55 | - var onEdit = function onEdit(targetKey, action) { | |
| 56 | - if (action === 'add') { | |
| 57 | - var currentKey = addItem(); | |
| 58 | - setActiveKey("".concat(currentKey)); | |
| 59 | - } else if (action === 'remove') { | |
| 60 | - deleteItem(Number(targetKey)); | |
| 61 | - setActiveKey("".concat(targetKey > 1 ? targetKey - 1 : 0)); | |
| 62 | - } else { | |
| 63 | - return null; | |
| 64 | - } | |
| 65 | - }; | |
| 66 | - | |
| 67 | - var getCurrentTabPaneName = function getCurrentTabPaneName(idx) { | |
| 68 | - return tabName instanceof Array ? tabName[idx] || idx + 1 : "".concat(tabName || '项目', " ").concat(idx + 1); | |
| 69 | - }; | |
| 70 | - | |
| 71 | - return /*#__PURE__*/React.createElement(_Tabs, _extends({ | |
| 72 | - type: type || 'line', | |
| 73 | - onChange: setActiveKey, | |
| 74 | - activeKey: activeKey, | |
| 75 | - onEdit: onEdit | |
| 76 | - }, restProps), displayList.map(function (item, idx) { | |
| 77 | - var fieldsProps = getFieldsProps(idx); | |
| 78 | - fieldsProps.displayType = displayType; | |
| 79 | - return /*#__PURE__*/React.createElement(TabPane, { | |
| 80 | - tab: getCurrentTabPaneName(idx), | |
| 81 | - key: "".concat(idx) | |
| 82 | - }, /*#__PURE__*/React.createElement(Core, fieldsProps)); | |
| 83 | - })); | |
| 84 | -}; | |
| 85 | - | |
| 86 | -export default TabList; | |
| \ No newline at end of file |
es/widgets/antd/listVirtual.js
deleted
100644 → 0
| 1 | -import "antd/es/table/style"; | |
| 2 | -import _Table from "antd/es/table"; | |
| 3 | -import "antd/es/button/style"; | |
| 4 | -import _Button from "antd/es/button"; | |
| 5 | -import _ArrowDownOutlined from "@ant-design/icons/lib/icons/ArrowDownOutlined"; | |
| 6 | -import _ArrowUpOutlined from "@ant-design/icons/lib/icons/ArrowUpOutlined"; | |
| 7 | -import "antd/es/popconfirm/style"; | |
| 8 | -import _Popconfirm from "antd/es/popconfirm"; | |
| 9 | -var _excluded = ["scrollY"]; | |
| 10 | - | |
| 11 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 12 | - | |
| 13 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 14 | - | |
| 15 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 16 | - | |
| 17 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 18 | - | |
| 19 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 20 | - | |
| 21 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 22 | - | |
| 23 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 24 | - | |
| 25 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 26 | - | |
| 27 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 28 | - | |
| 29 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 30 | - | |
| 31 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 32 | - | |
| 33 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 34 | - | |
| 35 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 36 | - | |
| 37 | -/* eslint-disable jsx-a11y/anchor-is-valid */ | |
| 38 | -import React from 'react'; | |
| 39 | -import Core from '../../index'; | |
| 40 | -import { useVT } from 'virtualizedtableforantd4'; | |
| 41 | -var FIELD_LENGTH = 170; | |
| 42 | - | |
| 43 | -var VirtualList = function VirtualList(_ref) { | |
| 44 | - var _ref$displayList = _ref.displayList, | |
| 45 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 46 | - dataIndex = _ref.dataIndex, | |
| 47 | - children = _ref.children, | |
| 48 | - deleteItem = _ref.deleteItem, | |
| 49 | - addItem = _ref.addItem, | |
| 50 | - moveItemUp = _ref.moveItemUp, | |
| 51 | - moveItemDown = _ref.moveItemDown, | |
| 52 | - flatten = _ref.flatten, | |
| 53 | - schema = _ref.schema, | |
| 54 | - listData = _ref.listData, | |
| 55 | - changeList = _ref.changeList; | |
| 56 | - var _schema$props = schema.props, | |
| 57 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 58 | - _schema$itemProps = schema.itemProps, | |
| 59 | - itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps; | |
| 60 | - | |
| 61 | - var _props$scrollY = props.scrollY, | |
| 62 | - scrollY = _props$scrollY === void 0 ? 600 : _props$scrollY, | |
| 63 | - rest = _objectWithoutProperties(props, _excluded); | |
| 64 | - | |
| 65 | - var _useVT = useVT(function () { | |
| 66 | - return { | |
| 67 | - scroll: { | |
| 68 | - y: scrollY | |
| 69 | - } | |
| 70 | - }; | |
| 71 | - }, []), | |
| 72 | - _useVT2 = _slicedToArray(_useVT, 2), | |
| 73 | - vt = _useVT2[0], | |
| 74 | - set_components = _useVT2[1]; | |
| 75 | - | |
| 76 | - var dataSource = displayList.map(function (item, idx) { | |
| 77 | - return { | |
| 78 | - index: idx | |
| 79 | - }; | |
| 80 | - }); | |
| 81 | - var columns = children.map(function (child) { | |
| 82 | - var item = flatten[child]; | |
| 83 | - var schema = item && item.schema || {}; | |
| 84 | - return { | |
| 85 | - dataIndex: child, | |
| 86 | - width: FIELD_LENGTH, | |
| 87 | - title: schema.required ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", { | |
| 88 | - className: "fr-label-required" | |
| 89 | - }, " *"), /*#__PURE__*/React.createElement("span", null, schema.title)) : schema.title, | |
| 90 | - render: function render(value, record, index) { | |
| 91 | - // Check: record.index 似乎是antd自己会给的,不错哦 | |
| 92 | - var childIndex = [].concat(_toConsumableArray(dataIndex), [record.index]); | |
| 93 | - return /*#__PURE__*/React.createElement(Core, { | |
| 94 | - hideTitle: true, | |
| 95 | - displayType: "inline", | |
| 96 | - key: index.toString(), | |
| 97 | - id: child, | |
| 98 | - dataIndex: childIndex | |
| 99 | - }); | |
| 100 | - } | |
| 101 | - }; | |
| 102 | - }); | |
| 103 | - | |
| 104 | - if (!props.hideDelete || Array.isArray(itemProps.buttons)) { | |
| 105 | - columns.push({ | |
| 106 | - title: '操作', | |
| 107 | - key: '$action', | |
| 108 | - fixed: 'right', | |
| 109 | - width: 120, | |
| 110 | - render: function render(value, record, idx) { | |
| 111 | - return /*#__PURE__*/React.createElement(React.Fragment, null, !props.hideDelete && /*#__PURE__*/React.createElement(_Popconfirm, { | |
| 112 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 113 | - onConfirm: function onConfirm() { | |
| 114 | - return deleteItem(idx); | |
| 115 | - }, | |
| 116 | - okText: "\u786E\u5B9A", | |
| 117 | - cancelText: "\u53D6\u6D88" | |
| 118 | - }, /*#__PURE__*/React.createElement("a", null, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_ArrowUpOutlined, { | |
| 119 | - style: { | |
| 120 | - color: '#1890ff', | |
| 121 | - fontSize: 16, | |
| 122 | - marginLeft: 4 | |
| 123 | - }, | |
| 124 | - onClick: function onClick() { | |
| 125 | - return moveItemUp(idx); | |
| 126 | - } | |
| 127 | - }), /*#__PURE__*/React.createElement(_ArrowDownOutlined, { | |
| 128 | - style: { | |
| 129 | - color: '#1890ff', | |
| 130 | - fontSize: 16, | |
| 131 | - marginLeft: 4 | |
| 132 | - }, | |
| 133 | - onClick: function onClick() { | |
| 134 | - return moveItemDown(idx); | |
| 135 | - } | |
| 136 | - })), Array.isArray(itemProps.buttons) ? itemProps.buttons.map(function (item, idx) { | |
| 137 | - var callback = item.callback, | |
| 138 | - text = item.text, | |
| 139 | - html = item.html; | |
| 140 | - | |
| 141 | - var onClick = function onClick() {}; | |
| 142 | - | |
| 143 | - if (typeof window[callback] === 'function') { | |
| 144 | - onClick = function onClick() { | |
| 145 | - window[callback]({ | |
| 146 | - value: listData, | |
| 147 | - onChange: changeList, | |
| 148 | - schema: schema | |
| 149 | - }); | |
| 150 | - }; | |
| 151 | - } | |
| 152 | - | |
| 153 | - return /*#__PURE__*/React.createElement("a", { | |
| 154 | - key: idx.toString(), | |
| 155 | - style: { | |
| 156 | - marginLeft: 8 | |
| 157 | - }, | |
| 158 | - size: "small", | |
| 159 | - onClick: onClick | |
| 160 | - }, /*#__PURE__*/React.createElement("span", { | |
| 161 | - dangerouslySetInnerHTML: { | |
| 162 | - __html: html || text | |
| 163 | - } | |
| 164 | - })); | |
| 165 | - }) : null); | |
| 166 | - } | |
| 167 | - }); | |
| 168 | - } | |
| 169 | - | |
| 170 | - return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", { | |
| 171 | - className: "w-100 mb2 tr" | |
| 172 | - }, !props.hideAdd && /*#__PURE__*/React.createElement(_Button, { | |
| 173 | - type: "primary", | |
| 174 | - size: "small", | |
| 175 | - onClick: addItem | |
| 176 | - }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 177 | - var callback = item.callback, | |
| 178 | - text = item.text, | |
| 179 | - html = item.html; | |
| 180 | - | |
| 181 | - var onClick = function onClick() {}; | |
| 182 | - | |
| 183 | - if (typeof window[callback] === 'function') { | |
| 184 | - onClick = function onClick() { | |
| 185 | - window[callback]({ | |
| 186 | - value: listData, | |
| 187 | - onChange: changeList, | |
| 188 | - schema: schema | |
| 189 | - }); | |
| 190 | - }; | |
| 191 | - } | |
| 192 | - | |
| 193 | - return /*#__PURE__*/React.createElement(_Button, { | |
| 194 | - key: idx.toString(), | |
| 195 | - style: { | |
| 196 | - marginLeft: 8 | |
| 197 | - }, | |
| 198 | - size: "small", | |
| 199 | - onClick: onClick | |
| 200 | - }, /*#__PURE__*/React.createElement("span", { | |
| 201 | - dangerouslySetInnerHTML: { | |
| 202 | - __html: html || text | |
| 203 | - } | |
| 204 | - })); | |
| 205 | - }) : null), /*#__PURE__*/React.createElement(_Table, _extends({ | |
| 206 | - rowKey: "index", | |
| 207 | - scroll: { | |
| 208 | - y: scrollY, | |
| 209 | - x: 'max-content' | |
| 210 | - }, | |
| 211 | - components: vt, | |
| 212 | - size: "small", | |
| 213 | - columns: columns, | |
| 214 | - dataSource: dataSource, | |
| 215 | - pagination: false | |
| 216 | - }, rest))); | |
| 217 | -}; | |
| 218 | - | |
| 219 | -export default VirtualList; | |
| \ No newline at end of file |
| ... | ... | @@ -19,17 +19,17 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return |
| 19 | 19 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } |
| 20 | 20 | |
| 21 | 21 | import React, { useState, useEffect } from 'react'; |
| 22 | -// import { useStore2 } from '../../hooks'; | |
| 22 | +import { useStore2 } from '../../hooks'; | |
| 23 | 23 | var Panel = _Collapse.Panel; |
| 24 | 24 | export default function Map(_ref) { |
| 25 | 25 | var children = _ref.children, |
| 26 | 26 | title = _ref.title, |
| 27 | 27 | rest = _objectWithoutProperties(_ref, _excluded); |
| 28 | 28 | |
| 29 | - var _ref2 = {}, | |
| 30 | - theme = _ref2.theme, | |
| 31 | - displayType = _ref2.displayType, | |
| 32 | - allCollapsed = _ref2.allCollapsed; // TODO! | |
| 29 | + var _useStore = useStore2(), | |
| 30 | + theme = _useStore.theme, | |
| 31 | + displayType = _useStore.displayType, | |
| 32 | + allCollapsed = _useStore.allCollapsed; | |
| 33 | 33 | |
| 34 | 34 | var _useState = useState(false), |
| 35 | 35 | _useState2 = _slicedToArray(_useState, 2), | ... | ... |
| 1 | 1 | import "antd/es/select/style"; |
| 2 | 2 | import _Select from "antd/es/select"; |
| 3 | -var _excluded = ["schema", "style", "options"]; | |
| 4 | 3 | |
| 5 | 4 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } |
| 6 | 5 | |
| ... | ... | @@ -8,19 +7,14 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va |
| 8 | 7 | |
| 9 | 8 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 10 | 9 | |
| 11 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 12 | - | |
| 13 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 14 | - | |
| 15 | 10 | import React from 'react'; |
| 11 | +import { createWidget } from '../../createWidget'; | |
| 16 | 12 | import { getArray } from '../../utils'; |
| 17 | 13 | |
| 18 | -var FrSelect = function FrSelect(_ref) { | |
| 14 | +var mapProps = function mapProps(_ref) { | |
| 19 | 15 | var schema = _ref.schema, |
| 20 | 16 | style = _ref.style, |
| 21 | - _options = _ref.options, | |
| 22 | - rest = _objectWithoutProperties(_ref, _excluded); | |
| 23 | - | |
| 17 | + _options = _ref.options; | |
| 24 | 18 | var options; // 如果已经有外部注入的options了,内部的schema就会被忽略 |
| 25 | 19 | |
| 26 | 20 | if (_options && Array.isArray(_options)) { |
| ... | ... | @@ -49,14 +43,13 @@ var FrSelect = function FrSelect(_ref) { |
| 49 | 43 | }); |
| 50 | 44 | } |
| 51 | 45 | |
| 52 | - var finalProps = _objectSpread({ | |
| 46 | + return { | |
| 53 | 47 | options: options, |
| 54 | 48 | style: _objectSpread({ |
| 55 | 49 | width: '100%' |
| 56 | 50 | }, style) |
| 57 | - }, rest); | |
| 58 | - | |
| 59 | - return /*#__PURE__*/React.createElement(_Select, finalProps); | |
| 51 | + }; | |
| 60 | 52 | }; |
| 61 | 53 | |
| 62 | -export default FrSelect; | |
| \ No newline at end of file | ||
| 54 | +var Component = createWidget(mapProps)(_Select); | |
| 55 | +export default Component; | |
| \ No newline at end of file | ... | ... |
lib/atom.css
renamed from
lib/form-render-core/src/atom.css
lib/connectForm.js
renamed from
lib/form-render-core/src/connectForm.js
lib/core/RenderChildren/RenderList/CardList.js
renamed from
lib/form-render-core/src/core/RenderChildren/RenderList/CardList.js
| ... | ... | @@ -9,17 +9,21 @@ require("antd/es/button/style"); |
| 9 | 9 | |
| 10 | 10 | var _button = _interopRequireDefault(require("antd/es/button")); |
| 11 | 11 | |
| 12 | -require("antd/es/popconfirm/style"); | |
| 12 | +require("antd/es/space/style"); | |
| 13 | 13 | |
| 14 | -var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 14 | +var _space = _interopRequireDefault(require("antd/es/space")); | |
| 15 | + | |
| 16 | +var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 15 | 17 | |
| 16 | -var _CloseOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/CloseOutlined")); | |
| 18 | +var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 17 | 19 | |
| 18 | 20 | var _CopyOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/CopyOutlined")); |
| 19 | 21 | |
| 20 | -var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 22 | +require("antd/es/popconfirm/style"); | |
| 21 | 23 | |
| 22 | -var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 24 | +var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 25 | + | |
| 26 | +var _DeleteOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/DeleteOutlined")); | |
| 23 | 27 | |
| 24 | 28 | var _react = _interopRequireDefault(require("react")); |
| 25 | 29 | |
| ... | ... | @@ -72,44 +76,44 @@ var CardList = function CardList(_ref) { |
| 72 | 76 | key: idx |
| 73 | 77 | }, /*#__PURE__*/_react.default.createElement("div", { |
| 74 | 78 | className: "fr-card-index" |
| 75 | - }, idx + 1), /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps), /*#__PURE__*/_react.default.createElement("div", { | |
| 79 | + }, idx + 1), /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps), /*#__PURE__*/_react.default.createElement(_space.default, { | |
| 76 | 80 | direction: "horizontal", |
| 77 | 81 | className: "fr-card-toolbar" |
| 78 | - }, !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 82 | + }, !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 83 | + title: "\u786E\u5B9A\u5220\u9664?", | |
| 84 | + onConfirm: function onConfirm() { | |
| 85 | + return deleteItem(idx); | |
| 86 | + }, | |
| 87 | + okText: "\u786E\u5B9A", | |
| 88 | + cancelText: "\u53D6\u6D88" | |
| 89 | + }, /*#__PURE__*/_react.default.createElement(_DeleteOutlined2.default, { | |
| 90 | + style: { | |
| 91 | + fontSize: 17, | |
| 92 | + marginLeft: 8 | |
| 93 | + } | |
| 94 | + })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement(_CopyOutlined2.default, { | |
| 79 | 95 | style: { |
| 80 | 96 | fontSize: 16, |
| 81 | - marginLeft: 4 | |
| 97 | + marginLeft: 8 | |
| 82 | 98 | }, |
| 83 | 99 | onClick: function onClick() { |
| 84 | - return moveItemUp(idx); | |
| 100 | + return copyItem(idx); | |
| 85 | 101 | } |
| 86 | - }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 102 | + }), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 87 | 103 | style: { |
| 88 | 104 | fontSize: 16, |
| 89 | 105 | marginLeft: 4 |
| 90 | 106 | }, |
| 91 | 107 | onClick: function onClick() { |
| 92 | - return moveItemDown(idx); | |
| 108 | + return moveItemUp(idx); | |
| 93 | 109 | } |
| 94 | - })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement(_CopyOutlined2.default, { | |
| 110 | + }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 95 | 111 | style: { |
| 96 | 112 | fontSize: 16, |
| 97 | - marginLeft: 8 | |
| 113 | + marginLeft: 4 | |
| 98 | 114 | }, |
| 99 | 115 | onClick: function onClick() { |
| 100 | - return copyItem(idx); | |
| 101 | - } | |
| 102 | - }), !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 103 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 104 | - onConfirm: function onConfirm() { | |
| 105 | - return deleteItem(idx); | |
| 106 | - }, | |
| 107 | - okText: "\u786E\u5B9A", | |
| 108 | - cancelText: "\u53D6\u6D88" | |
| 109 | - }, /*#__PURE__*/_react.default.createElement(_CloseOutlined2.default, { | |
| 110 | - style: { | |
| 111 | - fontSize: 16, | |
| 112 | - marginLeft: 8 | |
| 116 | + return moveItemDown(idx); | |
| 113 | 117 | } |
| 114 | 118 | })))); |
| 115 | 119 | })), /*#__PURE__*/_react.default.createElement("div", { | ... | ... |
lib/core/RenderChildren/RenderList/DrawerList.js
renamed from
lib/form-render-core/src/core/RenderChildren/RenderList/DrawerList.js
| ... | ... | @@ -19,6 +19,10 @@ require("antd/es/button/style"); |
| 19 | 19 | |
| 20 | 20 | var _button = _interopRequireDefault(require("antd/es/button")); |
| 21 | 21 | |
| 22 | +require("antd/es/space/style"); | |
| 23 | + | |
| 24 | +var _space = _interopRequireDefault(require("antd/es/space")); | |
| 25 | + | |
| 22 | 26 | var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); |
| 23 | 27 | |
| 24 | 28 | var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); |
| ... | ... | @@ -153,7 +157,7 @@ var DrawerList = function DrawerList(_ref) { |
| 153 | 157 | width: 120, |
| 154 | 158 | render: function render(value, record, idx) { |
| 155 | 159 | var index = value && value.$idx || 0; |
| 156 | - return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("a", { | |
| 160 | + return /*#__PURE__*/_react.default.createElement(_space.default, null, /*#__PURE__*/_react.default.createElement("a", { | |
| 157 | 161 | onClick: function onClick() { |
| 158 | 162 | return openDrawer(index); |
| 159 | 163 | } |
| ... | ... | @@ -164,15 +168,11 @@ var DrawerList = function DrawerList(_ref) { |
| 164 | 168 | }, |
| 165 | 169 | okText: "\u786E\u5B9A", |
| 166 | 170 | cancelText: "\u53D6\u6D88" |
| 167 | - }, /*#__PURE__*/_react.default.createElement("a", { | |
| 168 | - style: { | |
| 169 | - marginLeft: 8 | |
| 170 | - } | |
| 171 | - }, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 171 | + }, /*#__PURE__*/_react.default.createElement("a", null, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 172 | 172 | style: { |
| 173 | 173 | color: '#1890ff', |
| 174 | 174 | fontSize: 16, |
| 175 | - marginLeft: 8 | |
| 175 | + marginLeft: 4 | |
| 176 | 176 | }, |
| 177 | 177 | onClick: function onClick() { |
| 178 | 178 | return moveItemUp(idx); |
| ... | ... | @@ -181,7 +181,7 @@ var DrawerList = function DrawerList(_ref) { |
| 181 | 181 | style: { |
| 182 | 182 | color: '#1890ff', |
| 183 | 183 | fontSize: 16, |
| 184 | - marginLeft: 8 | |
| 184 | + marginLeft: 4 | |
| 185 | 185 | }, |
| 186 | 186 | onClick: function onClick() { |
| 187 | 187 | return moveItemDown(idx); | ... | ... |
lib/core/RenderChildren/RenderList/SimpleList.js
renamed from
lib/form-render-core/src/core/RenderChildren/RenderList/SimpleList.js
lib/core/RenderChildren/RenderList/TableList.js
renamed from
lib/widgets/antd/listTable.js
| ... | ... | @@ -13,6 +13,10 @@ require("antd/es/button/style"); |
| 13 | 13 | |
| 14 | 14 | var _button = _interopRequireDefault(require("antd/es/button")); |
| 15 | 15 | |
| 16 | +require("antd/es/space/style"); | |
| 17 | + | |
| 18 | +var _space = _interopRequireDefault(require("antd/es/space")); | |
| 19 | + | |
| 16 | 20 | var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); |
| 17 | 21 | |
| 18 | 22 | var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); |
| ... | ... | @@ -123,7 +127,7 @@ var TableList = function TableList(_ref) { |
| 123 | 127 | fixed: 'right', |
| 124 | 128 | width: 120, |
| 125 | 129 | render: function render(value, record, idx) { |
| 126 | - return /*#__PURE__*/_react.default.createElement("div", null, !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement("a", { | |
| 130 | + return /*#__PURE__*/_react.default.createElement(_space.default, null, !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement("a", { | |
| 127 | 131 | onClick: function onClick() { |
| 128 | 132 | return copyItem(idx); |
| 129 | 133 | } | ... | ... |
lib/core/RenderChildren/RenderList/VirtualList.js
renamed from
lib/form-render-core/src/core/RenderChildren/RenderList/VirtualList.js
| ... | ... | @@ -143,7 +143,7 @@ var VirtualList = function VirtualList(_ref) { |
| 143 | 143 | style: { |
| 144 | 144 | color: '#1890ff', |
| 145 | 145 | fontSize: 16, |
| 146 | - marginLeft: 8 | |
| 146 | + marginLeft: 4 | |
| 147 | 147 | }, |
| 148 | 148 | onClick: function onClick() { |
| 149 | 149 | return moveItemUp(idx); |
| ... | ... | @@ -152,7 +152,7 @@ var VirtualList = function VirtualList(_ref) { |
| 152 | 152 | style: { |
| 153 | 153 | color: '#1890ff', |
| 154 | 154 | fontSize: 16, |
| 155 | - marginLeft: 8 | |
| 155 | + marginLeft: 4 | |
| 156 | 156 | }, |
| 157 | 157 | onClick: function onClick() { |
| 158 | 158 | return moveItemDown(idx); | ... | ... |
lib/core/RenderChildren/RenderList/index.js
renamed from
lib/form-render-core/src/core/RenderChildren/RenderList/index.js
| ... | ... | @@ -25,8 +25,6 @@ var _DrawerList = _interopRequireDefault(require("./DrawerList")); |
| 25 | 25 | |
| 26 | 26 | var _VirtualList = _interopRequireDefault(require("./VirtualList")); |
| 27 | 27 | |
| 28 | -var _TabList = _interopRequireDefault(require("./TabList")); | |
| 29 | - | |
| 30 | 28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 31 | 29 | |
| 32 | 30 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } |
| ... | ... | @@ -194,9 +192,6 @@ var RenderList = function RenderList(_ref) { |
| 194 | 192 | case 'virtualList': |
| 195 | 193 | return /*#__PURE__*/_react.default.createElement(_VirtualList.default, displayProps); |
| 196 | 194 | |
| 197 | - case 'tabList': | |
| 198 | - return /*#__PURE__*/_react.default.createElement(_TabList.default, displayProps); | |
| 199 | - | |
| 200 | 195 | default: |
| 201 | 196 | return /*#__PURE__*/_react.default.createElement(_CardList.default, displayProps); |
| 202 | 197 | } | ... | ... |
lib/core/RenderChildren/RenderList/list.css
renamed from
lib/form-render-core/src/core/RenderChildren/RenderList/list.css
lib/core/RenderChildren/RenderObject.js
renamed from
lib/form-render-core/src/core/RenderChildren/RenderObject.js
lib/core/RenderField/ErrorMessage.css
renamed from
lib/form-render-core/src/core/RenderField/ErrorMessage.css
lib/core/RenderField/ErrorMessage.js
renamed from
lib/form-render-core/src/core/RenderField/ErrorMessage.js
lib/core/RenderField/ExtendedWidget.js
renamed from
lib/form-render-core/src/core/RenderField/ExtendedWidget.js
| ... | ... | @@ -169,14 +169,8 @@ var ExtendedWidget = function ExtendedWidget(_ref) { |
| 169 | 169 | }; |
| 170 | 170 | |
| 171 | 171 | var areEqual = function areEqual(prev, current) { |
| 172 | - if (prev.schema && current.schema) { | |
| 173 | - if (prev.schema.$id === '#') { | |
| 174 | - return false; | |
| 175 | - } | |
| 176 | - | |
| 177 | - if (prev.schema.hidden && current.schema.hidden) { | |
| 178 | - return true; | |
| 179 | - } | |
| 172 | + if (prev.schema && prev.schema.$id === '#') { | |
| 173 | + return false; | |
| 180 | 174 | } |
| 181 | 175 | |
| 182 | 176 | if (prev.readOnly !== current.readOnly) { | ... | ... |
lib/core/RenderField/Extra.css
renamed from
lib/form-render-core/src/core/RenderField/Extra.css
lib/core/RenderField/Extra.js
renamed from
lib/form-render-core/src/core/RenderField/Extra.js
lib/core/RenderField/Title.js
renamed from
lib/form-render-core/src/core/RenderField/Title.js
lib/core/RenderField/index.js
renamed from
lib/form-render-core/src/core/RenderField/index.js
| ... | ... | @@ -21,8 +21,6 @@ var _Extra = _interopRequireDefault(require("./Extra")); |
| 21 | 21 | |
| 22 | 22 | var _Title = _interopRequireDefault(require("./Title")); |
| 23 | 23 | |
| 24 | -var _validator = require("../../validator"); | |
| 25 | - | |
| 26 | 24 | var _ExtendedWidget = _interopRequireDefault(require("./ExtendedWidget")); |
| 27 | 25 | |
| 28 | 26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| ... | ... | @@ -35,18 +33,6 @@ function _extends() { _extends = Object.assign || function (target) { for (var i |
| 35 | 33 | |
| 36 | 34 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 37 | 35 | |
| 38 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 39 | - | |
| 40 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 41 | - | |
| 42 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 43 | - | |
| 44 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 45 | - | |
| 46 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 47 | - | |
| 48 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 49 | - | |
| 50 | 36 | // TODO: 之后不要直接用get,收口到一个内部方法getValue,便于全局 ctrl + f 查找 |
| 51 | 37 | var RenderField = function RenderField(props) { |
| 52 | 38 | var $id = props.$id, |
| ... | ... | @@ -65,23 +51,19 @@ var RenderField = function RenderField(props) { |
| 65 | 51 | displayType = props.displayType; |
| 66 | 52 | |
| 67 | 53 | var _useStore = (0, _hooks.useStore)(), |
| 68 | - formData = _useStore.formData, | |
| 69 | - flatten = _useStore.flatten; | |
| 54 | + formData = _useStore.formData; | |
| 70 | 55 | |
| 71 | 56 | var _useStore2 = (0, _hooks.useStore2)(), |
| 72 | 57 | debounceInput = _useStore2.debounceInput, |
| 73 | 58 | readOnly = _useStore2.readOnly, |
| 74 | 59 | disabled = _useStore2.disabled, |
| 75 | - showValidate = _useStore2.showValidate, | |
| 76 | - validateMessages = _useStore2.validateMessages, | |
| 77 | - locale = _useStore2.locale; | |
| 60 | + showValidate = _useStore2.showValidate; | |
| 78 | 61 | |
| 79 | 62 | var _useTools = (0, _hooks.useTools)(), |
| 80 | 63 | onValuesChange = _useTools.onValuesChange, |
| 81 | 64 | onItemChange = _useTools.onItemChange, |
| 82 | 65 | setEditing = _useTools.setEditing, |
| 83 | - touchKey = _useTools.touchKey, | |
| 84 | - _setErrors = _useTools._setErrors; | |
| 66 | + touchKey = _useTools.touchKey; | |
| 85 | 67 | |
| 86 | 68 | var formDataRef = (0, _react.useRef)(); |
| 87 | 69 | formDataRef.current = formData; // console.log('<renderField>', $id); |
| ... | ... | @@ -99,38 +81,7 @@ var RenderField = function RenderField(props) { |
| 99 | 81 | |
| 100 | 82 | var _readOnly = readOnly !== undefined ? readOnly : _schema.readOnly; |
| 101 | 83 | |
| 102 | - var _disabled = disabled !== undefined ? disabled : _schema.disabled; | |
| 103 | - | |
| 104 | - var removeDupErrors = function removeDupErrors(arr) { | |
| 105 | - if (!Array.isArray(arr)) { | |
| 106 | - console.log('in removeDups: param is not an array'); | |
| 107 | - return; | |
| 108 | - } | |
| 109 | - | |
| 110 | - var array = []; | |
| 111 | - | |
| 112 | - for (var i = 0; i < arr.length; i++) { | |
| 113 | - var sameNameIndex = array.findIndex(function (item) { | |
| 114 | - return item.name === arr[i].name; | |
| 115 | - }); | |
| 116 | - | |
| 117 | - if (sameNameIndex > -1) { | |
| 118 | - var sameNameItem = array[sameNameIndex]; | |
| 119 | - var error1 = sameNameItem.error; | |
| 120 | - var error2 = arr[i].error; | |
| 121 | - array[sameNameIndex] = { | |
| 122 | - name: sameNameItem.name, | |
| 123 | - error: error1.length > 0 && error2.length > 0 ? [].concat(_toConsumableArray(error1), _toConsumableArray(error2)) : [] | |
| 124 | - }; | |
| 125 | - } else { | |
| 126 | - array.push(arr[i]); | |
| 127 | - } | |
| 128 | - } | |
| 129 | - | |
| 130 | - return array.filter(function (item) { | |
| 131 | - return Array.isArray(item.error) && item.error.length > 0; | |
| 132 | - }); | |
| 133 | - }; // TODO: 优化一下,只有touch还是false的时候,setTouched | |
| 84 | + var _disabled = disabled !== undefined ? disabled : _schema.disabled; // TODO: 优化一下,只有touch还是false的时候,setTouched | |
| 134 | 85 | |
| 135 | 86 | |
| 136 | 87 | var onChange = function onChange(value) { |
| ... | ... | @@ -150,20 +101,6 @@ var RenderField = function RenderField(props) { |
| 150 | 101 | if (typeof onValuesChange === 'function') { |
| 151 | 102 | onValuesChange(_defineProperty({}, dataPath, value), formDataRef.current); |
| 152 | 103 | } |
| 153 | - | |
| 154 | - (0, _validator.validateField)({ | |
| 155 | - path: dataPath, | |
| 156 | - formData: formDataRef.current, | |
| 157 | - flatten: flatten, | |
| 158 | - options: { | |
| 159 | - locale: locale, | |
| 160 | - validateMessages: validateMessages | |
| 161 | - } | |
| 162 | - }).then(function (res) { | |
| 163 | - _setErrors(function (errors) { | |
| 164 | - return removeDupErrors([].concat(_toConsumableArray(errors), _toConsumableArray(res))); | |
| 165 | - }); | |
| 166 | - }); | |
| 167 | 104 | }; |
| 168 | 105 | |
| 169 | 106 | var titleProps = { | ... | ... |
lib/core/index.js
renamed from
lib/form-render-core/src/core/index.js
| ... | ... | @@ -153,12 +153,11 @@ var CoreRender = function CoreRender(_ref2) { |
| 153 | 153 | effectiveLabelWidth = _ref2.effectiveLabelWidth, |
| 154 | 154 | rest = _objectWithoutProperties(_ref2, _excluded2); |
| 155 | 155 | |
| 156 | - if (schema.hidden) { | |
| 157 | - return null; | |
| 158 | - } // 样式的逻辑全放在这层 | |
| 156 | + // if (schema.hidden) { | |
| 157 | + // return null; | |
| 158 | + // } | |
| 159 | + // 样式的逻辑全放在这层 | |
| 159 | 160 | // displayType 一层层网上找值 |
| 160 | - | |
| 161 | - | |
| 162 | 161 | var _displayType = schema.displayType || rest.displayType || displayType || 'column'; |
| 163 | 162 | |
| 164 | 163 | var isList = (0, _utils.isListType)(schema); |
| ... | ... | @@ -344,8 +343,7 @@ var CoreRender = function CoreRender(_ref2) { |
| 344 | 343 | style: columnStyle, |
| 345 | 344 | className: "".concat(containerClass, " ").concat(debugCss ? 'debug' : '') |
| 346 | 345 | }, /*#__PURE__*/_react.default.createElement(_RenderField.default, fieldProps, _children)); |
| 347 | -}; // haven't used | |
| 348 | - | |
| 346 | +}; | |
| 349 | 347 | |
| 350 | 348 | var areEqual = function areEqual(prev, current) { |
| 351 | 349 | if (prev.allTouched !== current.allTouched) { |
| ... | ... | @@ -364,24 +362,6 @@ var areEqual = function areEqual(prev, current) { |
| 364 | 362 | return false; |
| 365 | 363 | } |
| 366 | 364 | |
| 367 | - if (prev.readOnly !== current.readOnly) { | |
| 368 | - return false; | |
| 369 | - } | |
| 370 | - | |
| 371 | - if (prev.disabled !== current.disabled) { | |
| 372 | - return false; | |
| 373 | - } | |
| 374 | - | |
| 375 | - if (prev.schema && current.schema) { | |
| 376 | - if (prev.schema.$id === '#') { | |
| 377 | - return false; | |
| 378 | - } | |
| 379 | - } | |
| 380 | - | |
| 381 | - if ((0, _utils.isObjType)(prev.schema) && (0, _utils.isObjType)(current.schema)) { | |
| 382 | - return false; | |
| 383 | - } | |
| 384 | - | |
| 385 | 365 | if (JSON.stringify(prev.dependValues) !== JSON.stringify(current.dependValues)) { |
| 386 | 366 | return false; |
| 387 | 367 | } |
| ... | ... | @@ -393,5 +373,7 @@ var areEqual = function areEqual(prev, current) { |
| 393 | 373 | return false; |
| 394 | 374 | }; |
| 395 | 375 | |
| 376 | +var MCore = /*#__PURE__*/_react.default.memo(CoreRender, areEqual); | |
| 377 | + | |
| 396 | 378 | var _default = Core; |
| 397 | 379 | exports.default = _default; |
| \ No newline at end of file | ... | ... |
lib/createWidget.js
renamed from
lib/form-render-core/src/createWidget.js
lib/form-render-core/src/Watcher.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.default = void 0; | |
| 7 | - | |
| 8 | -var _react = require("react"); | |
| 9 | - | |
| 10 | -var _utils = require("./utils"); | |
| 11 | - | |
| 12 | -/* eslint-disable react-hooks/exhaustive-deps */ | |
| 13 | -var Watcher = function Watcher(_ref) { | |
| 14 | - var watchKey = _ref.watchKey, | |
| 15 | - watch = _ref.watch, | |
| 16 | - formData = _ref.formData, | |
| 17 | - firstMount = _ref.firstMount; | |
| 18 | - var value = (0, _utils.getValueByPath)(formData, watchKey); | |
| 19 | - var watchObj = watch[watchKey]; | |
| 20 | - (0, _react.useEffect)(function () { | |
| 21 | - var runWatcher = function runWatcher() { | |
| 22 | - if (typeof watchObj === 'function') { | |
| 23 | - try { | |
| 24 | - watchObj(value); | |
| 25 | - } catch (error) { | |
| 26 | - console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 27 | - } | |
| 28 | - } else if (watchObj && typeof watchObj.handler === 'function') { | |
| 29 | - try { | |
| 30 | - watchObj.handler(value); | |
| 31 | - } catch (error) { | |
| 32 | - console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 33 | - } | |
| 34 | - } | |
| 35 | - }; | |
| 36 | - | |
| 37 | - if (firstMount) { | |
| 38 | - var immediate = watchObj && watchObj.immediate; | |
| 39 | - | |
| 40 | - if (immediate) { | |
| 41 | - runWatcher(); | |
| 42 | - } | |
| 43 | - } else { | |
| 44 | - runWatcher(); | |
| 45 | - } | |
| 46 | - }, [JSON.stringify(value), firstMount]); | |
| 47 | - return null; | |
| 48 | -}; | |
| 49 | - | |
| 50 | -var _default = Watcher; | |
| 51 | -exports.default = _default; | |
| \ No newline at end of file |
lib/form-render-core/src/core/RenderChildren/RenderList/TabList.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 4 | - | |
| 5 | -Object.defineProperty(exports, "__esModule", { | |
| 6 | - value: true | |
| 7 | -}); | |
| 8 | -exports.default = void 0; | |
| 9 | - | |
| 10 | -require("antd/es/tabs/style"); | |
| 11 | - | |
| 12 | -var _tabs = _interopRequireDefault(require("antd/es/tabs")); | |
| 13 | - | |
| 14 | -var _react = _interopRequireWildcard(require("react")); | |
| 15 | - | |
| 16 | -var _index = _interopRequireDefault(require("../../index")); | |
| 17 | - | |
| 18 | -var _excluded = ["tabName", "type"]; | |
| 19 | - | |
| 20 | -function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | |
| 21 | - | |
| 22 | -function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | |
| 23 | - | |
| 24 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 25 | - | |
| 26 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 27 | - | |
| 28 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 29 | - | |
| 30 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 31 | - | |
| 32 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 33 | - | |
| 34 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 35 | - | |
| 36 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 37 | - | |
| 38 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 39 | - | |
| 40 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 41 | - | |
| 42 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 43 | - | |
| 44 | -var TabPane = _tabs.default.TabPane; | |
| 45 | - | |
| 46 | -var TabList = function TabList(_ref) { | |
| 47 | - var _ref$displayList = _ref.displayList, | |
| 48 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 49 | - listData = _ref.listData, | |
| 50 | - changeList = _ref.changeList, | |
| 51 | - schema = _ref.schema, | |
| 52 | - deleteItem = _ref.deleteItem, | |
| 53 | - copyItem = _ref.copyItem, | |
| 54 | - addItem = _ref.addItem, | |
| 55 | - moveItemUp = _ref.moveItemUp, | |
| 56 | - moveItemDown = _ref.moveItemDown, | |
| 57 | - displayType = _ref.displayType, | |
| 58 | - getFieldsProps = _ref.getFieldsProps; | |
| 59 | - | |
| 60 | - var _useState = (0, _react.useState)('0'), | |
| 61 | - _useState2 = _slicedToArray(_useState, 2), | |
| 62 | - activeKey = _useState2[0], | |
| 63 | - setActiveKey = _useState2[1]; | |
| 64 | - | |
| 65 | - var _schema$props = schema.props, | |
| 66 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 67 | - itemProps = schema.itemProps; | |
| 68 | - | |
| 69 | - var tabName = props.tabName, | |
| 70 | - type = props.type, | |
| 71 | - restProps = _objectWithoutProperties(props, _excluded); | |
| 72 | - | |
| 73 | - var onEdit = function onEdit(targetKey, action) { | |
| 74 | - if (action === 'add') { | |
| 75 | - var currentKey = addItem(); | |
| 76 | - setActiveKey("".concat(currentKey)); | |
| 77 | - } else if (action === 'remove') { | |
| 78 | - deleteItem(Number(targetKey)); | |
| 79 | - setActiveKey("".concat(targetKey > 1 ? targetKey - 1 : 0)); | |
| 80 | - } else { | |
| 81 | - return null; | |
| 82 | - } | |
| 83 | - }; | |
| 84 | - | |
| 85 | - var getCurrentTabPaneName = function getCurrentTabPaneName(idx) { | |
| 86 | - return tabName instanceof Array ? tabName[idx] || idx + 1 : "".concat(tabName || '项目', " ").concat(idx + 1); | |
| 87 | - }; | |
| 88 | - | |
| 89 | - return /*#__PURE__*/_react.default.createElement(_tabs.default, _extends({ | |
| 90 | - type: type || 'line', | |
| 91 | - onChange: setActiveKey, | |
| 92 | - activeKey: activeKey, | |
| 93 | - onEdit: onEdit | |
| 94 | - }, restProps), displayList.map(function (item, idx) { | |
| 95 | - var fieldsProps = getFieldsProps(idx); | |
| 96 | - fieldsProps.displayType = displayType; | |
| 97 | - return /*#__PURE__*/_react.default.createElement(TabPane, { | |
| 98 | - tab: getCurrentTabPaneName(idx), | |
| 99 | - key: "".concat(idx) | |
| 100 | - }, /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps)); | |
| 101 | - })); | |
| 102 | -}; | |
| 103 | - | |
| 104 | -var _default = TabList; | |
| 105 | -exports.default = _default; | |
| \ No newline at end of file |
lib/form-render-core/src/core/RenderChildren/RenderList/TableList.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.default = void 0; | |
| 7 | - | |
| 8 | -require("antd/es/table/style"); | |
| 9 | - | |
| 10 | -var _table = _interopRequireDefault(require("antd/es/table")); | |
| 11 | - | |
| 12 | -require("antd/es/button/style"); | |
| 13 | - | |
| 14 | -var _button = _interopRequireDefault(require("antd/es/button")); | |
| 15 | - | |
| 16 | -var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 17 | - | |
| 18 | -var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 19 | - | |
| 20 | -require("antd/es/popconfirm/style"); | |
| 21 | - | |
| 22 | -var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 23 | - | |
| 24 | -var _react = _interopRequireDefault(require("react")); | |
| 25 | - | |
| 26 | -var _index = _interopRequireDefault(require("../../index")); | |
| 27 | - | |
| 28 | -var _excluded = ["buttons"], | |
| 29 | - _excluded2 = ["pagination"]; | |
| 30 | - | |
| 31 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 32 | - | |
| 33 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 34 | - | |
| 35 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 36 | - | |
| 37 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 38 | - | |
| 39 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 40 | - | |
| 41 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 42 | - | |
| 43 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 44 | - | |
| 45 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 46 | - | |
| 47 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 48 | - | |
| 49 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 50 | - | |
| 51 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 52 | - | |
| 53 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 54 | - | |
| 55 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 56 | - | |
| 57 | -// import ArrowDown from '../../../components/ArrowDown'; | |
| 58 | -var FIELD_LENGTH = 170; | |
| 59 | - | |
| 60 | -var TableList = function TableList(_ref) { | |
| 61 | - var _ref$displayList = _ref.displayList, | |
| 62 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 63 | - dataIndex = _ref.dataIndex, | |
| 64 | - children = _ref.children, | |
| 65 | - deleteItem = _ref.deleteItem, | |
| 66 | - copyItem = _ref.copyItem, | |
| 67 | - addItem = _ref.addItem, | |
| 68 | - moveItemUp = _ref.moveItemUp, | |
| 69 | - moveItemDown = _ref.moveItemDown, | |
| 70 | - flatten = _ref.flatten, | |
| 71 | - schema = _ref.schema, | |
| 72 | - listData = _ref.listData, | |
| 73 | - changeList = _ref.changeList; | |
| 74 | - var _schema$props = schema.props, | |
| 75 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 76 | - _schema$itemProps = schema.itemProps, | |
| 77 | - itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps; | |
| 78 | - | |
| 79 | - var buttons = itemProps.buttons, | |
| 80 | - columnProps = _objectWithoutProperties(itemProps, _excluded); | |
| 81 | - | |
| 82 | - var _props$pagination = props.pagination, | |
| 83 | - pagination = _props$pagination === void 0 ? {} : _props$pagination, | |
| 84 | - rest = _objectWithoutProperties(props, _excluded2); | |
| 85 | - | |
| 86 | - var paginationConfig = pagination && _objectSpread({ | |
| 87 | - size: 'small', | |
| 88 | - hideOnSinglePage: true | |
| 89 | - }, pagination); | |
| 90 | - | |
| 91 | - var dataSource = displayList.map(function (item, idx) { | |
| 92 | - return { | |
| 93 | - index: idx | |
| 94 | - }; | |
| 95 | - }); | |
| 96 | - var columns = children.map(function (child) { | |
| 97 | - var item = flatten[child]; | |
| 98 | - var schema = item && item.schema || {}; | |
| 99 | - return _objectSpread({ | |
| 100 | - dataIndex: child, | |
| 101 | - title: schema.required ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", { | |
| 102 | - className: "fr-label-required" | |
| 103 | - }, " *"), /*#__PURE__*/_react.default.createElement("span", null, schema.title)) : schema.title, | |
| 104 | - width: FIELD_LENGTH, | |
| 105 | - render: function render(value, record, index) { | |
| 106 | - // Check: record.index 似乎是antd自己会给的,不错哦 | |
| 107 | - var childIndex = [].concat(_toConsumableArray(dataIndex), [record.index]); | |
| 108 | - return /*#__PURE__*/_react.default.createElement(_index.default, { | |
| 109 | - hideTitle: true, | |
| 110 | - displayType: "inline", | |
| 111 | - key: index.toString(), | |
| 112 | - id: child, | |
| 113 | - dataIndex: childIndex | |
| 114 | - }); | |
| 115 | - } | |
| 116 | - }, columnProps); | |
| 117 | - }); | |
| 118 | - | |
| 119 | - if (!props.hideDelete || !props.hideAdd || !props.hideCopy || !props.hideMove) { | |
| 120 | - columns.push({ | |
| 121 | - title: '操作', | |
| 122 | - key: '$action', | |
| 123 | - fixed: 'right', | |
| 124 | - width: 120, | |
| 125 | - render: function render(value, record, idx) { | |
| 126 | - return /*#__PURE__*/_react.default.createElement("div", null, !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement("a", { | |
| 127 | - onClick: function onClick() { | |
| 128 | - return copyItem(idx); | |
| 129 | - } | |
| 130 | - }, "\u590D\u5236"), !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 131 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 132 | - onConfirm: function onConfirm() { | |
| 133 | - return deleteItem(idx); | |
| 134 | - }, | |
| 135 | - okText: "\u786E\u5B9A", | |
| 136 | - cancelText: "\u53D6\u6D88" | |
| 137 | - }, /*#__PURE__*/_react.default.createElement("a", { | |
| 138 | - style: { | |
| 139 | - marginLeft: 8 | |
| 140 | - } | |
| 141 | - }, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 142 | - style: { | |
| 143 | - color: '#1890ff', | |
| 144 | - fontSize: 16, | |
| 145 | - marginLeft: 8 | |
| 146 | - }, | |
| 147 | - onClick: function onClick() { | |
| 148 | - return moveItemUp(idx); | |
| 149 | - } | |
| 150 | - }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 151 | - style: { | |
| 152 | - color: '#1890ff', | |
| 153 | - fontSize: 16, | |
| 154 | - marginLeft: 8 | |
| 155 | - }, | |
| 156 | - onClick: function onClick() { | |
| 157 | - return moveItemDown(idx); | |
| 158 | - } | |
| 159 | - }))); | |
| 160 | - } | |
| 161 | - }); | |
| 162 | - } | |
| 163 | - | |
| 164 | - return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", { | |
| 165 | - className: "w-100 mb2 tr" | |
| 166 | - }, !props.hideAdd && /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 167 | - type: "primary", | |
| 168 | - size: "small", | |
| 169 | - onClick: addItem | |
| 170 | - }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 171 | - var callback = item.callback, | |
| 172 | - text = item.text, | |
| 173 | - html = item.html; | |
| 174 | - | |
| 175 | - var onClick = function onClick() { | |
| 176 | - console.log({ | |
| 177 | - value: listData, | |
| 178 | - onChange: changeList, | |
| 179 | - schema: schema | |
| 180 | - }); | |
| 181 | - }; | |
| 182 | - | |
| 183 | - if (typeof window[callback] === 'function') { | |
| 184 | - onClick = function onClick() { | |
| 185 | - window[callback]({ | |
| 186 | - value: listData, | |
| 187 | - onChange: changeList, | |
| 188 | - schema: schema | |
| 189 | - }); | |
| 190 | - }; | |
| 191 | - } | |
| 192 | - | |
| 193 | - return /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 194 | - key: idx.toString(), | |
| 195 | - style: { | |
| 196 | - marginLeft: 8 | |
| 197 | - }, | |
| 198 | - size: "small", | |
| 199 | - onClick: onClick | |
| 200 | - }, /*#__PURE__*/_react.default.createElement("span", { | |
| 201 | - dangerouslySetInnerHTML: { | |
| 202 | - __html: html || text | |
| 203 | - } | |
| 204 | - })); | |
| 205 | - }) : null), /*#__PURE__*/_react.default.createElement(_table.default, _extends({ | |
| 206 | - scroll: { | |
| 207 | - x: 'max-content' | |
| 208 | - }, | |
| 209 | - columns: columns, | |
| 210 | - dataSource: dataSource, | |
| 211 | - rowKey: "index", | |
| 212 | - size: "small", | |
| 213 | - pagination: paginationConfig | |
| 214 | - }, rest))); | |
| 215 | -}; | |
| 216 | - | |
| 217 | -var _default = TableList; | |
| 218 | -exports.default = _default; | |
| \ No newline at end of file |
lib/form-render-core/src/index.d.ts
deleted
100644 → 0
| 1 | -import * as React from 'react'; | |
| 2 | -export interface Error { | |
| 3 | - /** 错误的数据路径 */ | |
| 4 | - name: string; | |
| 5 | - /** 错误的内容 */ | |
| 6 | - error: string[]; | |
| 7 | -} | |
| 8 | -export interface FormParams { | |
| 9 | - formData?: any; | |
| 10 | - onChange?: (data: any) => void; | |
| 11 | - onValidate?: (valid: any) => void; | |
| 12 | - showValidate?: boolean; | |
| 13 | - /** 数据分析接口,表单展示完成渲染时触发 */ | |
| 14 | - logOnMount?: (stats: any) => void; | |
| 15 | - /** 数据分析接口,表单提交成功时触发,获得本次表单填写的总时长 */ | |
| 16 | - logOnSubmit?: (stats: any) => void; | |
| 17 | -} | |
| 18 | - | |
| 19 | -export interface ValidateParams { | |
| 20 | - formData: any; | |
| 21 | - schema: any; | |
| 22 | - error: Error[]; | |
| 23 | - [k: string]: any; | |
| 24 | -} | |
| 25 | -export interface FormInstance { | |
| 26 | - formData: any; | |
| 27 | - schema: any; | |
| 28 | - flatten: any; | |
| 29 | - touchedKeys: string[]; | |
| 30 | - touchKey: (key: string) => void; | |
| 31 | - onItemChange: (path: string, value: any) => void; | |
| 32 | - setValueByPath: (path: string, value: any) => void; | |
| 33 | - getSchemaByPath: (path: string) => object; | |
| 34 | - setSchemaByPath: (path: string, value: any) => void; | |
| 35 | - setSchema: (settings: any) => void; | |
| 36 | - setValues: (formData: any) => void; | |
| 37 | - getValues: () => any; | |
| 38 | - resetFields: () => void; | |
| 39 | - submit: () => Promise<void> | Promise<any[]>; | |
| 40 | - submitData: any; | |
| 41 | - errorFields: Error[]; | |
| 42 | - isValidating: boolean; | |
| 43 | - outsideValidating: boolean; | |
| 44 | - isSubmitting: boolean; | |
| 45 | - endValidating: () => void; | |
| 46 | - endSubmitting: () => void; | |
| 47 | - setErrorFields: (error: Error[]) => void; | |
| 48 | - removeErrorField: (path: string) => void; | |
| 49 | - removeTouched: (path: string) => void; | |
| 50 | - changeTouchedKeys: (pathArray: string[]) => void; | |
| 51 | - isEditing: boolean; | |
| 52 | - setEditing: (status: boolean) => void; | |
| 53 | - syncStuff: (args: any) => void; | |
| 54 | - /** 折中升级方案中使用到,正常用不到 */ | |
| 55 | - init: () => void; | |
| 56 | - /** 数据分析接口,表单展示完成渲染时触发 */ | |
| 57 | - logOnMount: (args: any) => void; | |
| 58 | - /** 数据分析接口,表单提交成功时触发,获得本次表单填写的总时长 */ | |
| 59 | - logOnSubmit: (args: any) => void; | |
| 60 | - _setErrors: (args: any) => void; | |
| 61 | -} | |
| 62 | - | |
| 63 | -export type WatchProperties = { | |
| 64 | - [path: string]: | |
| 65 | - | { | |
| 66 | - handler: (value: any) => void; | |
| 67 | - immediate?: boolean; | |
| 68 | - } | |
| 69 | - | ((value: any) => void); | |
| 70 | -}; | |
| 71 | - | |
| 72 | -export interface FRProps { | |
| 73 | - /** 表单 id */ | |
| 74 | - id?: string | number; | |
| 75 | - /** 表单顶层的className */ | |
| 76 | - className?: string; | |
| 77 | - /** 表单顶层的样式 */ | |
| 78 | - style?: any; | |
| 79 | - /** 表单 schema */ | |
| 80 | - schema: any; | |
| 81 | - /** form单例 */ | |
| 82 | - form: FormInstance; | |
| 83 | - /** 组件和schema的映射规则 */ | |
| 84 | - mapping?: any; | |
| 85 | - /** 自定义组件 */ | |
| 86 | - widgets?: any; | |
| 87 | - /** 表单提交前钩子 */ | |
| 88 | - displayType?: string; | |
| 89 | - /** 只读模式 */ | |
| 90 | - readOnly?: boolean; | |
| 91 | - /** 禁用模式 */ | |
| 92 | - disabled?: boolean; | |
| 93 | - /** 标签宽度 */ | |
| 94 | - labelWidth?: string | number; | |
| 95 | - /** antd的全局config */ | |
| 96 | - configProvider?: any; | |
| 97 | - theme?: string | number; | |
| 98 | - /** 覆盖默认的校验信息 */ | |
| 99 | - validateMessages?: any; | |
| 100 | - /** 显示当前表单内部状态 */ | |
| 101 | - debug?: boolean; | |
| 102 | - /** 显示css布局提示线 */ | |
| 103 | - debugCss?: boolean; | |
| 104 | - locale?: string; | |
| 105 | - column?: number; | |
| 106 | - debounceInput?: boolean; | |
| 107 | - size?: string; | |
| 108 | - // 数据会作为 beforeFinish 的第四个参数传入 | |
| 109 | - config?: any; | |
| 110 | - // 类似于 vuejs 的 watch 的用法,监控值的变化,触发 callback | |
| 111 | - watch?: WatchProperties; | |
| 112 | - /** 对象组件是否折叠(全局的控制) */ | |
| 113 | - allCollapsed?: boolean; | |
| 114 | - /** 表单的全局共享属性 */ | |
| 115 | - globalProps?: any; | |
| 116 | - /** 表单首次加载钩子 */ | |
| 117 | - onMount?: () => void; | |
| 118 | - /** 表单提交前钩子 */ | |
| 119 | - beforeFinish?: (params: ValidateParams) => Error[] | Promise<Error[]>; | |
| 120 | - /** 表单提交后钩子 */ | |
| 121 | - onFinish?: (formData: any, error: Error[]) => void; | |
| 122 | - /** 时时与外部更新同步的钩子 */ | |
| 123 | - onValuesChange?: (changedValues: any, formData: any) => void; | |
| 124 | - /** 隐藏的数据是否去掉,默认不去掉(false) */ | |
| 125 | - removeHiddenData?: boolean; | |
| 126 | -} | |
| 127 | - | |
| 128 | -declare const FR: React.FC<FRProps>; | |
| 129 | - | |
| 130 | -export declare function useForm(params?: FormParams): FormInstance; | |
| 131 | - | |
| 132 | -export type ConnectedForm<T> = T & { | |
| 133 | - form: FormInstance; | |
| 134 | -}; | |
| 135 | - | |
| 136 | -export declare function connectForm<T extends {} = any>( | |
| 137 | - component: React.ComponentType<ConnectedForm<T>> | |
| 138 | -): React.ComponentType<T>; | |
| 139 | - | |
| 140 | -export default FR; |
lib/form-render-core/src/index.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -Object.defineProperty(exports, "connectForm", { | |
| 7 | - enumerable: true, | |
| 8 | - get: function get() { | |
| 9 | - return _connectForm.default; | |
| 10 | - } | |
| 11 | -}); | |
| 12 | -Object.defineProperty(exports, "createWidget", { | |
| 13 | - enumerable: true, | |
| 14 | - get: function get() { | |
| 15 | - return _createWidget.createWidget; | |
| 16 | - } | |
| 17 | -}); | |
| 18 | -exports.default = void 0; | |
| 19 | -Object.defineProperty(exports, "mapping", { | |
| 20 | - enumerable: true, | |
| 21 | - get: function get() { | |
| 22 | - return _mapping.mapping; | |
| 23 | - } | |
| 24 | -}); | |
| 25 | -Object.defineProperty(exports, "useForm", { | |
| 26 | - enumerable: true, | |
| 27 | - get: function get() { | |
| 28 | - return _useForm.default; | |
| 29 | - } | |
| 30 | -}); | |
| 31 | - | |
| 32 | -var _react = _interopRequireWildcard(require("react")); | |
| 33 | - | |
| 34 | -var _utils = require("./utils"); | |
| 35 | - | |
| 36 | -var _core = _interopRequireDefault(require("./core")); | |
| 37 | - | |
| 38 | -var _Watcher = _interopRequireDefault(require("./Watcher")); | |
| 39 | - | |
| 40 | -var _hooks = require("./hooks"); | |
| 41 | - | |
| 42 | -require("./atom.css"); | |
| 43 | - | |
| 44 | -require("./index.css"); | |
| 45 | - | |
| 46 | -var _mapping = require("./mapping"); | |
| 47 | - | |
| 48 | -var _useForm = _interopRequireDefault(require("./useForm")); | |
| 49 | - | |
| 50 | -var _connectForm = _interopRequireDefault(require("./connectForm")); | |
| 51 | - | |
| 52 | -var _createWidget = require("./createWidget"); | |
| 53 | - | |
| 54 | -var _excluded = ["id", "widgets", "mapping", "form", "className", "style", "beforeFinish", "onFinish", "displayType", "schema", "debug", "debugCss", "locale", "debounceInput", "size", "configProvider", "theme", "validateMessages", "watch", "config", "onMount", "labelWidth", "readOnly", "disabled", "allCollapsed", "onValuesChange", "column", "removeHiddenData", "globalProps"], | |
| 55 | - _excluded2 = ["onItemChange", "setEditing", "touchKey", "setValueByPath", "getSchemaByPath", "setSchemaByPath", "setSchema", "setValues", "getValues", "resetFields", "submit", "endValidating", "endSubmitting", "setErrorFields", "removeErrorField", "removeTouched", "changeTouchedKeys", "syncStuff", "logOnMount", "logOnSubmit", "setFirstMount", "_setErrors"], | |
| 56 | - _excluded3 = ["isOldVersion", "schema"]; | |
| 57 | - | |
| 58 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 59 | - | |
| 60 | -function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | |
| 61 | - | |
| 62 | -function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | |
| 63 | - | |
| 64 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 65 | - | |
| 66 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 67 | - | |
| 68 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 69 | - | |
| 70 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 71 | - | |
| 72 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 73 | - | |
| 74 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 75 | - | |
| 76 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 77 | - | |
| 78 | -var defaultFinish = function defaultFinish(data, errors) { | |
| 79 | - console.log('onFinish:', { | |
| 80 | - data: data, | |
| 81 | - errors: errors | |
| 82 | - }); | |
| 83 | -}; | |
| 84 | - | |
| 85 | -function App(_ref) { | |
| 86 | - var id = _ref.id, | |
| 87 | - widgets = _ref.widgets, | |
| 88 | - mapping = _ref.mapping, | |
| 89 | - form = _ref.form, | |
| 90 | - className = _ref.className, | |
| 91 | - style = _ref.style, | |
| 92 | - beforeFinish = _ref.beforeFinish, | |
| 93 | - _ref$onFinish = _ref.onFinish, | |
| 94 | - onFinish = _ref$onFinish === void 0 ? defaultFinish : _ref$onFinish, | |
| 95 | - _ref$displayType = _ref.displayType, | |
| 96 | - displayType = _ref$displayType === void 0 ? 'column' : _ref$displayType, | |
| 97 | - schema = _ref.schema, | |
| 98 | - debug = _ref.debug, | |
| 99 | - debugCss = _ref.debugCss, | |
| 100 | - _ref$locale = _ref.locale, | |
| 101 | - locale = _ref$locale === void 0 ? 'cn' : _ref$locale, | |
| 102 | - _ref$debounceInput = _ref.debounceInput, | |
| 103 | - debounceInput = _ref$debounceInput === void 0 ? false : _ref$debounceInput, | |
| 104 | - size = _ref.size, | |
| 105 | - configProvider = _ref.configProvider, | |
| 106 | - theme = _ref.theme, | |
| 107 | - validateMessages = _ref.validateMessages, | |
| 108 | - _ref$watch = _ref.watch, | |
| 109 | - watch = _ref$watch === void 0 ? {} : _ref$watch, | |
| 110 | - config = _ref.config, | |
| 111 | - onMount = _ref.onMount, | |
| 112 | - labelWidth = _ref.labelWidth, | |
| 113 | - readOnly = _ref.readOnly, | |
| 114 | - disabled = _ref.disabled, | |
| 115 | - _ref$allCollapsed = _ref.allCollapsed, | |
| 116 | - allCollapsed = _ref$allCollapsed === void 0 ? false : _ref$allCollapsed, | |
| 117 | - onValuesChange = _ref.onValuesChange, | |
| 118 | - column = _ref.column, | |
| 119 | - _ref$removeHiddenData = _ref.removeHiddenData, | |
| 120 | - removeHiddenData = _ref$removeHiddenData === void 0 ? false : _ref$removeHiddenData, | |
| 121 | - _ref$globalProps = _ref.globalProps, | |
| 122 | - globalProps = _ref$globalProps === void 0 ? {} : _ref$globalProps, | |
| 123 | - rest = _objectWithoutProperties(_ref, _excluded); | |
| 124 | - | |
| 125 | - try { | |
| 126 | - var _ = form.submit; | |
| 127 | - } catch (error) { | |
| 128 | - console.error('form 为必填 props,<FormRender /> 没有接收到 form 属性!'); | |
| 129 | - } | |
| 130 | - | |
| 131 | - var _column = schema && schema.column || column; | |
| 132 | - | |
| 133 | - var onItemChange = form.onItemChange, | |
| 134 | - setEditing = form.setEditing, | |
| 135 | - touchKey = form.touchKey, | |
| 136 | - setValueByPath = form.setValueByPath, | |
| 137 | - getSchemaByPath = form.getSchemaByPath, | |
| 138 | - setSchemaByPath = form.setSchemaByPath, | |
| 139 | - setSchema = form.setSchema, | |
| 140 | - setValues = form.setValues, | |
| 141 | - getValues = form.getValues, | |
| 142 | - resetFields = form.resetFields, | |
| 143 | - submit = form.submit, | |
| 144 | - endValidating = form.endValidating, | |
| 145 | - endSubmitting = form.endSubmitting, | |
| 146 | - setErrorFields = form.setErrorFields, | |
| 147 | - removeErrorField = form.removeErrorField, | |
| 148 | - removeTouched = form.removeTouched, | |
| 149 | - changeTouchedKeys = form.changeTouchedKeys, | |
| 150 | - syncStuff = form.syncStuff, | |
| 151 | - logOnMount = form.logOnMount, | |
| 152 | - logOnSubmit = form.logOnSubmit, | |
| 153 | - setFirstMount = form.setFirstMount, | |
| 154 | - _setErrors = form._setErrors, | |
| 155 | - valuesThatWillChange = _objectWithoutProperties(form, _excluded2); | |
| 156 | - | |
| 157 | - var submitData = valuesThatWillChange.submitData, | |
| 158 | - errorFields = valuesThatWillChange.errorFields, | |
| 159 | - isValidating = valuesThatWillChange.isValidating, | |
| 160 | - outsideValidating = valuesThatWillChange.outsideValidating, | |
| 161 | - isSubmitting = valuesThatWillChange.isSubmitting, | |
| 162 | - formData = valuesThatWillChange.formData, | |
| 163 | - flatten = valuesThatWillChange.flatten, | |
| 164 | - showValidate = valuesThatWillChange.showValidate, | |
| 165 | - firstMount = valuesThatWillChange.firstMount; | |
| 166 | - (0, _react.useEffect)(function () { | |
| 167 | - // Schema最外层的type是object来判断,没有的话,认为schema没有传 | |
| 168 | - if (schema && schema.type) { | |
| 169 | - setFirstMount(true); | |
| 170 | - syncStuff({ | |
| 171 | - schema: schema, | |
| 172 | - locale: locale, | |
| 173 | - validateMessages: validateMessages, | |
| 174 | - beforeFinish: beforeFinish, | |
| 175 | - onMount: onMount, | |
| 176 | - removeHiddenData: removeHiddenData | |
| 177 | - }); | |
| 178 | - } else {} | |
| 179 | - }, [JSON.stringify(schema)]); | |
| 180 | - (0, _react.useEffect)(function () { | |
| 181 | - if (!firstMount && schema && schema.type) { | |
| 182 | - if (typeof onMount === 'function') { | |
| 183 | - // 等一下 useForm 里接到第一份schema时,计算第一份data的骨架 | |
| 184 | - setTimeout(function () { | |
| 185 | - onMount(); | |
| 186 | - }, 0); | |
| 187 | - } | |
| 188 | - | |
| 189 | - setTimeout(onMountLogger, 0); | |
| 190 | - } | |
| 191 | - }, [JSON.stringify(schema), firstMount]); | |
| 192 | - | |
| 193 | - var onMountLogger = function onMountLogger() { | |
| 194 | - var start = new Date().getTime(); | |
| 195 | - | |
| 196 | - if (typeof logOnMount === 'function' || typeof logOnSubmit === 'function') { | |
| 197 | - sessionStorage.setItem('FORM_MOUNT_TIME', start); | |
| 198 | - sessionStorage.setItem('FORM_START', start); | |
| 199 | - } | |
| 200 | - | |
| 201 | - if (typeof logOnMount === 'function') { | |
| 202 | - var logParams = { | |
| 203 | - schema: schema, | |
| 204 | - url: location.href, | |
| 205 | - formData: JSON.stringify(form.getValues()), | |
| 206 | - formMount: (0, _utils.yymmdd)(start) | |
| 207 | - }; | |
| 208 | - | |
| 209 | - if (id) { | |
| 210 | - logParams.id = id; | |
| 211 | - } | |
| 212 | - | |
| 213 | - logOnMount(logParams); | |
| 214 | - } // 如果是要计算时间,在 onMount 时存一个时间戳 | |
| 215 | - | |
| 216 | - | |
| 217 | - if (typeof logOnSubmit === 'function') { | |
| 218 | - sessionStorage.setItem('NUMBER_OF_SUBMITS', 0); | |
| 219 | - sessionStorage.setItem('FAILED_ATTEMPTS', 0); | |
| 220 | - } | |
| 221 | - }; // 组件destroy的时候,destroy form,因为useForm可能在上层,所以不一定会跟着destroy | |
| 222 | - | |
| 223 | - | |
| 224 | - (0, _react.useEffect)(function () { | |
| 225 | - return function () { | |
| 226 | - form.resetFields(); | |
| 227 | - }; | |
| 228 | - }, []); | |
| 229 | - var store = (0, _react.useMemo)(function () { | |
| 230 | - return _objectSpread(_objectSpread({}, valuesThatWillChange), {}, { | |
| 231 | - globalProps: globalProps | |
| 232 | - }, rest); | |
| 233 | - }, [JSON.stringify(flatten), JSON.stringify(formData), JSON.stringify(errorFields), JSON.stringify(globalProps)]); // 不常用的context单独放一个地方 | |
| 234 | - | |
| 235 | - var store2 = (0, _react.useMemo)(function () { | |
| 236 | - return { | |
| 237 | - displayType: displayType, | |
| 238 | - theme: theme, | |
| 239 | - column: _column, | |
| 240 | - debounceInput: debounceInput, | |
| 241 | - debug: debug, | |
| 242 | - labelWidth: labelWidth, | |
| 243 | - locale: locale, | |
| 244 | - validateMessages: validateMessages, | |
| 245 | - readOnly: readOnly, | |
| 246 | - disabled: disabled, | |
| 247 | - allCollapsed: allCollapsed, | |
| 248 | - showValidate: showValidate | |
| 249 | - }; | |
| 250 | - }, [displayType, theme, _column, debounceInput, debug, labelWidth, locale, validateMessages, readOnly, disabled, allCollapsed, showValidate]); | |
| 251 | - var tools = (0, _react.useMemo)(function () { | |
| 252 | - return _objectSpread({ | |
| 253 | - widgets: widgets, | |
| 254 | - mapping: _objectSpread(_objectSpread({}, _mapping.mapping), mapping), | |
| 255 | - onValuesChange: onValuesChange | |
| 256 | - }, form); | |
| 257 | - }, []); | |
| 258 | - (0, _react.useEffect)(function () { | |
| 259 | - // 需要外部校验的情况,此时 submitting 还是 false | |
| 260 | - if (outsideValidating === true) { | |
| 261 | - Promise.resolve(beforeFinish(_objectSpread({ | |
| 262 | - data: submitData, | |
| 263 | - schema: schema, | |
| 264 | - errors: errorFields | |
| 265 | - }, config))).then(function (error) { | |
| 266 | - if (error) { | |
| 267 | - setErrorFields(error); | |
| 268 | - } | |
| 269 | - | |
| 270 | - endValidating(); | |
| 271 | - }); | |
| 272 | - return; | |
| 273 | - } // 如果validation结束,submitting开始 | |
| 274 | - | |
| 275 | - | |
| 276 | - if (isValidating === false && isSubmitting === true) { | |
| 277 | - endSubmitting(); | |
| 278 | - onFinish(submitData, errorFields); | |
| 279 | - | |
| 280 | - if (typeof logOnSubmit === 'function') { | |
| 281 | - var start = sessionStorage.getItem('FORM_START'); | |
| 282 | - var mount = sessionStorage.getItem('FORM_MOUNT_TIME'); | |
| 283 | - var numberOfSubmits = Number(sessionStorage.getItem('NUMBER_OF_SUBMITS')) + 1; | |
| 284 | - var end = new Date().getTime(); | |
| 285 | - var failedAttempts = Number(sessionStorage.getItem('FAILED_ATTEMPTS')); | |
| 286 | - | |
| 287 | - if (errorFields.length > 0) { | |
| 288 | - failedAttempts = failedAttempts + 1; | |
| 289 | - } | |
| 290 | - | |
| 291 | - var logParams = { | |
| 292 | - formMount: (0, _utils.yymmdd)(mount), | |
| 293 | - ms: end - start, | |
| 294 | - duration: (0, _utils.msToTime)(end - start), | |
| 295 | - numberOfSubmits: numberOfSubmits, | |
| 296 | - failedAttempts: failedAttempts, | |
| 297 | - url: location.href, | |
| 298 | - formData: JSON.stringify(submitData), | |
| 299 | - errors: JSON.stringify(errorFields), | |
| 300 | - schema: JSON.stringify(schema) | |
| 301 | - }; | |
| 302 | - | |
| 303 | - if (id) { | |
| 304 | - logParams.id = id; | |
| 305 | - } | |
| 306 | - | |
| 307 | - logOnSubmit(logParams); | |
| 308 | - sessionStorage.setItem('FORM_START', end); | |
| 309 | - sessionStorage.setItem('NUMBER_OF_SUBMITS', numberOfSubmits); | |
| 310 | - sessionStorage.setItem('FAILED_ATTEMPTS', failedAttempts); | |
| 311 | - } | |
| 312 | - } | |
| 313 | - }, [isValidating, isSubmitting, outsideValidating]); // TODO: fk doesn't work | |
| 314 | - | |
| 315 | - var sizeCls = ''; | |
| 316 | - | |
| 317 | - if (size === 'small') { | |
| 318 | - sizeCls = 'fr-form-small'; | |
| 319 | - } else if (size === 'large') { | |
| 320 | - sizeCls = 'fr-form-large'; | |
| 321 | - } | |
| 322 | - | |
| 323 | - var rootProps = { | |
| 324 | - className: "fr-container ".concat(sizeCls, " ").concat(className || '') | |
| 325 | - }; | |
| 326 | - | |
| 327 | - if (style && _typeof(style) === 'object') { | |
| 328 | - rootProps.style = style; | |
| 329 | - } | |
| 330 | - | |
| 331 | - if (id && ['number', 'string'].indexOf(_typeof(id)) > -1) { | |
| 332 | - rootProps.id = id; | |
| 333 | - } | |
| 334 | - | |
| 335 | - var debugForm = (0, _utils.getParamByName)('_debug_form'); | |
| 336 | - var debugFormCss = (0, _utils.getParamByName)('_debug_form_css'); | |
| 337 | - var isPre = location.href.indexOf('pre') > -1; | |
| 338 | - var watchList = Object.keys(watch); | |
| 339 | - return /*#__PURE__*/_react.default.createElement(_hooks.StoreCtx.Provider, { | |
| 340 | - value: store | |
| 341 | - }, /*#__PURE__*/_react.default.createElement(_hooks.Store2Ctx.Provider, { | |
| 342 | - value: store2 | |
| 343 | - }, /*#__PURE__*/_react.default.createElement(_hooks.Ctx.Provider, { | |
| 344 | - value: tools | |
| 345 | - }, /*#__PURE__*/_react.default.createElement("div", rootProps, isPre && debugForm || debug ? /*#__PURE__*/_react.default.createElement("div", { | |
| 346 | - className: "mv2 bg-black-05 pa2 br2" | |
| 347 | - }, /*#__PURE__*/_react.default.createElement("div", { | |
| 348 | - style: { | |
| 349 | - display: 'flex' | |
| 350 | - } | |
| 351 | - }, /*#__PURE__*/_react.default.createElement("span", null, "formData:"), /*#__PURE__*/_react.default.createElement("span", { | |
| 352 | - style: { | |
| 353 | - display: 'inline-block', | |
| 354 | - wordBreak: 'break-all', | |
| 355 | - maxWidth: 600 | |
| 356 | - } | |
| 357 | - }, JSON.stringify(form.formData))), /*#__PURE__*/_react.default.createElement("div", null, 'errorFields:' + JSON.stringify(form.errorFields)), /*#__PURE__*/_react.default.createElement("div", null, 'touchedKeys:' + JSON.stringify(form.touchedKeys)), /*#__PURE__*/_react.default.createElement("div", null, 'allTouched:' + JSON.stringify(form.allTouched)), /*#__PURE__*/_react.default.createElement("div", null, 'descriptor:' + JSON.stringify(window.descriptor))) : null, watchList.length > 0 ? watchList.map(function (item, idx) { | |
| 358 | - return /*#__PURE__*/_react.default.createElement(_Watcher.default, { | |
| 359 | - key: idx.toString(), | |
| 360 | - watchKey: item, | |
| 361 | - watch: watch, | |
| 362 | - formData: formData, | |
| 363 | - firstMount: firstMount | |
| 364 | - }); | |
| 365 | - }) : null, /*#__PURE__*/_react.default.createElement(_core.default, { | |
| 366 | - debugCss: isPre && debugFormCss || debugCss | |
| 367 | - }))))); | |
| 368 | -} | |
| 369 | - | |
| 370 | -var Wrapper = function Wrapper(props) { | |
| 371 | - var _ref2 = props || {}, | |
| 372 | - _ref2$isOldVersion = _ref2.isOldVersion, | |
| 373 | - isOldVersion = _ref2$isOldVersion === void 0 ? true : _ref2$isOldVersion, | |
| 374 | - schema = _ref2.schema, | |
| 375 | - rest = _objectWithoutProperties(_ref2, _excluded3); | |
| 376 | - | |
| 377 | - var _schema = (0, _react.useRef)(schema); | |
| 378 | - | |
| 379 | - if (isOldVersion) { | |
| 380 | - _schema.current = (0, _utils.updateSchemaToNewVersion)(schema); | |
| 381 | - } | |
| 382 | - | |
| 383 | - return /*#__PURE__*/_react.default.createElement(App, _extends({ | |
| 384 | - schema: _schema.current | |
| 385 | - }, rest)); | |
| 386 | -}; | |
| 387 | - | |
| 388 | -var _default = Wrapper; | |
| 389 | -exports.default = _default; | |
| \ No newline at end of file |
lib/form-render-core/src/mapping.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.extraSchemaList = void 0; | |
| 7 | -exports.getWidgetName = getWidgetName; | |
| 8 | -exports.mapping = void 0; | |
| 9 | -var mapping = { | |
| 10 | - default: 'input', | |
| 11 | - string: 'input', | |
| 12 | - array: 'list', | |
| 13 | - boolean: 'checkbox', | |
| 14 | - integer: 'number', | |
| 15 | - number: 'number', | |
| 16 | - object: 'map', | |
| 17 | - html: 'html', | |
| 18 | - 'string:upload': 'upload', | |
| 19 | - 'string:url': 'url', | |
| 20 | - 'string:dateTime': 'date', | |
| 21 | - 'string:date': 'date', | |
| 22 | - 'string:year': 'date', | |
| 23 | - 'string:month': 'date', | |
| 24 | - 'string:week': 'date', | |
| 25 | - 'string:quarter': 'date', | |
| 26 | - 'string:time': 'time', | |
| 27 | - 'string:textarea': 'textarea', | |
| 28 | - 'string:color': 'color', | |
| 29 | - 'string:image': 'imageInput', | |
| 30 | - 'range:time': 'timeRange', | |
| 31 | - 'range:dateTime': 'dateRange', | |
| 32 | - 'range:date': 'dateRange', | |
| 33 | - 'range:year': 'dateRange', | |
| 34 | - 'range:month': 'dateRange', | |
| 35 | - 'range:week': 'dateRange', | |
| 36 | - 'range:quarter': 'dateRange', | |
| 37 | - '*?enum': 'radio', | |
| 38 | - '*?enum_long': 'select', | |
| 39 | - 'array?enum': 'checkboxes', | |
| 40 | - 'array?enum_long': 'multiSelect', | |
| 41 | - '*?readOnly': 'html' // TODO: html widgets for list / object | |
| 42 | - | |
| 43 | -}; | |
| 44 | -exports.mapping = mapping; | |
| 45 | - | |
| 46 | -function getWidgetName(schema) { | |
| 47 | - var _mapping = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : mapping; | |
| 48 | - | |
| 49 | - var type = schema.type, | |
| 50 | - format = schema.format, | |
| 51 | - enums = schema.enum, | |
| 52 | - readOnly = schema.readOnly, | |
| 53 | - widget = schema.widget; // 如果已经注明了渲染widget,那最好 | |
| 54 | - // if (schema['ui:widget']) { | |
| 55 | - // return schema['ui:widget']; | |
| 56 | - // } | |
| 57 | - | |
| 58 | - var list = []; | |
| 59 | - | |
| 60 | - if (readOnly) { | |
| 61 | - list.push("".concat(type, "?readOnly")); | |
| 62 | - list.push('*?readOnly'); | |
| 63 | - } | |
| 64 | - | |
| 65 | - if (enums) { | |
| 66 | - // 根据enum长度来智能选择控件 | |
| 67 | - if (Array.isArray(enums) && (type === 'array' && enums.length > 6 || type !== 'array' && enums.length > 2)) { | |
| 68 | - list.push("".concat(type, "?enum_long")); | |
| 69 | - list.push('*?enum_long'); | |
| 70 | - } else { | |
| 71 | - list.push("".concat(type, "?enum")); // array 默认使用list,array?enum 默认使用checkboxes,*?enum 默认使用select | |
| 72 | - | |
| 73 | - list.push('*?enum'); | |
| 74 | - } | |
| 75 | - } | |
| 76 | - | |
| 77 | - var _widget = widget || format; | |
| 78 | - | |
| 79 | - if (_widget) { | |
| 80 | - list.push("".concat(type, ":").concat(_widget)); | |
| 81 | - } | |
| 82 | - | |
| 83 | - list.push(type); // 放在最后兜底,其他都不match时使用type默认的组件 | |
| 84 | - | |
| 85 | - var found = ''; | |
| 86 | - list.some(function (item) { | |
| 87 | - found = _mapping[item]; | |
| 88 | - return !!found; | |
| 89 | - }); | |
| 90 | - return found; | |
| 91 | -} | |
| 92 | - | |
| 93 | -var extraSchemaList = { | |
| 94 | - checkbox: { | |
| 95 | - valuePropName: 'checked' | |
| 96 | - }, | |
| 97 | - switch: { | |
| 98 | - valuePropName: 'checked' | |
| 99 | - } | |
| 100 | -}; | |
| 101 | -exports.extraSchemaList = extraSchemaList; | |
| \ No newline at end of file |
lib/form-render-core/src/utils.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.allPromiseFinish = allPromiseFinish; | |
| 7 | -exports.clone = exports.cleanEmpty = void 0; | |
| 8 | -exports.combineSchema = combineSchema; | |
| 9 | -exports.dataToKeys = exports.completeSchemaWithTheme = void 0; | |
| 10 | -exports.defaultGetValueFromEvent = defaultGetValueFromEvent; | |
| 11 | -exports.destructDataPath = destructDataPath; | |
| 12 | -exports.evaluateString = void 0; | |
| 13 | -exports.flattenSchema = flattenSchema; | |
| 14 | -exports.getArray = exports.generateDataSkeleton = void 0; | |
| 15 | -exports.getDataPath = getDataPath; | |
| 16 | -exports.getEnum = exports.getDisplayValue = exports.getDescriptorSimple = void 0; | |
| 17 | -exports.getFormat = getFormat; | |
| 18 | -exports.getKeyFromPath = void 0; | |
| 19 | -exports.getParamByName = getParamByName; | |
| 20 | -exports.getParentPath = getParentPath; | |
| 21 | -exports.getSaveNumber = exports.getParentProps = void 0; | |
| 22 | -exports.getSchemaFromFlatten = getSchemaFromFlatten; | |
| 23 | -exports.getValueByPath = getValueByPath; | |
| 24 | -exports.hasRepeat = hasRepeat; | |
| 25 | -exports.isCheckBoxType = isCheckBoxType; | |
| 26 | -exports.isCssLength = isCssLength; | |
| 27 | -exports.isDeepEqual = isDeepEqual; | |
| 28 | -exports.isEmail = void 0; | |
| 29 | -exports.isExpression = isExpression; | |
| 30 | -exports.isFunctionSchema = isFunctionSchema; | |
| 31 | -exports.isFunctionString = void 0; | |
| 32 | -exports.isListType = isListType; | |
| 33 | -exports.isLooselyNumber = isLooselyNumber; | |
| 34 | -exports.isObjType = isObjType; | |
| 35 | -exports.isObject = void 0; | |
| 36 | -exports.isUrl = isUrl; | |
| 37 | -exports.looseJsonParse = looseJsonParse; | |
| 38 | -exports.msToTime = msToTime; | |
| 39 | -exports.parseAllExpression = exports.oldSchemaToNew = exports.newSchemaToOld = void 0; | |
| 40 | -exports.parseFunction = parseFunction; | |
| 41 | -exports.parseRootValueInSchema = exports.parseFunctionString = void 0; | |
| 42 | -exports.parseSingleExpression = parseSingleExpression; | |
| 43 | -exports.updateSchemaToNewVersion = exports.translateMessage = exports.schemaContainsExpression = exports.removeHiddenFromResult = exports.removeEmptyItemFromList = exports.removeDups = exports.parseString = exports.parseSingleRootValue = void 0; | |
| 44 | -exports.yymmdd = yymmdd; | |
| 45 | - | |
| 46 | -var _lodashEs = require("lodash-es"); | |
| 47 | - | |
| 48 | -var _excluded = ["propsSchema"], | |
| 49 | - _excluded2 = ["schema"]; | |
| 50 | - | |
| 51 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 52 | - | |
| 53 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 54 | - | |
| 55 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 56 | - | |
| 57 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 58 | - | |
| 59 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 60 | - | |
| 61 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 62 | - | |
| 63 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 64 | - | |
| 65 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 66 | - | |
| 67 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 68 | - | |
| 69 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 70 | - | |
| 71 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 72 | - | |
| 73 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 74 | - | |
| 75 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 76 | - | |
| 77 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 78 | - | |
| 79 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 80 | - | |
| 81 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 82 | - | |
| 83 | -// window.log1 = value => { | |
| 84 | -// console.log('%ccommon:', 'color: #00A7F7; font-weight: 500;', value); | |
| 85 | -// }; | |
| 86 | -// window.log2 = value => { | |
| 87 | -// console.log('%cwarning:', 'color: #f50; font-weight: 500;', value); | |
| 88 | -// }; | |
| 89 | -// window.log3 = value => { | |
| 90 | -// console.log('%csuccess:', 'color: #87d068; font-weight: 500;', value); | |
| 91 | -// }; | |
| 92 | -// window.log4 = value => { | |
| 93 | -// console.log('%cspecial:', 'color: #722ed1; font-weight: 500;', value); | |
| 94 | -// }; | |
| 95 | -function getParamByName(name) { | |
| 96 | - var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href; | |
| 97 | - name = name.replace(/[\[\]]/g, '\\$&'); | |
| 98 | - var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), | |
| 99 | - results = regex.exec(url); | |
| 100 | - if (!results) return null; | |
| 101 | - if (!results[2]) return ''; | |
| 102 | - return decodeURIComponent(results[2].replace(/\+/g, ' ')); | |
| 103 | -} | |
| 104 | - | |
| 105 | -function isUrl(string) { | |
| 106 | - var protocolRE = /^(?:\w+:)?\/\/(\S+)$/; // const domainRE = /^[^\s\.]+\.\S{2,}$/; | |
| 107 | - | |
| 108 | - if (typeof string !== 'string') return false; | |
| 109 | - return protocolRE.test(string); | |
| 110 | -} | |
| 111 | - | |
| 112 | -function isCheckBoxType(schema, readOnly) { | |
| 113 | - if (readOnly) return false; | |
| 114 | - if (schema.widget === 'checkbox') return true; | |
| 115 | - | |
| 116 | - if (schema && schema.type === 'boolean') { | |
| 117 | - if (schema.enum) return false; | |
| 118 | - if (schema.widget === undefined) return true; | |
| 119 | - return false; | |
| 120 | - } | |
| 121 | -} // a[].b.c => a.b.c | |
| 122 | - | |
| 123 | - | |
| 124 | -function removeBrackets(string) { | |
| 125 | - if (typeof string === 'string') { | |
| 126 | - return string.replace(/\[\]/g, ''); | |
| 127 | - } else { | |
| 128 | - return string; | |
| 129 | - } | |
| 130 | -} | |
| 131 | - | |
| 132 | -function getParentPath(path) { | |
| 133 | - if (typeof path === 'string') { | |
| 134 | - var pathArr = path.split('.'); | |
| 135 | - | |
| 136 | - if (pathArr.length === 1) { | |
| 137 | - return '#'; | |
| 138 | - } | |
| 139 | - | |
| 140 | - pathArr.pop(); | |
| 141 | - return pathArr.join('.'); | |
| 142 | - } | |
| 143 | - | |
| 144 | - return '#'; | |
| 145 | -} | |
| 146 | - | |
| 147 | -function getValueByPath(formData, path) { | |
| 148 | - if (path === '#' || !path) { | |
| 149 | - return formData || {}; | |
| 150 | - } else if (typeof path === 'string') { | |
| 151 | - return (0, _lodashEs.get)(formData, path); | |
| 152 | - } else { | |
| 153 | - console.error('path has to be a string'); | |
| 154 | - } | |
| 155 | -} // path: 'a.b[1].c[0]' => { id: 'a.b[].c[]' dataIndex: [1,0] } | |
| 156 | - | |
| 157 | - | |
| 158 | -function destructDataPath(path) { | |
| 159 | - var id; | |
| 160 | - var dataIndex; | |
| 161 | - | |
| 162 | - if (path === '#') { | |
| 163 | - return { | |
| 164 | - id: '#', | |
| 165 | - dataIndex: [] | |
| 166 | - }; | |
| 167 | - } | |
| 168 | - | |
| 169 | - if (typeof path !== 'string') { | |
| 170 | - throw Error("path ".concat(path, " is not a string!!! Something wrong here")); | |
| 171 | - } | |
| 172 | - | |
| 173 | - var pattern = /\[[0-9]+\]/g; | |
| 174 | - var matchList = path.match(pattern); | |
| 175 | - | |
| 176 | - if (!matchList) { | |
| 177 | - id = path; | |
| 178 | - } else { | |
| 179 | - id = path.replace(pattern, '[]'); // 这个是match下来的结果,可安全处理 | |
| 180 | - | |
| 181 | - dataIndex = matchList.map(function (item) { | |
| 182 | - return Number(item.substring(1, item.length - 1)); | |
| 183 | - }); | |
| 184 | - } | |
| 185 | - | |
| 186 | - return { | |
| 187 | - id: id, | |
| 188 | - dataIndex: dataIndex | |
| 189 | - }; | |
| 190 | -} // id: 'a.b[].c[]' dataIndex: [1,0] => 'a.b[1].c[0]' | |
| 191 | - | |
| 192 | - | |
| 193 | -function getDataPath(id, dataIndex) { | |
| 194 | - if (id === '#') { | |
| 195 | - return id; | |
| 196 | - } | |
| 197 | - | |
| 198 | - if (typeof id !== 'string') { | |
| 199 | - throw Error("id ".concat(id, " is not a string!!! Something wrong here")); | |
| 200 | - } | |
| 201 | - | |
| 202 | - var _id = id; | |
| 203 | - | |
| 204 | - if (Array.isArray(dataIndex)) { | |
| 205 | - // const matches = id.match(/\[\]/g) || []; | |
| 206 | - // const count = matches.length; | |
| 207 | - dataIndex.forEach(function (item) { | |
| 208 | - _id = _id.replace(/\[\]/, "[".concat(item, "]")); | |
| 209 | - }); | |
| 210 | - } | |
| 211 | - | |
| 212 | - return removeBrackets(_id); | |
| 213 | -} | |
| 214 | - | |
| 215 | -function isObjType(schema) { | |
| 216 | - return schema && schema.type === 'object' && schema.properties && !schema.widget; | |
| 217 | -} // TODO: to support case that item is not an object | |
| 218 | - | |
| 219 | - | |
| 220 | -function isListType(schema) { | |
| 221 | - return schema && schema.type === 'array' && isObjType(schema.items) && schema.enum === undefined; | |
| 222 | -} // TODO: more tests to make sure weird & wrong schema won't crush | |
| 223 | - | |
| 224 | - | |
| 225 | -function flattenSchema() { | |
| 226 | - var _schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 227 | - | |
| 228 | - var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 229 | - var parent = arguments.length > 2 ? arguments[2] : undefined; | |
| 230 | - var result = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | |
| 231 | - var schema = clone(_schema); | |
| 232 | - var _name = name; | |
| 233 | - | |
| 234 | - if (!schema.$id) { | |
| 235 | - schema.$id = _name; // path as $id, for easy access to path in schema | |
| 236 | - } | |
| 237 | - | |
| 238 | - var children = []; | |
| 239 | - | |
| 240 | - if (isObjType(schema)) { | |
| 241 | - Object.entries(schema.properties).forEach(function (_ref) { | |
| 242 | - var _ref2 = _slicedToArray(_ref, 2), | |
| 243 | - key = _ref2[0], | |
| 244 | - value = _ref2[1]; | |
| 245 | - | |
| 246 | - var _key = isListType(value) ? key + '[]' : key; | |
| 247 | - | |
| 248 | - var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 249 | - children.push(uniqueName); | |
| 250 | - flattenSchema(value, uniqueName, _name, result); | |
| 251 | - }); | |
| 252 | - schema.properties = {}; | |
| 253 | - } | |
| 254 | - | |
| 255 | - if (isListType(schema)) { | |
| 256 | - Object.entries(schema.items.properties).forEach(function (_ref3) { | |
| 257 | - var _ref4 = _slicedToArray(_ref3, 2), | |
| 258 | - key = _ref4[0], | |
| 259 | - value = _ref4[1]; | |
| 260 | - | |
| 261 | - var _key = isListType(value) ? key + '[]' : key; | |
| 262 | - | |
| 263 | - var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 264 | - children.push(uniqueName); | |
| 265 | - flattenSchema(value, uniqueName, _name, result); | |
| 266 | - }); | |
| 267 | - schema.items.properties = {}; | |
| 268 | - } | |
| 269 | - | |
| 270 | - if (schema.type) { | |
| 271 | - result[_name] = { | |
| 272 | - parent: parent, | |
| 273 | - schema: schema, | |
| 274 | - children: children | |
| 275 | - }; | |
| 276 | - } | |
| 277 | - | |
| 278 | - return result; | |
| 279 | -} | |
| 280 | - | |
| 281 | -function getSchemaFromFlatten(flatten) { | |
| 282 | - var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 283 | - var schema = {}; | |
| 284 | - var item = clone(flatten[path]); | |
| 285 | - | |
| 286 | - if (item) { | |
| 287 | - schema = item.schema; // schema.$id && delete schema.$id; | |
| 288 | - | |
| 289 | - if (item.children.length > 0) { | |
| 290 | - item.children.forEach(function (child) { | |
| 291 | - if (!flatten[child]) return; | |
| 292 | - var key = getKeyFromPath(child); | |
| 293 | - | |
| 294 | - if (isObjType(schema)) { | |
| 295 | - schema.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 296 | - } | |
| 297 | - | |
| 298 | - if (isListType(schema)) { | |
| 299 | - schema.items.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 300 | - } | |
| 301 | - }); | |
| 302 | - } | |
| 303 | - } | |
| 304 | - | |
| 305 | - return schema; | |
| 306 | -} | |
| 307 | - | |
| 308 | -function stringContains(str, text) { | |
| 309 | - return str.indexOf(text) > -1; | |
| 310 | -} | |
| 311 | - | |
| 312 | -var isObject = function isObject(a) { | |
| 313 | - return stringContains(Object.prototype.toString.call(a), 'Object'); | |
| 314 | -}; | |
| 315 | - | |
| 316 | -exports.isObject = isObject; | |
| 317 | -var clone = _lodashEs.cloneDeep; // '3' => true, 3 => true, undefined => false | |
| 318 | - | |
| 319 | -exports.clone = clone; | |
| 320 | - | |
| 321 | -function isLooselyNumber(num) { | |
| 322 | - if (typeof num === 'number') return true; | |
| 323 | - | |
| 324 | - if (typeof num === 'string') { | |
| 325 | - return !Number.isNaN(Number(num)); | |
| 326 | - } | |
| 327 | - | |
| 328 | - return false; | |
| 329 | -} | |
| 330 | - | |
| 331 | -function isCssLength(str) { | |
| 332 | - if (typeof str !== 'string') return false; | |
| 333 | - return str.match(/^([0-9])*(%|px|rem|em)$/i); | |
| 334 | -} | |
| 335 | - | |
| 336 | -function isDeepEqual(param1, param2) { | |
| 337 | - if (param1 === undefined && param2 === undefined) return true;else if (param1 === undefined || param2 === undefined) return false; | |
| 338 | - if (param1 === null && param2 === null) return true;else if (param1 === null || param2 === null) return false;else if (param1.constructor !== param2.constructor) return false; | |
| 339 | - | |
| 340 | - if (param1.constructor === Array) { | |
| 341 | - if (param1.length !== param2.length) return false; | |
| 342 | - | |
| 343 | - for (var i = 0; i < param1.length; i++) { | |
| 344 | - if (param1[i].constructor === Array || param1[i].constructor === Object) { | |
| 345 | - if (!isDeepEqual(param1[i], param2[i])) return false; | |
| 346 | - } else if (param1[i] !== param2[i]) return false; | |
| 347 | - } | |
| 348 | - } else if (param1.constructor === Object) { | |
| 349 | - if (Object.keys(param1).length !== Object.keys(param2).length) return false; | |
| 350 | - | |
| 351 | - for (var _i2 = 0; _i2 < Object.keys(param1).length; _i2++) { | |
| 352 | - var key = Object.keys(param1)[_i2]; | |
| 353 | - | |
| 354 | - if (param1[key] && typeof param1[key] !== 'number' && (param1[key].constructor === Array || param1[key].constructor === Object)) { | |
| 355 | - if (!isDeepEqual(param1[key], param2[key])) return false; | |
| 356 | - } else if (param1[key] !== param2[key]) return false; | |
| 357 | - } | |
| 358 | - } else if (param1.constructor === String || param1.constructor === Number) { | |
| 359 | - return param1 === param2; | |
| 360 | - } | |
| 361 | - | |
| 362 | - return true; | |
| 363 | -} | |
| 364 | - | |
| 365 | -function getFormat(format) { | |
| 366 | - var dateFormat; | |
| 367 | - | |
| 368 | - switch (format) { | |
| 369 | - case 'date': | |
| 370 | - dateFormat = 'YYYY-MM-DD'; | |
| 371 | - break; | |
| 372 | - | |
| 373 | - case 'time': | |
| 374 | - dateFormat = 'HH:mm:ss'; | |
| 375 | - break; | |
| 376 | - | |
| 377 | - case 'dateTime': | |
| 378 | - dateFormat = 'YYYY-MM-DD HH:mm:ss'; | |
| 379 | - break; | |
| 380 | - | |
| 381 | - case 'week': | |
| 382 | - dateFormat = 'YYYY-w'; | |
| 383 | - break; | |
| 384 | - | |
| 385 | - case 'year': | |
| 386 | - dateFormat = 'YYYY'; | |
| 387 | - break; | |
| 388 | - | |
| 389 | - case 'quarter': | |
| 390 | - dateFormat = 'YYYY-Q'; | |
| 391 | - break; | |
| 392 | - | |
| 393 | - case 'month': | |
| 394 | - dateFormat = 'YYYY-MM'; | |
| 395 | - break; | |
| 396 | - | |
| 397 | - default: | |
| 398 | - // dateTime | |
| 399 | - if (typeof format === 'string') { | |
| 400 | - dateFormat = format; | |
| 401 | - } else { | |
| 402 | - dateFormat = 'YYYY-MM-DD'; | |
| 403 | - } | |
| 404 | - | |
| 405 | - } | |
| 406 | - | |
| 407 | - return dateFormat; | |
| 408 | -} | |
| 409 | - | |
| 410 | -function hasRepeat(list) { | |
| 411 | - return list.find(function (x, i, self) { | |
| 412 | - return i !== self.findIndex(function (y) { | |
| 413 | - return JSON.stringify(x) === JSON.stringify(y); | |
| 414 | - }); | |
| 415 | - }); | |
| 416 | -} | |
| 417 | - | |
| 418 | -function combineSchema() { | |
| 419 | - var propsSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 420 | - var uiSchema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 421 | - var propList = getChildren(propsSchema); | |
| 422 | - var newList = propList.map(function (p) { | |
| 423 | - var name = p.name; | |
| 424 | - var _p$schema = p.schema, | |
| 425 | - type = _p$schema.type, | |
| 426 | - options = _p$schema.enum, | |
| 427 | - properties = _p$schema.properties, | |
| 428 | - items = _p$schema.items; | |
| 429 | - var isObj = type === 'object' && properties; | |
| 430 | - var isArr = type === 'array' && items && !options; // enum + array 代表的多选框,没有sub | |
| 431 | - | |
| 432 | - var ui = name && uiSchema[p.name]; | |
| 433 | - | |
| 434 | - if (!ui) { | |
| 435 | - return p; | |
| 436 | - } // 如果是list,递归合并items | |
| 437 | - | |
| 438 | - | |
| 439 | - if (isArr) { | |
| 440 | - var newItems = combineSchema(items, ui.items || {}); | |
| 441 | - return _objectSpread(_objectSpread({}, p), {}, { | |
| 442 | - schema: _objectSpread(_objectSpread(_objectSpread({}, p.schema), ui), {}, { | |
| 443 | - items: newItems | |
| 444 | - }) | |
| 445 | - }); | |
| 446 | - } // object递归合并整个schema | |
| 447 | - | |
| 448 | - | |
| 449 | - if (isObj) { | |
| 450 | - var newSchema = combineSchema(p.schema, ui); | |
| 451 | - return _objectSpread(_objectSpread({}, p), {}, { | |
| 452 | - schema: newSchema | |
| 453 | - }); | |
| 454 | - } | |
| 455 | - | |
| 456 | - return _objectSpread(_objectSpread({}, p), {}, { | |
| 457 | - schema: _objectSpread(_objectSpread({}, p.schema), ui) | |
| 458 | - }); | |
| 459 | - }); | |
| 460 | - var newObj = {}; | |
| 461 | - newList.forEach(function (s) { | |
| 462 | - newObj[s.name] = s.schema; | |
| 463 | - }); | |
| 464 | - var topLevelUi = {}; | |
| 465 | - Object.keys(uiSchema).forEach(function (key) { | |
| 466 | - if (typeof key === 'string' && key.substring(0, 3) === 'ui:') { | |
| 467 | - topLevelUi[key] = uiSchema[key]; | |
| 468 | - } | |
| 469 | - }); | |
| 470 | - | |
| 471 | - if (isEmpty(newObj)) { | |
| 472 | - return _objectSpread(_objectSpread({}, propsSchema), topLevelUi); | |
| 473 | - } | |
| 474 | - | |
| 475 | - return _objectSpread(_objectSpread(_objectSpread({}, propsSchema), topLevelUi), {}, { | |
| 476 | - properties: newObj | |
| 477 | - }); | |
| 478 | -} | |
| 479 | - | |
| 480 | -function isEmpty(obj) { | |
| 481 | - return Object.keys(obj).length === 0; | |
| 482 | -} | |
| 483 | - | |
| 484 | -function getChildren(schema) { | |
| 485 | - if (!schema) return []; | |
| 486 | - var properties = schema.properties, | |
| 487 | - items = schema.items, | |
| 488 | - type = schema.type; | |
| 489 | - | |
| 490 | - if (!properties && !items) { | |
| 491 | - return []; | |
| 492 | - } | |
| 493 | - | |
| 494 | - var schemaSubs = {}; | |
| 495 | - | |
| 496 | - if (type === 'object') { | |
| 497 | - schemaSubs = properties; | |
| 498 | - } | |
| 499 | - | |
| 500 | - if (type === 'array') { | |
| 501 | - schemaSubs = items; | |
| 502 | - } | |
| 503 | - | |
| 504 | - return Object.keys(schemaSubs).map(function (name) { | |
| 505 | - return { | |
| 506 | - schema: schemaSubs[name], | |
| 507 | - name: name | |
| 508 | - }; | |
| 509 | - }); | |
| 510 | -} | |
| 511 | - | |
| 512 | -var parseString = function parseString(string) { | |
| 513 | - return Function('"use strict";return (' + string + ')')(); | |
| 514 | -}; | |
| 515 | - | |
| 516 | -exports.parseString = parseString; | |
| 517 | - | |
| 518 | -var evaluateString = function evaluateString(string, formData, rootValue) { | |
| 519 | - return Function("\"use strict\";\n const rootValue = ".concat(JSON.stringify(rootValue), ";\n const formData = ").concat(JSON.stringify(formData), ";\n return (").concat(string, ")"))(); | |
| 520 | -}; | |
| 521 | - | |
| 522 | -exports.evaluateString = evaluateString; | |
| 523 | - | |
| 524 | -function isExpression(func) { | |
| 525 | - // if (typeof func === 'function') { | |
| 526 | - // const funcString = func.toString(); | |
| 527 | - // return ( | |
| 528 | - // funcString.indexOf('formData') > -1 || | |
| 529 | - // funcString.indexOf('rootValue') > -1 | |
| 530 | - // ); | |
| 531 | - // } | |
| 532 | - if (typeof func !== 'string') return false; | |
| 533 | - var pattern = /^{{(.+)}}$/; | |
| 534 | - var reg1 = /^{{function\(.+}}$/; // const reg2 = /^{{(.+=>.+)}}$/; | |
| 535 | - | |
| 536 | - if (typeof func === 'string' && func.match(pattern) && !func.match(reg1)) { | |
| 537 | - return true; | |
| 538 | - } | |
| 539 | - | |
| 540 | - return false; | |
| 541 | -} | |
| 542 | - | |
| 543 | -var parseRootValueInSchema = function parseRootValueInSchema(schema, rootValue) { | |
| 544 | - var result = clone(schema); | |
| 545 | - | |
| 546 | - if (isObject(schema)) { | |
| 547 | - Object.keys(schema).forEach(function (key) { | |
| 548 | - var item = schema[key]; | |
| 549 | - | |
| 550 | - if (isObject(item)) { | |
| 551 | - result[key] = parseRootValueInSchema(item, rootValue); | |
| 552 | - } else if (typeof item === 'string') { | |
| 553 | - result[key] = parseSingleRootValue(item, rootValue); | |
| 554 | - } | |
| 555 | - }); | |
| 556 | - } else { | |
| 557 | - console.error('schema is not an object:', schema); | |
| 558 | - } | |
| 559 | - | |
| 560 | - return result; | |
| 561 | -}; // handle rootValue inside List | |
| 562 | - | |
| 563 | - | |
| 564 | -exports.parseRootValueInSchema = parseRootValueInSchema; | |
| 565 | - | |
| 566 | -var parseSingleRootValue = function parseSingleRootValue(expression) { | |
| 567 | - var rootValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 568 | - | |
| 569 | - if (typeof expression === 'string' && expression.indexOf('rootValue') > 0) { | |
| 570 | - var funcBody = expression.substring(2, expression.length - 2); | |
| 571 | - var str = "\n return ".concat(funcBody.replace(/rootValue/g, JSON.stringify(rootValue))); | |
| 572 | - | |
| 573 | - try { | |
| 574 | - return Function(str)(); | |
| 575 | - } catch (error) { | |
| 576 | - console.error(error, 'expression:', expression, 'rootValue:', rootValue); | |
| 577 | - return null; // 如果计算有错误,return null 最合适 | |
| 578 | - } | |
| 579 | - } else { | |
| 580 | - return expression; | |
| 581 | - } | |
| 582 | -}; | |
| 583 | - | |
| 584 | -exports.parseSingleRootValue = parseSingleRootValue; | |
| 585 | - | |
| 586 | -function parseSingleExpression(func) { | |
| 587 | - var formData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 588 | - var dataPath = arguments.length > 2 ? arguments[2] : undefined; | |
| 589 | - var parentPath = getParentPath(dataPath); | |
| 590 | - var parent = getValueByPath(formData, parentPath) || {}; | |
| 591 | - | |
| 592 | - if (typeof func === 'string') { | |
| 593 | - var funcBody = func.substring(2, func.length - 2); | |
| 594 | - var str = "\n return ".concat(funcBody.replace(/formData/g, JSON.stringify(formData)).replace(/rootValue/g, JSON.stringify(parent))); | |
| 595 | - | |
| 596 | - try { | |
| 597 | - return Function(str)(); | |
| 598 | - } catch (error) { | |
| 599 | - console.log(error, func, dataPath); | |
| 600 | - return null; // 如果计算有错误,return null 最合适 | |
| 601 | - } // const funcBody = func.substring(2, func.length - 2); | |
| 602 | - // //TODO: 这样有问题,例如 a.b.indexOf(), 会把 a.b.indexOf 当做值 | |
| 603 | - // const match1 = /formData.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 604 | - // const match2 = /rootValue.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 605 | - // const str = ` | |
| 606 | - // return (${funcBody | |
| 607 | - // .replaceAll(match1, (v, m1) => | |
| 608 | - // JSON.stringify(getValueByPath(formData, m1)) | |
| 609 | - // ) | |
| 610 | - // .replaceAll(match2, (v, m1) => | |
| 611 | - // JSON.stringify(getValueByPath(parent, m1)) | |
| 612 | - // )})`; | |
| 613 | - // try { | |
| 614 | - // return Function(str)(); | |
| 615 | - // } catch (error) { | |
| 616 | - // console.log(error); | |
| 617 | - // return func; | |
| 618 | - // } | |
| 619 | - | |
| 620 | - } else return func; | |
| 621 | -} | |
| 622 | - | |
| 623 | -var schemaContainsExpression = function schemaContainsExpression(schema) { | |
| 624 | - if (isObject(schema)) { | |
| 625 | - return Object.keys(schema).some(function (key) { | |
| 626 | - var value = schema[key]; | |
| 627 | - | |
| 628 | - if (typeof value === 'string') { | |
| 629 | - return isExpression(value); | |
| 630 | - } else if (isObject(value)) { | |
| 631 | - return schemaContainsExpression(value); | |
| 632 | - } else { | |
| 633 | - return false; | |
| 634 | - } | |
| 635 | - }); | |
| 636 | - } | |
| 637 | - | |
| 638 | - return false; | |
| 639 | -}; | |
| 640 | - | |
| 641 | -exports.schemaContainsExpression = schemaContainsExpression; | |
| 642 | - | |
| 643 | -var parseAllExpression = function parseAllExpression(_schema, formData, dataPath) { | |
| 644 | - var schema = clone(_schema); | |
| 645 | - Object.keys(schema).forEach(function (key) { | |
| 646 | - var value = schema[key]; | |
| 647 | - | |
| 648 | - if (isObject(value)) { | |
| 649 | - schema[key] = parseAllExpression(value, formData, dataPath); | |
| 650 | - } else if (isExpression(value)) { | |
| 651 | - schema[key] = parseSingleExpression(value, formData, dataPath); | |
| 652 | - } else if (typeof key === 'string' && key.toLowerCase().indexOf('props') > -1) { | |
| 653 | - // 有可能叫 xxxProps | |
| 654 | - var propsObj = schema[key]; | |
| 655 | - | |
| 656 | - if (isObject(propsObj)) { | |
| 657 | - Object.keys(propsObj).forEach(function (k) { | |
| 658 | - schema[key][k] = parseSingleExpression(propsObj[k], formData, dataPath); | |
| 659 | - }); | |
| 660 | - } | |
| 661 | - } | |
| 662 | - }); | |
| 663 | - return schema; | |
| 664 | -}; | |
| 665 | - | |
| 666 | -exports.parseAllExpression = parseAllExpression; | |
| 667 | - | |
| 668 | -function isFunctionSchema(schema) { | |
| 669 | - return Object.keys(schema).some(function (key) { | |
| 670 | - if (typeof schema[key] === 'function') { | |
| 671 | - return true; | |
| 672 | - } else if (typeof schema[key] === 'string') { | |
| 673 | - return isExpression(schema[key]); | |
| 674 | - } else if (_typeof(schema[key]) === 'object') { | |
| 675 | - return isFunctionSchema(schema[key]); | |
| 676 | - } else { | |
| 677 | - return false; | |
| 678 | - } | |
| 679 | - }); | |
| 680 | -} | |
| 681 | - | |
| 682 | -var getParentProps = function getParentProps(propName, id, flatten) { | |
| 683 | - try { | |
| 684 | - var item = flatten[id]; | |
| 685 | - if (item.schema[propName] !== undefined) return item.schema[propName]; | |
| 686 | - | |
| 687 | - if (item && item.parent) { | |
| 688 | - var parentSchema = flatten[item.parent].schema; | |
| 689 | - | |
| 690 | - if (parentSchema[propName] !== undefined) { | |
| 691 | - return parentSchema[propName]; | |
| 692 | - } else { | |
| 693 | - return getParentProps(propName, item.parent, flatten); | |
| 694 | - } | |
| 695 | - } | |
| 696 | - } catch (error) { | |
| 697 | - return undefined; | |
| 698 | - } | |
| 699 | -}; | |
| 700 | - | |
| 701 | -exports.getParentProps = getParentProps; | |
| 702 | - | |
| 703 | -var getSaveNumber = function getSaveNumber() { | |
| 704 | - var searchStr = localStorage.getItem('SAVES'); | |
| 705 | - | |
| 706 | - if (searchStr) { | |
| 707 | - try { | |
| 708 | - var saves = JSON.parse(searchStr); | |
| 709 | - var length = saves.length; | |
| 710 | - if (length) return length + 1; | |
| 711 | - } catch (error) { | |
| 712 | - return 1; | |
| 713 | - } | |
| 714 | - } else { | |
| 715 | - return 1; | |
| 716 | - } | |
| 717 | -}; | |
| 718 | - | |
| 719 | -exports.getSaveNumber = getSaveNumber; | |
| 720 | - | |
| 721 | -function looseJsonParse(obj) { | |
| 722 | - return Function('"use strict";return (' + obj + ')')(); | |
| 723 | -} | |
| 724 | - | |
| 725 | -var isFunctionString = function isFunctionString(fString) { | |
| 726 | - return typeof fString === 'string' && fString.indexOf('function(') === 0; | |
| 727 | -}; | |
| 728 | - | |
| 729 | -exports.isFunctionString = isFunctionString; | |
| 730 | - | |
| 731 | -function parseFunction(fString) { | |
| 732 | - if (isFunctionString(fString)) { | |
| 733 | - return Function('return ' + fString)(); | |
| 734 | - } | |
| 735 | - | |
| 736 | - return fString; | |
| 737 | -} // 获得propsSchema的children | |
| 738 | -// function getChildren2(schema) { | |
| 739 | -// if (!schema) return []; | |
| 740 | -// const { | |
| 741 | -// // object | |
| 742 | -// properties, | |
| 743 | -// // array | |
| 744 | -// items, | |
| 745 | -// type, | |
| 746 | -// } = schema; | |
| 747 | -// if (!properties && !items) { | |
| 748 | -// return []; | |
| 749 | -// } | |
| 750 | -// let schemaSubs = {}; | |
| 751 | -// if (type === 'object') { | |
| 752 | -// schemaSubs = properties; | |
| 753 | -// } | |
| 754 | -// if (type === 'array') { | |
| 755 | -// schemaSubs = items.properties; | |
| 756 | -// } | |
| 757 | -// return Object.keys(schemaSubs).map(name => ({ | |
| 758 | -// schema: schemaSubs[name], | |
| 759 | -// name, | |
| 760 | -// })); | |
| 761 | -// } | |
| 762 | - | |
| 763 | - | |
| 764 | -var oldSchemaToNew = function oldSchemaToNew(schema) { | |
| 765 | - if (schema && schema.propsSchema) { | |
| 766 | - var propsSchema = schema.propsSchema, | |
| 767 | - rest = _objectWithoutProperties(schema, _excluded); | |
| 768 | - | |
| 769 | - return _objectSpread({ | |
| 770 | - schema: propsSchema | |
| 771 | - }, rest); | |
| 772 | - } | |
| 773 | - | |
| 774 | - return schema; | |
| 775 | -}; | |
| 776 | - | |
| 777 | -exports.oldSchemaToNew = oldSchemaToNew; | |
| 778 | - | |
| 779 | -var newSchemaToOld = function newSchemaToOld(setting) { | |
| 780 | - if (setting && setting.schema) { | |
| 781 | - var schema = setting.schema, | |
| 782 | - rest = _objectWithoutProperties(setting, _excluded2); | |
| 783 | - | |
| 784 | - return _objectSpread({ | |
| 785 | - propsSchema: schema | |
| 786 | - }, rest); | |
| 787 | - } | |
| 788 | - | |
| 789 | - return setting; | |
| 790 | -}; // from FR | |
| 791 | - | |
| 792 | - | |
| 793 | -exports.newSchemaToOld = newSchemaToOld; | |
| 794 | - | |
| 795 | -var getEnum = function getEnum(schema) { | |
| 796 | - if (!schema) return undefined; | |
| 797 | - var itemEnum = schema && schema.items && schema.items.enum; | |
| 798 | - var schemaEnum = schema && schema.enum; | |
| 799 | - return itemEnum ? itemEnum : schemaEnum; | |
| 800 | -}; | |
| 801 | - | |
| 802 | -exports.getEnum = getEnum; | |
| 803 | - | |
| 804 | -var getArray = function getArray(arr) { | |
| 805 | - var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| 806 | - if (Array.isArray(arr)) return arr; | |
| 807 | - return defaultValue; | |
| 808 | -}; | |
| 809 | - | |
| 810 | -exports.getArray = getArray; | |
| 811 | - | |
| 812 | -var isEmail = function isEmail(value) { | |
| 813 | - var regex = '^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$'; | |
| 814 | - | |
| 815 | - if (value && new RegExp(regex).test(value)) { | |
| 816 | - return true; | |
| 817 | - } | |
| 818 | - | |
| 819 | - return false; | |
| 820 | -}; | |
| 821 | - | |
| 822 | -exports.isEmail = isEmail; | |
| 823 | - | |
| 824 | -function defaultGetValueFromEvent(valuePropName) { | |
| 825 | - var event = arguments.length <= 1 ? undefined : arguments[1]; | |
| 826 | - | |
| 827 | - if (event && event.target && valuePropName in event.target) { | |
| 828 | - return event.target[valuePropName]; | |
| 829 | - } | |
| 830 | - | |
| 831 | - return event; | |
| 832 | -} | |
| 833 | - | |
| 834 | -var getKeyFromPath = function getKeyFromPath() { | |
| 835 | - var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '#'; | |
| 836 | - | |
| 837 | - try { | |
| 838 | - var arr = path.split('.'); | |
| 839 | - var last = arr.slice(-1)[0]; | |
| 840 | - var result = last.replace('[]', ''); | |
| 841 | - return result; | |
| 842 | - } catch (error) { | |
| 843 | - console.error(error, 'getKeyFromPath'); | |
| 844 | - return ''; | |
| 845 | - } | |
| 846 | -}; // 更多的值获取 | |
| 847 | - | |
| 848 | - | |
| 849 | -exports.getKeyFromPath = getKeyFromPath; | |
| 850 | - | |
| 851 | -var getDisplayValue = function getDisplayValue(value, schema) { | |
| 852 | - if (typeof value === 'boolean') { | |
| 853 | - return value ? 'yes' : 'no'; | |
| 854 | - } | |
| 855 | - | |
| 856 | - if (isObjType(schema) || isListType(schema)) { | |
| 857 | - return '-'; | |
| 858 | - } | |
| 859 | - | |
| 860 | - if (Array.isArray(schema.enum) && Array.isArray(schema.enumNames)) { | |
| 861 | - try { | |
| 862 | - return schema.enumNames[schema.enum.indexOf(value)]; | |
| 863 | - } catch (error) { | |
| 864 | - return value; | |
| 865 | - } | |
| 866 | - } | |
| 867 | - | |
| 868 | - return value; | |
| 869 | -}; // 去掉数组里的空元素 {a: [null, {x:1}]} => {a: [{x:1}]} | |
| 870 | - | |
| 871 | - | |
| 872 | -exports.getDisplayValue = getDisplayValue; | |
| 873 | - | |
| 874 | -var removeEmptyItemFromList = function removeEmptyItemFromList(formData) { | |
| 875 | - var result = {}; | |
| 876 | - | |
| 877 | - if (isObject(formData)) { | |
| 878 | - Object.keys(formData).forEach(function (key) { | |
| 879 | - result[key] = removeEmptyItemFromList(formData[key]); | |
| 880 | - }); | |
| 881 | - } else if (Array.isArray(formData)) { | |
| 882 | - result = formData.filter(function (item) { | |
| 883 | - if ([false, 0, ''].indexOf(item) > -1) return true; | |
| 884 | - | |
| 885 | - if (item && JSON.stringify(item) !== '{}') { | |
| 886 | - return true; | |
| 887 | - } | |
| 888 | - | |
| 889 | - return false; | |
| 890 | - }); | |
| 891 | - } else { | |
| 892 | - result = formData; | |
| 893 | - } | |
| 894 | - | |
| 895 | - return result; | |
| 896 | -}; | |
| 897 | - | |
| 898 | -exports.removeEmptyItemFromList = removeEmptyItemFromList; | |
| 899 | - | |
| 900 | -var getDescriptorSimple = function getDescriptorSimple() { | |
| 901 | - var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 902 | - var path = arguments.length > 1 ? arguments[1] : undefined; | |
| 903 | - var result = {}; | |
| 904 | - | |
| 905 | - if (isObject(schema)) { | |
| 906 | - if (schema.type) { | |
| 907 | - switch (schema.type) { | |
| 908 | - case 'range': | |
| 909 | - result.type = 'array'; | |
| 910 | - break; | |
| 911 | - | |
| 912 | - case 'html': | |
| 913 | - result.type = 'string'; | |
| 914 | - break; | |
| 915 | - | |
| 916 | - default: | |
| 917 | - result.type = schema.type; | |
| 918 | - break; | |
| 919 | - } | |
| 920 | - } | |
| 921 | - | |
| 922 | - ['pattern', 'min', 'max', 'len', 'required'].forEach(function (key) { | |
| 923 | - if (Object.keys(schema).indexOf(key) > -1) { | |
| 924 | - result[key] = schema[key]; | |
| 925 | - } | |
| 926 | - }); | |
| 927 | - | |
| 928 | - switch (schema.format) { | |
| 929 | - case 'email': | |
| 930 | - case 'url': | |
| 931 | - result.type = schema.format; | |
| 932 | - break; | |
| 933 | - | |
| 934 | - default: | |
| 935 | - break; | |
| 936 | - } | |
| 937 | - | |
| 938 | - var handleRegx = function handleRegx(desc) { | |
| 939 | - if (desc.pattern && typeof desc.pattern === 'string') { | |
| 940 | - desc.pattern = new RegExp(desc.pattern); | |
| 941 | - } | |
| 942 | - | |
| 943 | - return desc; | |
| 944 | - }; // result be array | |
| 945 | - | |
| 946 | - | |
| 947 | - if (schema.rules) { | |
| 948 | - if (Array.isArray(schema.rules)) { | |
| 949 | - var requiredRule = schema.rules.find(function (rule) { | |
| 950 | - return rule.required === true; | |
| 951 | - }); | |
| 952 | - | |
| 953 | - if (requiredRule) { | |
| 954 | - result = _objectSpread(_objectSpread({}, result), requiredRule); | |
| 955 | - } | |
| 956 | - | |
| 957 | - result = [result].concat(_toConsumableArray(schema.rules)); | |
| 958 | - result = result.map(function (r) { | |
| 959 | - return handleRegx(r); | |
| 960 | - }); | |
| 961 | - } else if (isObject(schema.rules)) { | |
| 962 | - result = [result, schema.rules]; | |
| 963 | - result = result.map(function (r) { | |
| 964 | - return handleRegx(r); | |
| 965 | - }); | |
| 966 | - } | |
| 967 | - } else { | |
| 968 | - result = [result]; | |
| 969 | - } | |
| 970 | - } | |
| 971 | - | |
| 972 | - return _defineProperty({}, path, result); | |
| 973 | -}; // export const getDescriptorFromSchema = ({ schema, isRequired = true }) => { | |
| 974 | -// let result = {}; | |
| 975 | -// let singleResult = {}; | |
| 976 | -// if (schema.hidden === true) return { validator: () => true }; | |
| 977 | -// if (isObjType(schema)) { | |
| 978 | -// result.type = 'object'; | |
| 979 | -// if (isRequired && schema.required === true) { | |
| 980 | -// result.required = true; | |
| 981 | -// } | |
| 982 | -// result.fields = {}; | |
| 983 | -// Object.keys(schema.properties).forEach(key => { | |
| 984 | -// const item = schema.properties[key]; | |
| 985 | -// // 兼容旧的! | |
| 986 | -// if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 987 | -// item.required = true; | |
| 988 | -// } | |
| 989 | -// result.fields[key] = getDescriptorFromSchema({ | |
| 990 | -// schema: item, | |
| 991 | -// isRequired, | |
| 992 | -// }); | |
| 993 | -// }); | |
| 994 | -// } else if (isListType(schema)) { | |
| 995 | -// result.type = 'array'; | |
| 996 | -// if (isRequired && schema.required === true) { | |
| 997 | -// result.required = true; | |
| 998 | -// } | |
| 999 | -// if (typeof schema.min === 'number') { | |
| 1000 | -// result.min = schema.min; | |
| 1001 | -// } | |
| 1002 | -// if (typeof schema.max === 'number') { | |
| 1003 | -// result.max = schema.max; | |
| 1004 | -// } | |
| 1005 | -// result.defaultField = { type: 'object', fields: {} }; // 目前就默认只有object类型的 TODO: | |
| 1006 | -// Object.keys(schema.items.properties).forEach(key => { | |
| 1007 | -// const item = schema.items.properties[key]; | |
| 1008 | -// // 兼容旧的! | |
| 1009 | -// if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 1010 | -// item.required = true; | |
| 1011 | -// } | |
| 1012 | -// result.defaultField.fields[key] = getDescriptorFromSchema({ | |
| 1013 | -// schema: item, | |
| 1014 | -// isRequired, | |
| 1015 | -// }); | |
| 1016 | -// }); | |
| 1017 | -// } else { | |
| 1018 | -// // 单个的逻辑 | |
| 1019 | -// const processRule = item => { | |
| 1020 | -// if (schema.type) return { ...item, type: schema.type }; | |
| 1021 | -// if (item.pattern && typeof item.pattern === 'string') { | |
| 1022 | -// return { ...item, pattern: new RegExp(item.pattern) }; | |
| 1023 | -// } | |
| 1024 | -// return item; | |
| 1025 | -// }; | |
| 1026 | -// const { required, ...rest } = schema; | |
| 1027 | -// ['type', 'pattern', 'min', 'max', 'len'].forEach(key => { | |
| 1028 | -// if (Object.keys(rest).indexOf(key) > -1) { | |
| 1029 | -// singleResult[key] = rest[key]; | |
| 1030 | -// } | |
| 1031 | -// }); | |
| 1032 | -// switch (schema.type) { | |
| 1033 | -// case 'range': | |
| 1034 | -// singleResult.type = 'array'; | |
| 1035 | -// break; | |
| 1036 | -// case 'html': | |
| 1037 | -// singleResult.type = 'string'; | |
| 1038 | -// break; | |
| 1039 | -// default: | |
| 1040 | -// break; | |
| 1041 | -// } | |
| 1042 | -// switch (schema.format) { | |
| 1043 | -// case 'email': | |
| 1044 | -// case 'url': | |
| 1045 | -// singleResult.type = schema.format; | |
| 1046 | -// break; | |
| 1047 | -// default: | |
| 1048 | -// break; | |
| 1049 | -// } | |
| 1050 | -// let requiredRule; | |
| 1051 | -// if (isRequired && schema.required === true) { | |
| 1052 | -// requiredRule = { required: true, type: singleResult.type || 'string' }; | |
| 1053 | -// } | |
| 1054 | -// if (schema.rules) { | |
| 1055 | -// if (Array.isArray(schema.rules)) { | |
| 1056 | -// const _rules = []; | |
| 1057 | -// schema.rules.forEach(item => { | |
| 1058 | -// if (item.required === true) { | |
| 1059 | -// if (isRequired) { | |
| 1060 | -// requiredRule = item; | |
| 1061 | -// } | |
| 1062 | -// } else { | |
| 1063 | -// _rules.push(processRule(item)); | |
| 1064 | -// } | |
| 1065 | -// }); | |
| 1066 | -// result = [singleResult, ..._rules]; | |
| 1067 | -// } else if (isObject(schema.rules)) { | |
| 1068 | -// // TODO: 规范上不允许rules是object,省一点事儿 | |
| 1069 | -// result = [singleResult, processRule(schema.rules)]; | |
| 1070 | -// } else { | |
| 1071 | -// result = singleResult; | |
| 1072 | -// } | |
| 1073 | -// } else { | |
| 1074 | -// result = singleResult; | |
| 1075 | -// } | |
| 1076 | -// if (requiredRule) { | |
| 1077 | -// if (Array.isArray(result)) { | |
| 1078 | -// result.push(requiredRule); | |
| 1079 | -// } else if (isObject(result)) { | |
| 1080 | -// result = [result, requiredRule]; | |
| 1081 | -// } | |
| 1082 | -// } | |
| 1083 | -// if (schema.format === 'image') { | |
| 1084 | -// const imgValidator = { | |
| 1085 | -// validator: (rule, value) => { | |
| 1086 | -// const pattern = /([/|.|w|s|-])*.(jpg|gif|png|bmp|apng|webp|jpeg|json)/; | |
| 1087 | -// if (!value) return true; // 这里判断宽一点,undefined、null、'' 都当做没有填写 | |
| 1088 | -// return !!pattern.exec(value) || isUrl(value); | |
| 1089 | -// }, | |
| 1090 | -// message: '${title}的类型不是image', | |
| 1091 | -// }; | |
| 1092 | -// if (Array.isArray(result)) { | |
| 1093 | -// result.push(imgValidator); | |
| 1094 | -// } else if (isObject(result)) { | |
| 1095 | -// result = [result, imgValidator]; | |
| 1096 | -// } | |
| 1097 | -// } | |
| 1098 | -// } | |
| 1099 | -// return result; | |
| 1100 | -// }; | |
| 1101 | -// schema = { | |
| 1102 | -// type: 'object', | |
| 1103 | -// properties: { | |
| 1104 | -// x: { | |
| 1105 | -// type: 'object', | |
| 1106 | -// properties: { | |
| 1107 | -// y: { | |
| 1108 | -// type: 'string', | |
| 1109 | -// required: true, | |
| 1110 | -// }, | |
| 1111 | -// }, | |
| 1112 | -// }, | |
| 1113 | -// }, | |
| 1114 | -// }; | |
| 1115 | -// path = 'x.y' | |
| 1116 | -// return {required: true, message?: 'xxxx'} | |
| 1117 | -// export const isPathRequired = (path, schema) => { | |
| 1118 | -// let pathArr = path.split('.'); | |
| 1119 | -// while (pathArr.length > 0) { | |
| 1120 | -// let [_path, ...rest] = pathArr; | |
| 1121 | -// _path = _path.split('[')[0]; | |
| 1122 | -// let childSchema; | |
| 1123 | -// if (isObjType(schema)) { | |
| 1124 | -// childSchema = schema.properties[_path]; | |
| 1125 | -// } else if (isListType(schema)) { | |
| 1126 | -// childSchema = schema.items.properties[_path]; | |
| 1127 | -// } | |
| 1128 | -// pathArr = rest; | |
| 1129 | -// if (childSchema) { | |
| 1130 | -// return isPathRequired(rest.join('.'), childSchema); | |
| 1131 | -// } | |
| 1132 | -// // 单个的逻辑 | |
| 1133 | -// let result = { required: false }; | |
| 1134 | -// if (schema.required === true) { | |
| 1135 | -// result.required = true; | |
| 1136 | -// } | |
| 1137 | -// if (schema.rules) { | |
| 1138 | -// const requiredItem = schema.rules.find(item => item.required); | |
| 1139 | -// if (requiredItem) { | |
| 1140 | -// result = requiredItem; | |
| 1141 | -// } | |
| 1142 | -// } | |
| 1143 | -// return result; | |
| 1144 | -// } | |
| 1145 | -// }; | |
| 1146 | -// _path 只供内部递归使用 | |
| 1147 | - | |
| 1148 | - | |
| 1149 | -exports.getDescriptorSimple = getDescriptorSimple; | |
| 1150 | - | |
| 1151 | -var generateDataSkeleton = function generateDataSkeleton(schema, formData) { | |
| 1152 | - var _formData = clone(formData); | |
| 1153 | - | |
| 1154 | - var result = _formData; | |
| 1155 | - | |
| 1156 | - if (isObjType(schema)) { | |
| 1157 | - if (_formData === undefined || _typeof(_formData) !== 'object') { | |
| 1158 | - _formData = {}; | |
| 1159 | - result = {}; | |
| 1160 | - } | |
| 1161 | - | |
| 1162 | - Object.keys(schema.properties).forEach(function (key) { | |
| 1163 | - var childSchema = schema.properties[key]; | |
| 1164 | - var childData = _formData[key]; | |
| 1165 | - var childResult = generateDataSkeleton(childSchema, childData); | |
| 1166 | - result[key] = childResult; | |
| 1167 | - }); | |
| 1168 | - } else if (_formData !== undefined) {// result = _formData; | |
| 1169 | - } else { | |
| 1170 | - if (schema.default !== undefined) { | |
| 1171 | - result = clone(schema.default); | |
| 1172 | - } else if (schema.type === 'boolean' && !schema.widget) { | |
| 1173 | - // result = false; | |
| 1174 | - result = undefined; | |
| 1175 | - } else { | |
| 1176 | - result = undefined; | |
| 1177 | - } | |
| 1178 | - } | |
| 1179 | - | |
| 1180 | - return result; | |
| 1181 | -}; | |
| 1182 | - | |
| 1183 | -exports.generateDataSkeleton = generateDataSkeleton; | |
| 1184 | - | |
| 1185 | -var translateMessage = function translateMessage(msg, schema) { | |
| 1186 | - if (typeof msg !== 'string') { | |
| 1187 | - return ''; | |
| 1188 | - } | |
| 1189 | - | |
| 1190 | - if (!schema) return msg; | |
| 1191 | - msg = msg.replace('${title}', schema.title); | |
| 1192 | - msg = msg.replace('${type}', schema.format || schema.type); // 兼容代码 | |
| 1193 | - | |
| 1194 | - if (typeof schema.min === 'number') { | |
| 1195 | - msg = msg.replace('${min}', schema.min); | |
| 1196 | - } | |
| 1197 | - | |
| 1198 | - if (typeof schema.max === 'number') { | |
| 1199 | - msg = msg.replace('${max}', schema.max); | |
| 1200 | - } | |
| 1201 | - | |
| 1202 | - if (schema.rules) { | |
| 1203 | - var minRule = schema.rules.find(function (r) { | |
| 1204 | - return r.min !== undefined; | |
| 1205 | - }); | |
| 1206 | - | |
| 1207 | - if (minRule) { | |
| 1208 | - msg = msg.replace('${min}', minRule.min); | |
| 1209 | - } | |
| 1210 | - | |
| 1211 | - var maxRule = schema.rules.find(function (r) { | |
| 1212 | - return r.max !== undefined; | |
| 1213 | - }); | |
| 1214 | - | |
| 1215 | - if (maxRule) { | |
| 1216 | - msg = msg.replace('${max}', maxRule.max); | |
| 1217 | - } | |
| 1218 | - | |
| 1219 | - var lenRule = schema.rules.find(function (r) { | |
| 1220 | - return r.len !== undefined; | |
| 1221 | - }); | |
| 1222 | - | |
| 1223 | - if (lenRule) { | |
| 1224 | - msg = msg.replace('${len}', lenRule.len); | |
| 1225 | - } | |
| 1226 | - | |
| 1227 | - var patternRule = schema.rules.find(function (r) { | |
| 1228 | - return r.pattern !== undefined; | |
| 1229 | - }); | |
| 1230 | - | |
| 1231 | - if (patternRule) { | |
| 1232 | - msg = msg.replace('${pattern}', patternRule.pattern); | |
| 1233 | - } | |
| 1234 | - } | |
| 1235 | - | |
| 1236 | - return msg; | |
| 1237 | -}; // "objectName": { | |
| 1238 | -// "title": "对象", | |
| 1239 | -// "description": "这是一个对象类型", | |
| 1240 | -// "type": "object", | |
| 1241 | -// "properties": { | |
| 1242 | -// } | |
| 1243 | -// } | |
| 1244 | -// "listName": { | |
| 1245 | -// "title": "对象数组", | |
| 1246 | -// "description": "对象数组嵌套功能", | |
| 1247 | -// "type": "array", | |
| 1248 | -// "items": { | |
| 1249 | -// "type": "object", | |
| 1250 | -// "properties": { | |
| 1251 | -// } | |
| 1252 | -// } | |
| 1253 | -// } | |
| 1254 | - | |
| 1255 | - | |
| 1256 | -exports.translateMessage = translateMessage; | |
| 1257 | - | |
| 1258 | -var changeSchema = function changeSchema(_schema, singleChange) { | |
| 1259 | - var schema = clone(_schema); | |
| 1260 | - schema = singleChange(schema); | |
| 1261 | - | |
| 1262 | - if (isObjType(schema)) { | |
| 1263 | - var requiredKeys = []; | |
| 1264 | - | |
| 1265 | - if (Array.isArray(schema.required)) { | |
| 1266 | - requiredKeys = schema.required; | |
| 1267 | - delete schema.required; | |
| 1268 | - } | |
| 1269 | - | |
| 1270 | - Object.keys(schema.properties).forEach(function (key) { | |
| 1271 | - var item = schema.properties[key]; | |
| 1272 | - | |
| 1273 | - if (requiredKeys.indexOf(key) > -1) { | |
| 1274 | - item.required = true; | |
| 1275 | - } | |
| 1276 | - | |
| 1277 | - schema.properties[key] = changeSchema(item, singleChange); | |
| 1278 | - }); | |
| 1279 | - } else if (isListType(schema)) { | |
| 1280 | - Object.keys(schema.items.properties).forEach(function (key) { | |
| 1281 | - var item = schema.items.properties[key]; | |
| 1282 | - schema.items.properties[key] = changeSchema(item, singleChange); | |
| 1283 | - }); | |
| 1284 | - } | |
| 1285 | - | |
| 1286 | - return schema; | |
| 1287 | -}; | |
| 1288 | - | |
| 1289 | -var updateSchemaToNewVersion = function updateSchemaToNewVersion(schema) { | |
| 1290 | - return changeSchema(schema, updateSingleSchema); | |
| 1291 | -}; | |
| 1292 | - | |
| 1293 | -exports.updateSchemaToNewVersion = updateSchemaToNewVersion; | |
| 1294 | - | |
| 1295 | -var updateSingleSchema = function updateSingleSchema(schema) { | |
| 1296 | - try { | |
| 1297 | - schema.rules = schema.rules || []; | |
| 1298 | - schema.props = schema.props || {}; | |
| 1299 | - | |
| 1300 | - if (schema['ui:options']) { | |
| 1301 | - schema.props = schema['ui:options']; | |
| 1302 | - delete schema['ui:options']; | |
| 1303 | - } | |
| 1304 | - | |
| 1305 | - if (schema.pattern) { | |
| 1306 | - var validItem = { | |
| 1307 | - pattern: schema.pattern | |
| 1308 | - }; | |
| 1309 | - | |
| 1310 | - if (schema.message && schema.message.pattern) { | |
| 1311 | - validItem.message = schema.message.pattern; | |
| 1312 | - } | |
| 1313 | - | |
| 1314 | - schema.rules.push(validItem); | |
| 1315 | - delete schema.pattern; | |
| 1316 | - delete schema.message; | |
| 1317 | - } // min / max | |
| 1318 | - | |
| 1319 | - | |
| 1320 | - if (schema.minLength) { | |
| 1321 | - schema.min = schema.minLength; | |
| 1322 | - delete schema.minLength; | |
| 1323 | - } | |
| 1324 | - | |
| 1325 | - if (schema.maxLength) { | |
| 1326 | - schema.max = schema.maxLength; | |
| 1327 | - delete schema.maxLength; | |
| 1328 | - } | |
| 1329 | - | |
| 1330 | - if (schema.minItems) { | |
| 1331 | - schema.min = schema.minItems; | |
| 1332 | - delete schema.minItems; | |
| 1333 | - } | |
| 1334 | - | |
| 1335 | - if (schema.maxItems) { | |
| 1336 | - schema.max = schema.maxItems; | |
| 1337 | - delete schema.maxItems; | |
| 1338 | - } | |
| 1339 | - | |
| 1340 | - if (schema.step) { | |
| 1341 | - schema.props.step = schema.step; | |
| 1342 | - delete schema.step; | |
| 1343 | - } // ui:xxx | |
| 1344 | - | |
| 1345 | - | |
| 1346 | - if (schema['ui:className']) { | |
| 1347 | - schema.className = schema['ui:className']; | |
| 1348 | - delete schema['ui:className']; | |
| 1349 | - } | |
| 1350 | - | |
| 1351 | - if (schema['ui:hidden']) { | |
| 1352 | - schema.hidden = schema['ui:hidden']; | |
| 1353 | - delete schema['ui:hidden']; | |
| 1354 | - } | |
| 1355 | - | |
| 1356 | - if (schema['ui:readonly']) { | |
| 1357 | - schema.readOnly = schema['ui:readonly']; // 改成驼峰了 | |
| 1358 | - | |
| 1359 | - delete schema['ui:readonly']; | |
| 1360 | - } | |
| 1361 | - | |
| 1362 | - if (schema['ui:disabled']) { | |
| 1363 | - schema.disabled = schema['ui:disabled']; | |
| 1364 | - delete schema['ui:disabled']; | |
| 1365 | - } | |
| 1366 | - | |
| 1367 | - if (schema['ui:width']) { | |
| 1368 | - schema.width = schema['ui:width']; | |
| 1369 | - delete schema['ui:width']; | |
| 1370 | - } | |
| 1371 | - | |
| 1372 | - if (schema['ui:displayType']) { | |
| 1373 | - schema.displayType = schema['ui:displayType']; | |
| 1374 | - delete schema['ui:displayType']; | |
| 1375 | - } | |
| 1376 | - | |
| 1377 | - if (schema['ui:column']) { | |
| 1378 | - schema.column = schema['ui:column']; | |
| 1379 | - delete schema['ui:column']; | |
| 1380 | - } | |
| 1381 | - | |
| 1382 | - if (schema['ui:widget']) { | |
| 1383 | - schema.widget = schema['ui:widget']; | |
| 1384 | - delete schema['ui:widget']; | |
| 1385 | - } | |
| 1386 | - | |
| 1387 | - if (schema['ui:labelWidth']) { | |
| 1388 | - schema.labelWidth = schema['ui:labelWidth']; | |
| 1389 | - delete schema['ui:labelWidth']; | |
| 1390 | - } | |
| 1391 | - | |
| 1392 | - if (schema.rules && schema.rules.length === 0) { | |
| 1393 | - delete schema.rules; | |
| 1394 | - } | |
| 1395 | - | |
| 1396 | - if (typeof schema.props === 'function' || isObject(schema.props) && Object.keys(schema.props).length > 0) {} else { | |
| 1397 | - delete schema.props; | |
| 1398 | - } | |
| 1399 | - | |
| 1400 | - return schema; | |
| 1401 | - } catch (error) { | |
| 1402 | - console.error('schema转换失败!', error); | |
| 1403 | - return schema; | |
| 1404 | - } | |
| 1405 | -}; // 检验一个string是 function(传统活箭头函数) | |
| 1406 | - | |
| 1407 | - | |
| 1408 | -var parseFunctionString = function parseFunctionString(string) { | |
| 1409 | - if (typeof string !== 'string') return false; | |
| 1410 | - var reg1 = /^{{(function.+)}}$/; | |
| 1411 | - var reg2 = /^{{(.+=>.+)}}$/; | |
| 1412 | - | |
| 1413 | - if (string.match(reg1)) { | |
| 1414 | - return string.match(reg1)[1]; | |
| 1415 | - } | |
| 1416 | - | |
| 1417 | - if (string.match(reg2)) { | |
| 1418 | - return string.match(reg2)[1]; | |
| 1419 | - } | |
| 1420 | - | |
| 1421 | - return false; | |
| 1422 | -}; | |
| 1423 | - | |
| 1424 | -exports.parseFunctionString = parseFunctionString; | |
| 1425 | - | |
| 1426 | -var completeSchemaWithTheme = function completeSchemaWithTheme() { | |
| 1427 | - var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 1428 | - var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 1429 | - var result = {}; | |
| 1430 | - | |
| 1431 | - if (isObject(schema)) { | |
| 1432 | - if (schema.theme && theme[schema.theme]) { | |
| 1433 | - result = _objectSpread(_objectSpread({}, schema), theme[schema.theme]); | |
| 1434 | - } | |
| 1435 | - | |
| 1436 | - Object.keys(schema).forEach(function (key) { | |
| 1437 | - result[key] = completeSchemaWithTheme(schema[key], theme); | |
| 1438 | - }); | |
| 1439 | - } else { | |
| 1440 | - result = schema; | |
| 1441 | - } | |
| 1442 | - | |
| 1443 | - return result; | |
| 1444 | -}; | |
| 1445 | - | |
| 1446 | -exports.completeSchemaWithTheme = completeSchemaWithTheme; | |
| 1447 | - | |
| 1448 | -var cleanEmpty = function cleanEmpty(obj) { | |
| 1449 | - if (Array.isArray(obj)) { | |
| 1450 | - return obj.map(function (v) { | |
| 1451 | - return v && isObject(v) ? cleanEmpty(v) : v; | |
| 1452 | - }).filter(function (v) { | |
| 1453 | - return !(v == undefined); | |
| 1454 | - }); | |
| 1455 | - } else if (isObject(obj)) { | |
| 1456 | - return Object.entries(obj).map(function (_ref6) { | |
| 1457 | - var _ref7 = _slicedToArray(_ref6, 2), | |
| 1458 | - k = _ref7[0], | |
| 1459 | - v = _ref7[1]; | |
| 1460 | - | |
| 1461 | - return [k, v && isObject(v) ? cleanEmpty(v) : v]; | |
| 1462 | - }).reduce(function (a, _ref8) { | |
| 1463 | - var _ref9 = _slicedToArray(_ref8, 2), | |
| 1464 | - k = _ref9[0], | |
| 1465 | - v = _ref9[1]; | |
| 1466 | - | |
| 1467 | - return v == undefined ? a : (a[k] = v, a); | |
| 1468 | - }, {}); | |
| 1469 | - } else { | |
| 1470 | - return obj; | |
| 1471 | - } | |
| 1472 | -}; // const x = { a: 1, b: { c: 2 }, d: [{ e: 3, f: [{ g: 5 }] }, { e: 4 }] }; | |
| 1473 | -// ['a', 'b.c', 'd[0].e', 'd[0].f[0].g', 'd[1].e'] | |
| 1474 | - | |
| 1475 | - | |
| 1476 | -exports.cleanEmpty = cleanEmpty; | |
| 1477 | - | |
| 1478 | -var dataToKeys = function dataToKeys(data) { | |
| 1479 | - var rootKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | |
| 1480 | - var result = []; | |
| 1481 | - | |
| 1482 | - if (rootKey && rootKey.slice(-1) !== ']') { | |
| 1483 | - result.push(rootKey); | |
| 1484 | - } | |
| 1485 | - | |
| 1486 | - var isComplex = function isComplex(data) { | |
| 1487 | - return isObject(data) || Array.isArray(data); | |
| 1488 | - }; | |
| 1489 | - | |
| 1490 | - if (isObject(data)) { | |
| 1491 | - Object.keys(data).forEach(function (key) { | |
| 1492 | - var item = data[key]; | |
| 1493 | - var itemRootKey = rootKey ? rootKey + '.' + key : key; | |
| 1494 | - | |
| 1495 | - if (isComplex(item)) { | |
| 1496 | - var itemKeys = dataToKeys(item, itemRootKey); | |
| 1497 | - result = [].concat(_toConsumableArray(result), _toConsumableArray(itemKeys)); | |
| 1498 | - } else { | |
| 1499 | - result.push(itemRootKey); | |
| 1500 | - } | |
| 1501 | - }); | |
| 1502 | - } else if (Array.isArray(data)) { | |
| 1503 | - data.forEach(function (item, idx) { | |
| 1504 | - var itemRootKey = rootKey ? "".concat(rootKey, "[").concat(idx, "]") : "[".concat(idx, "]"); | |
| 1505 | - | |
| 1506 | - if (isComplex(item)) { | |
| 1507 | - var itemKeys = dataToKeys(item, itemRootKey); | |
| 1508 | - result = [].concat(_toConsumableArray(result), _toConsumableArray(itemKeys)); | |
| 1509 | - } else { | |
| 1510 | - result.push(itemRootKey); | |
| 1511 | - } | |
| 1512 | - }); | |
| 1513 | - } else {} | |
| 1514 | - | |
| 1515 | - return result; | |
| 1516 | -}; | |
| 1517 | - | |
| 1518 | -exports.dataToKeys = dataToKeys; | |
| 1519 | - | |
| 1520 | -var removeHiddenFromResult = function removeHiddenFromResult(data, flatten) { | |
| 1521 | - var result = clone(data); | |
| 1522 | - var keys = dataToKeys(result); | |
| 1523 | - keys.forEach(function (key) { | |
| 1524 | - var _destructDataPath = destructDataPath(key), | |
| 1525 | - id = _destructDataPath.id, | |
| 1526 | - dataIndex = _destructDataPath.dataIndex; | |
| 1527 | - | |
| 1528 | - if (flatten[id]) { | |
| 1529 | - var _ref10 = flatten[id].schema || {}, | |
| 1530 | - hidden = _ref10.hidden; | |
| 1531 | - | |
| 1532 | - if (isExpression(hidden)) { | |
| 1533 | - hidden = parseSingleExpression(hidden, result, key); | |
| 1534 | - } | |
| 1535 | - | |
| 1536 | - if ((0, _lodashEs.get)(result, key) !== undefined && hidden) { | |
| 1537 | - (0, _lodashEs.set)(result, key, undefined); | |
| 1538 | - } | |
| 1539 | - } | |
| 1540 | - }); | |
| 1541 | - return result; | |
| 1542 | -}; | |
| 1543 | - | |
| 1544 | -exports.removeHiddenFromResult = removeHiddenFromResult; | |
| 1545 | - | |
| 1546 | -function msToTime(duration) { | |
| 1547 | - var seconds = Math.floor(duration / 1000 % 60); | |
| 1548 | - var minutes = Math.floor(duration / (1000 * 60) % 60); | |
| 1549 | - var hours = Math.floor(duration / (1000 * 60 * 60) % 24); | |
| 1550 | - hours = hours < 10 ? '0' + hours : hours; | |
| 1551 | - minutes = minutes < 10 ? '0' + minutes : minutes; | |
| 1552 | - seconds = seconds < 10 ? '0' + seconds : seconds; | |
| 1553 | - return hours + ':' + minutes + ':' + seconds; | |
| 1554 | -} | |
| 1555 | - | |
| 1556 | -function yymmdd(timeStamp) { | |
| 1557 | - var date_ob = new Date(Number(timeStamp)); | |
| 1558 | - | |
| 1559 | - var adjustZero = function adjustZero(num) { | |
| 1560 | - return ('0' + num).slice(-2); | |
| 1561 | - }; | |
| 1562 | - | |
| 1563 | - var day = adjustZero(date_ob.getDate()); | |
| 1564 | - var month = adjustZero(date_ob.getMonth()); | |
| 1565 | - var year = date_ob.getFullYear(); | |
| 1566 | - var hours = adjustZero(date_ob.getHours()); | |
| 1567 | - var minutes = adjustZero(date_ob.getMinutes()); | |
| 1568 | - var seconds = adjustZero(date_ob.getSeconds()); | |
| 1569 | - return "".concat(year, "-").concat(month, "-").concat(day, " ").concat(hours, ":").concat(minutes, ":").concat(seconds); | |
| 1570 | -} | |
| 1571 | - | |
| 1572 | -function allPromiseFinish(promiseList) { | |
| 1573 | - var hasError = false; | |
| 1574 | - var count = promiseList.length; | |
| 1575 | - var results = []; | |
| 1576 | - | |
| 1577 | - if (!promiseList.length) { | |
| 1578 | - return Promise.resolve([]); | |
| 1579 | - } | |
| 1580 | - | |
| 1581 | - return new Promise(function (resolve, reject) { | |
| 1582 | - promiseList.forEach(function (promise, index) { | |
| 1583 | - promise.catch(function (e) { | |
| 1584 | - hasError = true; | |
| 1585 | - return e; | |
| 1586 | - }).then(function (result) { | |
| 1587 | - count -= 1; | |
| 1588 | - results[index] = result; | |
| 1589 | - | |
| 1590 | - if (count > 0) { | |
| 1591 | - return; | |
| 1592 | - } | |
| 1593 | - | |
| 1594 | - if (hasError) { | |
| 1595 | - reject(results); | |
| 1596 | - } | |
| 1597 | - | |
| 1598 | - resolve(results); | |
| 1599 | - }); | |
| 1600 | - }); | |
| 1601 | - }); | |
| 1602 | -} | |
| 1603 | - | |
| 1604 | -var removeDups = function removeDups(arr) { | |
| 1605 | - if (!Array.isArray(arr)) { | |
| 1606 | - console.log('in removeDups: param is not an array'); | |
| 1607 | - return; | |
| 1608 | - } | |
| 1609 | - | |
| 1610 | - var array = []; | |
| 1611 | - | |
| 1612 | - for (var i = 0; i < arr.length; i++) { | |
| 1613 | - if (array.indexOf(arr[i]) === -1) { | |
| 1614 | - array.push(arr[i]); | |
| 1615 | - } | |
| 1616 | - } | |
| 1617 | - | |
| 1618 | - return array; | |
| 1619 | -}; | |
| 1620 | - | |
| 1621 | -exports.removeDups = removeDups; | |
| \ No newline at end of file |
lib/form-render-core/src/validator.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.validateField = exports.validateAll = exports.parseSchemaExpression = void 0; | |
| 7 | - | |
| 8 | -var _utils = require("./utils"); | |
| 9 | - | |
| 10 | -var _validateMessageCN = require("./validateMessageCN"); | |
| 11 | - | |
| 12 | -var _validateMessage = require("./validateMessage"); | |
| 13 | - | |
| 14 | -var _asyncValidator = _interopRequireDefault(require("async-validator")); | |
| 15 | - | |
| 16 | -var _lodashEs = require("lodash-es"); | |
| 17 | - | |
| 18 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 19 | - | |
| 20 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 21 | - | |
| 22 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 23 | - | |
| 24 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 25 | - | |
| 26 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 27 | - | |
| 28 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 29 | - | |
| 30 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 31 | - | |
| 32 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 33 | - | |
| 34 | -var parseSchemaExpression = function parseSchemaExpression(schema, formData, path) { | |
| 35 | - if (!(0, _utils.isObject)(schema)) return schema; | |
| 36 | - var result = {}; | |
| 37 | - Object.keys(schema).forEach(function (key) { | |
| 38 | - var item = schema[key]; | |
| 39 | - | |
| 40 | - if ((0, _utils.isObject)(item)) { | |
| 41 | - result[key] = parseSchemaExpression(item, formData, path); | |
| 42 | - } else if ((0, _utils.isExpression)(item)) { | |
| 43 | - result[key] = (0, _utils.parseSingleExpression)(item, formData, path); | |
| 44 | - } else { | |
| 45 | - result[key] = item; | |
| 46 | - } | |
| 47 | - }); | |
| 48 | - return result; | |
| 49 | -}; | |
| 50 | - | |
| 51 | -exports.parseSchemaExpression = parseSchemaExpression; | |
| 52 | - | |
| 53 | -var getRelatedPaths = function getRelatedPaths(path, flatten) { | |
| 54 | - var parentPaths = []; | |
| 55 | - var pathArr = path.split('.'); | |
| 56 | - | |
| 57 | - while (pathArr.length > 0) { | |
| 58 | - parentPaths.push(pathArr.join('.')); | |
| 59 | - pathArr.pop(); | |
| 60 | - } | |
| 61 | - | |
| 62 | - var result = [].concat(parentPaths); | |
| 63 | - parentPaths.forEach(function (path) { | |
| 64 | - var _destructDataPath = (0, _utils.destructDataPath)(path), | |
| 65 | - id = _destructDataPath.id, | |
| 66 | - dataIndex = _destructDataPath.dataIndex; | |
| 67 | - | |
| 68 | - if (flatten[id] && flatten[id].schema && Array.isArray(flatten[id].schema.dependecies)) { | |
| 69 | - var deps = flatten[id].schema.dependecies; | |
| 70 | - var fullPathDeps = deps.map(function (dep) { | |
| 71 | - return (0, _utils.getDataPath)(dep, dataIndex); | |
| 72 | - }); | |
| 73 | - result = [].concat(_toConsumableArray(result), _toConsumableArray(fullPathDeps)); | |
| 74 | - } | |
| 75 | - }); | |
| 76 | - return (0, _utils.removeDups)(result).map(function (path) { | |
| 77 | - if (path.slice(-1) === ']') { | |
| 78 | - var pattern = /\[[0-9]+\]$/; | |
| 79 | - return path.replace(pattern, ''); | |
| 80 | - } else { | |
| 81 | - return path; | |
| 82 | - } | |
| 83 | - }); | |
| 84 | -}; | |
| 85 | - | |
| 86 | -var validateField = function validateField(_ref) { | |
| 87 | - var path = _ref.path, | |
| 88 | - formData = _ref.formData, | |
| 89 | - flatten = _ref.flatten, | |
| 90 | - options = _ref.options; | |
| 91 | - var paths = getRelatedPaths(path, flatten); // console.log('all relevant paths:', paths); | |
| 92 | - | |
| 93 | - var promiseArray = paths.map(function (path) { | |
| 94 | - var _destructDataPath2 = (0, _utils.destructDataPath)(path), | |
| 95 | - id = _destructDataPath2.id, | |
| 96 | - dataIndex = _destructDataPath2.dataIndex; | |
| 97 | - | |
| 98 | - if (flatten[id] || flatten["".concat(id, "[]")]) { | |
| 99 | - var item = flatten[id] || flatten["".concat(id, "[]")]; | |
| 100 | - var singleData = (0, _lodashEs.get)(formData, path); | |
| 101 | - var schema = item.schema || {}; | |
| 102 | - var finalSchema = parseSchemaExpression(schema, formData, path); | |
| 103 | - return validateSingle(singleData, finalSchema, path, options); // is a promise | |
| 104 | - } else { | |
| 105 | - return Promise.resolve(); | |
| 106 | - } | |
| 107 | - }); | |
| 108 | - return (0, _utils.allPromiseFinish)(promiseArray).then(function (res) { | |
| 109 | - var errorFields = res.filter(function (item) { | |
| 110 | - return Array.isArray(item) && item.length > 0; | |
| 111 | - }).map(function (item) { | |
| 112 | - var name = item[0].field; | |
| 113 | - var error = item.map(function (m) { | |
| 114 | - return m.message; | |
| 115 | - }).filter(function (m) { | |
| 116 | - return !!m; | |
| 117 | - }); | |
| 118 | - return { | |
| 119 | - name: name, | |
| 120 | - error: error | |
| 121 | - }; | |
| 122 | - }); | |
| 123 | - return errorFields; | |
| 124 | - }).catch(function (e) { | |
| 125 | - console.log(e); | |
| 126 | - }); | |
| 127 | -}; // pathFromData => allPath | |
| 128 | - | |
| 129 | - | |
| 130 | -exports.validateField = validateField; | |
| 131 | - | |
| 132 | -var getAllPaths = function getAllPaths(paths, flatten) { | |
| 133 | - if (!Array.isArray(paths)) return []; | |
| 134 | - | |
| 135 | - var result = _toConsumableArray(paths).filter(function (p) { | |
| 136 | - return p.indexOf(']') > -1; | |
| 137 | - }).map(function (p1) { | |
| 138 | - var last = p1.lastIndexOf(']'); | |
| 139 | - return p1.substring(0, last + 1); | |
| 140 | - }); | |
| 141 | - | |
| 142 | - var uniqueResult = (0, _utils.removeDups)(result); | |
| 143 | - var allFlattenPath = Object.keys(flatten); | |
| 144 | - | |
| 145 | - var res = _toConsumableArray(paths); | |
| 146 | - | |
| 147 | - uniqueResult.forEach(function (result) { | |
| 148 | - var _destructDataPath3 = (0, _utils.destructDataPath)(result), | |
| 149 | - id = _destructDataPath3.id, | |
| 150 | - dataIndex = _destructDataPath3.dataIndex; | |
| 151 | - | |
| 152 | - if (flatten[id]) { | |
| 153 | - var children = allFlattenPath.filter(function (f) { | |
| 154 | - return f.indexOf(id) === 0 && f !== id; | |
| 155 | - }); | |
| 156 | - var childrenWithIndex = children.map(function (child) { | |
| 157 | - var p = (0, _utils.getDataPath)(child, dataIndex); | |
| 158 | - return p.split('[]')[0]; | |
| 159 | - }).filter(function (i) { | |
| 160 | - return !!i; | |
| 161 | - }); | |
| 162 | - res = [].concat(_toConsumableArray(res), _toConsumableArray((0, _utils.removeDups)(childrenWithIndex))); | |
| 163 | - } | |
| 164 | - }); | |
| 165 | - return (0, _utils.removeDups)(res); | |
| 166 | -}; | |
| 167 | - | |
| 168 | -var validateAll = function validateAll(_ref2) { | |
| 169 | - var formData = _ref2.formData, | |
| 170 | - flatten = _ref2.flatten, | |
| 171 | - options = _ref2.options; | |
| 172 | - var paths = (0, _utils.dataToKeys)(formData); | |
| 173 | - var allPaths = getAllPaths(paths, flatten); // console.log(formData, dataToKeys(formData), 'dataToKeysdataToKeys'); | |
| 174 | - // console.log('allPaths', allPaths); | |
| 175 | - | |
| 176 | - var promiseArray = allPaths.map(function (path) { | |
| 177 | - var _destructDataPath4 = (0, _utils.destructDataPath)(path), | |
| 178 | - id = _destructDataPath4.id, | |
| 179 | - dataIndex = _destructDataPath4.dataIndex; | |
| 180 | - | |
| 181 | - if (flatten[id] || flatten["".concat(id, "[]")]) { | |
| 182 | - var item = flatten[id] || flatten["".concat(id, "[]")]; | |
| 183 | - var singleData = (0, _lodashEs.get)(formData, path); | |
| 184 | - var schema = item.schema || {}; | |
| 185 | - var finalSchema = parseSchemaExpression(schema, formData, path); | |
| 186 | - return validateSingle(singleData, finalSchema, path, options); // is a promise | |
| 187 | - } else { | |
| 188 | - return Promise.resolve(); | |
| 189 | - } | |
| 190 | - }); | |
| 191 | - return (0, _utils.allPromiseFinish)(promiseArray).then(function (res) { | |
| 192 | - var errorFields = res.filter(function (item) { | |
| 193 | - return Array.isArray(item) && item.length > 0 && item[0].message !== null; | |
| 194 | - }) // NOTICE: different from validateField | |
| 195 | - .map(function (item) { | |
| 196 | - var name = item[0].field; | |
| 197 | - var error = item.map(function (m) { | |
| 198 | - return m.message; | |
| 199 | - }).filter(function (m) { | |
| 200 | - return !!m; | |
| 201 | - }); | |
| 202 | - return { | |
| 203 | - name: name, | |
| 204 | - error: error | |
| 205 | - }; | |
| 206 | - }); | |
| 207 | - return errorFields; | |
| 208 | - }).catch(function (e) { | |
| 209 | - console.log(e); | |
| 210 | - }); | |
| 211 | -}; | |
| 212 | - | |
| 213 | -exports.validateAll = validateAll; | |
| 214 | - | |
| 215 | -var validateSingle = function validateSingle(data) { | |
| 216 | - var schema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 217 | - var path = arguments.length > 2 ? arguments[2] : undefined; | |
| 218 | - var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | |
| 219 | - | |
| 220 | - if (schema.hidden) { | |
| 221 | - return Promise.resolve(); | |
| 222 | - } | |
| 223 | - | |
| 224 | - var _options$validateMess = options.validateMessages, | |
| 225 | - validateMessages = _options$validateMess === void 0 ? {} : _options$validateMess, | |
| 226 | - _options$locale = options.locale, | |
| 227 | - locale = _options$locale === void 0 ? 'cn' : _options$locale; | |
| 228 | - var cn = _validateMessageCN.defaultValidateMessagesCN; | |
| 229 | - var en = _validateMessage.defaultValidateMessages; | |
| 230 | - var descriptor = (0, _utils.getDescriptorSimple)(schema, path); // console.log('descriptor, schema, path', descriptor, schema, path, data); | |
| 231 | - // TODO: 有些情况会出现没有rules,需要看一下,先兜底 | |
| 232 | - | |
| 233 | - var validator; | |
| 234 | - | |
| 235 | - try { | |
| 236 | - validator = new _asyncValidator.default(descriptor); | |
| 237 | - } catch (error) { | |
| 238 | - return Promise.resolve(); | |
| 239 | - } | |
| 240 | - | |
| 241 | - var messageFeed = locale === 'en' ? en : cn; | |
| 242 | - (0, _lodashEs.merge)(messageFeed, validateMessages); | |
| 243 | - validator.messages(messageFeed); | |
| 244 | - return validator.validate(_defineProperty({}, path, data)).then(function (res) { | |
| 245 | - return [{ | |
| 246 | - field: path, | |
| 247 | - message: null | |
| 248 | - }]; | |
| 249 | - }).catch(function (_ref3) { | |
| 250 | - var errors = _ref3.errors, | |
| 251 | - fields = _ref3.fields; | |
| 252 | - return errors; | |
| 253 | - }); | |
| 254 | -}; // 无用 | |
| 255 | -// const theRest = () => { | |
| 256 | -// let _schema = getSchemaFromFlatten(flatten); | |
| 257 | -// if (Object.keys(_schema).length === 0) return Promise.resolve(); | |
| 258 | -// const descriptor = getDescriptorFromSchema({ | |
| 259 | -// schema: _schema, | |
| 260 | -// isRequired, | |
| 261 | -// }).fields; | |
| 262 | -// window.descriptor = descriptor; | |
| 263 | -// let touchVerifyList = []; | |
| 264 | -// // 如果是最后的校验,所有key都touch了,就不用算这个了 | |
| 265 | -// // 因为要整个构建validator在list的情况太复杂了,所以required单独拿出来处理,但是这边有不少单独处理逻辑,例如message | |
| 266 | -// if (!isRequired) { | |
| 267 | -// touchedKeys.forEach(key => { | |
| 268 | -// const keyRequired = isPathRequired(key, _schema); | |
| 269 | -// const val = get(formData, key); | |
| 270 | -// const nullValue = [undefined, null, ''].indexOf(val) > -1; // 注意 0 不是 | |
| 271 | -// const isEmptyMultiSelect = Array.isArray(val) && val.length === 0; | |
| 272 | -// if ((nullValue || isEmptyMultiSelect) && keyRequired.required) { | |
| 273 | -// const _message = | |
| 274 | -// keyRequired.message || validateMessages.required || '${title}必填'; | |
| 275 | -// touchVerifyList.push({ name: key, error: [_message] }); | |
| 276 | -// } | |
| 277 | -// }); | |
| 278 | -// } | |
| 279 | -// const cn = defaultValidateMessagesCN; | |
| 280 | -// const en = defaultValidateMessages; | |
| 281 | -// // TODO: 有些情况会出现没有rules,需要看一下,先兜底 | |
| 282 | -// let validator; | |
| 283 | -// try { | |
| 284 | -// validator = new Validator(descriptor); | |
| 285 | -// } catch (error) { | |
| 286 | -// return Promise.resolve([]); | |
| 287 | -// } | |
| 288 | -// let messageFeed = locale === 'en' ? en : cn; | |
| 289 | -// merge(messageFeed, validateMessages); | |
| 290 | -// validator.messages(messageFeed); | |
| 291 | -// return validator | |
| 292 | -// .validate(formData || {}) | |
| 293 | -// .then(res => { | |
| 294 | -// if (touchVerifyList.length > 0) return touchVerifyList; | |
| 295 | -// return []; | |
| 296 | -// }) | |
| 297 | -// .catch(({ errors, fields }) => { | |
| 298 | -// // error的name改成正常的path | |
| 299 | -// let normalizedErrors = getArray(errors).map(err => { | |
| 300 | -// const _path = formatPathFromValidator(err.field); | |
| 301 | -// return { name: _path, error: [err.message] }; | |
| 302 | -// }); | |
| 303 | -// // 添加touched的required | |
| 304 | -// normalizedErrors = [...normalizedErrors, ...touchVerifyList]; | |
| 305 | -// // 合并同名的error | |
| 306 | -// let _errorFields = []; | |
| 307 | -// normalizedErrors.forEach(item => { | |
| 308 | -// const matchIndex = _errorFields.findIndex( | |
| 309 | -// ele => ele.name === item.name | |
| 310 | -// ); | |
| 311 | -// if (matchIndex === -1) { | |
| 312 | -// _errorFields.push(item); | |
| 313 | -// } else { | |
| 314 | -// _errorFields[matchIndex].error = [ | |
| 315 | -// ..._errorFields[matchIndex].error, | |
| 316 | -// ...item.error, | |
| 317 | -// ]; | |
| 318 | -// } | |
| 319 | -// }); | |
| 320 | -// return _errorFields; | |
| 321 | -// }); | |
| 322 | -// }; | |
| 323 | -// var d3 = { | |
| 324 | -// list: { | |
| 325 | -// type: 'array', | |
| 326 | -// defaultField: { | |
| 327 | -// type: 'object', | |
| 328 | -// fields: { | |
| 329 | -// userName: [ | |
| 330 | -// { | |
| 331 | -// bind: false, | |
| 332 | -// title: 'User Name', | |
| 333 | -// type: 'string', | |
| 334 | -// rules: [ | |
| 335 | -// { | |
| 336 | -// min: 5, | |
| 337 | -// message: '长度需要大于5', | |
| 338 | -// }, | |
| 339 | -// { | |
| 340 | -// max: 12, | |
| 341 | -// }, | |
| 342 | -// ], | |
| 343 | -// }, | |
| 344 | -// { | |
| 345 | -// min: 5, | |
| 346 | -// message: '长度需要大于5', | |
| 347 | -// }, | |
| 348 | -// { | |
| 349 | -// max: 12, | |
| 350 | -// }, | |
| 351 | -// ], | |
| 352 | -// selectName: { | |
| 353 | -// title: '单选', | |
| 354 | -// type: 'string', | |
| 355 | -// enum: ['a', 'b', 'c'], | |
| 356 | -// enumNames: ['早', '中', '晚'], | |
| 357 | -// required: true, | |
| 358 | -// }, | |
| 359 | -// checkboxName: { | |
| 360 | -// title: '是否判断', | |
| 361 | -// type: 'boolean', | |
| 362 | -// valuePropName: 'checked', | |
| 363 | -// }, | |
| 364 | -// }, | |
| 365 | -// }, | |
| 366 | -// }, | |
| 367 | -// }; | |
| 368 | -// var d2 = { | |
| 369 | -// list: { | |
| 370 | -// type: 'array', | |
| 371 | -// defaultField: { | |
| 372 | -// type: 'object', | |
| 373 | -// fields: { | |
| 374 | -// userName: [ | |
| 375 | -// { | |
| 376 | -// bind: false, | |
| 377 | -// title: 'User Name', | |
| 378 | -// type: 'string', | |
| 379 | -// rules: [ | |
| 380 | -// { | |
| 381 | -// min: 5, | |
| 382 | -// message: '长度需要大于5', | |
| 383 | -// }, | |
| 384 | -// { | |
| 385 | -// max: 12, | |
| 386 | -// }, | |
| 387 | -// ], | |
| 388 | -// }, | |
| 389 | -// { | |
| 390 | -// min: 5, | |
| 391 | -// message: '长度需要大于5', | |
| 392 | -// }, | |
| 393 | -// { | |
| 394 | -// max: 12, | |
| 395 | -// }, | |
| 396 | -// ], | |
| 397 | -// selectName: { | |
| 398 | -// title: '单选', | |
| 399 | -// type: 'string', | |
| 400 | -// enum: ['a', 'b', 'c'], | |
| 401 | -// enumNames: ['早', '中', '晚'], | |
| 402 | -// required: true, | |
| 403 | -// }, | |
| 404 | -// checkboxName: { | |
| 405 | -// title: '是否判断', | |
| 406 | -// type: 'boolean', | |
| 407 | -// valuePropName: 'checked', | |
| 408 | -// }, | |
| 409 | -// }, | |
| 410 | -// }, | |
| 411 | -// }, | |
| 412 | -// }; | |
| \ No newline at end of file |
lib/hooks.js
renamed from
lib/form-render-core/src/hooks.js
lib/index.css
renamed from
lib/form-render-core/src/index.css
| ... | ... | @@ -54,10 +54,9 @@ export interface FormInstance { |
| 54 | 54 | /** 折中升级方案中使用到,正常用不到 */ |
| 55 | 55 | init: () => void; |
| 56 | 56 | /** 数据分析接口,表单展示完成渲染时触发 */ |
| 57 | - logOnMount: (args: any) => void; | |
| 57 | + logOnMount?: (any) => void; | |
| 58 | 58 | /** 数据分析接口,表单提交成功时触发,获得本次表单填写的总时长 */ |
| 59 | - logOnSubmit: (args: any) => void; | |
| 60 | - _setErrors: (args: any) => void; | |
| 59 | + logOnSubmit?: (any) => void; | |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | 62 | export type WatchProperties = { | ... | ... |
| 1 | 1 | "use strict"; |
| 2 | 2 | |
| 3 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 4 | - | |
| 5 | 3 | Object.defineProperty(exports, "__esModule", { |
| 6 | 4 | value: true |
| 7 | 5 | }); |
| 8 | 6 | Object.defineProperty(exports, "connectForm", { |
| 9 | 7 | enumerable: true, |
| 10 | 8 | get: function get() { |
| 11 | - return _src.connectForm; | |
| 9 | + return _connectForm.default; | |
| 12 | 10 | } |
| 13 | 11 | }); |
| 14 | 12 | Object.defineProperty(exports, "createWidget", { |
| 15 | 13 | enumerable: true, |
| 16 | 14 | get: function get() { |
| 17 | - return _src.createWidget; | |
| 15 | + return _createWidget.createWidget; | |
| 18 | 16 | } |
| 19 | 17 | }); |
| 20 | 18 | exports.default = void 0; |
| 21 | 19 | Object.defineProperty(exports, "mapping", { |
| 22 | 20 | enumerable: true, |
| 23 | 21 | get: function get() { |
| 24 | - return _src.mapping; | |
| 22 | + return _mapping.mapping; | |
| 25 | 23 | } |
| 26 | 24 | }); |
| 27 | 25 | Object.defineProperty(exports, "useForm", { |
| 28 | 26 | enumerable: true, |
| 29 | 27 | get: function get() { |
| 30 | - return _src.useForm; | |
| 28 | + return _useForm.default; | |
| 31 | 29 | } |
| 32 | 30 | }); |
| 33 | 31 | Object.defineProperty(exports, "widgets", { |
| ... | ... | @@ -41,15 +39,33 @@ require("antd/es/config-provider/style"); |
| 41 | 39 | |
| 42 | 40 | var _configProvider = _interopRequireDefault(require("antd/es/config-provider")); |
| 43 | 41 | |
| 44 | -var _react = _interopRequireDefault(require("react")); | |
| 42 | +var _react = _interopRequireWildcard(require("react")); | |
| 45 | 43 | |
| 46 | -var _zh_CN = _interopRequireDefault(require("antd/lib/locale/zh_CN")); | |
| 44 | +var _utils = require("./utils"); | |
| 45 | + | |
| 46 | +var _core = _interopRequireDefault(require("./core")); | |
| 47 | 47 | |
| 48 | -var _src = _interopRequireWildcard(require("./form-render-core/src")); | |
| 48 | +var _hooks = require("./hooks"); | |
| 49 | 49 | |
| 50 | 50 | var _antd = require("./widgets/antd"); |
| 51 | 51 | |
| 52 | -var _excluded = ["widgets", "configProvider"]; | |
| 52 | +var _mapping = require("./mapping"); | |
| 53 | + | |
| 54 | +var _zh_CN = _interopRequireDefault(require("antd/lib/locale/zh_CN")); | |
| 55 | + | |
| 56 | +require("./atom.css"); | |
| 57 | + | |
| 58 | +require("./index.css"); | |
| 59 | + | |
| 60 | +var _useForm = _interopRequireDefault(require("./useForm")); | |
| 61 | + | |
| 62 | +var _connectForm = _interopRequireDefault(require("./connectForm")); | |
| 63 | + | |
| 64 | +var _createWidget = require("./createWidget"); | |
| 65 | + | |
| 66 | +var _excluded = ["id", "widgets", "mapping", "form", "className", "style", "beforeFinish", "onFinish", "displayType", "schema", "debug", "debugCss", "locale", "debounceInput", "size", "configProvider", "theme", "validateMessages", "watch", "config", "onMount", "labelWidth", "readOnly", "disabled", "allCollapsed", "onValuesChange", "column", "removeHiddenData", "globalProps"], | |
| 67 | + _excluded2 = ["onItemChange", "setEditing", "touchKey", "setValueByPath", "getSchemaByPath", "setSchemaByPath", "setSchema", "setValues", "getValues", "resetFields", "submit", "endValidating", "endSubmitting", "setErrorFields", "removeErrorField", "removeTouched", "changeTouchedKeys", "syncStuff", "logOnMount", "logOnSubmit", "setFirstMount"], | |
| 68 | + _excluded3 = ["isOldVersion", "schema"]; | |
| 53 | 69 | |
| 54 | 70 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } |
| 55 | 71 | |
| ... | ... | @@ -59,6 +75,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de |
| 59 | 75 | |
| 60 | 76 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } |
| 61 | 77 | |
| 78 | +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 79 | + | |
| 62 | 80 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } |
| 63 | 81 | |
| 64 | 82 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } |
| ... | ... | @@ -69,17 +87,348 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return |
| 69 | 87 | |
| 70 | 88 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } |
| 71 | 89 | |
| 72 | -var FR = function FR(_ref) { | |
| 73 | - var widgets = _ref.widgets, | |
| 90 | +var defaultFinish = function defaultFinish(data, errors) { | |
| 91 | + console.log('onFinish:', { | |
| 92 | + data: data, | |
| 93 | + errors: errors | |
| 94 | + }); | |
| 95 | +}; | |
| 96 | + | |
| 97 | +function App(_ref) { | |
| 98 | + var id = _ref.id, | |
| 99 | + widgets = _ref.widgets, | |
| 100 | + mapping = _ref.mapping, | |
| 101 | + form = _ref.form, | |
| 102 | + className = _ref.className, | |
| 103 | + style = _ref.style, | |
| 104 | + beforeFinish = _ref.beforeFinish, | |
| 105 | + _ref$onFinish = _ref.onFinish, | |
| 106 | + onFinish = _ref$onFinish === void 0 ? defaultFinish : _ref$onFinish, | |
| 107 | + _ref$displayType = _ref.displayType, | |
| 108 | + displayType = _ref$displayType === void 0 ? 'column' : _ref$displayType, | |
| 109 | + schema = _ref.schema, | |
| 110 | + debug = _ref.debug, | |
| 111 | + debugCss = _ref.debugCss, | |
| 112 | + _ref$locale = _ref.locale, | |
| 113 | + locale = _ref$locale === void 0 ? 'cn' : _ref$locale, | |
| 114 | + _ref$debounceInput = _ref.debounceInput, | |
| 115 | + debounceInput = _ref$debounceInput === void 0 ? false : _ref$debounceInput, | |
| 116 | + size = _ref.size, | |
| 74 | 117 | configProvider = _ref.configProvider, |
| 118 | + theme = _ref.theme, | |
| 119 | + validateMessages = _ref.validateMessages, | |
| 120 | + _ref$watch = _ref.watch, | |
| 121 | + watch = _ref$watch === void 0 ? {} : _ref$watch, | |
| 122 | + config = _ref.config, | |
| 123 | + onMount = _ref.onMount, | |
| 124 | + labelWidth = _ref.labelWidth, | |
| 125 | + readOnly = _ref.readOnly, | |
| 126 | + disabled = _ref.disabled, | |
| 127 | + _ref$allCollapsed = _ref.allCollapsed, | |
| 128 | + allCollapsed = _ref$allCollapsed === void 0 ? false : _ref$allCollapsed, | |
| 129 | + onValuesChange = _ref.onValuesChange, | |
| 130 | + column = _ref.column, | |
| 131 | + _ref$removeHiddenData = _ref.removeHiddenData, | |
| 132 | + removeHiddenData = _ref$removeHiddenData === void 0 ? false : _ref$removeHiddenData, | |
| 133 | + _ref$globalProps = _ref.globalProps, | |
| 134 | + globalProps = _ref$globalProps === void 0 ? {} : _ref$globalProps, | |
| 75 | 135 | rest = _objectWithoutProperties(_ref, _excluded); |
| 76 | 136 | |
| 137 | + try { | |
| 138 | + var _ = form.submit; | |
| 139 | + } catch (error) { | |
| 140 | + console.error('form 为必填 props,<FormRender /> 没有接收到 form 属性!'); | |
| 141 | + } | |
| 142 | + | |
| 143 | + var _column = schema && schema.column || column; | |
| 144 | + | |
| 145 | + var onItemChange = form.onItemChange, | |
| 146 | + setEditing = form.setEditing, | |
| 147 | + touchKey = form.touchKey, | |
| 148 | + setValueByPath = form.setValueByPath, | |
| 149 | + getSchemaByPath = form.getSchemaByPath, | |
| 150 | + setSchemaByPath = form.setSchemaByPath, | |
| 151 | + setSchema = form.setSchema, | |
| 152 | + setValues = form.setValues, | |
| 153 | + getValues = form.getValues, | |
| 154 | + resetFields = form.resetFields, | |
| 155 | + submit = form.submit, | |
| 156 | + endValidating = form.endValidating, | |
| 157 | + endSubmitting = form.endSubmitting, | |
| 158 | + setErrorFields = form.setErrorFields, | |
| 159 | + removeErrorField = form.removeErrorField, | |
| 160 | + removeTouched = form.removeTouched, | |
| 161 | + changeTouchedKeys = form.changeTouchedKeys, | |
| 162 | + syncStuff = form.syncStuff, | |
| 163 | + logOnMount = form.logOnMount, | |
| 164 | + logOnSubmit = form.logOnSubmit, | |
| 165 | + setFirstMount = form.setFirstMount, | |
| 166 | + valuesThatWillChange = _objectWithoutProperties(form, _excluded2); | |
| 167 | + | |
| 168 | + var submitData = valuesThatWillChange.submitData, | |
| 169 | + errorFields = valuesThatWillChange.errorFields, | |
| 170 | + isValidating = valuesThatWillChange.isValidating, | |
| 171 | + outsideValidating = valuesThatWillChange.outsideValidating, | |
| 172 | + isSubmitting = valuesThatWillChange.isSubmitting, | |
| 173 | + formData = valuesThatWillChange.formData, | |
| 174 | + flatten = valuesThatWillChange.flatten, | |
| 175 | + showValidate = valuesThatWillChange.showValidate, | |
| 176 | + firstMount = valuesThatWillChange.firstMount; | |
| 177 | + (0, _react.useEffect)(function () { | |
| 178 | + // Schema最外层的type是object来判断,没有的话,认为schema没有传 | |
| 179 | + if (schema && schema.type) { | |
| 180 | + setFirstMount(true); | |
| 181 | + syncStuff({ | |
| 182 | + schema: schema, | |
| 183 | + locale: locale, | |
| 184 | + validateMessages: validateMessages, | |
| 185 | + beforeFinish: beforeFinish, | |
| 186 | + onMount: onMount, | |
| 187 | + removeHiddenData: removeHiddenData | |
| 188 | + }); | |
| 189 | + } else {} | |
| 190 | + }, [JSON.stringify(schema)]); | |
| 191 | + (0, _react.useEffect)(function () { | |
| 192 | + if (!firstMount && schema && schema.type) { | |
| 193 | + if (typeof onMount === 'function') { | |
| 194 | + // 等一下 useForm 里接到第一份schema时,计算第一份data的骨架 | |
| 195 | + setTimeout(function () { | |
| 196 | + onMount(); | |
| 197 | + }, 0); | |
| 198 | + } | |
| 199 | + | |
| 200 | + setTimeout(onMountLogger, 0); | |
| 201 | + } | |
| 202 | + }, [JSON.stringify(schema), firstMount]); | |
| 203 | + | |
| 204 | + var onMountLogger = function onMountLogger() { | |
| 205 | + var start = new Date().getTime(); | |
| 206 | + | |
| 207 | + if (typeof logOnMount === 'function' || typeof logOnSubmit === 'function') { | |
| 208 | + sessionStorage.setItem('FORM_MOUNT_TIME', start); | |
| 209 | + sessionStorage.setItem('FORM_START', start); | |
| 210 | + } | |
| 211 | + | |
| 212 | + if (typeof logOnMount === 'function') { | |
| 213 | + var logParams = { | |
| 214 | + schema: schema, | |
| 215 | + url: location.href, | |
| 216 | + formData: JSON.stringify(form.getValues()), | |
| 217 | + formMount: (0, _utils.yymmdd)(start) | |
| 218 | + }; | |
| 219 | + | |
| 220 | + if (id) { | |
| 221 | + logParams.id = id; | |
| 222 | + } | |
| 223 | + | |
| 224 | + logOnMount(logParams); | |
| 225 | + } // 如果是要计算时间,在 onMount 时存一个时间戳 | |
| 226 | + | |
| 227 | + | |
| 228 | + if (typeof logOnSubmit === 'function') { | |
| 229 | + sessionStorage.setItem('NUMBER_OF_SUBMITS', 0); | |
| 230 | + sessionStorage.setItem('FAILED_ATTEMPTS', 0); | |
| 231 | + } | |
| 232 | + }; // 组件destroy的时候,destroy form,因为useForm可能在上层,所以不一定会跟着destroy | |
| 233 | + | |
| 234 | + | |
| 235 | + (0, _react.useEffect)(function () { | |
| 236 | + return function () { | |
| 237 | + form.resetFields(); | |
| 238 | + }; | |
| 239 | + }, []); | |
| 240 | + var store = (0, _react.useMemo)(function () { | |
| 241 | + return _objectSpread(_objectSpread({}, valuesThatWillChange), {}, { | |
| 242 | + globalProps: globalProps | |
| 243 | + }, rest); | |
| 244 | + }, [JSON.stringify(flatten), JSON.stringify(formData), JSON.stringify(errorFields), JSON.stringify(globalProps)]); // 不常用的context单独放一个地方 | |
| 245 | + | |
| 246 | + var store2 = (0, _react.useMemo)(function () { | |
| 247 | + return { | |
| 248 | + displayType: displayType, | |
| 249 | + theme: theme, | |
| 250 | + column: _column, | |
| 251 | + debounceInput: debounceInput, | |
| 252 | + debug: debug, | |
| 253 | + labelWidth: labelWidth, | |
| 254 | + locale: locale, | |
| 255 | + readOnly: readOnly, | |
| 256 | + disabled: disabled, | |
| 257 | + allCollapsed: allCollapsed, | |
| 258 | + showValidate: showValidate | |
| 259 | + }; | |
| 260 | + }, [displayType, theme, _column, debounceInput, debug, labelWidth, locale, readOnly, disabled, allCollapsed, showValidate]); | |
| 261 | + var tools = (0, _react.useMemo)(function () { | |
| 262 | + return _objectSpread({ | |
| 263 | + widgets: _objectSpread(_objectSpread({}, _antd.widgets), widgets), | |
| 264 | + mapping: _objectSpread(_objectSpread({}, _mapping.mapping), mapping), | |
| 265 | + onValuesChange: onValuesChange | |
| 266 | + }, form); | |
| 267 | + }, []); | |
| 268 | + (0, _react.useEffect)(function () { | |
| 269 | + // 需要外部校验的情况,此时 submitting 还是 false | |
| 270 | + if (outsideValidating === true) { | |
| 271 | + Promise.resolve(beforeFinish(_objectSpread({ | |
| 272 | + data: submitData, | |
| 273 | + schema: schema, | |
| 274 | + errors: errorFields | |
| 275 | + }, config))).then(function (error) { | |
| 276 | + if (error) { | |
| 277 | + setErrorFields(error); | |
| 278 | + } | |
| 279 | + | |
| 280 | + endValidating(); | |
| 281 | + }); | |
| 282 | + return; | |
| 283 | + } // 如果validation结束,submitting开始 | |
| 284 | + | |
| 285 | + | |
| 286 | + if (isValidating === false && isSubmitting === true) { | |
| 287 | + endSubmitting(); | |
| 288 | + onFinish(submitData, errorFields); | |
| 289 | + | |
| 290 | + if (typeof logOnSubmit === 'function') { | |
| 291 | + var start = sessionStorage.getItem('FORM_START'); | |
| 292 | + var mount = sessionStorage.getItem('FORM_MOUNT_TIME'); | |
| 293 | + var numberOfSubmits = Number(sessionStorage.getItem('NUMBER_OF_SUBMITS')) + 1; | |
| 294 | + var end = new Date().getTime(); | |
| 295 | + var failedAttempts = Number(sessionStorage.getItem('FAILED_ATTEMPTS')); | |
| 296 | + | |
| 297 | + if (errorFields.length > 0) { | |
| 298 | + failedAttempts = failedAttempts + 1; | |
| 299 | + } | |
| 300 | + | |
| 301 | + var logParams = { | |
| 302 | + formMount: (0, _utils.yymmdd)(mount), | |
| 303 | + ms: end - start, | |
| 304 | + duration: (0, _utils.msToTime)(end - start), | |
| 305 | + numberOfSubmits: numberOfSubmits, | |
| 306 | + failedAttempts: failedAttempts, | |
| 307 | + url: location.href, | |
| 308 | + formData: JSON.stringify(submitData), | |
| 309 | + errors: JSON.stringify(errorFields), | |
| 310 | + schema: JSON.stringify(schema) | |
| 311 | + }; | |
| 312 | + | |
| 313 | + if (id) { | |
| 314 | + logParams.id = id; | |
| 315 | + } | |
| 316 | + | |
| 317 | + logOnSubmit(logParams); | |
| 318 | + sessionStorage.setItem('FORM_START', end); | |
| 319 | + sessionStorage.setItem('NUMBER_OF_SUBMITS', numberOfSubmits); | |
| 320 | + sessionStorage.setItem('FAILED_ATTEMPTS', failedAttempts); | |
| 321 | + } | |
| 322 | + } | |
| 323 | + }, [isValidating, isSubmitting, outsideValidating]); // TODO: fk doesn't work | |
| 324 | + | |
| 325 | + var sizeCls = ''; | |
| 326 | + | |
| 327 | + if (size === 'small') { | |
| 328 | + sizeCls = 'fr-form-small'; | |
| 329 | + } else if (size === 'large') { | |
| 330 | + sizeCls = 'fr-form-large'; | |
| 331 | + } | |
| 332 | + | |
| 333 | + var rootProps = { | |
| 334 | + className: "fr-container ".concat(sizeCls, " ").concat(className || '') | |
| 335 | + }; | |
| 336 | + | |
| 337 | + if (style && _typeof(style) === 'object') { | |
| 338 | + rootProps.style = style; | |
| 339 | + } | |
| 340 | + | |
| 341 | + if (id && ['number', 'string'].indexOf(_typeof(id)) > -1) { | |
| 342 | + rootProps.id = id; | |
| 343 | + } | |
| 344 | + | |
| 345 | + var watchList = Object.keys(watch); | |
| 77 | 346 | return /*#__PURE__*/_react.default.createElement(_configProvider.default, _extends({ |
| 78 | 347 | locale: _zh_CN.default |
| 79 | - }, configProvider), /*#__PURE__*/_react.default.createElement(_src.default, _extends({ | |
| 80 | - widgets: _objectSpread(_objectSpread({}, _antd.widgets), widgets) | |
| 81 | - }, rest))); | |
| 348 | + }, configProvider), /*#__PURE__*/_react.default.createElement(_hooks.StoreCtx.Provider, { | |
| 349 | + value: store | |
| 350 | + }, /*#__PURE__*/_react.default.createElement(_hooks.Store2Ctx.Provider, { | |
| 351 | + value: store2 | |
| 352 | + }, /*#__PURE__*/_react.default.createElement(_hooks.Ctx.Provider, { | |
| 353 | + value: tools | |
| 354 | + }, /*#__PURE__*/_react.default.createElement("div", rootProps, debug ? /*#__PURE__*/_react.default.createElement("div", { | |
| 355 | + className: "mv2 bg-black-05 pa2 br2" | |
| 356 | + }, /*#__PURE__*/_react.default.createElement("div", { | |
| 357 | + style: { | |
| 358 | + display: 'flex' | |
| 359 | + } | |
| 360 | + }, /*#__PURE__*/_react.default.createElement("span", null, "formData:"), /*#__PURE__*/_react.default.createElement("span", { | |
| 361 | + style: { | |
| 362 | + display: 'inline-block', | |
| 363 | + wordBreak: 'break-all', | |
| 364 | + maxWidth: 600 | |
| 365 | + } | |
| 366 | + }, JSON.stringify(form.formData))), /*#__PURE__*/_react.default.createElement("div", null, 'errorFields:' + JSON.stringify(form.errorFields)), /*#__PURE__*/_react.default.createElement("div", null, 'touchedKeys:' + JSON.stringify(form.touchedKeys)), /*#__PURE__*/_react.default.createElement("div", null, 'allTouched:' + JSON.stringify(form.allTouched)), /*#__PURE__*/_react.default.createElement("div", null, 'descriptor:' + JSON.stringify(window.descriptor))) : null, watchList.length > 0 ? watchList.map(function (item, idx) { | |
| 367 | + return /*#__PURE__*/_react.default.createElement(Watcher, { | |
| 368 | + key: idx.toString(), | |
| 369 | + watchKey: item, | |
| 370 | + watch: watch, | |
| 371 | + formData: formData, | |
| 372 | + firstMount: firstMount | |
| 373 | + }); | |
| 374 | + }) : null, /*#__PURE__*/_react.default.createElement(_core.default, { | |
| 375 | + debugCss: debugCss | |
| 376 | + })))))); | |
| 377 | +} | |
| 378 | + | |
| 379 | +var Wrapper = function Wrapper(props) { | |
| 380 | + var _props$isOldVersion = props.isOldVersion, | |
| 381 | + isOldVersion = _props$isOldVersion === void 0 ? true : _props$isOldVersion, | |
| 382 | + schema = props.schema, | |
| 383 | + rest = _objectWithoutProperties(props, _excluded3); | |
| 384 | + | |
| 385 | + var _schema = (0, _react.useRef)(schema); | |
| 386 | + | |
| 387 | + if (isOldVersion) { | |
| 388 | + _schema.current = (0, _utils.updateSchemaToNewVersion)(schema); | |
| 389 | + } | |
| 390 | + | |
| 391 | + return /*#__PURE__*/_react.default.createElement(App, _extends({ | |
| 392 | + schema: _schema.current | |
| 393 | + }, rest)); | |
| 82 | 394 | }; |
| 83 | 395 | |
| 84 | -var _default = FR; | |
| 85 | -exports.default = _default; | |
| \ No newline at end of file | ||
| 396 | +var _default = Wrapper; | |
| 397 | +exports.default = _default; | |
| 398 | + | |
| 399 | +var Watcher = function Watcher(_ref2) { | |
| 400 | + var watchKey = _ref2.watchKey, | |
| 401 | + watch = _ref2.watch, | |
| 402 | + formData = _ref2.formData, | |
| 403 | + firstMount = _ref2.firstMount; | |
| 404 | + var value = (0, _utils.getValueByPath)(formData, watchKey); | |
| 405 | + var watchObj = watch[watchKey]; | |
| 406 | + (0, _react.useEffect)(function () { | |
| 407 | + var runWatcher = function runWatcher() { | |
| 408 | + if (typeof watchObj === 'function') { | |
| 409 | + try { | |
| 410 | + watchObj(value); | |
| 411 | + } catch (error) { | |
| 412 | + console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 413 | + } | |
| 414 | + } else if (watchObj && typeof watchObj.handler === 'function') { | |
| 415 | + try { | |
| 416 | + watchObj.handler(value); | |
| 417 | + } catch (error) { | |
| 418 | + console.log("".concat(watchKey, "\u5BF9\u5E94\u7684watch\u51FD\u6570\u6267\u884C\u62A5\u9519\uFF1A"), error); | |
| 419 | + } | |
| 420 | + } | |
| 421 | + }; | |
| 422 | + | |
| 423 | + if (firstMount) { | |
| 424 | + var immediate = watchObj && watchObj.immediate; | |
| 425 | + | |
| 426 | + if (immediate) { | |
| 427 | + runWatcher(); | |
| 428 | + } | |
| 429 | + } else { | |
| 430 | + runWatcher(); | |
| 431 | + } | |
| 432 | + }, [JSON.stringify(value), firstMount]); | |
| 433 | + return null; | |
| 434 | +}; | |
| \ No newline at end of file | ... | ... |
lib/processData.js
renamed from
lib/form-render-core/src/processData.js
lib/useDebounce.js
renamed from
lib/form-render-core/src/useDebounce.js
lib/useForm.js
renamed from
lib/form-render-core/src/useForm.js
| ... | ... | @@ -76,6 +76,8 @@ var useForm = function useForm(props) { |
| 76 | 76 | // 是否所有表单元素都被碰过了(一键开关,用于提交的时候,默认所有都被touch了) |
| 77 | 77 | touchedKeys: [], |
| 78 | 78 | // 碰过的key(用于submit之前,判断哪些被碰过了) |
| 79 | + flatten: {}, | |
| 80 | + // schema 的转换结构,便于处理 | |
| 79 | 81 | finalFlatten: {}, |
| 80 | 82 | // 表达式等被处理过的flatten,用于渲染 |
| 81 | 83 | firstMount: true |
| ... | ... | @@ -121,11 +123,13 @@ var useForm = function useForm(props) { |
| 121 | 123 | isEditing = state.isEditing, |
| 122 | 124 | allTouched = state.allTouched, |
| 123 | 125 | touchedKeys = state.touchedKeys, |
| 126 | + flatten = state.flatten, | |
| 124 | 127 | finalFlatten = state.finalFlatten, |
| 125 | 128 | firstMount = state.firstMount; |
| 126 | 129 | _errorFields.current = errorFields; |
| 127 | 130 | _outErrorFields.current = outErrorFields; |
| 128 | 131 | _touchedKeys.current = touchedKeys; |
| 132 | + _flatten.current = flatten; | |
| 129 | 133 | _finalFlatten.current = finalFlatten; |
| 130 | 134 | var dataFromOutside = props && props.hasOwnProperty('formData'); |
| 131 | 135 | var formData = dataFromOutside ? _formData : innerData; // 生成一个基础结构,确保对象内的必填元素也被校验 |
| ... | ... | @@ -148,39 +152,55 @@ var useForm = function useForm(props) { |
| 148 | 152 | } |
| 149 | 153 | }, [JSON.stringify(_errorFields.current), JSON.stringify(_outErrorFields.current)]); |
| 150 | 154 | (0, _react.useEffect)(function () { |
| 151 | - if (schemaRef.current) { | |
| 152 | - var newFlatten = (0, _utils.clone)(_flatten.current); | |
| 153 | - | |
| 154 | - if (firstMount) { | |
| 155 | - _flatten.current = (0, _utils.flattenSchema)(schemaRef.current); | |
| 156 | - setState({ | |
| 157 | - firstMount: false | |
| 158 | - }); | |
| 159 | - } else { | |
| 160 | - // 统一的处理expression | |
| 161 | - Object.entries(_flatten.current).forEach(function (_ref2) { | |
| 162 | - var _ref3 = _slicedToArray(_ref2, 2), | |
| 163 | - path = _ref3[0], | |
| 164 | - info = _ref3[1]; | |
| 165 | - | |
| 166 | - if ((0, _utils.schemaContainsExpression)(info.schema)) { | |
| 167 | - var arrayLikeIndex = path.indexOf(']'); | |
| 168 | - var isArrayItem = arrayLikeIndex > -1 && arrayLikeIndex < path.length - 1; | |
| 169 | - var hasRootValue = JSON.stringify(info.schema).indexOf('rootValue') > -1; | |
| 170 | - | |
| 171 | - if (isArrayItem && hasRootValue) {// do nothing | |
| 172 | - } else { | |
| 173 | - newFlatten[path].schema = (0, _utils.parseAllExpression)(info.schema, _data.current, path); | |
| 174 | - } | |
| 175 | - } | |
| 176 | - }); | |
| 177 | - } | |
| 155 | + if (schemaRef.current && firstMount) { | |
| 156 | + var _flatten2 = (0, _utils.flattenSchema)(schemaRef.current); | |
| 178 | 157 | |
| 179 | 158 | setState({ |
| 180 | - finalFlatten: newFlatten | |
| 159 | + flatten: _flatten2, | |
| 160 | + firstMount: false | |
| 181 | 161 | }); |
| 182 | 162 | } |
| 183 | - }, [JSON.stringify(schemaRef.current), JSON.stringify(_data.current), firstMount]); // All form methods are down here ---------------------------------------------------------------- | |
| 163 | + }, [JSON.stringify(schemaRef.current), firstMount]); // 统一的处理expression | |
| 164 | + | |
| 165 | + (0, _react.useEffect)(function () { | |
| 166 | + if (firstMount) { | |
| 167 | + return; | |
| 168 | + } | |
| 169 | + | |
| 170 | + var newFlatten = (0, _utils.clone)(_flatten.current); | |
| 171 | + Object.entries(_flatten.current).forEach(function (_ref2) { | |
| 172 | + var _ref3 = _slicedToArray(_ref2, 2), | |
| 173 | + path = _ref3[0], | |
| 174 | + info = _ref3[1]; | |
| 175 | + | |
| 176 | + if ((0, _utils.schemaContainsExpression)(info.schema)) { | |
| 177 | + var arrayLikeIndex = path.indexOf(']'); | |
| 178 | + var isArrayItem = arrayLikeIndex > -1 && arrayLikeIndex < path.length - 1; | |
| 179 | + var hasRootValue = JSON.stringify(info.schema).indexOf('rootValue') > -1; | |
| 180 | + | |
| 181 | + if (isArrayItem && hasRootValue) {// do nothing | |
| 182 | + } else { | |
| 183 | + newFlatten[path].schema = (0, _utils.parseAllExpression)(info.schema, _data.current, path); | |
| 184 | + } | |
| 185 | + } | |
| 186 | + }); | |
| 187 | + setState({ | |
| 188 | + finalFlatten: newFlatten | |
| 189 | + }); | |
| 190 | + }, [JSON.stringify(_flatten.current), JSON.stringify(_data.current), firstMount]); | |
| 191 | + (0, _react.useEffect)(function () { | |
| 192 | + if (firstMount) return; | |
| 193 | + (0, _validator.validateAll)({ | |
| 194 | + formData: _data.current, | |
| 195 | + flatten: _finalFlatten.current, | |
| 196 | + isRequired: allTouched, | |
| 197 | + touchedKeys: _touchedKeys.current, | |
| 198 | + locale: localeRef.current, | |
| 199 | + validateMessages: validateMessagesRef.current | |
| 200 | + }).then(function (res) { | |
| 201 | + _setErrors(res); | |
| 202 | + }); | |
| 203 | + }, [JSON.stringify(_data.current)]); // All form methods are down here ---------------------------------------------------------------- | |
| 184 | 204 | // 两个兼容 0.x 的函数 |
| 185 | 205 | |
| 186 | 206 | var _setData = function _setData(data) { |
| ... | ... | @@ -191,8 +211,7 @@ var useForm = function useForm(props) { |
| 191 | 211 | formData: data |
| 192 | 212 | }); |
| 193 | 213 | } |
| 194 | - }; // Allow function to get the old value | |
| 195 | - | |
| 214 | + }; | |
| 196 | 215 | |
| 197 | 216 | var _setErrors = function _setErrors(errors) { |
| 198 | 217 | if (typeof _onValidate === 'function') { |
| ... | ... | @@ -203,18 +222,9 @@ var useForm = function useForm(props) { |
| 203 | 222 | _onValidate(oldFormatErrors); |
| 204 | 223 | } |
| 205 | 224 | |
| 206 | - if (typeof errors === 'function') { | |
| 207 | - setState(function (_ref4) { | |
| 208 | - var errorFields = _ref4.errorFields; | |
| 209 | - return { | |
| 210 | - errorFields: errors(errorFields) | |
| 211 | - }; | |
| 212 | - }); | |
| 213 | - } else { | |
| 214 | - setState({ | |
| 215 | - errorFields: errors | |
| 216 | - }); | |
| 217 | - } | |
| 225 | + setState({ | |
| 226 | + errorFields: errors | |
| 227 | + }); | |
| 218 | 228 | }; |
| 219 | 229 | |
| 220 | 230 | var setFirstMount = function setFirstMount(value) { |
| ... | ... | @@ -273,12 +283,12 @@ var useForm = function useForm(props) { |
| 273 | 283 | // ] |
| 274 | 284 | |
| 275 | 285 | |
| 276 | - var syncStuff = function syncStuff(_ref5) { | |
| 277 | - var schema = _ref5.schema, | |
| 278 | - locale = _ref5.locale, | |
| 279 | - validateMessages = _ref5.validateMessages, | |
| 280 | - beforeFinish = _ref5.beforeFinish, | |
| 281 | - removeHiddenData = _ref5.removeHiddenData; | |
| 286 | + var syncStuff = function syncStuff(_ref4) { | |
| 287 | + var schema = _ref4.schema, | |
| 288 | + locale = _ref4.locale, | |
| 289 | + validateMessages = _ref4.validateMessages, | |
| 290 | + beforeFinish = _ref4.beforeFinish, | |
| 291 | + removeHiddenData = _ref4.removeHiddenData; | |
| 282 | 292 | schemaRef.current = schema; |
| 283 | 293 | localeRef.current = locale; |
| 284 | 294 | validateMessagesRef.current = validateMessages; |
| ... | ... | @@ -288,11 +298,11 @@ var useForm = function useForm(props) { |
| 288 | 298 | }; |
| 289 | 299 | |
| 290 | 300 | var setSchema = function setSchema(settings) { |
| 291 | - var newFlatten = (0, _utils.clone)(_finalFlatten.current); | |
| 301 | + var newFlatten = (0, _utils.clone)(_flatten.current); | |
| 292 | 302 | |
| 293 | 303 | try { |
| 294 | 304 | Object.keys(settings).forEach(function (path) { |
| 295 | - if (!_finalFlatten.current[path]) { | |
| 305 | + if (!_flatten.current[path]) { | |
| 296 | 306 | console.error("path\uFF1A'".concat(path, "' \u4E0D\u5B58\u5728(form.setSchemaByPath)")); |
| 297 | 307 | } else { |
| 298 | 308 | var newSchema = settings[path]; |
| ... | ... | @@ -303,30 +313,30 @@ var useForm = function useForm(props) { |
| 303 | 313 | } |
| 304 | 314 | }); |
| 305 | 315 | setState({ |
| 306 | - finalFlatten: newFlatten | |
| 316 | + flatten: newFlatten | |
| 307 | 317 | }); |
| 308 | - _finalFlatten.current = newFlatten; | |
| 318 | + _flatten.current = newFlatten; | |
| 309 | 319 | } catch (error) { |
| 310 | 320 | console.error(error, 'setSchema'); |
| 311 | 321 | } |
| 312 | 322 | }; |
| 313 | 323 | |
| 314 | 324 | var setSchemaByPath = function setSchemaByPath(path, newSchema) { |
| 315 | - if (!_finalFlatten.current[path]) { | |
| 325 | + if (!_flatten.current[path]) { | |
| 316 | 326 | console.error("path\uFF1A'".concat(path, "' \u4E0D\u5B58\u5728(form.setSchemaByPath)")); |
| 317 | 327 | return; |
| 318 | 328 | } |
| 319 | 329 | |
| 320 | - var newFlatten = (0, _utils.clone)(_finalFlatten.current); | |
| 330 | + var newFlatten = (0, _utils.clone)(_flatten.current); | |
| 321 | 331 | |
| 322 | 332 | try { |
| 323 | 333 | var _newSchema = typeof newSchema === 'function' ? newSchema(newFlatten[path].schema) : newSchema; |
| 324 | 334 | |
| 325 | 335 | newFlatten[path].schema = _objectSpread(_objectSpread({}, newFlatten[path].schema), _newSchema); |
| 326 | 336 | setState({ |
| 327 | - finalFlatten: newFlatten | |
| 337 | + flatten: newFlatten | |
| 328 | 338 | }); |
| 329 | - _finalFlatten.current = newFlatten; | |
| 339 | + _flatten.current = newFlatten; | |
| 330 | 340 | } catch (error) { |
| 331 | 341 | console.error(error, 'setSchemaByPath'); |
| 332 | 342 | } |
| ... | ... | @@ -334,7 +344,7 @@ var useForm = function useForm(props) { |
| 334 | 344 | |
| 335 | 345 | var getSchemaByPath = function getSchemaByPath(path) { |
| 336 | 346 | try { |
| 337 | - return _finalFlatten.current[path].schema; | |
| 347 | + return _flatten.current[path].schema; | |
| 338 | 348 | } catch (error) { |
| 339 | 349 | console.log(error, 'getSchemaByPath'); |
| 340 | 350 | return {}; |
| ... | ... | @@ -376,7 +386,7 @@ var useForm = function useForm(props) { |
| 376 | 386 | }; |
| 377 | 387 | |
| 378 | 388 | var setValues = function setValues(newFormData) { |
| 379 | - var newData = (0, _processData.transformDataWithBind2)(newFormData, _finalFlatten.current); | |
| 389 | + var newData = (0, _processData.transformDataWithBind2)(newFormData, _flatten.current); | |
| 380 | 390 | |
| 381 | 391 | _setData(newData); |
| 382 | 392 | }; |
| ... | ... | @@ -391,10 +401,10 @@ var useForm = function useForm(props) { |
| 391 | 401 | return (0, _validator.validateAll)({ |
| 392 | 402 | formData: _data.current, |
| 393 | 403 | flatten: _finalFlatten.current, |
| 394 | - options: { | |
| 395 | - locale: localeRef.current, | |
| 396 | - validateMessages: validateMessagesRef.current | |
| 397 | - } | |
| 404 | + touchedKeys: [], | |
| 405 | + isRequired: true, | |
| 406 | + locale: localeRef.current, | |
| 407 | + validateMessages: validateMessagesRef.current | |
| 398 | 408 | }).then(function (errors) { |
| 399 | 409 | setState({ |
| 400 | 410 | errorFields: errors |
| ... | ... | @@ -437,13 +447,13 @@ var useForm = function useForm(props) { |
| 437 | 447 | }); |
| 438 | 448 | }; |
| 439 | 449 | |
| 440 | - var resetFields = function resetFields(options) { | |
| 450 | + var resetFields = function resetFields() { | |
| 441 | 451 | setState({ |
| 442 | - formData: (options === null || options === void 0 ? void 0 : options.formData) || {}, | |
| 443 | - submitData: (options === null || options === void 0 ? void 0 : options.submitData) || {}, | |
| 444 | - errorFields: (options === null || options === void 0 ? void 0 : options.errorFields) || [], | |
| 445 | - touchedKeys: (options === null || options === void 0 ? void 0 : options.touchedKeys) || [], | |
| 446 | - allTouched: (options === null || options === void 0 ? void 0 : options.allTouched) || false | |
| 452 | + formData: {}, | |
| 453 | + submitData: {}, | |
| 454 | + errorFields: [], | |
| 455 | + touchedKeys: [], | |
| 456 | + allTouched: false | |
| 447 | 457 | }); |
| 448 | 458 | }; |
| 449 | 459 | |
| ... | ... | @@ -503,9 +513,7 @@ var useForm = function useForm(props) { |
| 503 | 513 | setFirstMount: setFirstMount, |
| 504 | 514 | // logs |
| 505 | 515 | logOnMount: logOnMount, |
| 506 | - logOnSubmit: logOnSubmit, | |
| 507 | - // inner api, DON'T USE | |
| 508 | - _setErrors: _setErrors | |
| 516 | + logOnSubmit: logOnSubmit | |
| 509 | 517 | }; |
| 510 | 518 | return form; |
| 511 | 519 | }; | ... | ... |
| ... | ... | @@ -3,10 +3,90 @@ |
| 3 | 3 | Object.defineProperty(exports, "__esModule", { |
| 4 | 4 | value: true |
| 5 | 5 | }); |
| 6 | -exports.getArray = void 0; | |
| 6 | +exports.clone = exports.cleanEmpty = void 0; | |
| 7 | +exports.combineSchema = combineSchema; | |
| 8 | +exports.completeSchemaWithTheme = void 0; | |
| 9 | +exports.defaultGetValueFromEvent = defaultGetValueFromEvent; | |
| 10 | +exports.destructDataPath = destructDataPath; | |
| 11 | +exports.evaluateString = void 0; | |
| 12 | +exports.flattenSchema = flattenSchema; | |
| 13 | +exports.getArray = exports.generateDataSkeleton = exports.formatPathFromValidator = void 0; | |
| 14 | +exports.getDataPath = getDataPath; | |
| 15 | +exports.getEnum = exports.getDisplayValue = exports.getDescriptorFromSchema = void 0; | |
| 7 | 16 | exports.getFormat = getFormat; |
| 17 | +exports.getKeyFromPath = void 0; | |
| 18 | +exports.getParentPath = getParentPath; | |
| 19 | +exports.getSaveNumber = exports.getParentProps = void 0; | |
| 20 | +exports.getSchemaFromFlatten = getSchemaFromFlatten; | |
| 21 | +exports.getValueByPath = getValueByPath; | |
| 22 | +exports.hasRepeat = hasRepeat; | |
| 23 | +exports.isCheckBoxType = isCheckBoxType; | |
| 24 | +exports.isCssLength = isCssLength; | |
| 25 | +exports.isDeepEqual = isDeepEqual; | |
| 26 | +exports.isEmail = void 0; | |
| 27 | +exports.isExpression = isExpression; | |
| 28 | +exports.isFunctionSchema = isFunctionSchema; | |
| 29 | +exports.isFunctionString = void 0; | |
| 30 | +exports.isListType = isListType; | |
| 31 | +exports.isLooselyNumber = isLooselyNumber; | |
| 32 | +exports.isObjType = isObjType; | |
| 33 | +exports.isPathRequired = exports.isObject = void 0; | |
| 8 | 34 | exports.isUrl = isUrl; |
| 35 | +exports.looseJsonParse = looseJsonParse; | |
| 36 | +exports.msToTime = msToTime; | |
| 37 | +exports.parseAllExpression = exports.oldSchemaToNew = exports.newSchemaToOld = void 0; | |
| 38 | +exports.parseFunction = parseFunction; | |
| 39 | +exports.parseRootValueInSchema = exports.parseFunctionString = void 0; | |
| 40 | +exports.parseSingleExpression = parseSingleExpression; | |
| 41 | +exports.updateSchemaToNewVersion = exports.translateMessage = exports.schemaContainsExpression = exports.removeHiddenFromResult = exports.removeEmptyItemFromList = exports.parseString = exports.parseSingleRootValue = void 0; | |
| 42 | +exports.yymmdd = yymmdd; | |
| 9 | 43 | |
| 44 | +var _lodashEs = require("lodash-es"); | |
| 45 | + | |
| 46 | +var _excluded = ["propsSchema"], | |
| 47 | + _excluded2 = ["schema"], | |
| 48 | + _excluded3 = ["required"]; | |
| 49 | + | |
| 50 | +function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); } | |
| 51 | + | |
| 52 | +function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 53 | + | |
| 54 | +function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 55 | + | |
| 56 | +function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 57 | + | |
| 58 | +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 59 | + | |
| 60 | +function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 61 | + | |
| 62 | +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 63 | + | |
| 64 | +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 65 | + | |
| 66 | +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 67 | + | |
| 68 | +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 69 | + | |
| 70 | +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 71 | + | |
| 72 | +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 73 | + | |
| 74 | +function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 75 | + | |
| 76 | +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 77 | + | |
| 78 | +// window.log1 = value => { | |
| 79 | +// console.log('%ccommon:', 'color: #00A7F7; font-weight: 500;', value); | |
| 80 | +// }; | |
| 81 | +// window.log2 = value => { | |
| 82 | +// console.log('%cwarning:', 'color: #f50; font-weight: 500;', value); | |
| 83 | +// }; | |
| 84 | +// window.log3 = value => { | |
| 85 | +// console.log('%csuccess:', 'color: #87d068; font-weight: 500;', value); | |
| 86 | +// }; | |
| 87 | +// window.log4 = value => { | |
| 88 | +// console.log('%cspecial:', 'color: #722ed1; font-weight: 500;', value); | |
| 89 | +// }; | |
| 10 | 90 | function isUrl(string) { |
| 11 | 91 | var protocolRE = /^(?:\w+:)?\/\/(\S+)$/; // const domainRE = /^[^\s\.]+\.\S{2,}$/; |
| 12 | 92 | |
| ... | ... | @@ -14,13 +94,258 @@ function isUrl(string) { |
| 14 | 94 | return protocolRE.test(string); |
| 15 | 95 | } |
| 16 | 96 | |
| 17 | -var getArray = function getArray(arr) { | |
| 18 | - var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| 19 | - if (Array.isArray(arr)) return arr; | |
| 20 | - return defaultValue; | |
| 97 | +function isCheckBoxType(schema, readOnly) { | |
| 98 | + if (readOnly) return false; | |
| 99 | + if (schema.widget === 'checkbox') return true; | |
| 100 | + | |
| 101 | + if (schema && schema.type === 'boolean') { | |
| 102 | + if (schema.enum) return false; | |
| 103 | + if (schema.widget === undefined) return true; | |
| 104 | + return false; | |
| 105 | + } | |
| 106 | +} // a[].b.c => a.b.c | |
| 107 | + | |
| 108 | + | |
| 109 | +function removeBrackets(string) { | |
| 110 | + if (typeof string === 'string') { | |
| 111 | + return string.replace(/\[\]/g, ''); | |
| 112 | + } else { | |
| 113 | + return string; | |
| 114 | + } | |
| 115 | +} | |
| 116 | + | |
| 117 | +function getParentPath(path) { | |
| 118 | + if (typeof path === 'string') { | |
| 119 | + var pathArr = path.split('.'); | |
| 120 | + | |
| 121 | + if (pathArr.length === 1) { | |
| 122 | + return '#'; | |
| 123 | + } | |
| 124 | + | |
| 125 | + pathArr.pop(); | |
| 126 | + return pathArr.join('.'); | |
| 127 | + } | |
| 128 | + | |
| 129 | + return '#'; | |
| 130 | +} | |
| 131 | + | |
| 132 | +function getValueByPath(formData, path) { | |
| 133 | + if (path === '#' || !path) { | |
| 134 | + return formData || {}; | |
| 135 | + } else if (typeof path === 'string') { | |
| 136 | + return (0, _lodashEs.get)(formData, path); | |
| 137 | + } else { | |
| 138 | + console.error('path has to be a string'); | |
| 139 | + } | |
| 140 | +} // path: 'a.b[1].c[0]' => { id: 'a.b[].c[]' dataIndex: [1,0] } | |
| 141 | + | |
| 142 | + | |
| 143 | +function destructDataPath(path) { | |
| 144 | + var id; | |
| 145 | + var dataIndex; | |
| 146 | + | |
| 147 | + if (path === '#') { | |
| 148 | + return { | |
| 149 | + id: '#', | |
| 150 | + dataIndex: [] | |
| 151 | + }; | |
| 152 | + } | |
| 153 | + | |
| 154 | + if (typeof path !== 'string') { | |
| 155 | + throw Error("path ".concat(path, " is not a string!!! Something wrong here")); | |
| 156 | + } | |
| 157 | + | |
| 158 | + var pattern = /\[[0-9]+\]/g; | |
| 159 | + var matchList = path.match(pattern); | |
| 160 | + | |
| 161 | + if (!matchList) { | |
| 162 | + id = path; | |
| 163 | + } else { | |
| 164 | + id = path.replace(pattern, '[]'); // 这个是match下来的结果,可安全处理 | |
| 165 | + | |
| 166 | + dataIndex = matchList.map(function (item) { | |
| 167 | + return Number(item.substring(1, item.length - 1)); | |
| 168 | + }); | |
| 169 | + } | |
| 170 | + | |
| 171 | + return { | |
| 172 | + id: id, | |
| 173 | + dataIndex: dataIndex | |
| 174 | + }; | |
| 175 | +} // id: 'a.b[].c[]' dataIndex: [1,0] => 'a.b[1].c[0]' | |
| 176 | + | |
| 177 | + | |
| 178 | +function getDataPath(id, dataIndex) { | |
| 179 | + if (id === '#') { | |
| 180 | + return id; | |
| 181 | + } | |
| 182 | + | |
| 183 | + if (typeof id !== 'string') { | |
| 184 | + throw Error("id ".concat(id, " is not a string!!! Something wrong here")); | |
| 185 | + } | |
| 186 | + | |
| 187 | + var _id = id; | |
| 188 | + | |
| 189 | + if (Array.isArray(dataIndex)) { | |
| 190 | + // const matches = id.match(/\[\]/g) || []; | |
| 191 | + // const count = matches.length; | |
| 192 | + dataIndex.forEach(function (item) { | |
| 193 | + _id = _id.replace(/\[\]/, "[".concat(item, "]")); | |
| 194 | + }); | |
| 195 | + } | |
| 196 | + | |
| 197 | + return removeBrackets(_id); | |
| 198 | +} | |
| 199 | + | |
| 200 | +function isObjType(schema) { | |
| 201 | + return schema && schema.type === 'object' && schema.properties && !schema.widget; | |
| 202 | +} // TODO: to support case that item is not an object | |
| 203 | + | |
| 204 | + | |
| 205 | +function isListType(schema) { | |
| 206 | + return schema && schema.type === 'array' && isObjType(schema.items) && schema.enum === undefined; | |
| 207 | +} // TODO: more tests to make sure weird & wrong schema won't crush | |
| 208 | + | |
| 209 | + | |
| 210 | +function flattenSchema() { | |
| 211 | + var _schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 212 | + | |
| 213 | + var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 214 | + var parent = arguments.length > 2 ? arguments[2] : undefined; | |
| 215 | + var result = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | |
| 216 | + var schema = clone(_schema); | |
| 217 | + var _name = name; | |
| 218 | + | |
| 219 | + if (!schema.$id) { | |
| 220 | + schema.$id = _name; // path as $id, for easy access to path in schema | |
| 221 | + } | |
| 222 | + | |
| 223 | + var children = []; | |
| 224 | + | |
| 225 | + if (isObjType(schema)) { | |
| 226 | + Object.entries(schema.properties).forEach(function (_ref) { | |
| 227 | + var _ref2 = _slicedToArray(_ref, 2), | |
| 228 | + key = _ref2[0], | |
| 229 | + value = _ref2[1]; | |
| 230 | + | |
| 231 | + var _key = isListType(value) ? key + '[]' : key; | |
| 232 | + | |
| 233 | + var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 234 | + children.push(uniqueName); | |
| 235 | + flattenSchema(value, uniqueName, _name, result); | |
| 236 | + }); | |
| 237 | + schema.properties = {}; | |
| 238 | + } | |
| 239 | + | |
| 240 | + if (isListType(schema)) { | |
| 241 | + Object.entries(schema.items.properties).forEach(function (_ref3) { | |
| 242 | + var _ref4 = _slicedToArray(_ref3, 2), | |
| 243 | + key = _ref4[0], | |
| 244 | + value = _ref4[1]; | |
| 245 | + | |
| 246 | + var _key = isListType(value) ? key + '[]' : key; | |
| 247 | + | |
| 248 | + var uniqueName = _name === '#' ? _key : _name + '.' + _key; | |
| 249 | + children.push(uniqueName); | |
| 250 | + flattenSchema(value, uniqueName, _name, result); | |
| 251 | + }); | |
| 252 | + schema.items.properties = {}; | |
| 253 | + } | |
| 254 | + | |
| 255 | + if (schema.type) { | |
| 256 | + result[_name] = { | |
| 257 | + parent: parent, | |
| 258 | + schema: schema, | |
| 259 | + children: children | |
| 260 | + }; | |
| 261 | + } | |
| 262 | + | |
| 263 | + return result; | |
| 264 | +} | |
| 265 | + | |
| 266 | +function getSchemaFromFlatten(flatten) { | |
| 267 | + var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#'; | |
| 268 | + var schema = {}; | |
| 269 | + var item = clone(flatten[path]); | |
| 270 | + | |
| 271 | + if (item) { | |
| 272 | + schema = item.schema; // schema.$id && delete schema.$id; | |
| 273 | + | |
| 274 | + if (item.children.length > 0) { | |
| 275 | + item.children.forEach(function (child) { | |
| 276 | + if (!flatten[child]) return; | |
| 277 | + var key = getKeyFromPath(child); | |
| 278 | + | |
| 279 | + if (isObjType(schema)) { | |
| 280 | + schema.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 281 | + } | |
| 282 | + | |
| 283 | + if (isListType(schema)) { | |
| 284 | + schema.items.properties[key] = getSchemaFromFlatten(flatten, child); | |
| 285 | + } | |
| 286 | + }); | |
| 287 | + } | |
| 288 | + } | |
| 289 | + | |
| 290 | + return schema; | |
| 291 | +} | |
| 292 | + | |
| 293 | +function stringContains(str, text) { | |
| 294 | + return str.indexOf(text) > -1; | |
| 295 | +} | |
| 296 | + | |
| 297 | +var isObject = function isObject(a) { | |
| 298 | + return stringContains(Object.prototype.toString.call(a), 'Object'); | |
| 21 | 299 | }; |
| 22 | 300 | |
| 23 | -exports.getArray = getArray; | |
| 301 | +exports.isObject = isObject; | |
| 302 | +var clone = _lodashEs.cloneDeep; // '3' => true, 3 => true, undefined => false | |
| 303 | + | |
| 304 | +exports.clone = clone; | |
| 305 | + | |
| 306 | +function isLooselyNumber(num) { | |
| 307 | + if (typeof num === 'number') return true; | |
| 308 | + | |
| 309 | + if (typeof num === 'string') { | |
| 310 | + return !Number.isNaN(Number(num)); | |
| 311 | + } | |
| 312 | + | |
| 313 | + return false; | |
| 314 | +} | |
| 315 | + | |
| 316 | +function isCssLength(str) { | |
| 317 | + if (typeof str !== 'string') return false; | |
| 318 | + return str.match(/^([0-9])*(%|px|rem|em)$/i); | |
| 319 | +} | |
| 320 | + | |
| 321 | +function isDeepEqual(param1, param2) { | |
| 322 | + if (param1 === undefined && param2 === undefined) return true;else if (param1 === undefined || param2 === undefined) return false; | |
| 323 | + if (param1 === null && param2 === null) return true;else if (param1 === null || param2 === null) return false;else if (param1.constructor !== param2.constructor) return false; | |
| 324 | + | |
| 325 | + if (param1.constructor === Array) { | |
| 326 | + if (param1.length !== param2.length) return false; | |
| 327 | + | |
| 328 | + for (var i = 0; i < param1.length; i++) { | |
| 329 | + if (param1[i].constructor === Array || param1[i].constructor === Object) { | |
| 330 | + if (!isDeepEqual(param1[i], param2[i])) return false; | |
| 331 | + } else if (param1[i] !== param2[i]) return false; | |
| 332 | + } | |
| 333 | + } else if (param1.constructor === Object) { | |
| 334 | + if (Object.keys(param1).length !== Object.keys(param2).length) return false; | |
| 335 | + | |
| 336 | + for (var _i2 = 0; _i2 < Object.keys(param1).length; _i2++) { | |
| 337 | + var key = Object.keys(param1)[_i2]; | |
| 338 | + | |
| 339 | + if (param1[key] && typeof param1[key] !== 'number' && (param1[key].constructor === Array || param1[key].constructor === Object)) { | |
| 340 | + if (!isDeepEqual(param1[key], param2[key])) return false; | |
| 341 | + } else if (param1[key] !== param2[key]) return false; | |
| 342 | + } | |
| 343 | + } else if (param1.constructor === String || param1.constructor === Number) { | |
| 344 | + return param1 === param2; | |
| 345 | + } | |
| 346 | + | |
| 347 | + return true; | |
| 348 | +} | |
| 24 | 349 | |
| 25 | 350 | function getFormat(format) { |
| 26 | 351 | var dateFormat; |
| ... | ... | @@ -65,4 +390,1146 @@ function getFormat(format) { |
| 65 | 390 | } |
| 66 | 391 | |
| 67 | 392 | return dateFormat; |
| 393 | +} | |
| 394 | + | |
| 395 | +function hasRepeat(list) { | |
| 396 | + return list.find(function (x, i, self) { | |
| 397 | + return i !== self.findIndex(function (y) { | |
| 398 | + return JSON.stringify(x) === JSON.stringify(y); | |
| 399 | + }); | |
| 400 | + }); | |
| 401 | +} | |
| 402 | + | |
| 403 | +function combineSchema() { | |
| 404 | + var propsSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 405 | + var uiSchema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 406 | + var propList = getChildren(propsSchema); | |
| 407 | + var newList = propList.map(function (p) { | |
| 408 | + var name = p.name; | |
| 409 | + var _p$schema = p.schema, | |
| 410 | + type = _p$schema.type, | |
| 411 | + options = _p$schema.enum, | |
| 412 | + properties = _p$schema.properties, | |
| 413 | + items = _p$schema.items; | |
| 414 | + var isObj = type === 'object' && properties; | |
| 415 | + var isArr = type === 'array' && items && !options; // enum + array 代表的多选框,没有sub | |
| 416 | + | |
| 417 | + var ui = name && uiSchema[p.name]; | |
| 418 | + | |
| 419 | + if (!ui) { | |
| 420 | + return p; | |
| 421 | + } // 如果是list,递归合并items | |
| 422 | + | |
| 423 | + | |
| 424 | + if (isArr) { | |
| 425 | + var newItems = combineSchema(items, ui.items || {}); | |
| 426 | + return _objectSpread(_objectSpread({}, p), {}, { | |
| 427 | + schema: _objectSpread(_objectSpread(_objectSpread({}, p.schema), ui), {}, { | |
| 428 | + items: newItems | |
| 429 | + }) | |
| 430 | + }); | |
| 431 | + } // object递归合并整个schema | |
| 432 | + | |
| 433 | + | |
| 434 | + if (isObj) { | |
| 435 | + var newSchema = combineSchema(p.schema, ui); | |
| 436 | + return _objectSpread(_objectSpread({}, p), {}, { | |
| 437 | + schema: newSchema | |
| 438 | + }); | |
| 439 | + } | |
| 440 | + | |
| 441 | + return _objectSpread(_objectSpread({}, p), {}, { | |
| 442 | + schema: _objectSpread(_objectSpread({}, p.schema), ui) | |
| 443 | + }); | |
| 444 | + }); | |
| 445 | + var newObj = {}; | |
| 446 | + newList.forEach(function (s) { | |
| 447 | + newObj[s.name] = s.schema; | |
| 448 | + }); | |
| 449 | + var topLevelUi = {}; | |
| 450 | + Object.keys(uiSchema).forEach(function (key) { | |
| 451 | + if (typeof key === 'string' && key.substring(0, 3) === 'ui:') { | |
| 452 | + topLevelUi[key] = uiSchema[key]; | |
| 453 | + } | |
| 454 | + }); | |
| 455 | + | |
| 456 | + if (isEmpty(newObj)) { | |
| 457 | + return _objectSpread(_objectSpread({}, propsSchema), topLevelUi); | |
| 458 | + } | |
| 459 | + | |
| 460 | + return _objectSpread(_objectSpread(_objectSpread({}, propsSchema), topLevelUi), {}, { | |
| 461 | + properties: newObj | |
| 462 | + }); | |
| 463 | +} | |
| 464 | + | |
| 465 | +function isEmpty(obj) { | |
| 466 | + return Object.keys(obj).length === 0; | |
| 467 | +} | |
| 468 | + | |
| 469 | +function getChildren(schema) { | |
| 470 | + if (!schema) return []; | |
| 471 | + var properties = schema.properties, | |
| 472 | + items = schema.items, | |
| 473 | + type = schema.type; | |
| 474 | + | |
| 475 | + if (!properties && !items) { | |
| 476 | + return []; | |
| 477 | + } | |
| 478 | + | |
| 479 | + var schemaSubs = {}; | |
| 480 | + | |
| 481 | + if (type === 'object') { | |
| 482 | + schemaSubs = properties; | |
| 483 | + } | |
| 484 | + | |
| 485 | + if (type === 'array') { | |
| 486 | + schemaSubs = items; | |
| 487 | + } | |
| 488 | + | |
| 489 | + return Object.keys(schemaSubs).map(function (name) { | |
| 490 | + return { | |
| 491 | + schema: schemaSubs[name], | |
| 492 | + name: name | |
| 493 | + }; | |
| 494 | + }); | |
| 495 | +} | |
| 496 | + | |
| 497 | +var parseString = function parseString(string) { | |
| 498 | + return Function('"use strict";return (' + string + ')')(); | |
| 499 | +}; | |
| 500 | + | |
| 501 | +exports.parseString = parseString; | |
| 502 | + | |
| 503 | +var evaluateString = function evaluateString(string, formData, rootValue) { | |
| 504 | + return Function("\"use strict\";\n const rootValue = ".concat(JSON.stringify(rootValue), ";\n const formData = ").concat(JSON.stringify(formData), ";\n return (").concat(string, ")"))(); | |
| 505 | +}; | |
| 506 | + | |
| 507 | +exports.evaluateString = evaluateString; | |
| 508 | + | |
| 509 | +function isExpression(func) { | |
| 510 | + // if (typeof func === 'function') { | |
| 511 | + // const funcString = func.toString(); | |
| 512 | + // return ( | |
| 513 | + // funcString.indexOf('formData') > -1 || | |
| 514 | + // funcString.indexOf('rootValue') > -1 | |
| 515 | + // ); | |
| 516 | + // } | |
| 517 | + if (typeof func !== 'string') return false; | |
| 518 | + var pattern = /^{{(.+)}}$/; | |
| 519 | + var reg1 = /^{{function\(.+}}$/; // const reg2 = /^{{(.+=>.+)}}$/; | |
| 520 | + | |
| 521 | + if (typeof func === 'string' && func.match(pattern) && !func.match(reg1)) { | |
| 522 | + return true; | |
| 523 | + } | |
| 524 | + | |
| 525 | + return false; | |
| 526 | +} | |
| 527 | + | |
| 528 | +var parseRootValueInSchema = function parseRootValueInSchema(schema, rootValue) { | |
| 529 | + var result = clone(schema); | |
| 530 | + | |
| 531 | + if (isObject(schema)) { | |
| 532 | + Object.keys(schema).forEach(function (key) { | |
| 533 | + var item = schema[key]; | |
| 534 | + | |
| 535 | + if (isObject(item)) { | |
| 536 | + result[key] = parseRootValueInSchema(item, rootValue); | |
| 537 | + } else if (typeof item === 'string') { | |
| 538 | + result[key] = parseSingleRootValue(item, rootValue); | |
| 539 | + } | |
| 540 | + }); | |
| 541 | + } else { | |
| 542 | + console.error('schema is not an object:', schema); | |
| 543 | + } | |
| 544 | + | |
| 545 | + return result; | |
| 546 | +}; // handle rootValue inside List | |
| 547 | + | |
| 548 | + | |
| 549 | +exports.parseRootValueInSchema = parseRootValueInSchema; | |
| 550 | + | |
| 551 | +var parseSingleRootValue = function parseSingleRootValue(expression) { | |
| 552 | + var rootValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 553 | + | |
| 554 | + if (typeof expression === 'string' && expression.indexOf('rootValue') > 0) { | |
| 555 | + var funcBody = expression.substring(2, expression.length - 2); | |
| 556 | + var str = "\n return ".concat(funcBody.replace(/rootValue/g, JSON.stringify(rootValue))); | |
| 557 | + | |
| 558 | + try { | |
| 559 | + return Function(str)(); | |
| 560 | + } catch (error) { | |
| 561 | + console.error(error, 'expression:', expression, 'rootValue:', rootValue); | |
| 562 | + return null; // 如果计算有错误,return null 最合适 | |
| 563 | + } | |
| 564 | + } else { | |
| 565 | + return expression; | |
| 566 | + } | |
| 567 | +}; | |
| 568 | + | |
| 569 | +exports.parseSingleRootValue = parseSingleRootValue; | |
| 570 | + | |
| 571 | +function parseSingleExpression(func) { | |
| 572 | + var formData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 573 | + var dataPath = arguments.length > 2 ? arguments[2] : undefined; | |
| 574 | + var parentPath = getParentPath(dataPath); | |
| 575 | + var parent = getValueByPath(formData, parentPath) || {}; | |
| 576 | + | |
| 577 | + if (typeof func === 'string') { | |
| 578 | + var funcBody = func.substring(2, func.length - 2); | |
| 579 | + var str = "\n return ".concat(funcBody.replace(/formData/g, JSON.stringify(formData)).replace(/rootValue/g, JSON.stringify(parent))); | |
| 580 | + | |
| 581 | + try { | |
| 582 | + return Function(str)(); | |
| 583 | + } catch (error) { | |
| 584 | + console.log(error, func, dataPath); | |
| 585 | + return null; // 如果计算有错误,return null 最合适 | |
| 586 | + } // const funcBody = func.substring(2, func.length - 2); | |
| 587 | + // //TODO: 这样有问题,例如 a.b.indexOf(), 会把 a.b.indexOf 当做值 | |
| 588 | + // const match1 = /formData.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 589 | + // const match2 = /rootValue.([a-zA-Z0-9.$_\[\]]+)/g; | |
| 590 | + // const str = ` | |
| 591 | + // return (${funcBody | |
| 592 | + // .replaceAll(match1, (v, m1) => | |
| 593 | + // JSON.stringify(getValueByPath(formData, m1)) | |
| 594 | + // ) | |
| 595 | + // .replaceAll(match2, (v, m1) => | |
| 596 | + // JSON.stringify(getValueByPath(parent, m1)) | |
| 597 | + // )})`; | |
| 598 | + // try { | |
| 599 | + // return Function(str)(); | |
| 600 | + // } catch (error) { | |
| 601 | + // console.log(error); | |
| 602 | + // return func; | |
| 603 | + // } | |
| 604 | + | |
| 605 | + } else return func; | |
| 606 | +} | |
| 607 | + | |
| 608 | +var schemaContainsExpression = function schemaContainsExpression(schema) { | |
| 609 | + if (isObject(schema)) { | |
| 610 | + return Object.keys(schema).some(function (key) { | |
| 611 | + var value = schema[key]; | |
| 612 | + | |
| 613 | + if (typeof value === 'string') { | |
| 614 | + return isExpression(value); | |
| 615 | + } else if (isObject(value)) { | |
| 616 | + return schemaContainsExpression(value); | |
| 617 | + } else { | |
| 618 | + return false; | |
| 619 | + } | |
| 620 | + }); | |
| 621 | + } | |
| 622 | + | |
| 623 | + return false; | |
| 624 | +}; | |
| 625 | + | |
| 626 | +exports.schemaContainsExpression = schemaContainsExpression; | |
| 627 | + | |
| 628 | +var parseAllExpression = function parseAllExpression(_schema, formData, dataPath) { | |
| 629 | + var schema = clone(_schema); | |
| 630 | + Object.keys(schema).forEach(function (key) { | |
| 631 | + var value = schema[key]; | |
| 632 | + | |
| 633 | + if (isObject(value)) { | |
| 634 | + schema[key] = parseAllExpression(value, formData, dataPath); | |
| 635 | + } else if (isExpression(value)) { | |
| 636 | + schema[key] = parseSingleExpression(value, formData, dataPath); | |
| 637 | + } else if (typeof key === 'string' && key.toLowerCase().indexOf('props') > -1) { | |
| 638 | + // 有可能叫 xxxProps | |
| 639 | + var propsObj = schema[key]; | |
| 640 | + | |
| 641 | + if (isObject(propsObj)) { | |
| 642 | + Object.keys(propsObj).forEach(function (k) { | |
| 643 | + schema[key][k] = parseSingleExpression(propsObj[k], formData, dataPath); | |
| 644 | + }); | |
| 645 | + } | |
| 646 | + } | |
| 647 | + }); | |
| 648 | + return schema; | |
| 649 | +}; | |
| 650 | + | |
| 651 | +exports.parseAllExpression = parseAllExpression; | |
| 652 | + | |
| 653 | +function isFunctionSchema(schema) { | |
| 654 | + return Object.keys(schema).some(function (key) { | |
| 655 | + if (typeof schema[key] === 'function') { | |
| 656 | + return true; | |
| 657 | + } else if (typeof schema[key] === 'string') { | |
| 658 | + return isExpression(schema[key]); | |
| 659 | + } else if (_typeof(schema[key]) === 'object') { | |
| 660 | + return isFunctionSchema(schema[key]); | |
| 661 | + } else { | |
| 662 | + return false; | |
| 663 | + } | |
| 664 | + }); | |
| 665 | +} | |
| 666 | + | |
| 667 | +var getParentProps = function getParentProps(propName, id, flatten) { | |
| 668 | + try { | |
| 669 | + var item = flatten[id]; | |
| 670 | + if (item.schema[propName] !== undefined) return item.schema[propName]; | |
| 671 | + | |
| 672 | + if (item && item.parent) { | |
| 673 | + var parentSchema = flatten[item.parent].schema; | |
| 674 | + | |
| 675 | + if (parentSchema[propName] !== undefined) { | |
| 676 | + return parentSchema[propName]; | |
| 677 | + } else { | |
| 678 | + return getParentProps(propName, item.parent, flatten); | |
| 679 | + } | |
| 680 | + } | |
| 681 | + } catch (error) { | |
| 682 | + return undefined; | |
| 683 | + } | |
| 684 | +}; | |
| 685 | + | |
| 686 | +exports.getParentProps = getParentProps; | |
| 687 | + | |
| 688 | +var getSaveNumber = function getSaveNumber() { | |
| 689 | + var searchStr = localStorage.getItem('SAVES'); | |
| 690 | + | |
| 691 | + if (searchStr) { | |
| 692 | + try { | |
| 693 | + var saves = JSON.parse(searchStr); | |
| 694 | + var length = saves.length; | |
| 695 | + if (length) return length + 1; | |
| 696 | + } catch (error) { | |
| 697 | + return 1; | |
| 698 | + } | |
| 699 | + } else { | |
| 700 | + return 1; | |
| 701 | + } | |
| 702 | +}; | |
| 703 | + | |
| 704 | +exports.getSaveNumber = getSaveNumber; | |
| 705 | + | |
| 706 | +function looseJsonParse(obj) { | |
| 707 | + return Function('"use strict";return (' + obj + ')')(); | |
| 708 | +} | |
| 709 | + | |
| 710 | +var isFunctionString = function isFunctionString(fString) { | |
| 711 | + return typeof fString === 'string' && fString.indexOf('function(') === 0; | |
| 712 | +}; | |
| 713 | + | |
| 714 | +exports.isFunctionString = isFunctionString; | |
| 715 | + | |
| 716 | +function parseFunction(fString) { | |
| 717 | + if (isFunctionString(fString)) { | |
| 718 | + return Function('return ' + fString)(); | |
| 719 | + } | |
| 720 | + | |
| 721 | + return fString; | |
| 722 | +} // 获得propsSchema的children | |
| 723 | +// function getChildren2(schema) { | |
| 724 | +// if (!schema) return []; | |
| 725 | +// const { | |
| 726 | +// // object | |
| 727 | +// properties, | |
| 728 | +// // array | |
| 729 | +// items, | |
| 730 | +// type, | |
| 731 | +// } = schema; | |
| 732 | +// if (!properties && !items) { | |
| 733 | +// return []; | |
| 734 | +// } | |
| 735 | +// let schemaSubs = {}; | |
| 736 | +// if (type === 'object') { | |
| 737 | +// schemaSubs = properties; | |
| 738 | +// } | |
| 739 | +// if (type === 'array') { | |
| 740 | +// schemaSubs = items.properties; | |
| 741 | +// } | |
| 742 | +// return Object.keys(schemaSubs).map(name => ({ | |
| 743 | +// schema: schemaSubs[name], | |
| 744 | +// name, | |
| 745 | +// })); | |
| 746 | +// } | |
| 747 | + | |
| 748 | + | |
| 749 | +var oldSchemaToNew = function oldSchemaToNew(schema) { | |
| 750 | + if (schema && schema.propsSchema) { | |
| 751 | + var propsSchema = schema.propsSchema, | |
| 752 | + rest = _objectWithoutProperties(schema, _excluded); | |
| 753 | + | |
| 754 | + return _objectSpread({ | |
| 755 | + schema: propsSchema | |
| 756 | + }, rest); | |
| 757 | + } | |
| 758 | + | |
| 759 | + return schema; | |
| 760 | +}; | |
| 761 | + | |
| 762 | +exports.oldSchemaToNew = oldSchemaToNew; | |
| 763 | + | |
| 764 | +var newSchemaToOld = function newSchemaToOld(setting) { | |
| 765 | + if (setting && setting.schema) { | |
| 766 | + var schema = setting.schema, | |
| 767 | + rest = _objectWithoutProperties(setting, _excluded2); | |
| 768 | + | |
| 769 | + return _objectSpread({ | |
| 770 | + propsSchema: schema | |
| 771 | + }, rest); | |
| 772 | + } | |
| 773 | + | |
| 774 | + return setting; | |
| 775 | +}; // from FR | |
| 776 | + | |
| 777 | + | |
| 778 | +exports.newSchemaToOld = newSchemaToOld; | |
| 779 | + | |
| 780 | +var getEnum = function getEnum(schema) { | |
| 781 | + if (!schema) return undefined; | |
| 782 | + var itemEnum = schema && schema.items && schema.items.enum; | |
| 783 | + var schemaEnum = schema && schema.enum; | |
| 784 | + return itemEnum ? itemEnum : schemaEnum; | |
| 785 | +}; | |
| 786 | + | |
| 787 | +exports.getEnum = getEnum; | |
| 788 | + | |
| 789 | +var getArray = function getArray(arr) { | |
| 790 | + var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | |
| 791 | + if (Array.isArray(arr)) return arr; | |
| 792 | + return defaultValue; | |
| 793 | +}; | |
| 794 | + | |
| 795 | +exports.getArray = getArray; | |
| 796 | + | |
| 797 | +var isEmail = function isEmail(value) { | |
| 798 | + var regex = '^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$'; | |
| 799 | + | |
| 800 | + if (value && new RegExp(regex).test(value)) { | |
| 801 | + return true; | |
| 802 | + } | |
| 803 | + | |
| 804 | + return false; | |
| 805 | +}; | |
| 806 | + | |
| 807 | +exports.isEmail = isEmail; | |
| 808 | + | |
| 809 | +function defaultGetValueFromEvent(valuePropName) { | |
| 810 | + var event = arguments.length <= 1 ? undefined : arguments[1]; | |
| 811 | + | |
| 812 | + if (event && event.target && valuePropName in event.target) { | |
| 813 | + return event.target[valuePropName]; | |
| 814 | + } | |
| 815 | + | |
| 816 | + return event; | |
| 817 | +} | |
| 818 | + | |
| 819 | +var getKeyFromPath = function getKeyFromPath() { | |
| 820 | + var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '#'; | |
| 821 | + | |
| 822 | + try { | |
| 823 | + var arr = path.split('.'); | |
| 824 | + var last = arr.slice(-1)[0]; | |
| 825 | + var result = last.replace('[]', ''); | |
| 826 | + return result; | |
| 827 | + } catch (error) { | |
| 828 | + console.error(error, 'getKeyFromPath'); | |
| 829 | + return ''; | |
| 830 | + } | |
| 831 | +}; // 更多的值获取 | |
| 832 | + | |
| 833 | + | |
| 834 | +exports.getKeyFromPath = getKeyFromPath; | |
| 835 | + | |
| 836 | +var getDisplayValue = function getDisplayValue(value, schema) { | |
| 837 | + if (typeof value === 'boolean') { | |
| 838 | + return value ? 'yes' : 'no'; | |
| 839 | + } | |
| 840 | + | |
| 841 | + if (isObjType(schema) || isListType(schema)) { | |
| 842 | + return '-'; | |
| 843 | + } | |
| 844 | + | |
| 845 | + if (Array.isArray(schema.enum) && Array.isArray(schema.enumNames)) { | |
| 846 | + try { | |
| 847 | + return schema.enumNames[schema.enum.indexOf(value)]; | |
| 848 | + } catch (error) { | |
| 849 | + return value; | |
| 850 | + } | |
| 851 | + } | |
| 852 | + | |
| 853 | + return value; | |
| 854 | +}; // 去掉数组里的空元素 {a: [null, {x:1}]} => {a: [{x:1}]} | |
| 855 | + | |
| 856 | + | |
| 857 | +exports.getDisplayValue = getDisplayValue; | |
| 858 | + | |
| 859 | +var removeEmptyItemFromList = function removeEmptyItemFromList(formData) { | |
| 860 | + var result = {}; | |
| 861 | + | |
| 862 | + if (isObject(formData)) { | |
| 863 | + Object.keys(formData).forEach(function (key) { | |
| 864 | + result[key] = removeEmptyItemFromList(formData[key]); | |
| 865 | + }); | |
| 866 | + } else if (Array.isArray(formData)) { | |
| 867 | + result = formData.filter(function (item) { | |
| 868 | + if ([false, 0, ''].indexOf(item) > -1) return true; | |
| 869 | + | |
| 870 | + if (item && JSON.stringify(item) !== '{}') { | |
| 871 | + return true; | |
| 872 | + } | |
| 873 | + | |
| 874 | + return false; | |
| 875 | + }); | |
| 876 | + } else { | |
| 877 | + result = formData; | |
| 878 | + } | |
| 879 | + | |
| 880 | + return result; | |
| 881 | +}; | |
| 882 | + | |
| 883 | +exports.removeEmptyItemFromList = removeEmptyItemFromList; | |
| 884 | + | |
| 885 | +var getDescriptorFromSchema = function getDescriptorFromSchema(_ref5) { | |
| 886 | + var schema = _ref5.schema, | |
| 887 | + _ref5$isRequired = _ref5.isRequired, | |
| 888 | + isRequired = _ref5$isRequired === void 0 ? true : _ref5$isRequired; | |
| 889 | + var result = {}; | |
| 890 | + var singleResult = {}; | |
| 891 | + if (schema.hidden === true) return { | |
| 892 | + validator: function validator() { | |
| 893 | + return true; | |
| 894 | + } | |
| 895 | + }; | |
| 896 | + | |
| 897 | + if (isObjType(schema)) { | |
| 898 | + result.type = 'object'; | |
| 899 | + | |
| 900 | + if (isRequired && schema.required === true) { | |
| 901 | + result.required = true; | |
| 902 | + } | |
| 903 | + | |
| 904 | + result.fields = {}; | |
| 905 | + Object.keys(schema.properties).forEach(function (key) { | |
| 906 | + var item = schema.properties[key]; // 兼容旧的! | |
| 907 | + | |
| 908 | + if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 909 | + item.required = true; | |
| 910 | + } | |
| 911 | + | |
| 912 | + result.fields[key] = getDescriptorFromSchema({ | |
| 913 | + schema: item, | |
| 914 | + isRequired: isRequired | |
| 915 | + }); | |
| 916 | + }); | |
| 917 | + } else if (isListType(schema)) { | |
| 918 | + result.type = 'array'; | |
| 919 | + | |
| 920 | + if (isRequired && schema.required === true) { | |
| 921 | + result.required = true; | |
| 922 | + } | |
| 923 | + | |
| 924 | + if (typeof schema.min === 'number') { | |
| 925 | + result.min = schema.min; | |
| 926 | + } | |
| 927 | + | |
| 928 | + if (typeof schema.max === 'number') { | |
| 929 | + result.max = schema.max; | |
| 930 | + } | |
| 931 | + | |
| 932 | + result.defaultField = { | |
| 933 | + type: 'object', | |
| 934 | + fields: {} | |
| 935 | + }; // 目前就默认只有object类型的 TODO: | |
| 936 | + | |
| 937 | + Object.keys(schema.items.properties).forEach(function (key) { | |
| 938 | + var item = schema.items.properties[key]; // 兼容旧的! | |
| 939 | + | |
| 940 | + if (Array.isArray(schema.required) && schema.required.indexOf(key) > -1) { | |
| 941 | + item.required = true; | |
| 942 | + } | |
| 943 | + | |
| 944 | + result.defaultField.fields[key] = getDescriptorFromSchema({ | |
| 945 | + schema: item, | |
| 946 | + isRequired: isRequired | |
| 947 | + }); | |
| 948 | + }); | |
| 949 | + } else { | |
| 950 | + // 单个的逻辑 | |
| 951 | + var processRule = function processRule(item) { | |
| 952 | + if (schema.type) return _objectSpread(_objectSpread({}, item), {}, { | |
| 953 | + type: schema.type | |
| 954 | + }); | |
| 955 | + | |
| 956 | + if (item.pattern && typeof item.pattern === 'string') { | |
| 957 | + return _objectSpread(_objectSpread({}, item), {}, { | |
| 958 | + pattern: new RegExp(item.pattern) | |
| 959 | + }); | |
| 960 | + } | |
| 961 | + | |
| 962 | + return item; | |
| 963 | + }; | |
| 964 | + | |
| 965 | + var required = schema.required, | |
| 966 | + rest = _objectWithoutProperties(schema, _excluded3); | |
| 967 | + | |
| 968 | + ['type', 'pattern', 'min', 'max', 'len'].forEach(function (key) { | |
| 969 | + if (Object.keys(rest).indexOf(key) > -1) { | |
| 970 | + singleResult[key] = rest[key]; | |
| 971 | + } | |
| 972 | + }); | |
| 973 | + | |
| 974 | + switch (schema.type) { | |
| 975 | + case 'range': | |
| 976 | + // 暂时先去掉,应该没事了,在range组件的源头上把奇怪的case干掉了 | |
| 977 | + // const rangeValidator = { | |
| 978 | + // validator: (rule, value) => { | |
| 979 | + // const rangeRequired = schema.required === true && isRequired; | |
| 980 | + // // range组件点击clear,会变成 ['',''] | |
| 981 | + // // range组件对应的值bind的时候,会变成 [undefined,undefined] | |
| 982 | + // const notValid1 = value[0] === '' && value[1] === ''; | |
| 983 | + // const notValid2 = value[0] === undefined && value[1] === undefined; | |
| 984 | + // if (rangeRequired) { | |
| 985 | + // if (!value) return false; | |
| 986 | + // if (Array.isArray(value)) { | |
| 987 | + // if (notValid1 || notValid2) { | |
| 988 | + // return false; | |
| 989 | + // } | |
| 990 | + // return true; | |
| 991 | + // } | |
| 992 | + // return true; | |
| 993 | + // } else { | |
| 994 | + // return true; | |
| 995 | + // } | |
| 996 | + // }, | |
| 997 | + // type: 'array', | |
| 998 | + // message: '${title}必填', | |
| 999 | + // }; | |
| 1000 | + // singleResult = rangeValidator; | |
| 1001 | + singleResult.type = 'array'; | |
| 1002 | + break; | |
| 1003 | + | |
| 1004 | + case 'html': | |
| 1005 | + singleResult.type = 'string'; | |
| 1006 | + break; | |
| 1007 | + | |
| 1008 | + default: | |
| 1009 | + break; | |
| 1010 | + } | |
| 1011 | + | |
| 1012 | + switch (schema.format) { | |
| 1013 | + case 'email': | |
| 1014 | + case 'url': | |
| 1015 | + singleResult.type = schema.format; | |
| 1016 | + break; | |
| 1017 | + | |
| 1018 | + default: | |
| 1019 | + break; | |
| 1020 | + } | |
| 1021 | + | |
| 1022 | + var requiredRule; | |
| 1023 | + | |
| 1024 | + if (isRequired && schema.required === true) { | |
| 1025 | + requiredRule = { | |
| 1026 | + required: true, | |
| 1027 | + type: singleResult.type || 'string' | |
| 1028 | + }; | |
| 1029 | + } | |
| 1030 | + | |
| 1031 | + if (schema.rules) { | |
| 1032 | + if (Array.isArray(schema.rules)) { | |
| 1033 | + var _rules = []; | |
| 1034 | + schema.rules.forEach(function (item) { | |
| 1035 | + if (item.required === true) { | |
| 1036 | + if (isRequired) { | |
| 1037 | + requiredRule = item; | |
| 1038 | + } | |
| 1039 | + } else { | |
| 1040 | + _rules.push(processRule(item)); | |
| 1041 | + } | |
| 1042 | + }); | |
| 1043 | + result = [singleResult].concat(_rules); | |
| 1044 | + } else if (isObject(schema.rules)) { | |
| 1045 | + // TODO: 规范上不允许rules是object,省一点事儿 | |
| 1046 | + result = [singleResult, processRule(schema.rules)]; | |
| 1047 | + } else { | |
| 1048 | + result = singleResult; | |
| 1049 | + } | |
| 1050 | + } else { | |
| 1051 | + result = singleResult; | |
| 1052 | + } | |
| 1053 | + | |
| 1054 | + if (requiredRule) { | |
| 1055 | + if (Array.isArray(result)) { | |
| 1056 | + result.push(requiredRule); | |
| 1057 | + } else if (isObject(result)) { | |
| 1058 | + result = [result, requiredRule]; | |
| 1059 | + } | |
| 1060 | + } | |
| 1061 | + | |
| 1062 | + if (schema.format === 'image') { | |
| 1063 | + var imgValidator = { | |
| 1064 | + validator: function validator(rule, value) { | |
| 1065 | + var pattern = /([/|.|w|s|-])*.(jpg|gif|png|bmp|apng|webp|jpeg|json)/; | |
| 1066 | + if (!value) return true; // 这里判断宽一点,undefined、null、'' 都当做没有填写 | |
| 1067 | + | |
| 1068 | + return !!pattern.exec(value) || isUrl(value); | |
| 1069 | + }, | |
| 1070 | + message: '${title}的类型不是image' | |
| 1071 | + }; | |
| 1072 | + | |
| 1073 | + if (Array.isArray(result)) { | |
| 1074 | + result.push(imgValidator); | |
| 1075 | + } else if (isObject(result)) { | |
| 1076 | + result = [result, imgValidator]; | |
| 1077 | + } | |
| 1078 | + } | |
| 1079 | + } | |
| 1080 | + | |
| 1081 | + return result; | |
| 1082 | +}; // async-validator 产出的path没法用,转一下 | |
| 1083 | +// "list.1.userName" => "list[1].userName" | |
| 1084 | + | |
| 1085 | + | |
| 1086 | +exports.getDescriptorFromSchema = getDescriptorFromSchema; | |
| 1087 | + | |
| 1088 | +var formatPathFromValidator = function formatPathFromValidator(err) { | |
| 1089 | + var errArr = err.split('.'); | |
| 1090 | + return errArr.map(function (item) { | |
| 1091 | + if (isNaN(Number(item))) { | |
| 1092 | + return item; | |
| 1093 | + } else { | |
| 1094 | + return "[".concat(item, "]"); | |
| 1095 | + } | |
| 1096 | + }).reduce(function (a, b) { | |
| 1097 | + if (b[0] === '[' || a === '') { | |
| 1098 | + return a + b; | |
| 1099 | + } else { | |
| 1100 | + return a + '.' + b; | |
| 1101 | + } | |
| 1102 | + }, ''); | |
| 1103 | +}; // schema = { | |
| 1104 | +// type: 'object', | |
| 1105 | +// properties: { | |
| 1106 | +// x: { | |
| 1107 | +// type: 'object', | |
| 1108 | +// properties: { | |
| 1109 | +// y: { | |
| 1110 | +// type: 'string', | |
| 1111 | +// required: true, | |
| 1112 | +// }, | |
| 1113 | +// }, | |
| 1114 | +// }, | |
| 1115 | +// }, | |
| 1116 | +// }; | |
| 1117 | +// path = 'x.y' | |
| 1118 | +// return {required: true, message?: 'xxxx'} | |
| 1119 | + | |
| 1120 | + | |
| 1121 | +exports.formatPathFromValidator = formatPathFromValidator; | |
| 1122 | + | |
| 1123 | +var isPathRequired = function isPathRequired(path, schema) { | |
| 1124 | + var pathArr = path.split('.'); | |
| 1125 | + | |
| 1126 | + while (pathArr.length > 0) { | |
| 1127 | + var _pathArr = pathArr, | |
| 1128 | + _pathArr2 = _toArray(_pathArr), | |
| 1129 | + _path = _pathArr2[0], | |
| 1130 | + rest = _pathArr2.slice(1); | |
| 1131 | + | |
| 1132 | + _path = _path.split('[')[0]; | |
| 1133 | + var childSchema = void 0; | |
| 1134 | + | |
| 1135 | + if (isObjType(schema)) { | |
| 1136 | + childSchema = schema.properties[_path]; | |
| 1137 | + } else if (isListType(schema)) { | |
| 1138 | + childSchema = schema.items.properties[_path]; | |
| 1139 | + } | |
| 1140 | + | |
| 1141 | + pathArr = rest; | |
| 1142 | + | |
| 1143 | + if (childSchema) { | |
| 1144 | + return isPathRequired(rest.join('.'), childSchema); | |
| 1145 | + } // 单个的逻辑 | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + var result = { | |
| 1149 | + required: false | |
| 1150 | + }; | |
| 1151 | + | |
| 1152 | + if (schema.required === true) { | |
| 1153 | + result.required = true; | |
| 1154 | + } | |
| 1155 | + | |
| 1156 | + if (schema.rules) { | |
| 1157 | + var requiredItem = schema.rules.find(function (item) { | |
| 1158 | + return item.required; | |
| 1159 | + }); | |
| 1160 | + | |
| 1161 | + if (requiredItem) { | |
| 1162 | + result = requiredItem; | |
| 1163 | + } | |
| 1164 | + } | |
| 1165 | + | |
| 1166 | + return result; | |
| 1167 | + } | |
| 1168 | +}; // _path 只供内部递归使用 | |
| 1169 | + | |
| 1170 | + | |
| 1171 | +exports.isPathRequired = isPathRequired; | |
| 1172 | + | |
| 1173 | +var generateDataSkeleton = function generateDataSkeleton(schema, formData) { | |
| 1174 | + var _formData = clone(formData); | |
| 1175 | + | |
| 1176 | + var result = _formData; | |
| 1177 | + | |
| 1178 | + if (isObjType(schema)) { | |
| 1179 | + if (_formData === undefined || _typeof(_formData) !== 'object') { | |
| 1180 | + _formData = {}; | |
| 1181 | + result = {}; | |
| 1182 | + } | |
| 1183 | + | |
| 1184 | + Object.keys(schema.properties).forEach(function (key) { | |
| 1185 | + var childSchema = schema.properties[key]; | |
| 1186 | + var childData = _formData[key]; | |
| 1187 | + var childResult = generateDataSkeleton(childSchema, childData); | |
| 1188 | + result[key] = childResult; | |
| 1189 | + }); | |
| 1190 | + } else if (_formData !== undefined) {// result = _formData; | |
| 1191 | + } else { | |
| 1192 | + if (schema.default !== undefined) { | |
| 1193 | + result = clone(schema.default); | |
| 1194 | + } else if (schema.type === 'boolean' && !schema.widget) { | |
| 1195 | + // result = false; | |
| 1196 | + result = undefined; | |
| 1197 | + } else { | |
| 1198 | + result = undefined; | |
| 1199 | + } | |
| 1200 | + } | |
| 1201 | + | |
| 1202 | + return result; | |
| 1203 | +}; | |
| 1204 | + | |
| 1205 | +exports.generateDataSkeleton = generateDataSkeleton; | |
| 1206 | + | |
| 1207 | +var translateMessage = function translateMessage(msg, schema) { | |
| 1208 | + if (typeof msg !== 'string') { | |
| 1209 | + return ''; | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + if (!schema) return msg; | |
| 1213 | + msg = msg.replace('${title}', schema.title); | |
| 1214 | + msg = msg.replace('${type}', schema.format || schema.type); // 兼容代码 | |
| 1215 | + | |
| 1216 | + if (typeof schema.min === 'number') { | |
| 1217 | + msg = msg.replace('${min}', schema.min); | |
| 1218 | + } | |
| 1219 | + | |
| 1220 | + if (typeof schema.max === 'number') { | |
| 1221 | + msg = msg.replace('${max}', schema.max); | |
| 1222 | + } | |
| 1223 | + | |
| 1224 | + if (schema.rules) { | |
| 1225 | + var minRule = schema.rules.find(function (r) { | |
| 1226 | + return r.min !== undefined; | |
| 1227 | + }); | |
| 1228 | + | |
| 1229 | + if (minRule) { | |
| 1230 | + msg = msg.replace('${min}', minRule.min); | |
| 1231 | + } | |
| 1232 | + | |
| 1233 | + var maxRule = schema.rules.find(function (r) { | |
| 1234 | + return r.max !== undefined; | |
| 1235 | + }); | |
| 1236 | + | |
| 1237 | + if (maxRule) { | |
| 1238 | + msg = msg.replace('${max}', maxRule.max); | |
| 1239 | + } | |
| 1240 | + | |
| 1241 | + var lenRule = schema.rules.find(function (r) { | |
| 1242 | + return r.len !== undefined; | |
| 1243 | + }); | |
| 1244 | + | |
| 1245 | + if (lenRule) { | |
| 1246 | + msg = msg.replace('${len}', lenRule.len); | |
| 1247 | + } | |
| 1248 | + | |
| 1249 | + var patternRule = schema.rules.find(function (r) { | |
| 1250 | + return r.pattern !== undefined; | |
| 1251 | + }); | |
| 1252 | + | |
| 1253 | + if (patternRule) { | |
| 1254 | + msg = msg.replace('${pattern}', patternRule.pattern); | |
| 1255 | + } | |
| 1256 | + } | |
| 1257 | + | |
| 1258 | + return msg; | |
| 1259 | +}; // "objectName": { | |
| 1260 | +// "title": "对象", | |
| 1261 | +// "description": "这是一个对象类型", | |
| 1262 | +// "type": "object", | |
| 1263 | +// "properties": { | |
| 1264 | +// } | |
| 1265 | +// } | |
| 1266 | +// "listName": { | |
| 1267 | +// "title": "对象数组", | |
| 1268 | +// "description": "对象数组嵌套功能", | |
| 1269 | +// "type": "array", | |
| 1270 | +// "items": { | |
| 1271 | +// "type": "object", | |
| 1272 | +// "properties": { | |
| 1273 | +// } | |
| 1274 | +// } | |
| 1275 | +// } | |
| 1276 | + | |
| 1277 | + | |
| 1278 | +exports.translateMessage = translateMessage; | |
| 1279 | + | |
| 1280 | +var changeSchema = function changeSchema(_schema, singleChange) { | |
| 1281 | + var schema = clone(_schema); | |
| 1282 | + schema = singleChange(schema); | |
| 1283 | + | |
| 1284 | + if (isObjType(schema)) { | |
| 1285 | + var requiredKeys = []; | |
| 1286 | + | |
| 1287 | + if (Array.isArray(schema.required)) { | |
| 1288 | + requiredKeys = schema.required; | |
| 1289 | + delete schema.required; | |
| 1290 | + } | |
| 1291 | + | |
| 1292 | + Object.keys(schema.properties).forEach(function (key) { | |
| 1293 | + var item = schema.properties[key]; | |
| 1294 | + | |
| 1295 | + if (requiredKeys.indexOf(key) > -1) { | |
| 1296 | + item.required = true; | |
| 1297 | + } | |
| 1298 | + | |
| 1299 | + schema.properties[key] = changeSchema(item, singleChange); | |
| 1300 | + }); | |
| 1301 | + } else if (isListType(schema)) { | |
| 1302 | + Object.keys(schema.items.properties).forEach(function (key) { | |
| 1303 | + var item = schema.items.properties[key]; | |
| 1304 | + schema.items.properties[key] = changeSchema(item, singleChange); | |
| 1305 | + }); | |
| 1306 | + } | |
| 1307 | + | |
| 1308 | + return schema; | |
| 1309 | +}; | |
| 1310 | + | |
| 1311 | +var updateSchemaToNewVersion = function updateSchemaToNewVersion(schema) { | |
| 1312 | + return changeSchema(schema, updateSingleSchema); | |
| 1313 | +}; | |
| 1314 | + | |
| 1315 | +exports.updateSchemaToNewVersion = updateSchemaToNewVersion; | |
| 1316 | + | |
| 1317 | +var updateSingleSchema = function updateSingleSchema(schema) { | |
| 1318 | + try { | |
| 1319 | + schema.rules = schema.rules || []; | |
| 1320 | + schema.props = schema.props || {}; | |
| 1321 | + | |
| 1322 | + if (schema['ui:options']) { | |
| 1323 | + schema.props = schema['ui:options']; | |
| 1324 | + delete schema['ui:options']; | |
| 1325 | + } | |
| 1326 | + | |
| 1327 | + if (schema.pattern) { | |
| 1328 | + var validItem = { | |
| 1329 | + pattern: schema.pattern | |
| 1330 | + }; | |
| 1331 | + | |
| 1332 | + if (schema.message && schema.message.pattern) { | |
| 1333 | + validItem.message = schema.message.pattern; | |
| 1334 | + } | |
| 1335 | + | |
| 1336 | + schema.rules.push(validItem); | |
| 1337 | + delete schema.pattern; | |
| 1338 | + delete schema.message; | |
| 1339 | + } // min / max | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + if (schema.minLength) { | |
| 1343 | + schema.min = schema.minLength; | |
| 1344 | + delete schema.minLength; | |
| 1345 | + } | |
| 1346 | + | |
| 1347 | + if (schema.maxLength) { | |
| 1348 | + schema.max = schema.maxLength; | |
| 1349 | + delete schema.maxLength; | |
| 1350 | + } | |
| 1351 | + | |
| 1352 | + if (schema.minItems) { | |
| 1353 | + schema.min = schema.minItems; | |
| 1354 | + delete schema.minItems; | |
| 1355 | + } | |
| 1356 | + | |
| 1357 | + if (schema.maxItems) { | |
| 1358 | + schema.max = schema.maxItems; | |
| 1359 | + delete schema.maxItems; | |
| 1360 | + } | |
| 1361 | + | |
| 1362 | + if (schema.step) { | |
| 1363 | + schema.props.step = schema.step; | |
| 1364 | + delete schema.step; | |
| 1365 | + } // ui:xxx | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + if (schema['ui:className']) { | |
| 1369 | + schema.className = schema['ui:className']; | |
| 1370 | + delete schema['ui:className']; | |
| 1371 | + } | |
| 1372 | + | |
| 1373 | + if (schema['ui:hidden']) { | |
| 1374 | + schema.hidden = schema['ui:hidden']; | |
| 1375 | + delete schema['ui:hidden']; | |
| 1376 | + } | |
| 1377 | + | |
| 1378 | + if (schema['ui:readonly']) { | |
| 1379 | + schema.readOnly = schema['ui:readonly']; // 改成驼峰了 | |
| 1380 | + | |
| 1381 | + delete schema['ui:readonly']; | |
| 1382 | + } | |
| 1383 | + | |
| 1384 | + if (schema['ui:disabled']) { | |
| 1385 | + schema.disabled = schema['ui:disabled']; | |
| 1386 | + delete schema['ui:disabled']; | |
| 1387 | + } | |
| 1388 | + | |
| 1389 | + if (schema['ui:width']) { | |
| 1390 | + schema.width = schema['ui:width']; | |
| 1391 | + delete schema['ui:width']; | |
| 1392 | + } | |
| 1393 | + | |
| 1394 | + if (schema['ui:displayType']) { | |
| 1395 | + schema.displayType = schema['ui:displayType']; | |
| 1396 | + delete schema['ui:displayType']; | |
| 1397 | + } | |
| 1398 | + | |
| 1399 | + if (schema['ui:column']) { | |
| 1400 | + schema.column = schema['ui:column']; | |
| 1401 | + delete schema['ui:column']; | |
| 1402 | + } | |
| 1403 | + | |
| 1404 | + if (schema['ui:widget']) { | |
| 1405 | + schema.widget = schema['ui:widget']; | |
| 1406 | + delete schema['ui:widget']; | |
| 1407 | + } | |
| 1408 | + | |
| 1409 | + if (schema['ui:labelWidth']) { | |
| 1410 | + schema.labelWidth = schema['ui:labelWidth']; | |
| 1411 | + delete schema['ui:labelWidth']; | |
| 1412 | + } | |
| 1413 | + | |
| 1414 | + if (schema.rules && schema.rules.length === 0) { | |
| 1415 | + delete schema.rules; | |
| 1416 | + } | |
| 1417 | + | |
| 1418 | + if (typeof schema.props === 'function' || isObject(schema.props) && Object.keys(schema.props).length > 0) {} else { | |
| 1419 | + delete schema.props; | |
| 1420 | + } | |
| 1421 | + | |
| 1422 | + return schema; | |
| 1423 | + } catch (error) { | |
| 1424 | + console.error('schema转换失败!', error); | |
| 1425 | + return schema; | |
| 1426 | + } | |
| 1427 | +}; // 检验一个string是 function(传统活箭头函数) | |
| 1428 | + | |
| 1429 | + | |
| 1430 | +var parseFunctionString = function parseFunctionString(string) { | |
| 1431 | + if (typeof string !== 'string') return false; | |
| 1432 | + var reg1 = /^{{(function.+)}}$/; | |
| 1433 | + var reg2 = /^{{(.+=>.+)}}$/; | |
| 1434 | + | |
| 1435 | + if (string.match(reg1)) { | |
| 1436 | + return string.match(reg1)[1]; | |
| 1437 | + } | |
| 1438 | + | |
| 1439 | + if (string.match(reg2)) { | |
| 1440 | + return string.match(reg2)[1]; | |
| 1441 | + } | |
| 1442 | + | |
| 1443 | + return false; | |
| 1444 | +}; | |
| 1445 | + | |
| 1446 | +exports.parseFunctionString = parseFunctionString; | |
| 1447 | + | |
| 1448 | +var completeSchemaWithTheme = function completeSchemaWithTheme() { | |
| 1449 | + var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | |
| 1450 | + var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
| 1451 | + var result = {}; | |
| 1452 | + | |
| 1453 | + if (isObject(schema)) { | |
| 1454 | + if (schema.theme && theme[schema.theme]) { | |
| 1455 | + result = _objectSpread(_objectSpread({}, schema), theme[schema.theme]); | |
| 1456 | + } | |
| 1457 | + | |
| 1458 | + Object.keys(schema).forEach(function (key) { | |
| 1459 | + result[key] = completeSchemaWithTheme(schema[key], theme); | |
| 1460 | + }); | |
| 1461 | + } else { | |
| 1462 | + result = schema; | |
| 1463 | + } | |
| 1464 | + | |
| 1465 | + return result; | |
| 1466 | +}; | |
| 1467 | + | |
| 1468 | +exports.completeSchemaWithTheme = completeSchemaWithTheme; | |
| 1469 | + | |
| 1470 | +var cleanEmpty = function cleanEmpty(obj) { | |
| 1471 | + if (Array.isArray(obj)) { | |
| 1472 | + return obj.map(function (v) { | |
| 1473 | + return v && isObject(v) ? cleanEmpty(v) : v; | |
| 1474 | + }).filter(function (v) { | |
| 1475 | + return !(v == undefined); | |
| 1476 | + }); | |
| 1477 | + } else if (isObject(obj)) { | |
| 1478 | + return Object.entries(obj).map(function (_ref6) { | |
| 1479 | + var _ref7 = _slicedToArray(_ref6, 2), | |
| 1480 | + k = _ref7[0], | |
| 1481 | + v = _ref7[1]; | |
| 1482 | + | |
| 1483 | + return [k, v && isObject(v) ? cleanEmpty(v) : v]; | |
| 1484 | + }).reduce(function (a, _ref8) { | |
| 1485 | + var _ref9 = _slicedToArray(_ref8, 2), | |
| 1486 | + k = _ref9[0], | |
| 1487 | + v = _ref9[1]; | |
| 1488 | + | |
| 1489 | + return v == undefined ? a : (a[k] = v, a); | |
| 1490 | + }, {}); | |
| 1491 | + } else { | |
| 1492 | + return obj; | |
| 1493 | + } | |
| 1494 | +}; | |
| 1495 | + | |
| 1496 | +exports.cleanEmpty = cleanEmpty; | |
| 1497 | + | |
| 1498 | +var removeHiddenFromResult = function removeHiddenFromResult(data, flatten) { | |
| 1499 | + Object.keys(flatten).forEach(function (key) { | |
| 1500 | + var hidden = flatten[key].schema && flatten[key].schema.hidden === true; // Remark: 有表达式的情况, 暂时不去掉了(有业务反而是希望留下的),就去掉 hidden = true 的 | |
| 1501 | + | |
| 1502 | + if ((0, _lodashEs.get)(data, key) !== undefined && hidden) { | |
| 1503 | + (0, _lodashEs.set)(data, key, undefined); | |
| 1504 | + } | |
| 1505 | + }); | |
| 1506 | + return data; | |
| 1507 | +}; | |
| 1508 | + | |
| 1509 | +exports.removeHiddenFromResult = removeHiddenFromResult; | |
| 1510 | + | |
| 1511 | +function msToTime(duration) { | |
| 1512 | + var seconds = Math.floor(duration / 1000 % 60); | |
| 1513 | + var minutes = Math.floor(duration / (1000 * 60) % 60); | |
| 1514 | + var hours = Math.floor(duration / (1000 * 60 * 60) % 24); | |
| 1515 | + hours = hours < 10 ? '0' + hours : hours; | |
| 1516 | + minutes = minutes < 10 ? '0' + minutes : minutes; | |
| 1517 | + seconds = seconds < 10 ? '0' + seconds : seconds; | |
| 1518 | + return hours + ':' + minutes + ':' + seconds; | |
| 1519 | +} | |
| 1520 | + | |
| 1521 | +function yymmdd(timeStamp) { | |
| 1522 | + var date_ob = new Date(Number(timeStamp)); | |
| 1523 | + | |
| 1524 | + var adjustZero = function adjustZero(num) { | |
| 1525 | + return ('0' + num).slice(-2); | |
| 1526 | + }; | |
| 1527 | + | |
| 1528 | + var day = adjustZero(date_ob.getDate()); | |
| 1529 | + var month = adjustZero(date_ob.getMonth()); | |
| 1530 | + var year = date_ob.getFullYear(); | |
| 1531 | + var hours = adjustZero(date_ob.getHours()); | |
| 1532 | + var minutes = adjustZero(date_ob.getMinutes()); | |
| 1533 | + var seconds = adjustZero(date_ob.getSeconds()); | |
| 1534 | + return "".concat(year, "-").concat(month, "-").concat(day, " ").concat(hours, ":").concat(minutes, ":").concat(seconds); | |
| 68 | 1535 | } |
| \ No newline at end of file | ... | ... |
lib/validateMessage.js
renamed from
lib/form-render-core/src/validateMessage.js
lib/validateMessageCN.js
renamed from
lib/form-render-core/src/validateMessageCN.js
lib/validator.js
0 → 100644
| 1 | +"use strict"; | |
| 2 | + | |
| 3 | +Object.defineProperty(exports, "__esModule", { | |
| 4 | + value: true | |
| 5 | +}); | |
| 6 | +exports.validateAll = void 0; | |
| 7 | + | |
| 8 | +var _utils = require("./utils"); | |
| 9 | + | |
| 10 | +var _validateMessageCN = require("./validateMessageCN"); | |
| 11 | + | |
| 12 | +var _validateMessage = require("./validateMessage"); | |
| 13 | + | |
| 14 | +var _asyncValidator = _interopRequireDefault(require("async-validator")); | |
| 15 | + | |
| 16 | +var _lodashEs = require("lodash-es"); | |
| 17 | + | |
| 18 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 19 | + | |
| 20 | +function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 21 | + | |
| 22 | +function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 23 | + | |
| 24 | +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 25 | + | |
| 26 | +function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 27 | + | |
| 28 | +function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 29 | + | |
| 30 | +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 31 | + | |
| 32 | +var validateAll = function validateAll(_ref) { | |
| 33 | + var formData = _ref.formData, | |
| 34 | + flatten = _ref.flatten, | |
| 35 | + _ref$isRequired = _ref.isRequired, | |
| 36 | + isRequired = _ref$isRequired === void 0 ? true : _ref$isRequired, | |
| 37 | + _ref$touchedKeys = _ref.touchedKeys, | |
| 38 | + touchedKeys = _ref$touchedKeys === void 0 ? [] : _ref$touchedKeys, | |
| 39 | + _ref$locale = _ref.locale, | |
| 40 | + locale = _ref$locale === void 0 ? 'cn' : _ref$locale, | |
| 41 | + _ref$validateMessages = _ref.validateMessages, | |
| 42 | + validateMessages = _ref$validateMessages === void 0 ? {} : _ref$validateMessages; | |
| 43 | + | |
| 44 | + var _schema = (0, _utils.getSchemaFromFlatten)(flatten); | |
| 45 | + | |
| 46 | + if (Object.keys(_schema).length === 0) return Promise.resolve(); | |
| 47 | + var descriptor = (0, _utils.getDescriptorFromSchema)({ | |
| 48 | + schema: _schema, | |
| 49 | + isRequired: isRequired | |
| 50 | + }).fields; | |
| 51 | + window.descriptor = descriptor; // console.log(descriptor, '&&&& descriptor', formData); | |
| 52 | + | |
| 53 | + var touchVerifyList = []; // 如果是最后的校验,所有key都touch了,就不用算这个了 | |
| 54 | + // 因为要整个构建validator在list的情况太复杂了,所以required单独拿出来处理,但是这边有不少单独处理逻辑,例如message | |
| 55 | + | |
| 56 | + if (!isRequired) { | |
| 57 | + touchedKeys.forEach(function (key) { | |
| 58 | + var keyRequired = (0, _utils.isPathRequired)(key, _schema); | |
| 59 | + var val = (0, _lodashEs.get)(formData, key); | |
| 60 | + var nullValue = [undefined, null, ''].indexOf(val) > -1; // 注意 0 不是 | |
| 61 | + | |
| 62 | + var isEmptyMultiSelect = Array.isArray(val) && val.length === 0; | |
| 63 | + | |
| 64 | + if ((nullValue || isEmptyMultiSelect) && keyRequired.required) { | |
| 65 | + var _message = keyRequired.message || validateMessages.required || '${title}必填'; | |
| 66 | + | |
| 67 | + touchVerifyList.push({ | |
| 68 | + name: key, | |
| 69 | + error: [_message] | |
| 70 | + }); | |
| 71 | + } | |
| 72 | + }); | |
| 73 | + } | |
| 74 | + | |
| 75 | + var cn = _validateMessageCN.defaultValidateMessagesCN; | |
| 76 | + var en = _validateMessage.defaultValidateMessages; // TODO: 有些情况会出现没有rules,需要看一下,先兜底 | |
| 77 | + | |
| 78 | + var validator; | |
| 79 | + | |
| 80 | + try { | |
| 81 | + validator = new _asyncValidator.default(descriptor); | |
| 82 | + } catch (error) { | |
| 83 | + return Promise.resolve([]); | |
| 84 | + } | |
| 85 | + | |
| 86 | + var messageFeed = locale === 'en' ? en : cn; | |
| 87 | + (0, _lodashEs.merge)(messageFeed, validateMessages); | |
| 88 | + validator.messages(messageFeed); | |
| 89 | + return validator.validate(formData || {}).then(function (res) { | |
| 90 | + if (touchVerifyList.length > 0) return touchVerifyList; | |
| 91 | + return []; | |
| 92 | + }).catch(function (_ref2) { | |
| 93 | + var errors = _ref2.errors, | |
| 94 | + fields = _ref2.fields; | |
| 95 | + // error的name改成正常的path | |
| 96 | + var normalizedErrors = (0, _utils.getArray)(errors).map(function (err) { | |
| 97 | + var _path = (0, _utils.formatPathFromValidator)(err.field); | |
| 98 | + | |
| 99 | + return { | |
| 100 | + name: _path, | |
| 101 | + error: [err.message] | |
| 102 | + }; | |
| 103 | + }); // 添加touched的required | |
| 104 | + | |
| 105 | + normalizedErrors = [].concat(_toConsumableArray(normalizedErrors), touchVerifyList); // 合并同名的error | |
| 106 | + | |
| 107 | + var _errorFields = []; | |
| 108 | + normalizedErrors.forEach(function (item) { | |
| 109 | + var matchIndex = _errorFields.findIndex(function (ele) { | |
| 110 | + return ele.name === item.name; | |
| 111 | + }); | |
| 112 | + | |
| 113 | + if (matchIndex === -1) { | |
| 114 | + _errorFields.push(item); | |
| 115 | + } else { | |
| 116 | + _errorFields[matchIndex].error = [].concat(_toConsumableArray(_errorFields[matchIndex].error), _toConsumableArray(item.error)); | |
| 117 | + } | |
| 118 | + }); | |
| 119 | + return _errorFields; | |
| 120 | + }); | |
| 121 | +}; // useEffect(() => { | |
| 122 | +// const descriptor = { | |
| 123 | +// roles: { | |
| 124 | +// type: 'array', | |
| 125 | +// required: true, | |
| 126 | +// defaultField: { | |
| 127 | +// type: 'object', | |
| 128 | +// fields: { | |
| 129 | +// street: { type: 'string', required: true }, | |
| 130 | +// city: { type: 'string', required: true, message: 'invalid zip' }, | |
| 131 | +// zip: [ | |
| 132 | +// { | |
| 133 | +// type: 'string', | |
| 134 | +// required: true, | |
| 135 | +// message: 'invalid zip', | |
| 136 | +// }, | |
| 137 | +// { | |
| 138 | +// len: 8, | |
| 139 | +// message: 'xxx', | |
| 140 | +// }, | |
| 141 | +// ], | |
| 142 | +// res: { | |
| 143 | +// type: 'array', | |
| 144 | +// required: true, | |
| 145 | +// defaultField: { | |
| 146 | +// type: 'object', | |
| 147 | +// fields: { | |
| 148 | +// city: { type: 'string', required: true }, | |
| 149 | +// }, | |
| 150 | +// }, | |
| 151 | +// }, | |
| 152 | +// }, | |
| 153 | +// }, | |
| 154 | +// }, | |
| 155 | +// name: { | |
| 156 | +// type: 'string', | |
| 157 | +// required: true, | |
| 158 | +// validator: (rule, value) => value === 'muji', | |
| 159 | +// }, | |
| 160 | +// age: { | |
| 161 | +// type: 'number', | |
| 162 | +// min: 12, | |
| 163 | +// max: 13, | |
| 164 | +// message: 'asdfadsf', | |
| 165 | +// // asyncValidator: (rule, value) => { | |
| 166 | +// // return new Promise((resolve, reject) => { | |
| 167 | +// // if (value < 18) { | |
| 168 | +// // reject('too young'); // reject with error message | |
| 169 | +// // } else { | |
| 170 | +// // resolve(); | |
| 171 | +// // } | |
| 172 | +// // }); | |
| 173 | +// // }, | |
| 174 | +// }, | |
| 175 | +// }; | |
| 176 | +// const validator = new Validator(descriptor); | |
| 177 | +// // validator.validate({ name: 'muji' }, (errors, fields) => { | |
| 178 | +// // if (errors) { | |
| 179 | +// // // validation failed, errors is an array of all errors | |
| 180 | +// // // fields is an object keyed by field name with an array of | |
| 181 | +// // // errors per field | |
| 182 | +// // return handleErrors(errors, fields); | |
| 183 | +// // } | |
| 184 | +// // // validation passed | |
| 185 | +// // }); | |
| 186 | +// validator | |
| 187 | +// .validate({ name: 'muji', age: 16, roles: [{ zip: '123', res: [{}] }] }) | |
| 188 | +// .then(() => { | |
| 189 | +// // validation passed or without error message | |
| 190 | +// }) | |
| 191 | +// .catch(({ errors, fields }) => { | |
| 192 | +// return handleErrors(errors, fields); | |
| 193 | +// }); | |
| 194 | +// }, []); | |
| 195 | + | |
| 196 | + | |
| 197 | +exports.validateAll = validateAll; | |
| 198 | +var d3 = { | |
| 199 | + list: { | |
| 200 | + type: 'array', | |
| 201 | + defaultField: { | |
| 202 | + type: 'object', | |
| 203 | + fields: { | |
| 204 | + userName: [{ | |
| 205 | + bind: false, | |
| 206 | + title: 'User Name', | |
| 207 | + type: 'string', | |
| 208 | + rules: [{ | |
| 209 | + min: 5, | |
| 210 | + message: '长度需要大于5' | |
| 211 | + }, { | |
| 212 | + max: 12 | |
| 213 | + }] | |
| 214 | + }, { | |
| 215 | + min: 5, | |
| 216 | + message: '长度需要大于5' | |
| 217 | + }, { | |
| 218 | + max: 12 | |
| 219 | + }], | |
| 220 | + selectName: { | |
| 221 | + title: '单选', | |
| 222 | + type: 'string', | |
| 223 | + enum: ['a', 'b', 'c'], | |
| 224 | + enumNames: ['早', '中', '晚'], | |
| 225 | + required: true | |
| 226 | + }, | |
| 227 | + checkboxName: { | |
| 228 | + title: '是否判断', | |
| 229 | + type: 'boolean', | |
| 230 | + valuePropName: 'checked' | |
| 231 | + } | |
| 232 | + } | |
| 233 | + } | |
| 234 | + } | |
| 235 | +}; | |
| 236 | +var d2 = { | |
| 237 | + list: { | |
| 238 | + type: 'array', | |
| 239 | + defaultField: { | |
| 240 | + type: 'object', | |
| 241 | + fields: { | |
| 242 | + userName: [{ | |
| 243 | + bind: false, | |
| 244 | + title: 'User Name', | |
| 245 | + type: 'string', | |
| 246 | + rules: [{ | |
| 247 | + min: 5, | |
| 248 | + message: '长度需要大于5' | |
| 249 | + }, { | |
| 250 | + max: 12 | |
| 251 | + }] | |
| 252 | + }, { | |
| 253 | + min: 5, | |
| 254 | + message: '长度需要大于5' | |
| 255 | + }, { | |
| 256 | + max: 12 | |
| 257 | + }], | |
| 258 | + selectName: { | |
| 259 | + title: '单选', | |
| 260 | + type: 'string', | |
| 261 | + enum: ['a', 'b', 'c'], | |
| 262 | + enumNames: ['早', '中', '晚'], | |
| 263 | + required: true | |
| 264 | + }, | |
| 265 | + checkboxName: { | |
| 266 | + title: '是否判断', | |
| 267 | + type: 'boolean', | |
| 268 | + valuePropName: 'checked' | |
| 269 | + } | |
| 270 | + } | |
| 271 | + } | |
| 272 | + } | |
| 273 | +}; | |
| \ No newline at end of file | ... | ... |
| ... | ... | @@ -81,7 +81,7 @@ var DateRange = function DateRange(_ref) { |
| 81 | 81 | var emptyList2 = stringList[0] === undefined || stringList[1] === undefined; |
| 82 | 82 | |
| 83 | 83 | if (emptyList1 || emptyList2) { |
| 84 | - onChange(null); | |
| 84 | + onChange(undefined); | |
| 85 | 85 | } else { |
| 86 | 86 | onChange(stringList); |
| 87 | 87 | } | ... | ... |
| ... | ... | @@ -17,10 +17,6 @@ require("antd/es/checkbox/style"); |
| 17 | 17 | |
| 18 | 18 | var _checkbox = _interopRequireDefault(require("antd/es/checkbox")); |
| 19 | 19 | |
| 20 | -require("antd/es/tree-select/style"); | |
| 21 | - | |
| 22 | -var _treeSelect = _interopRequireDefault(require("antd/es/tree-select")); | |
| 23 | - | |
| 24 | 20 | require("antd/es/input-number/style"); |
| 25 | 21 | |
| 26 | 22 | var _inputNumber = _interopRequireDefault(require("antd/es/input-number")); |
| ... | ... | @@ -35,6 +31,8 @@ var _list = _interopRequireDefault(require("./list")); |
| 35 | 31 | |
| 36 | 32 | var _map = _interopRequireDefault(require("./map")); |
| 37 | 33 | |
| 34 | +var _createWidget = require("../../createWidget"); | |
| 35 | + | |
| 38 | 36 | var _imageInput = _interopRequireDefault(require("./imageInput")); |
| 39 | 37 | |
| 40 | 38 | var _urlInput = _interopRequireDefault(require("./urlInput")); |
| ... | ... | @@ -57,19 +55,17 @@ var _dateRange = _interopRequireDefault(require("./dateRange")); |
| 57 | 55 | |
| 58 | 56 | var _timeRange = _interopRequireDefault(require("./timeRange")); |
| 59 | 57 | |
| 60 | -var _color = _interopRequireDefault(require("./color")); | |
| 58 | +var _excluded = ["style"]; | |
| 61 | 59 | |
| 62 | -var _slider = _interopRequireDefault(require("./slider")); | |
| 60 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 63 | 61 | |
| 64 | -var _upload = _interopRequireDefault(require("./upload")); | |
| 62 | +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 65 | 63 | |
| 66 | -var _excluded = ["style"], | |
| 67 | - _excluded2 = ["autoSize"], | |
| 68 | - _excluded3 = ["style"]; | |
| 64 | +function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 69 | 65 | |
| 70 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 66 | +function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 71 | 67 | |
| 72 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 68 | +function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 73 | 69 | |
| 74 | 70 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } |
| 75 | 71 | |
| ... | ... | @@ -77,40 +73,58 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va |
| 77 | 73 | |
| 78 | 74 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 79 | 75 | |
| 80 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 76 | +function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | |
| 81 | 77 | |
| 82 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 78 | +function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | |
| 83 | 79 | |
| 84 | -// const Cascader = React.lazy(() => import('antd/es/cascader')); | |
| 85 | -var TextArea = _input.default.TextArea; | |
| 80 | +var TreeSelect = _react.default.lazy(function () { | |
| 81 | + return Promise.resolve().then(function () { | |
| 82 | + return _interopRequireWildcard(require('antd/es/tree-select')); | |
| 83 | + }); | |
| 84 | +}); // const Cascader = React.lazy(() => import('antd/es/cascader')); | |
| 86 | 85 | |
| 87 | -var FrNumber = function FrNumber(_ref) { | |
| 88 | - var style = _ref.style, | |
| 89 | - rest = _objectWithoutProperties(_ref, _excluded); | |
| 90 | 86 | |
| 91 | - return /*#__PURE__*/_react.default.createElement(_inputNumber.default, _extends({ | |
| 87 | +var color = _react.default.lazy(function () { | |
| 88 | + return Promise.resolve().then(function () { | |
| 89 | + return _interopRequireWildcard(require('./color')); | |
| 90 | + }); | |
| 91 | +}); | |
| 92 | + | |
| 93 | +var slider = _react.default.lazy(function () { | |
| 94 | + return Promise.resolve().then(function () { | |
| 95 | + return _interopRequireWildcard(require('./slider')); | |
| 96 | + }); | |
| 97 | +}); | |
| 98 | + | |
| 99 | +var upload = _react.default.lazy(function () { | |
| 100 | + return Promise.resolve().then(function () { | |
| 101 | + return _interopRequireWildcard(require('./upload')); | |
| 102 | + }); | |
| 103 | +}); | |
| 104 | + | |
| 105 | +var TextArea = _input.default.TextArea; | |
| 106 | +var FrNumber = (0, _createWidget.createWidget)(function (_ref) { | |
| 107 | + var style = _ref.style; | |
| 108 | + return { | |
| 92 | 109 | style: _objectSpread({ |
| 93 | 110 | width: '100%' |
| 94 | 111 | }, style) |
| 95 | - }, rest)); | |
| 96 | -}; | |
| 97 | - | |
| 98 | -var FrTextArea = function FrTextArea(_ref2) { | |
| 99 | - var autoSize = _ref2.autoSize, | |
| 100 | - rest = _objectWithoutProperties(_ref2, _excluded2); | |
| 101 | - | |
| 102 | - return /*#__PURE__*/_react.default.createElement(TextArea, _extends({ | |
| 112 | + }; | |
| 113 | +})(_inputNumber.default); | |
| 114 | +var FrTextArea = (0, _createWidget.createWidget)(function (_ref2) { | |
| 115 | + var autoSize = _ref2.autoSize; | |
| 116 | + return { | |
| 103 | 117 | autoSize: autoSize ? autoSize : { |
| 104 | 118 | minRows: 3 |
| 105 | 119 | } |
| 106 | - }, rest)); | |
| 107 | -}; | |
| 120 | + }; | |
| 121 | +})(TextArea); | |
| 108 | 122 | |
| 109 | 123 | var FrTreeSelect = function FrTreeSelect(_ref3) { |
| 110 | 124 | var style = _ref3.style, |
| 111 | - rest = _objectWithoutProperties(_ref3, _excluded3); | |
| 125 | + rest = _objectWithoutProperties(_ref3, _excluded); | |
| 112 | 126 | |
| 113 | - return /*#__PURE__*/_react.default.createElement(_treeSelect.default, _extends({ | |
| 127 | + return /*#__PURE__*/_react.default.createElement(TreeSelect, _extends({ | |
| 114 | 128 | style: _objectSpread({ |
| 115 | 129 | width: '100%' |
| 116 | 130 | }, style) |
| ... | ... | @@ -125,7 +139,7 @@ var widgets = { |
| 125 | 139 | checkbox: _checkbox.default, |
| 126 | 140 | checkboxes: _checkboxes.default, |
| 127 | 141 | // checkbox多选 |
| 128 | - color: _color.default, | |
| 142 | + color: color, | |
| 129 | 143 | date: _date.default, |
| 130 | 144 | time: _time.default, |
| 131 | 145 | dateRange: _dateRange.default, |
| ... | ... | @@ -139,11 +153,11 @@ var widgets = { |
| 139 | 153 | number: FrNumber, |
| 140 | 154 | radio: _radio.default, |
| 141 | 155 | select: _select.default, |
| 142 | - slider: _slider.default, | |
| 156 | + slider: slider, | |
| 143 | 157 | // 带滚条的number |
| 144 | 158 | switch: _switch.default, |
| 145 | 159 | textarea: FrTextArea, |
| 146 | - upload: _upload.default, | |
| 160 | + upload: upload, | |
| 147 | 161 | html: _html.default, |
| 148 | 162 | rate: _rate.default, |
| 149 | 163 | treeSelect: FrTreeSelect // cascader: FrCascader, | ... | ... |
lib/widgets/antd/listCard.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.default = void 0; | |
| 7 | - | |
| 8 | -require("antd/es/button/style"); | |
| 9 | - | |
| 10 | -var _button = _interopRequireDefault(require("antd/es/button")); | |
| 11 | - | |
| 12 | -require("antd/es/popconfirm/style"); | |
| 13 | - | |
| 14 | -var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 15 | - | |
| 16 | -var _CloseOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/CloseOutlined")); | |
| 17 | - | |
| 18 | -var _CopyOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/CopyOutlined")); | |
| 19 | - | |
| 20 | -var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 21 | - | |
| 22 | -var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 23 | - | |
| 24 | -var _react = _interopRequireDefault(require("react")); | |
| 25 | - | |
| 26 | -var _index = _interopRequireDefault(require("../../index")); | |
| 27 | - | |
| 28 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 29 | - | |
| 30 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 31 | - | |
| 32 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 33 | - | |
| 34 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 35 | - | |
| 36 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 37 | - | |
| 38 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 39 | - | |
| 40 | -var CardList = function CardList(_ref) { | |
| 41 | - var _ref$displayList = _ref.displayList, | |
| 42 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 43 | - listData = _ref.listData, | |
| 44 | - changeList = _ref.changeList, | |
| 45 | - schema = _ref.schema, | |
| 46 | - deleteItem = _ref.deleteItem, | |
| 47 | - copyItem = _ref.copyItem, | |
| 48 | - addItem = _ref.addItem, | |
| 49 | - moveItemUp = _ref.moveItemUp, | |
| 50 | - moveItemDown = _ref.moveItemDown, | |
| 51 | - displayType = _ref.displayType, | |
| 52 | - getFieldsProps = _ref.getFieldsProps; | |
| 53 | - var _schema$props = schema.props, | |
| 54 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 55 | - itemProps = schema.itemProps; | |
| 56 | - var addBtnProps = { | |
| 57 | - type: 'dashed', | |
| 58 | - children: '新增一条' | |
| 59 | - }; | |
| 60 | - | |
| 61 | - if (props.addBtnProps && _typeof(props.addBtnProps) === 'object') { | |
| 62 | - addBtnProps = _objectSpread(_objectSpread({}, addBtnProps), props.addBtnProps); | |
| 63 | - } | |
| 64 | - | |
| 65 | - return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", { | |
| 66 | - className: "fr-card-list" | |
| 67 | - }, displayList.map(function (item, idx) { | |
| 68 | - var fieldsProps = getFieldsProps(idx); | |
| 69 | - fieldsProps.displayType = displayType; | |
| 70 | - return /*#__PURE__*/_react.default.createElement("div", { | |
| 71 | - className: "fr-card-item ".concat(displayType === 'row' ? 'fr-card-item-row' : ''), | |
| 72 | - key: idx | |
| 73 | - }, /*#__PURE__*/_react.default.createElement("div", { | |
| 74 | - className: "fr-card-index" | |
| 75 | - }, idx + 1), /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps), /*#__PURE__*/_react.default.createElement("div", { | |
| 76 | - direction: "horizontal", | |
| 77 | - className: "fr-card-toolbar" | |
| 78 | - }, !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 79 | - style: { | |
| 80 | - fontSize: 16, | |
| 81 | - marginLeft: 4 | |
| 82 | - }, | |
| 83 | - onClick: function onClick() { | |
| 84 | - return moveItemUp(idx); | |
| 85 | - } | |
| 86 | - }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 87 | - style: { | |
| 88 | - fontSize: 16, | |
| 89 | - marginLeft: 4 | |
| 90 | - }, | |
| 91 | - onClick: function onClick() { | |
| 92 | - return moveItemDown(idx); | |
| 93 | - } | |
| 94 | - })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement(_CopyOutlined2.default, { | |
| 95 | - style: { | |
| 96 | - fontSize: 16, | |
| 97 | - marginLeft: 8 | |
| 98 | - }, | |
| 99 | - onClick: function onClick() { | |
| 100 | - return copyItem(idx); | |
| 101 | - } | |
| 102 | - }), !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 103 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 104 | - onConfirm: function onConfirm() { | |
| 105 | - return deleteItem(idx); | |
| 106 | - }, | |
| 107 | - okText: "\u786E\u5B9A", | |
| 108 | - cancelText: "\u53D6\u6D88" | |
| 109 | - }, /*#__PURE__*/_react.default.createElement(_CloseOutlined2.default, { | |
| 110 | - style: { | |
| 111 | - fontSize: 16, | |
| 112 | - marginLeft: 8 | |
| 113 | - } | |
| 114 | - })))); | |
| 115 | - })), /*#__PURE__*/_react.default.createElement("div", { | |
| 116 | - style: { | |
| 117 | - marginTop: displayList.length > 0 ? 0 : 8 | |
| 118 | - } | |
| 119 | - }, !props.hideAdd && /*#__PURE__*/_react.default.createElement(_button.default, _extends({ | |
| 120 | - onClick: addItem | |
| 121 | - }, addBtnProps)), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 122 | - var callback = item.callback, | |
| 123 | - text = item.text, | |
| 124 | - html = item.html; | |
| 125 | - | |
| 126 | - var onClick = function onClick() { | |
| 127 | - console.log({ | |
| 128 | - value: listData, | |
| 129 | - onChange: changeList, | |
| 130 | - schema: schema | |
| 131 | - }); | |
| 132 | - }; | |
| 133 | - | |
| 134 | - if (typeof window[callback] === 'function') { | |
| 135 | - onClick = function onClick() { | |
| 136 | - window[callback]({ | |
| 137 | - value: listData, | |
| 138 | - onChange: changeList, | |
| 139 | - schema: schema | |
| 140 | - }); | |
| 141 | - }; | |
| 142 | - } | |
| 143 | - | |
| 144 | - return /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 145 | - key: idx.toString(), | |
| 146 | - style: { | |
| 147 | - marginLeft: 8 | |
| 148 | - }, | |
| 149 | - type: "dashed", | |
| 150 | - onClick: onClick | |
| 151 | - }, /*#__PURE__*/_react.default.createElement("span", { | |
| 152 | - dangerouslySetInnerHTML: { | |
| 153 | - __html: html || text | |
| 154 | - } | |
| 155 | - })); | |
| 156 | - }) : null)); | |
| 157 | -}; | |
| 158 | - | |
| 159 | -var _default = CardList; | |
| 160 | -exports.default = _default; | |
| \ No newline at end of file |
lib/widgets/antd/listDrawer.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 4 | - | |
| 5 | -Object.defineProperty(exports, "__esModule", { | |
| 6 | - value: true | |
| 7 | -}); | |
| 8 | -exports.default = void 0; | |
| 9 | - | |
| 10 | -require("antd/es/table/style"); | |
| 11 | - | |
| 12 | -var _table = _interopRequireDefault(require("antd/es/table")); | |
| 13 | - | |
| 14 | -require("antd/es/drawer/style"); | |
| 15 | - | |
| 16 | -var _drawer = _interopRequireDefault(require("antd/es/drawer")); | |
| 17 | - | |
| 18 | -require("antd/es/button/style"); | |
| 19 | - | |
| 20 | -var _button = _interopRequireDefault(require("antd/es/button")); | |
| 21 | - | |
| 22 | -var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 23 | - | |
| 24 | -var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 25 | - | |
| 26 | -require("antd/es/popconfirm/style"); | |
| 27 | - | |
| 28 | -var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 29 | - | |
| 30 | -var _react = _interopRequireWildcard(require("react")); | |
| 31 | - | |
| 32 | -var _index = _interopRequireDefault(require("../../index")); | |
| 33 | - | |
| 34 | -var _hooks = require("../../../hooks"); | |
| 35 | - | |
| 36 | -var _utils = require("../../../utils"); | |
| 37 | - | |
| 38 | -var _ErrorMessage = _interopRequireDefault(require("../../RenderField/ErrorMessage")); | |
| 39 | - | |
| 40 | -var _excluded = ["buttons"], | |
| 41 | - _excluded2 = ["pagination"]; | |
| 42 | - | |
| 43 | -function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | |
| 44 | - | |
| 45 | -function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | |
| 46 | - | |
| 47 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 48 | - | |
| 49 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 50 | - | |
| 51 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 52 | - | |
| 53 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 54 | - | |
| 55 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 56 | - | |
| 57 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 58 | - | |
| 59 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 60 | - | |
| 61 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 62 | - | |
| 63 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 64 | - | |
| 65 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 66 | - | |
| 67 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 68 | - | |
| 69 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 70 | - | |
| 71 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 72 | - | |
| 73 | -var FIELD_LENGTH = 170; | |
| 74 | - | |
| 75 | -var DrawerList = function DrawerList(_ref) { | |
| 76 | - var _extends2; | |
| 77 | - | |
| 78 | - var _ref$displayList = _ref.displayList, | |
| 79 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 80 | - dataPath = _ref.dataPath, | |
| 81 | - children = _ref.children, | |
| 82 | - deleteItem = _ref.deleteItem, | |
| 83 | - addItem = _ref.addItem, | |
| 84 | - moveItemDown = _ref.moveItemDown, | |
| 85 | - moveItemUp = _ref.moveItemUp, | |
| 86 | - flatten = _ref.flatten, | |
| 87 | - errorFields = _ref.errorFields, | |
| 88 | - getFieldsProps = _ref.getFieldsProps, | |
| 89 | - schema = _ref.schema, | |
| 90 | - changeList = _ref.changeList, | |
| 91 | - listData = _ref.listData; | |
| 92 | - var _schema$props = schema.props, | |
| 93 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 94 | - _schema$itemProps = schema.itemProps, | |
| 95 | - itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps; | |
| 96 | - | |
| 97 | - var buttons = itemProps.buttons, | |
| 98 | - columnProps = _objectWithoutProperties(itemProps, _excluded); | |
| 99 | - | |
| 100 | - var _props$pagination = props.pagination, | |
| 101 | - pagination = _props$pagination === void 0 ? {} : _props$pagination, | |
| 102 | - rest = _objectWithoutProperties(props, _excluded2); | |
| 103 | - | |
| 104 | - var paginationConfig = pagination && _objectSpread({ | |
| 105 | - size: 'small', | |
| 106 | - hideOnSinglePage: true | |
| 107 | - }, pagination); | |
| 108 | - | |
| 109 | - var currentIndex = (0, _react.useRef)(-1); | |
| 110 | - | |
| 111 | - var _useSet = (0, _hooks.useSet)({ | |
| 112 | - showDrawer: false | |
| 113 | - }), | |
| 114 | - _useSet2 = _slicedToArray(_useSet, 2), | |
| 115 | - state = _useSet2[0], | |
| 116 | - setState = _useSet2[1]; | |
| 117 | - | |
| 118 | - var showDrawer = state.showDrawer; | |
| 119 | - var dataSource = displayList.map(function (item, index) { | |
| 120 | - return _objectSpread(_objectSpread({}, item), {}, { | |
| 121 | - $idx: index | |
| 122 | - }); | |
| 123 | - }); | |
| 124 | - var columns = children.map(function (child) { | |
| 125 | - var item = flatten[child]; | |
| 126 | - var schema = item && item.schema || {}; | |
| 127 | - | |
| 128 | - var _dataIndex = (0, _utils.getKeyFromPath)(child); | |
| 129 | - | |
| 130 | - return _objectSpread({ | |
| 131 | - dataIndex: _dataIndex, | |
| 132 | - title: schema.required ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", { | |
| 133 | - className: "fr-label-required" | |
| 134 | - }, " *"), /*#__PURE__*/_react.default.createElement("span", null, schema.title)) : schema.title, | |
| 135 | - width: FIELD_LENGTH, | |
| 136 | - render: function render(value, record) { | |
| 137 | - var childPath = (0, _utils.getDataPath)(child, [record.$idx]); | |
| 138 | - var errorObj = errorFields.find(function (item) { | |
| 139 | - return item.name == childPath; | |
| 140 | - }) || {}; //TODO: 万一error在更深的层,这个办法是find不到的,会展示那一行没有提示。可以整一行加一个红线的方式处理 | |
| 141 | - | |
| 142 | - return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", null, (0, _utils.getDisplayValue)(value, schema)), errorObj.error && /*#__PURE__*/_react.default.createElement(_ErrorMessage.default, { | |
| 143 | - message: errorObj.error, | |
| 144 | - schema: schema | |
| 145 | - })); | |
| 146 | - } | |
| 147 | - }, columnProps); | |
| 148 | - }); | |
| 149 | - columns.push({ | |
| 150 | - title: '操作', | |
| 151 | - key: '$action', | |
| 152 | - fixed: 'right', | |
| 153 | - width: 120, | |
| 154 | - render: function render(value, record, idx) { | |
| 155 | - var index = value && value.$idx || 0; | |
| 156 | - return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("a", { | |
| 157 | - onClick: function onClick() { | |
| 158 | - return openDrawer(index); | |
| 159 | - } | |
| 160 | - }, "\u7F16\u8F91"), !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 161 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 162 | - onConfirm: function onConfirm() { | |
| 163 | - return deleteItem(index); | |
| 164 | - }, | |
| 165 | - okText: "\u786E\u5B9A", | |
| 166 | - cancelText: "\u53D6\u6D88" | |
| 167 | - }, /*#__PURE__*/_react.default.createElement("a", null, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 168 | - style: { | |
| 169 | - color: '#1890ff', | |
| 170 | - fontSize: 16, | |
| 171 | - marginLeft: 4 | |
| 172 | - }, | |
| 173 | - onClick: function onClick() { | |
| 174 | - return moveItemUp(idx); | |
| 175 | - } | |
| 176 | - }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 177 | - style: { | |
| 178 | - color: '#1890ff', | |
| 179 | - fontSize: 16, | |
| 180 | - marginLeft: 4 | |
| 181 | - }, | |
| 182 | - onClick: function onClick() { | |
| 183 | - return moveItemDown(idx); | |
| 184 | - } | |
| 185 | - }))); | |
| 186 | - } | |
| 187 | - }); | |
| 188 | - var fieldsProps = getFieldsProps(currentIndex.current); | |
| 189 | - | |
| 190 | - var openDrawer = function openDrawer(index) { | |
| 191 | - currentIndex.current = index; | |
| 192 | - setState({ | |
| 193 | - showDrawer: true | |
| 194 | - }); | |
| 195 | - }; | |
| 196 | - | |
| 197 | - var closeDrawer = function closeDrawer() { | |
| 198 | - currentIndex.current = -1; | |
| 199 | - setState({ | |
| 200 | - showDrawer: false | |
| 201 | - }); | |
| 202 | - }; | |
| 203 | - | |
| 204 | - var handleAdd = function handleAdd() { | |
| 205 | - var newIndex = addItem(); | |
| 206 | - openDrawer(newIndex); | |
| 207 | - }; | |
| 208 | - | |
| 209 | - return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", { | |
| 210 | - className: "w-100 mb2 tr" | |
| 211 | - }, !props.hideAdd && /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 212 | - type: "primary", | |
| 213 | - size: "small", | |
| 214 | - onClick: handleAdd | |
| 215 | - }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 216 | - var callback = item.callback, | |
| 217 | - text = item.text, | |
| 218 | - html = item.html; | |
| 219 | - | |
| 220 | - var onClick = function onClick() { | |
| 221 | - console.log({ | |
| 222 | - value: listData, | |
| 223 | - onChange: changeList, | |
| 224 | - schema: schema | |
| 225 | - }); | |
| 226 | - }; | |
| 227 | - | |
| 228 | - if (typeof window[callback] === 'function') { | |
| 229 | - onClick = function onClick() { | |
| 230 | - window[callback]({ | |
| 231 | - value: listData, | |
| 232 | - onChange: changeList, | |
| 233 | - schema: schema | |
| 234 | - }); | |
| 235 | - }; | |
| 236 | - } | |
| 237 | - | |
| 238 | - return /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 239 | - key: idx.toString(), | |
| 240 | - style: { | |
| 241 | - marginLeft: 8 | |
| 242 | - }, | |
| 243 | - size: "small", | |
| 244 | - onClick: onClick | |
| 245 | - }, /*#__PURE__*/_react.default.createElement("span", { | |
| 246 | - dangerouslySetInnerHTML: { | |
| 247 | - __html: html || text | |
| 248 | - } | |
| 249 | - })); | |
| 250 | - }) : null), /*#__PURE__*/_react.default.createElement(_drawer.default, { | |
| 251 | - width: "600", | |
| 252 | - title: "\u7F16\u8F91", | |
| 253 | - placement: "right", | |
| 254 | - onClose: closeDrawer, | |
| 255 | - visible: showDrawer, | |
| 256 | - destroyOnClose: true // 必须要加,currentIndex不是一个state,Core不会重新渲染就跪了 | |
| 257 | - | |
| 258 | - }, /*#__PURE__*/_react.default.createElement("div", { | |
| 259 | - className: "fr-container" | |
| 260 | - }, /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps))), /*#__PURE__*/_react.default.createElement(_table.default, _extends((_extends2 = { | |
| 261 | - size: "small", | |
| 262 | - scroll: { | |
| 263 | - x: 'max-content' | |
| 264 | - }, | |
| 265 | - columns: columns, | |
| 266 | - dataSource: dataSource, | |
| 267 | - rowClassName: function rowClassName(record, idx) { | |
| 268 | - var index = record && record.$idx; | |
| 269 | - var hasError = errorFields.find(function (item) { | |
| 270 | - return item.name.indexOf("".concat(dataPath, "[").concat(index, "]")) > -1; | |
| 271 | - }); | |
| 272 | - return hasError ? 'fr-row-error' : ''; | |
| 273 | - }, | |
| 274 | - rowKey: "$idx" | |
| 275 | - }, _defineProperty(_extends2, "size", "small"), _defineProperty(_extends2, "pagination", paginationConfig), _extends2), rest))); | |
| 276 | -}; | |
| 277 | - | |
| 278 | -var _default = DrawerList; | |
| 279 | -exports.default = _default; | |
| \ No newline at end of file |
lib/widgets/antd/listSimple.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.default = void 0; | |
| 7 | - | |
| 8 | -require("antd/es/button/style"); | |
| 9 | - | |
| 10 | -var _button = _interopRequireDefault(require("antd/es/button")); | |
| 11 | - | |
| 12 | -var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 13 | - | |
| 14 | -var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 15 | - | |
| 16 | -var _CopyOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/CopyOutlined")); | |
| 17 | - | |
| 18 | -require("antd/es/popconfirm/style"); | |
| 19 | - | |
| 20 | -var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 21 | - | |
| 22 | -var _DeleteOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/DeleteOutlined")); | |
| 23 | - | |
| 24 | -var _react = _interopRequireDefault(require("react")); | |
| 25 | - | |
| 26 | -var _index = _interopRequireDefault(require("../../index")); | |
| 27 | - | |
| 28 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 29 | - | |
| 30 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 31 | - | |
| 32 | -function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
| 33 | - | |
| 34 | -function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
| 35 | - | |
| 36 | -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
| 37 | - | |
| 38 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 39 | - | |
| 40 | -var SimpleList = function SimpleList(_ref) { | |
| 41 | - var schema = _ref.schema, | |
| 42 | - _ref$displayList = _ref.displayList, | |
| 43 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 44 | - listData = _ref.listData, | |
| 45 | - changeList = _ref.changeList, | |
| 46 | - deleteItem = _ref.deleteItem, | |
| 47 | - addItem = _ref.addItem, | |
| 48 | - copyItem = _ref.copyItem, | |
| 49 | - moveItemUp = _ref.moveItemUp, | |
| 50 | - moveItemDown = _ref.moveItemDown, | |
| 51 | - getFieldsProps = _ref.getFieldsProps; | |
| 52 | - var _schema$props = schema.props, | |
| 53 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 54 | - itemProps = schema.itemProps; | |
| 55 | - var addBtnProps = { | |
| 56 | - type: 'dashed', | |
| 57 | - children: '新增一条' | |
| 58 | - }; | |
| 59 | - | |
| 60 | - if (props.addBtnProps && _typeof(props.addBtnProps) === 'object') { | |
| 61 | - addBtnProps = _objectSpread(_objectSpread({}, addBtnProps), props.addBtnProps); | |
| 62 | - } | |
| 63 | - | |
| 64 | - return /*#__PURE__*/_react.default.createElement("div", { | |
| 65 | - className: "fr-list-1" | |
| 66 | - }, displayList.map(function (item, idx) { | |
| 67 | - var fieldsProps = getFieldsProps(idx); | |
| 68 | - fieldsProps.displayType = 'inline'; | |
| 69 | - | |
| 70 | - if (props.hideTitle) { | |
| 71 | - fieldsProps.hideTitle = true; | |
| 72 | - } | |
| 73 | - | |
| 74 | - return /*#__PURE__*/_react.default.createElement("div", { | |
| 75 | - key: idx, | |
| 76 | - style: { | |
| 77 | - display: 'flex' | |
| 78 | - } | |
| 79 | - }, /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps), /*#__PURE__*/_react.default.createElement("div", { | |
| 80 | - style: { | |
| 81 | - marginTop: 6 | |
| 82 | - } | |
| 83 | - }, !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 84 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 85 | - onConfirm: function onConfirm() { | |
| 86 | - return deleteItem(idx); | |
| 87 | - }, | |
| 88 | - okText: "\u786E\u5B9A", | |
| 89 | - cancelText: "\u53D6\u6D88" | |
| 90 | - }, /*#__PURE__*/_react.default.createElement(_DeleteOutlined2.default, { | |
| 91 | - style: { | |
| 92 | - fontSize: 17, | |
| 93 | - marginLeft: 8 | |
| 94 | - } | |
| 95 | - })), !props.hideAdd && !props.hideCopy && /*#__PURE__*/_react.default.createElement(_CopyOutlined2.default, { | |
| 96 | - style: { | |
| 97 | - fontSize: 15, | |
| 98 | - marginLeft: 8 | |
| 99 | - }, | |
| 100 | - onClick: function onClick() { | |
| 101 | - return copyItem(idx); | |
| 102 | - } | |
| 103 | - }), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 104 | - style: { | |
| 105 | - fontSize: 16, | |
| 106 | - marginLeft: 8 | |
| 107 | - }, | |
| 108 | - onClick: function onClick() { | |
| 109 | - return moveItemUp(idx); | |
| 110 | - } | |
| 111 | - }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 112 | - style: { | |
| 113 | - fontSize: 16, | |
| 114 | - marginLeft: 8 | |
| 115 | - }, | |
| 116 | - onClick: function onClick() { | |
| 117 | - return moveItemDown(idx); | |
| 118 | - } | |
| 119 | - })))); | |
| 120 | - }), /*#__PURE__*/_react.default.createElement("div", { | |
| 121 | - style: { | |
| 122 | - marginTop: displayList.length > 0 ? 0 : 8 | |
| 123 | - } | |
| 124 | - }, !props.hideAdd && /*#__PURE__*/_react.default.createElement(_button.default, _extends({ | |
| 125 | - onClick: addItem | |
| 126 | - }, addBtnProps)), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 127 | - var callback = item.callback, | |
| 128 | - text = item.text, | |
| 129 | - html = item.html; | |
| 130 | - | |
| 131 | - var onClick = function onClick() { | |
| 132 | - console.log({ | |
| 133 | - value: listData, | |
| 134 | - onChange: changeList, | |
| 135 | - schema: schema | |
| 136 | - }); | |
| 137 | - }; | |
| 138 | - | |
| 139 | - if (typeof window[callback] === 'function') { | |
| 140 | - onClick = function onClick() { | |
| 141 | - window[callback]({ | |
| 142 | - value: listData, | |
| 143 | - onChange: changeList, | |
| 144 | - schema: schema | |
| 145 | - }); | |
| 146 | - }; | |
| 147 | - } | |
| 148 | - | |
| 149 | - return /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 150 | - key: idx.toString(), | |
| 151 | - style: { | |
| 152 | - marginLeft: 8 | |
| 153 | - }, | |
| 154 | - type: "dashed", | |
| 155 | - onClick: onClick | |
| 156 | - }, /*#__PURE__*/_react.default.createElement("span", { | |
| 157 | - dangerouslySetInnerHTML: { | |
| 158 | - __html: html || text | |
| 159 | - } | |
| 160 | - })); | |
| 161 | - }) : null)); | |
| 162 | -}; | |
| 163 | - | |
| 164 | -var _default = SimpleList; | |
| 165 | -exports.default = _default; | |
| \ No newline at end of file |
lib/widgets/antd/listTab.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | |
| 4 | - | |
| 5 | -Object.defineProperty(exports, "__esModule", { | |
| 6 | - value: true | |
| 7 | -}); | |
| 8 | -exports.default = void 0; | |
| 9 | - | |
| 10 | -require("antd/es/tabs/style"); | |
| 11 | - | |
| 12 | -var _tabs = _interopRequireDefault(require("antd/es/tabs")); | |
| 13 | - | |
| 14 | -var _react = _interopRequireWildcard(require("react")); | |
| 15 | - | |
| 16 | -var _index = _interopRequireDefault(require("../../index")); | |
| 17 | - | |
| 18 | -var _excluded = ["tabName", "type"]; | |
| 19 | - | |
| 20 | -function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | |
| 21 | - | |
| 22 | -function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | |
| 23 | - | |
| 24 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 25 | - | |
| 26 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 27 | - | |
| 28 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 29 | - | |
| 30 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 31 | - | |
| 32 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 33 | - | |
| 34 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 35 | - | |
| 36 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 37 | - | |
| 38 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 39 | - | |
| 40 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 41 | - | |
| 42 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 43 | - | |
| 44 | -var TabPane = _tabs.default.TabPane; | |
| 45 | - | |
| 46 | -var TabList = function TabList(_ref) { | |
| 47 | - var _ref$displayList = _ref.displayList, | |
| 48 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 49 | - listData = _ref.listData, | |
| 50 | - changeList = _ref.changeList, | |
| 51 | - schema = _ref.schema, | |
| 52 | - deleteItem = _ref.deleteItem, | |
| 53 | - copyItem = _ref.copyItem, | |
| 54 | - addItem = _ref.addItem, | |
| 55 | - moveItemUp = _ref.moveItemUp, | |
| 56 | - moveItemDown = _ref.moveItemDown, | |
| 57 | - displayType = _ref.displayType, | |
| 58 | - getFieldsProps = _ref.getFieldsProps; | |
| 59 | - | |
| 60 | - var _useState = (0, _react.useState)('0'), | |
| 61 | - _useState2 = _slicedToArray(_useState, 2), | |
| 62 | - activeKey = _useState2[0], | |
| 63 | - setActiveKey = _useState2[1]; | |
| 64 | - | |
| 65 | - var _schema$props = schema.props, | |
| 66 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 67 | - itemProps = schema.itemProps; | |
| 68 | - | |
| 69 | - var tabName = props.tabName, | |
| 70 | - type = props.type, | |
| 71 | - restProps = _objectWithoutProperties(props, _excluded); | |
| 72 | - | |
| 73 | - var onEdit = function onEdit(targetKey, action) { | |
| 74 | - if (action === 'add') { | |
| 75 | - var currentKey = addItem(); | |
| 76 | - setActiveKey("".concat(currentKey)); | |
| 77 | - } else if (action === 'remove') { | |
| 78 | - deleteItem(Number(targetKey)); | |
| 79 | - setActiveKey("".concat(targetKey > 1 ? targetKey - 1 : 0)); | |
| 80 | - } else { | |
| 81 | - return null; | |
| 82 | - } | |
| 83 | - }; | |
| 84 | - | |
| 85 | - var getCurrentTabPaneName = function getCurrentTabPaneName(idx) { | |
| 86 | - return tabName instanceof Array ? tabName[idx] || idx + 1 : "".concat(tabName || '项目', " ").concat(idx + 1); | |
| 87 | - }; | |
| 88 | - | |
| 89 | - return /*#__PURE__*/_react.default.createElement(_tabs.default, _extends({ | |
| 90 | - type: type || 'line', | |
| 91 | - onChange: setActiveKey, | |
| 92 | - activeKey: activeKey, | |
| 93 | - onEdit: onEdit | |
| 94 | - }, restProps), displayList.map(function (item, idx) { | |
| 95 | - var fieldsProps = getFieldsProps(idx); | |
| 96 | - fieldsProps.displayType = displayType; | |
| 97 | - return /*#__PURE__*/_react.default.createElement(TabPane, { | |
| 98 | - tab: getCurrentTabPaneName(idx), | |
| 99 | - key: "".concat(idx) | |
| 100 | - }, /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps)); | |
| 101 | - })); | |
| 102 | -}; | |
| 103 | - | |
| 104 | -var _default = TabList; | |
| 105 | -exports.default = _default; | |
| \ No newline at end of file |
lib/widgets/antd/listVirtual.js
deleted
100644 → 0
| 1 | -"use strict"; | |
| 2 | - | |
| 3 | -Object.defineProperty(exports, "__esModule", { | |
| 4 | - value: true | |
| 5 | -}); | |
| 6 | -exports.default = void 0; | |
| 7 | - | |
| 8 | -require("antd/es/table/style"); | |
| 9 | - | |
| 10 | -var _table = _interopRequireDefault(require("antd/es/table")); | |
| 11 | - | |
| 12 | -require("antd/es/button/style"); | |
| 13 | - | |
| 14 | -var _button = _interopRequireDefault(require("antd/es/button")); | |
| 15 | - | |
| 16 | -var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined")); | |
| 17 | - | |
| 18 | -var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined")); | |
| 19 | - | |
| 20 | -require("antd/es/popconfirm/style"); | |
| 21 | - | |
| 22 | -var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm")); | |
| 23 | - | |
| 24 | -var _react = _interopRequireDefault(require("react")); | |
| 25 | - | |
| 26 | -var _index = _interopRequireDefault(require("../../index")); | |
| 27 | - | |
| 28 | -var _virtualizedtableforantd = require("virtualizedtableforantd4"); | |
| 29 | - | |
| 30 | -var _excluded = ["scrollY"]; | |
| 31 | - | |
| 32 | -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| 33 | - | |
| 34 | -function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | |
| 35 | - | |
| 36 | -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
| 37 | - | |
| 38 | -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 39 | - | |
| 40 | -function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
| 41 | - | |
| 42 | -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
| 43 | - | |
| 44 | -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
| 45 | - | |
| 46 | -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
| 47 | - | |
| 48 | -function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
| 49 | - | |
| 50 | -function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
| 51 | - | |
| 52 | -function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
| 53 | - | |
| 54 | -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
| 55 | - | |
| 56 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 57 | - | |
| 58 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 59 | - | |
| 60 | -var FIELD_LENGTH = 170; | |
| 61 | - | |
| 62 | -var VirtualList = function VirtualList(_ref) { | |
| 63 | - var _ref$displayList = _ref.displayList, | |
| 64 | - displayList = _ref$displayList === void 0 ? [] : _ref$displayList, | |
| 65 | - dataIndex = _ref.dataIndex, | |
| 66 | - children = _ref.children, | |
| 67 | - deleteItem = _ref.deleteItem, | |
| 68 | - addItem = _ref.addItem, | |
| 69 | - moveItemUp = _ref.moveItemUp, | |
| 70 | - moveItemDown = _ref.moveItemDown, | |
| 71 | - flatten = _ref.flatten, | |
| 72 | - schema = _ref.schema, | |
| 73 | - listData = _ref.listData, | |
| 74 | - changeList = _ref.changeList; | |
| 75 | - var _schema$props = schema.props, | |
| 76 | - props = _schema$props === void 0 ? {} : _schema$props, | |
| 77 | - _schema$itemProps = schema.itemProps, | |
| 78 | - itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps; | |
| 79 | - | |
| 80 | - var _props$scrollY = props.scrollY, | |
| 81 | - scrollY = _props$scrollY === void 0 ? 600 : _props$scrollY, | |
| 82 | - rest = _objectWithoutProperties(props, _excluded); | |
| 83 | - | |
| 84 | - var _useVT = (0, _virtualizedtableforantd.useVT)(function () { | |
| 85 | - return { | |
| 86 | - scroll: { | |
| 87 | - y: scrollY | |
| 88 | - } | |
| 89 | - }; | |
| 90 | - }, []), | |
| 91 | - _useVT2 = _slicedToArray(_useVT, 2), | |
| 92 | - vt = _useVT2[0], | |
| 93 | - set_components = _useVT2[1]; | |
| 94 | - | |
| 95 | - var dataSource = displayList.map(function (item, idx) { | |
| 96 | - return { | |
| 97 | - index: idx | |
| 98 | - }; | |
| 99 | - }); | |
| 100 | - var columns = children.map(function (child) { | |
| 101 | - var item = flatten[child]; | |
| 102 | - var schema = item && item.schema || {}; | |
| 103 | - return { | |
| 104 | - dataIndex: child, | |
| 105 | - width: FIELD_LENGTH, | |
| 106 | - title: schema.required ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", { | |
| 107 | - className: "fr-label-required" | |
| 108 | - }, " *"), /*#__PURE__*/_react.default.createElement("span", null, schema.title)) : schema.title, | |
| 109 | - render: function render(value, record, index) { | |
| 110 | - // Check: record.index 似乎是antd自己会给的,不错哦 | |
| 111 | - var childIndex = [].concat(_toConsumableArray(dataIndex), [record.index]); | |
| 112 | - return /*#__PURE__*/_react.default.createElement(_index.default, { | |
| 113 | - hideTitle: true, | |
| 114 | - displayType: "inline", | |
| 115 | - key: index.toString(), | |
| 116 | - id: child, | |
| 117 | - dataIndex: childIndex | |
| 118 | - }); | |
| 119 | - } | |
| 120 | - }; | |
| 121 | - }); | |
| 122 | - | |
| 123 | - if (!props.hideDelete || Array.isArray(itemProps.buttons)) { | |
| 124 | - columns.push({ | |
| 125 | - title: '操作', | |
| 126 | - key: '$action', | |
| 127 | - fixed: 'right', | |
| 128 | - width: 120, | |
| 129 | - render: function render(value, record, idx) { | |
| 130 | - return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, { | |
| 131 | - title: "\u786E\u5B9A\u5220\u9664?", | |
| 132 | - onConfirm: function onConfirm() { | |
| 133 | - return deleteItem(idx); | |
| 134 | - }, | |
| 135 | - okText: "\u786E\u5B9A", | |
| 136 | - cancelText: "\u53D6\u6D88" | |
| 137 | - }, /*#__PURE__*/_react.default.createElement("a", null, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, { | |
| 138 | - style: { | |
| 139 | - color: '#1890ff', | |
| 140 | - fontSize: 16, | |
| 141 | - marginLeft: 4 | |
| 142 | - }, | |
| 143 | - onClick: function onClick() { | |
| 144 | - return moveItemUp(idx); | |
| 145 | - } | |
| 146 | - }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, { | |
| 147 | - style: { | |
| 148 | - color: '#1890ff', | |
| 149 | - fontSize: 16, | |
| 150 | - marginLeft: 4 | |
| 151 | - }, | |
| 152 | - onClick: function onClick() { | |
| 153 | - return moveItemDown(idx); | |
| 154 | - } | |
| 155 | - })), Array.isArray(itemProps.buttons) ? itemProps.buttons.map(function (item, idx) { | |
| 156 | - var callback = item.callback, | |
| 157 | - text = item.text, | |
| 158 | - html = item.html; | |
| 159 | - | |
| 160 | - var onClick = function onClick() {}; | |
| 161 | - | |
| 162 | - if (typeof window[callback] === 'function') { | |
| 163 | - onClick = function onClick() { | |
| 164 | - window[callback]({ | |
| 165 | - value: listData, | |
| 166 | - onChange: changeList, | |
| 167 | - schema: schema | |
| 168 | - }); | |
| 169 | - }; | |
| 170 | - } | |
| 171 | - | |
| 172 | - return /*#__PURE__*/_react.default.createElement("a", { | |
| 173 | - key: idx.toString(), | |
| 174 | - style: { | |
| 175 | - marginLeft: 8 | |
| 176 | - }, | |
| 177 | - size: "small", | |
| 178 | - onClick: onClick | |
| 179 | - }, /*#__PURE__*/_react.default.createElement("span", { | |
| 180 | - dangerouslySetInnerHTML: { | |
| 181 | - __html: html || text | |
| 182 | - } | |
| 183 | - })); | |
| 184 | - }) : null); | |
| 185 | - } | |
| 186 | - }); | |
| 187 | - } | |
| 188 | - | |
| 189 | - return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", { | |
| 190 | - className: "w-100 mb2 tr" | |
| 191 | - }, !props.hideAdd && /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 192 | - type: "primary", | |
| 193 | - size: "small", | |
| 194 | - onClick: addItem | |
| 195 | - }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) { | |
| 196 | - var callback = item.callback, | |
| 197 | - text = item.text, | |
| 198 | - html = item.html; | |
| 199 | - | |
| 200 | - var onClick = function onClick() {}; | |
| 201 | - | |
| 202 | - if (typeof window[callback] === 'function') { | |
| 203 | - onClick = function onClick() { | |
| 204 | - window[callback]({ | |
| 205 | - value: listData, | |
| 206 | - onChange: changeList, | |
| 207 | - schema: schema | |
| 208 | - }); | |
| 209 | - }; | |
| 210 | - } | |
| 211 | - | |
| 212 | - return /*#__PURE__*/_react.default.createElement(_button.default, { | |
| 213 | - key: idx.toString(), | |
| 214 | - style: { | |
| 215 | - marginLeft: 8 | |
| 216 | - }, | |
| 217 | - size: "small", | |
| 218 | - onClick: onClick | |
| 219 | - }, /*#__PURE__*/_react.default.createElement("span", { | |
| 220 | - dangerouslySetInnerHTML: { | |
| 221 | - __html: html || text | |
| 222 | - } | |
| 223 | - })); | |
| 224 | - }) : null), /*#__PURE__*/_react.default.createElement(_table.default, _extends({ | |
| 225 | - rowKey: "index", | |
| 226 | - scroll: { | |
| 227 | - y: scrollY, | |
| 228 | - x: 'max-content' | |
| 229 | - }, | |
| 230 | - components: vt, | |
| 231 | - size: "small", | |
| 232 | - columns: columns, | |
| 233 | - dataSource: dataSource, | |
| 234 | - pagination: false | |
| 235 | - }, rest))); | |
| 236 | -}; | |
| 237 | - | |
| 238 | -var _default = VirtualList; | |
| 239 | -exports.default = _default; | |
| \ No newline at end of file |
| ... | ... | @@ -13,6 +13,8 @@ var _collapse = _interopRequireDefault(require("antd/es/collapse")); |
| 13 | 13 | |
| 14 | 14 | var _react = _interopRequireWildcard(require("react")); |
| 15 | 15 | |
| 16 | +var _hooks = require("../../hooks"); | |
| 17 | + | |
| 16 | 18 | var _excluded = ["children", "title"]; |
| 17 | 19 | |
| 18 | 20 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } |
| ... | ... | @@ -37,7 +39,6 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return |
| 37 | 39 | |
| 38 | 40 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } |
| 39 | 41 | |
| 40 | -// import { useStore2 } from '../../hooks'; | |
| 41 | 42 | var Panel = _collapse.default.Panel; |
| 42 | 43 | |
| 43 | 44 | function Map(_ref) { |
| ... | ... | @@ -45,10 +46,10 @@ function Map(_ref) { |
| 45 | 46 | title = _ref.title, |
| 46 | 47 | rest = _objectWithoutProperties(_ref, _excluded); |
| 47 | 48 | |
| 48 | - var _ref2 = {}, | |
| 49 | - theme = _ref2.theme, | |
| 50 | - displayType = _ref2.displayType, | |
| 51 | - allCollapsed = _ref2.allCollapsed; // TODO! | |
| 49 | + var _useStore = (0, _hooks.useStore2)(), | |
| 50 | + theme = _useStore.theme, | |
| 51 | + displayType = _useStore.displayType, | |
| 52 | + allCollapsed = _useStore.allCollapsed; | |
| 52 | 53 | |
| 53 | 54 | var _useState = (0, _react.useState)(false), |
| 54 | 55 | _useState2 = _slicedToArray(_useState, 2), | ... | ... |
| ... | ... | @@ -11,9 +11,9 @@ var _select = _interopRequireDefault(require("antd/es/select")); |
| 11 | 11 | |
| 12 | 12 | var _react = _interopRequireDefault(require("react")); |
| 13 | 13 | |
| 14 | -var _utils = require("../../utils"); | |
| 14 | +var _createWidget = require("../../createWidget"); | |
| 15 | 15 | |
| 16 | -var _excluded = ["schema", "style", "options"]; | |
| 16 | +var _utils = require("../../utils"); | |
| 17 | 17 | |
| 18 | 18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 19 | 19 | |
| ... | ... | @@ -23,16 +23,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va |
| 23 | 23 | |
| 24 | 24 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } |
| 25 | 25 | |
| 26 | -function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
| 27 | - | |
| 28 | -function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
| 29 | - | |
| 30 | -var FrSelect = function FrSelect(_ref) { | |
| 26 | +var mapProps = function mapProps(_ref) { | |
| 31 | 27 | var schema = _ref.schema, |
| 32 | 28 | style = _ref.style, |
| 33 | - _options = _ref.options, | |
| 34 | - rest = _objectWithoutProperties(_ref, _excluded); | |
| 35 | - | |
| 29 | + _options = _ref.options; | |
| 36 | 30 | var options; // 如果已经有外部注入的options了,内部的schema就会被忽略 |
| 37 | 31 | |
| 38 | 32 | if (_options && Array.isArray(_options)) { |
| ... | ... | @@ -61,15 +55,14 @@ var FrSelect = function FrSelect(_ref) { |
| 61 | 55 | }); |
| 62 | 56 | } |
| 63 | 57 | |
| 64 | - var finalProps = _objectSpread({ | |
| 58 | + return { | |
| 65 | 59 | options: options, |
| 66 | 60 | style: _objectSpread({ |
| 67 | 61 | width: '100%' |
| 68 | 62 | }, style) |
| 69 | - }, rest); | |
| 70 | - | |
| 71 | - return /*#__PURE__*/_react.default.createElement(_select.default, finalProps); | |
| 63 | + }; | |
| 72 | 64 | }; |
| 73 | 65 | |
| 74 | -var _default = FrSelect; | |
| 66 | +var Component = (0, _createWidget.createWidget)(mapProps)(_select.default); | |
| 67 | +var _default = Component; | |
| 75 | 68 | exports.default = _default; |
| \ No newline at end of file | ... | ... |
| 1 | 1 | { |
| 2 | 2 | "name": "form-render", |
| 3 | - "version": "v1.8.4-beta.2", | |
| 3 | + "version": "1.7.3", | |
| 4 | 4 | "description": "通过 JSON Schema 生成标准 Form,常用于自定义搭建配置界面生成", |
| 5 | 5 | "repository": { |
| 6 | 6 | "type": "git", |
| ... | ... | @@ -22,6 +22,13 @@ |
| 22 | 22 | } |
| 23 | 23 | ], |
| 24 | 24 | "scripts": { |
| 25 | + "build": "father-build", | |
| 26 | + "prepare": "npm run build", | |
| 27 | + "postpublish": "git push --tags", | |
| 28 | + "beta": "npm publish --tag beta", | |
| 29 | + "prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"", | |
| 30 | + "test": "umi-test", | |
| 31 | + "test:coverage": "umi-test --coverage" | |
| 25 | 32 | }, |
| 26 | 33 | "main": "lib/index.js", |
| 27 | 34 | "module": "es/index.js", | ... | ... |