UNPKG

@aliretail/react-materials-components

Version:
98 lines (75 loc) 2.79 kB
import _Select from "@alifd/next/es/select"; import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; var _excluded = ["value", "onChange", "requestConfig"], _excluded2 = ["service", "searchFormat"]; import * as React from 'react'; import { useRequest } from 'ahooks'; import request from '@aliretail/react-universal-request'; function HookSelect(props) { var value = props.value, onChange = props.onChange, requestConfig = props.requestConfig, otherProps = _objectWithoutPropertiesLoose(props, _excluded); var service = requestConfig.service, searchFormat = requestConfig.searchFormat, otherConfig = _objectWithoutPropertiesLoose(requestConfig, _excluded2); var insertSelectProps = {}; var getService = function getService(inputValue, actionType) { if (actionType === void 0) { actionType = 'inputChange'; } var _service = service; if (requestConfig !== null && requestConfig !== void 0 && requestConfig.searchFormat) { var searchValue = searchFormat(inputValue, actionType); Object.assign(_service, searchValue); } return _service; }; var _useRequest = useRequest(function (opt) { return request(opt || getService(value)); }, otherConfig), run = _useRequest.run, error = _useRequest.error, data = _useRequest.data, loading = _useRequest.loading; // 获取 Select 状态 var state; if (error) { state = 'error'; } if (loading) { state = 'loading'; } // onSearch 自动包装 if (otherProps.showSearch && !otherProps.onSearch && otherProps.filterLocal === false) { var onSearchFunc = function onSearchFunc(inputValue, actionType) { run(getService(inputValue, actionType)); }; insertSelectProps.onSearch = function (inputValue) { return onSearchFunc(inputValue, 'inputChange'); }; insertSelectProps.onSearchClear = function (actionType) { return onSearchFunc('', actionType); }; } // 如果传入的 props 中有 onSearch if (otherProps.onSearch) { otherProps.onSearch = function (inputValue) { return otherProps.onSearch(inputValue, 'inputChange'); }; otherProps.onSearchClear = function (actionType) { return otherProps.onSearch('', actionType); }; } var getDataSource = function getDataSource() { if ((data === null || data === void 0 ? void 0 : data.success) === true) { return data.result; } return data; }; return /*#__PURE__*/React.createElement(_Select, _extends({ value: value, onChange: onChange, state: state, dataSource: getDataSource() }, insertSelectProps, otherProps)); } export default HookSelect;