UNPKG

knk

Version:

react components based on react

133 lines (132 loc) 4.22 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; /** * 动态数据异步选择组件 */ import React, { useState, useEffect } from 'react'; import { Spin, message, Select } from 'antd'; import { fetch } from '../../../common/api'; import { log, isObjEmpty, isEqual } from '../../../common/tool'; var Option = Select.Option; // 封装组件 var DynamicAjaxSelect = /*#__PURE__*/React.forwardRef(function (props, ref) { var isFirst = React.useRef(true); var prevApiData = React.useRef(props.apiData); 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 () { var result = isEqual(prevApiData.current, props.apiData); if (isFirst.current && props.firstShowAll) { // 首次加载获取数据 handleGetList(); } if (!result) { // 参数不一样才重新获取数据 handleGetList(); } return function () { isFirst.current = false; prevApiData.current = props.apiData; }; }, [props.apiData]); var handleGetList = function handleGetList() { var apiPath = props.apiPath, apiData = props.apiData, apiMethod = props.apiMethod, listName = props.listName, onChange = props.onChange, value = props.value; // 当为空时,不显示 if (!isFirst.current && isObjEmpty(apiData)) { // 重置选择 if (Array.isArray(value)) { onChange([]); } else if (value) onChange({}); // 重置数据 setListData([]); return; } setLoading(true); fetch(apiPath, apiData, apiMethod).then(function (data) { setListData(data.data); }).catch(function (err) { log.error(err); message.error("\u83B7\u53D6".concat(listName, "\u5931\u8D25:").concat(err.message)); }).finally(function () { setLoading(false); }); }; 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 { var groupId; listData.some(function (item) { if (item[listValueKey].toString() === key || item[listValueKey].toString() === value) { groupId = item.chanceGroup.toString(); return true; } return false; }); onChange(key ? _defineProperty({ groupId: groupId }, listValueKey, key) : _defineProperty({ groupId: groupId }, listValueKey, null)); } }; var value = props.value, listName = props.listName, listValueKey = props.listValueKey, listNameKey = props.listNameKey, disabled = props.disabled; var selectValue; if (Array.isArray(value)) { selectValue = value[0]; } else { selectValue = value; } return /*#__PURE__*/React.createElement(Spin, { spinning: loading, className: "app-dynamic-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]); }))); }); export default DynamicAjaxSelect;