knk
Version:
react components based on react
109 lines • 3.54 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/**
* 异步选择组件(多选)
*/
import React, { useState, useEffect, forwardRef } from 'react';
import { Spin, message, Select } from 'antd';
import { fetch } from '../../../common/api';
import { log } from '../../../common/tool';
import PropTypes from 'prop-types';
var Option = Select.Option;
var AjaxSelectMulit = /*#__PURE__*/forwardRef(function (props, ref) {
var isOpen = React.useRef(true);
var jaxSelectMulitRef = React.useRef();
jaxSelectMulitRef = ref;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
loading = _useState2[0],
setLoading = _useState2[1];
var _useState3 = useState([]),
_useState4 = _slicedToArray(_useState3, 2),
listData = _useState4[0],
setListData = _useState4[1];
useEffect(function () {
handleGetList();
return function () {
isOpen.current = false;
};
}, []);
var handleGetList = function handleGetList() {
var apiPath = props.apiPath,
apiData = props.apiData,
apiMethod = props.apiMethod,
listName = props.listName;
setLoading(true);
fetch(apiPath, apiData, apiMethod).then(function (data) {
// 判断如果卸载,则不再设置数据
if (!isOpen.current) return;
setLoading(false);
setListData(data.data);
}).catch(function (err) {
log.error(err);
setLoading(false);
message.error("\u83B7\u53D6".concat(listName, "\u5931\u8D25"));
});
};
var handleChange = function handleChange(value) {
var onChange = props.onChange,
listValueKey = props.listValueKey,
listNameKey = props.listNameKey;
value.map(function (item) {
item[listValueKey] = item.key;
item[listNameKey] = item.label;
delete item.key;
delete item.label;
});
onChange(value);
};
var value = props.value;
var listName = props.listName,
listValueKey = props.listValueKey,
listNameKey = props.listNameKey,
disabled = props.disabled;
value = value && Array.isArray(value) ? value : [];
value = listData && listData.length ? value.map(function (item) {
var target = listData.find(function (listItem) {
return listItem[listValueKey] === item[listValueKey];
});
return target && {
key: "".concat(target[listValueKey]),
label: target[listNameKey]
};
}).filter(function (item) {
return item != null;
}) : [];
return /*#__PURE__*/React.createElement(Spin, {
spinning: loading,
className: "app-ajax-select-multi"
}, /*#__PURE__*/React.createElement(Select, {
ref: jaxSelectMulitRef,
mode: "multiple",
showSearch: true,
allowClear: true,
value: value,
onChange: handleChange,
placeholder: "\u9009\u62E9".concat(listName),
optionFilterProp: "children",
labelInValue: true,
notFoundContent: "\u672A\u627E\u5230".concat(listName),
disabled: disabled
}, listData.map(function (item, index) {
return /*#__PURE__*/React.createElement(Option, {
key: index,
value: item[listValueKey] ? item[listValueKey].toString() : '',
title: item[listNameKey]
}, item[listNameKey]);
})));
});
AjaxSelectMulit.propTypes = {
apiPath: PropTypes.string,
apiData: PropTypes.object,
value: PropTypes.array,
apiMethod: PropTypes.string,
listName: PropTypes.string,
listNameKey: PropTypes.string,
listValueKey: PropTypes.string,
onChange: PropTypes.func,
disabled: PropTypes.bool
};
export default AjaxSelectMulit;