import type { ModalBuiltinKey } from '../constants/modalBuiltinKeys'; export type ModalAction = 'end' | 'cancel'; export type { ModalBuiltinKey }; /** 内置弹层类型 */ export type BuiltinModalType = 'modal' | 'drawer' | 'popover'; /** 与 {@link BuiltinModalType} 相同,保留兼容 */ export type ModalType = BuiltinModalType; /** * 用户可通过 module augmentation 扩展的弹层类型注册表。 * @example * declare module 'use-modal-ref' { * interface ModalTypeRegistry { * sidePanel: ModalTypeItem; * } * } */ // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ModalTypeRegistry {} /** 内置 + 用户扩展的弹层类型 */ export type ExtendedModalType = BuiltinModalType | Extract; export type ModalTypeItem = { visible: string, onClose: string, }; /** 内置弹层类型的默认字段映射(字面量,用于 props 类型推导) */ export type BuiltinModalTypeMap = { modal: { readonly visible: 'visible', readonly onClose: 'onCancel' }, drawer: { readonly visible: 'visible', readonly onClose: 'onClose' }, popover: { readonly visible: 'visible', readonly onClose: 'onClose' }, }; /** 内置映射 + 用户扩展映射(扩展项运行时通过 mergeModalType 注册,编译期可选) */ export type MergeableModalTypeMap = BuiltinModalTypeMap & { [K in keyof ModalTypeRegistry]?: ModalTypeRegistry[K]; }; /** mergeModalType 入参:允许覆盖内置类型的字段映射为任意字符串 */ export type MergeModalTypeInput = Partial<{ [K in keyof MergeableModalTypeMap]: ModalTypeItem; }>; export type ModalTypeMap = { // eslint-disable-next-line no-unused-vars [key in BuiltinModalType]: ModalTypeItem } & ModalTypeRegistry; type EndModalMethod = (result?: U, onDone?: () => void) => Promise; type CancelModalMethod = (ex?: R, onDone?: () => void) => Promise; type BuildModalPropsFromItem = { readonly [K in Item['visible']]: boolean; } & { readonly [K in Item['onClose']]: CancelModalMethod; }; /** 将显隐字段名 From 替换为 To(用于 visibleKey 泛型) */ type ReplaceVisibleProp< Props, From extends string, To extends string, > = Omit & Record; type RegistryModalTypeItem

