sunmao-sdk
Version:
榫卯-开箱即用赋能-sdk
121 lines (99 loc) • 5.81 kB
JavaScript
;
import "antd/es/checkbox/style";
import _Checkbox from "antd/es/checkbox";
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); }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
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."); }
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); }
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; }
function _iterableToArrayLimit(arr, i) { var _i = arr && (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; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useEffect } from "react";
import * as commonUtils from "../../utils/commonUtils";
import * as formUtils from "../../utils/formUtils";
var Group = _Checkbox.Group;
/**
* commonProps 独立配置
* showSearch : 是否可输入搜索,默认否 openSearch
* requestFunc : 外部传进网络请求方法,只能接受返回数组[] ,切记
* url : 如需网络获取选择列表,需提供url
* params : 网络请求参数
*
* list : 默认选择列表
* key : 对象key对应字段,即返回的value
* text : 对象name对应字段,即显示的name
* showName : 对象name对应字段,即显示的name
* specialReturn : 特殊返回函数,进行特殊处理,如不止需要返回选中 value时,可通过此方法返回,返回为数组或对象,结合antd的API,自行注意
*
*
* uiOptions 为antd中Select自带属性,参考antd中API
*
* bs:func 传值,不能被转json后
*/
var displayName = "CheckboxCommon";
var CheckboxCommon = function CheckboxCommon(props) {
var onChange = props.onChange,
name = props.name,
value = props.value,
_props$schema$commonP = props.schema.commonProps,
commonProps = _props$schema$commonP === void 0 ? {} : _props$schema$commonP,
uiOptions = props.options;
var requestFunc = commonProps.requestFunc,
url = commonProps.url,
params = commonProps.params,
list = commonProps.list,
_commonProps$key = commonProps.key,
key = _commonProps$key === void 0 ? "key" : _commonProps$key,
_commonProps$text = commonProps.text,
text = _commonProps$text === void 0 ? "preName" : _commonProps$text,
okPath = commonProps.okPath;
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
set = _useState2[1];
useEffect(function () {
if (list) setList(list); // 需要网络请求
else if (url && requestFunc) {
post("");
}
}, []);
var post = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
// 组装请求参数
requestFunc(url, params, okPath).then(function (req) {
setList(commonUtils.getFirstList(req));
});
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function post() {
return _ref.apply(this, arguments);
};
}();
var setList = function setList(data) {
var list = formUtils.getCPDicList(data, key, text);
list && set(list);
};
var handleChange = function handleChange(value) {
onChange(name, (value === null || value === void 0 ? void 0 : value.length) ? value : null);
};
return state && /*#__PURE__*/React.createElement(Group, _extends({
style: {
width: "100%"
},
value: Array.isArray(value) ? value : [],
options: state,
onChange: handleChange
}, uiOptions));
};
export default CheckboxCommon;