@flatbiz/antd
Version:
168 lines (164 loc) • 6.85 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import _RedoOutlined from '@ant-design/icons/es/icons/RedoOutlined.js';
import { hooks } from '@wove/react/hooks';
import { _ as _objectWithoutProperties, a as _slicedToArray, b as _objectSpread2, i as _defineProperty } from './_rollupPluginBabelHelpers-BYm17lo8.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-BNGBHA33.js';
import { jsx } from 'react/jsx-runtime';
var _excluded = ["showAllOption", "serviceConfig", "onSelectorListChange", "onSelectorRequestError", "requestMessageConfig", "fieldNames", "onLabelRenderAdapter"];
/**
* 选择器简单包装组件
* @param props
* @returns
* ```
* 1. 只支持 search + 服务 功能
* 2. 不会缓存接口数据
* 3. 不会对value、onChange做任何处理
* ```
*/
var SelectorWrapperSearch = function SelectorWrapperSearch(props) {
var showAllOption = props.showAllOption,
serviceConfig = props.serviceConfig,
onSelectorListChange = props.onSelectorListChange,
onSelectorRequestError = props.onSelectorRequestError,
requestMessageConfig = props.requestMessageConfig,
fieldNames = props.fieldNames,
onLabelRenderAdapter = props.onLabelRenderAdapter,
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 searchKeyword = (fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.searchKeyword) || 'searchKeyword';
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(keyword) {
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, _objectSpread2(_objectSpread2({}, serviceParams), {}, _defineProperty({}, searchKeyword, keyword)))).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 || 'disabled']
});
});
}
respAdapterData.forEach(function (item) {
if (onLabelRenderAdapter) {
item.label = onLabelRenderAdapter(item);
}
});
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);
}
});
};
var onSearch = hooks.useDebounceCallback(function (value) {
setDataSource([]);
void startDataSourceRequest(value);
}, 300);
fbaHooks.useEffectCustom(function () {
if (props.customKeyword) {
void startDataSourceRequest(props.customKeyword);
}
}, [props.customKeyword]);
var loading = requestStatus === 'request-progress';
return /*#__PURE__*/jsx(Select, _objectSpread2(_objectSpread2({
showSearch: true,
allowClear: true,
dropdownMatchSelectWidth: false
}, otherProps), {}, {
style: _objectSpread2({
width: '100%'
}, props.style),
options: dataSource,
filterOption: false,
onSearch: onSearch,
notFoundContent: /*#__PURE__*/jsx(RequestStatus, {
status: requestStatus,
loading: loading,
messageConfig: _objectSpread2({
'request-init': '请输入搜索条件'
}, 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 { SelectorWrapperSearch as S };
//# sourceMappingURL=selector-wrapper-search-SSgguTqk.js.map