Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import { Doraemon } from '@doraemon-ui/miniprogram.core-js' import { getCurrentPage, findComponentNode, type NativeButtonProps, type PresetColor, type DefaultButtonHandle, type NativeButtonHandle, type MPInst, } from '@doraemon-ui/miniprogram.shared' const { getCurrentInstance } = Doraemon.util /** * 操作按钮的类型 * * @export * @interface DialogButton */ export type DialogButton = Omit< NativeButtonProps, 'size' | 'type'| 'plain' | 'loading' > & { /** 标题 */ text?: string /** 按钮类型 */ type?: PresetColor /** 是否文字加粗 */ bold?: boolean /** 类名 */ className?: string } & NativeButtonHandle<DialogButton> /** * 对话框对应参数的类型 * * @export * @interface DialogProps */ export interface DialogProps { /** 自定义类名前缀 */ prefixCls?: string /** 弹窗对应的自定义样式 */ bodyStyle?: CSSStyleDeclaration /** 是否显示蒙层 */ mask?: boolean /** 点击蒙层是否允许关闭 */ maskClosable?: boolean /** 是否可见 */ visible?: boolean /** 设置蒙层的 z-index */ zIndex?: number /** 是否显示关闭按钮 */ closable?: boolean /** 点击操作按钮后后是否关闭 */ buttonClosable?: boolean /** 是否显示垂直按钮布局 */ verticalButtons?: boolean /** 图片 */ image?: string /** 标题 */ title?: string /** 内容 */ content?: string /** 操作按钮列表 */ buttons?: DialogButton[] } /** * 对话框的参数 * * @export */ export type DialogShowOptions = { /** 组件的选择器 */ selector?: string, /** 页面的实例 */ inst?: MPInst } /** * show 方法对应参数的类型 * * @export */ export type DialogShowProps = Omit< DialogProps, 'visible' > & { /** 点击关闭按钮或蒙层的回调函数 */ onClose?: () => void /** 关闭后的回调函数 */ onClosed?: () => void } function show (props?: DialogShowProps, options?: DialogShowOptions): () => void function show (props?: DialogShowProps, selector?: string, inst?: MPInst): () => void function show (props?: DialogShowProps, selector?: DialogShowOptions | string, inst?: MPInst): () => void { let opts: DialogShowOptions = { selector: '#dora-dialog', inst: getCurrentPage(), } if (typeof selector === 'string') { opts.selector = selector as string if (inst) { opts.inst = inst } } else if (typeof selector === 'object') { opts = { ...opts, ...selector as DialogShowOptions, } } const comp = findComponentNode<Doraemon>(opts.selector, opts.inst) const instance = getCurrentInstance(comp) const { onClose, onClosed, ...restProps } = props instance.setData({ ...restProps, visible: true }) ;(comp as any).onClose = function handleClose () { if (!instance.data.visible) { return } instance.setData({ visible: false }, () => { onClose?.() }) } ;(comp as any).onPopupClosed = function handleClosed () { onClosed?.() } return (comp as any).onClose.bind(comp) } /** * alert 方法对应参数的类型 * * @export */ export type DialogAlertProps = Omit< DialogShowProps, 'buttonClosable' | 'buttons' > & { /** 确定按钮的文字 */ confirmText?: string /** 确定按钮的类型 */ confirmType?: PresetColor /** 确定按钮的点击事件 */ onConfirm?: DefaultButtonHandle<DialogButton> } function alert (props?: DialogAlertProps, options?: DialogShowOptions): Promise<void> function alert (props?: DialogAlertProps, selector?: string, inst?: MPInst): Promise<void> function alert (props?: DialogAlertProps, selector?: DialogShowOptions | string, inst?: MPInst): Promise<void> { const { confirmText, confirmType, onConfirm, ...restProps } = props return new Promise<void>((resolve) => { show.call(null, { ...restProps, buttonClosable: true, buttons: [{ type: confirmType ?? 'balanced', text: confirmText ?? '确定', onClick (...args) { onConfirm?.(...args) }, }], onClose: () => { resolve() }, } as DialogProps, selector, inst) }) } /** * confirm 方法对应参数的类型 * * @export */ export type DialogConfirmProps = DialogAlertProps & { /** 取消按钮的文字 */ cancelText?: string /** 取消按钮的类型 */ cancelType?: PresetColor /** 取消按钮的点击事件 */ onCancel?: DefaultButtonHandle<DialogButton> } function confirm (props?: DialogConfirmProps, options?: DialogShowOptions): Promise<boolean> function confirm (props?: DialogConfirmProps, selector?: string, inst?: MPInst): Promise<boolean> function confirm (props?: DialogConfirmProps, selector?: DialogShowOptions | string, inst?: MPInst): Promise<boolean> { const { confirmText, confirmType, onConfirm, cancelText, cancelType, onCancel, ...restProps } = props return new Promise<boolean>((resolve) => { show.call(null, { ...restProps, buttonClosable: true, buttons: [{ type: cancelType ?? 'dark', text: cancelText ?? '取消', async onClick (...args) { await onCancel?.(...args) resolve(false) }, }, { type: confirmType ?? 'balanced', text: confirmText ?? '确定', async onClick (...args) { await onConfirm?.(...args) resolve(true) }, }], onClose: () => { restProps.onClose?.() resolve(false) }, } as DialogProps, selector, inst) }) } export { show, alert, confirm, } |