UNPKG

@flatbiz/antd

Version:
148 lines (144 loc) 5.9 kB
/*! @flatjs/forge MIT @flatbiz/antd */ import _RedoOutlined from '@ant-design/icons/es/icons/RedoOutlined.js'; import { _ as _objectWithoutProperties, a as _slicedToArray, b as _objectSpread2 } from './_rollupPluginBabelHelpers-BspM60Sw.js'; import { isArray } from '@dimjs/lang/is-array'; import { get } from '@dimjs/utils/get/get'; import { useState, useMemo } from 'react'; import { Select, Button } from 'antd'; import { fbaHooks } from './fba-hooks/index.js'; import { R as RequestStatus } from './request-status-Cjtei7Bl.js'; import { jsx } from 'react/jsx-runtime'; var _excluded = ["showAllOption", "serviceConfig", "onSelectorListChange", "onSelectorRequestError", "requestMessageConfig", "fieldNames"]; /** * 选择器简单包装组件 * @param props * @returns * ``` * 1. 不支持search效果 * 2. 不会缓存接口数据 * 3. 不会对value、onChange做任何处理 * ``` */ var SelectorWrapperSimple = function SelectorWrapperSimple(props) { var showAllOption = props.showAllOption, serviceConfig = props.serviceConfig, onSelectorListChange = props.onSelectorListChange, onSelectorRequestError = props.onSelectorRequestError, requestMessageConfig = props.requestMessageConfig, fieldNames = props.fieldNames, otherProps = _objectWithoutProperties(props, _excluded); var _useState = useState('request-init'), _useState2 = _slicedToArray(_useState, 2), requestStatus = _useState2[0], setRequestStatus = _useState2[1]; var _useState3 = useState(), _useState4 = _slicedToArray(_useState3, 2), dataSource = _useState4[0], setDataSource = _useState4[1]; var optionsItemLabelField = (fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.label) || 'label'; var optionsItemValueField = (fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.value) || 'value'; var optionsItemDisabledField = (fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.disabled) || 'disabled'; var serviceParams = serviceConfig.params; var allOptionConfig = useMemo(function () { var isTrue = showAllOption === true; if (showAllOption) { return { label: isTrue ? '全部' : showAllOption.label, value: isTrue ? '' : showAllOption.value }; } return null; }, [showAllOption]); var serviceRespDataAdapter = function serviceRespDataAdapter(respData) { var result = respData; if (serviceConfig.onRequestResultAdapter) { result = serviceConfig.onRequestResultAdapter(respData); } else if (fieldNames !== null && fieldNames !== void 0 && fieldNames.list) { result = get(respData, fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.list); } if (result && !isArray(result)) { console.warn('待渲染数据为非数组结构', result); return []; } return result || []; }; var startDataSourceRequest = function startDataSourceRequest() { return new Promise(function ($return, $error) { var _serviceConfig$onRequ, respData, respAdapterData; var $Try_1_Post = function () { try { return $return(); } catch ($boundEx) { return $error($boundEx); } }; var $Try_1_Catch = function (error) { try { setRequestStatus('request-error'); onSelectorRequestError === null || onSelectorRequestError === void 0 || onSelectorRequestError(error); return $Try_1_Post(); } catch ($boundEx) { return $error($boundEx); } }; try { setRequestStatus('request-progress'); return Promise.resolve((_serviceConfig$onRequ = serviceConfig.onRequest) === null || _serviceConfig$onRequ === void 0 ? void 0 : _serviceConfig$onRequ.call(serviceConfig, serviceParams)).then(function ($await_2) { try { respData = $await_2; respAdapterData = serviceRespDataAdapter(respData); onSelectorListChange === null || onSelectorListChange === void 0 || onSelectorListChange(respAdapterData); if (optionsItemLabelField && optionsItemValueField) { respAdapterData = respAdapterData.map(function (item) { return _objectSpread2(_objectSpread2({}, item), {}, { label: item[optionsItemLabelField], value: item[optionsItemValueField], disabled: item[optionsItemDisabledField] }); }); } if (allOptionConfig) { respAdapterData.unshift(allOptionConfig); } setDataSource(respAdapterData); setRequestStatus('request-success'); return $Try_1_Post(); } catch ($boundEx) { return $Try_1_Catch($boundEx); } }, $Try_1_Catch); } catch (error) { $Try_1_Catch(error); } }); }; fbaHooks.useEffectCustom(function () { void startDataSourceRequest(); }, []); var loading = requestStatus === 'request-progress'; return /*#__PURE__*/jsx(Select, _objectSpread2(_objectSpread2({ optionFilterProp: "label" }, otherProps), {}, { style: _objectSpread2({ width: '100%' }, props.style), options: dataSource, notFoundContent: /*#__PURE__*/jsx(RequestStatus, { status: requestStatus, loading: loading, messageConfig: requestMessageConfig, errorButton: /*#__PURE__*/jsx(Button, { type: "primary", onClick: startDataSourceRequest, children: "\u91CD\u65B0\u83B7\u53D6\u6570\u636E" }) }), suffixIcon: requestStatus === 'request-error' ? /*#__PURE__*/jsx(_RedoOutlined, { spin: loading, onClick: startDataSourceRequest }) : undefined, loading: loading })); }; export { SelectorWrapperSimple as S }; //# sourceMappingURL=selector-wrapper-simple-DYfXLpbR.js.map