import type { Result as AhooksResult, Options, Service } from 'ahooks/lib/useRequest/src/types'; import { Popconfirm } from 'antd'; import React from 'react'; /** 请求结果类型,扩展 ahooks 的 Result */ type RequestResult = AhooksResult & { /** 请求是否成功 */ success: boolean; }; /** withRequest 配置参数 */ export type WithRequestConfig = { /** * 确认方式 * - false: 不确认,点击直接调用接口 * - true: Modal 弹框确认 * - 'popconfirm': 气泡确认框 */ confirm?: boolean | 'popconfirm'; /** 确认提示文本 */ confirmText?: React.ReactNode; /** 气泡确认框属性 */ popconfirmProps?: Partial>; /** 气泡确认框描述 */ description?: React.ReactNode; /** 是否绑定点击事件触发请求 */ clickable?: boolean; /** 请求成功回调 */ onSuccess?: (data?: TData, params?: TParams) => void; /** 请求失败回调 */ onFail?: (error: any) => void; }; /** 请求实例 ref 类型 */ export type WithRequestRef = RequestResult; /** 请求配置 */ export type RequestConfig = { /** 请求函数 */ service?: Service; /** useRequest 配置项 */ options?: Options; /** 格式化 props */ format?: (request: RequestResult, props: Props) => Props; }; /** 包装后组件的额外 Props */ export type WithRequestProps = WithRequestConfig & { /** 获取请求实例的 ref */ requestRef?: React.Ref>; /** 请求配置 */ request?: RequestConfig; }; /** * withRequest 高阶组件 * 为任意组件添加请求能力,支持确认框、加载状态等功能 * * @example * ```tsx * const RequestButton = withRequest({ * clickable: true, * confirm: true, * confirmText: '确定执行吗?', * })(Button); * * fetch('/api/action') }} * onSuccess={(res) => console.log('成功', res)} * > * 点击执行 * * ``` */ declare function withRequest(defaultConfig?: WithRequestConfig): >(WrappedComponent: React.ComponentType) => { ({ request: requestConfig, requestRef, onSuccess, onFail, confirm, confirmText, description, popconfirmProps, clickable, ...restProps }: Props & WithRequestConfig & { /** 获取请求实例的 ref */ requestRef?: React.Ref> | undefined; /** 请求配置 */ request?: RequestConfig | undefined; }): React.JSX.Element; displayName: string; }; export { withRequest };