= ModalTypeRegistry[P] extends ModalTypeItem ? ModalTypeRegistry[P] : ModalTypeItem; type PropsFromTypeItem< Item extends ModalTypeItem, VK extends string | undefined = undefined, > = VK extends string ? ReplaceVisibleProp< BuildModalPropsFromItem, Item['visible'], VK > : BuildModalPropsFromItem; /** modal.props 类型:P 决定默认字段名,VK 覆盖显隐字段名 */ export type ModalRefProps< P extends ExtendedModalType, VK extends string | undefined = undefined, > = P extends BuiltinModalType ? PropsFromTypeItem : P extends keyof ModalTypeRegistry ? PropsFromTypeItem, VK> : Record; /** @deprecated 请优先使用 ModalRefProps

;保留以兼容旧引用 */ export type ModalPropsTypeMap = { [P in BuiltinModalType]: ModalRefProps

; }; export type ModalBeforeCloseHandler = ( next: (ok: any) => void, action: ModalAction, ) => void | Promise; /** 结束关闭前钩子:可改写 endModal / resolve 的最终返回值 */ export type ModalBeforeEndHandler = ( value?: U, ) => Promise; /** 取消关闭前钩子:可改写 cancel 原因 / reject 值 */ export type ModalBeforeCancelHandler = ( reason?: R, ) => Promise; export type ModalModalOptions = { modalDataEvent?: boolean, afterModal?: (newData: any, options?: ModalModalOptions) => void, beforeCloseModal?: ModalBeforeCloseHandler, beforeEndModal?: ModalBeforeEndHandler, beforeCancelModal?: ModalBeforeCancelHandler, afterCloseModal?: ( newData: any, action: ModalAction, modal: ModalRef ) => void | Promise; forceVisible?: boolean, alwaysResolve?: boolean, [key: string]: any }; /** 从 custom 泛型中排除 modal 内置属性键 */ export type ModalCustomMap> = { [K in keyof C as K extends ModalBuiltinKey ? never : K]: C[K]; }; /** 将 Hook options 泛型 O 规范为可用于类型推断的对象形状(默认宽 ModalRefOption 时不做字面量推断) */ export type ResolveHookOptions = ModalRefOption extends O ? {} : O; /** 从 options.custom 字面量推断 custom 形状(忽略宽类型与 undefined) */ export type ExtractCustomMap = O extends { custom: infer Raw extends Record } ? undefined extends Raw ? {} : string extends keyof Raw ? {} : ModalCustomMap : {}; /** 合并显式 C 泛型与 options.custom 推断 */ export type ResolveCustomMap< C extends Record, O, > = keyof ExtractCustomMap extends never ? ModalCustomMap : ExtractCustomMap; /** 从 options.visibleKey 字面量推断 VK(忽略宽 string 类型) */ type IsLiteralString = string extends S ? false : S extends string ? true : false; export type ExtractVisibleKey = O extends { visibleKey: infer VK } ? IsLiteralString extends true ? VK : undefined : undefined; /** 合并显式 VK 泛型与 options.visibleKey 推断 */ export type ResolveVisibleKey< VK extends string | undefined, O, > = ExtractVisibleKey extends infer Inferred ? [Inferred] extends [undefined] ? VK : Inferred extends string ? Inferred : VK : VK; export type ModalRefOption< P extends ExtendedModalType, T extends Record, U, C extends Record = {}, R = unknown, > = { useImperativeHandle?: boolean, alwaysResolve?: boolean, /** 覆盖 modalTypeMap 默认显隐字段名,生成 modal.props.[visibleKey](如 Ant Design 5 Drawer 使用 open) */ visibleKey?: string, /** 自定义属性,不可使用 modal 内置属性名(如 modal、visible、endModal 等) */ custom?: ModalCustomMap, /** 打开前钩子(仅通过 options 配置,不暴露在 modal 公开 API 上) */ beforeModal?: ( newData: Partial, pause: (result: any, isError?: boolean) => void, options: Record ) => void | Partial | Promise>; afterModal?: (newData: any, options?: ModalModalOptions) => void | Promise; init?: (newData: T, options: Record) => void | Promise; beforeCloseModal?: ( next: (ok: any) => void, action: ModalAction, modal: ModalRef, ) => void | Promise; afterCloseModal?: ( newData: T, action: ModalAction, modal: ModalRef, ) => void | Promise; [key: string]: any }; /** modal.data 只读数据形状 */ export type ModalRefData> = Partial> & { [key: string]: any }; export type ModalRef< P extends ExtendedModalType = 'modal', T extends Record = Record, U = any, C extends Record = {}, VK extends string | undefined = undefined, R = unknown, > = { readonly visible: boolean; readonly data: ModalRefData; readonly props: ModalRefProps; readonly options: ModalRefOption; readonly modalOptions: ModalModalOptions; readonly modalPromise: Promise | null; modal(newData?: Partial, options?: ModalModalOptions): Promise; endModal: EndModalMethod; cancelModal: CancelModalMethod; } & ModalCustomMap; /** useCommonRef / useModalRef / useDrawerRef 返回值 */ export type UseCommonRefReturn< P extends ExtendedModalType, T extends Record, U = any, C extends Record = {}, VK extends string | undefined = undefined, R = unknown, > = { modal: ModalRef; data: T; setData: (newData: T | ((data: T) => T)) => void; }; export interface ModalResult

> { modal: ModalRef, data: T, } export interface ModalData extends Partial { onOK?: (data: any) => any; onCancel?: (data: any) => any; [key: string]: any; } export interface ModalVisibleProps { pending?: Promise, resolve: (value: any) => any, reject: (value: any) => any } /** 允许按字符串动态访问方法的 modal ref(命令式 API 使用) */ export type ModalRefDynamic = ModalRef & Record; export type { EndModalMethod, CancelModalMethod };