knk
Version:
react components based on react
121 lines (119 loc) • 3.76 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/* eslint-disable consistent-return */
/* eslint-disable func-names */
/**
* 异步选择组件
*/
import React, { useState, useEffect, forwardRef } from 'react';
import PropTypes from 'prop-types';
import { Spin, message, Select } from 'antd';
import { fetch } from '../../../common/api';
import { log } from '../../../common/tool';
var Option = Select.Option;
// 封装组件
var AjaxSelect = /*#__PURE__*/forwardRef(function (props, 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();
}, []);
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) {
setLoading(false);
setListData(data.data);
}).catch(function (err) {
log.error(err);
message.error("\u83B7\u53D6".concat(listName, "\u5931\u8D25:").concat(err.message));
});
};
var handleChange = function handleChange(key) {
var value = props.value,
listValueKey = props.listValueKey,
listNameKey = props.listNameKey,
onChange = props.onChange;
if (Array.isArray(value)) {
if (!key) return onChange(['', '']);
var label;
listData.some(function (item) {
if (item[listValueKey].toString() === key) {
label = item[listNameKey];
return true;
}
return false;
});
onChange([key, label]);
} else {
onChange(key || '');
}
};
var value = props.value,
listName = props.listName,
listValueKey = props.listValueKey,
listNameKey = props.listNameKey,
disabled = props.disabled;
var selectValue;
if (Array.isArray(value)) {
// eslint-disable-next-line prefer-destructuring
selectValue = value[0];
} else {
selectValue = value;
}
return /*#__PURE__*/React.createElement(Spin, {
spinning: loading,
className: "app-ajax-select"
}, /*#__PURE__*/React.createElement(Select, {
showSearch: true,
optionFilterProp: "children",
value: selectValue,
onChange: handleChange,
placeholder: "\u9009\u62E9".concat(listName),
notFoundContent: "\u672A\u627E\u5230".concat(listName),
allowClear: true,
disabled: disabled,
ref: ref
}, /*#__PURE__*/React.createElement(Option, {
value: "",
title: "\u9009\u62E9".concat(listName)
}, "\u9009\u62E9", listName), listData.map(function (item, index) {
return /*#__PURE__*/React.createElement(Option, {
key: index,
value: item[listValueKey].toString(),
title: item[listNameKey]
}, item[listNameKey]);
})));
});
AjaxSelect.propTypes = {
apiPath: PropTypes.string.isRequired,
// api接口,由于直接诶读取data.data的数组数据,所以该API不能使用分页
apiData: PropTypes.object,
// api请求附加的数据
apiMethod: PropTypes.string,
// api请求method
listName: PropTypes.string.isRequired,
// 缺省列表名称
listValueKey: PropTypes.string.isRequired,
// 列表值的key值
listNameKey: PropTypes.string.isRequired,
// 列表名称key值
value: PropTypes.oneOfType([
// 值,支持字符串和数组形式
PropTypes.string,
// key值
PropTypes.array // [key, label]形式
]),
onChange: PropTypes.func,
// 获取值变化函数
disabled: PropTypes.bool
};
export default AjaxSelect;