All files dialog.ts

0% Statements 0/50
0% Branches 0/25
0% Functions 0/16
0% Lines 0/49

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import {
  getCurrentPage,
  findComponentNode,
  usePopupStateHOC,
  isTrue,
  isFalse,
  type NativeButtonProps,
  type NativeButtonEvent,
  type PresetColor,
  type DefaultButtonHandle,
  type NativeButtonHandle,
  type MiniprogramPublicInstance,
} from '@doraemon-ui/miniprogram.shared'
import type { DialogInstance } from '.'

export type { NativeButtonHandle, NativeButtonEvent }

/**
 * 操作按钮的类型
 *
 * @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?: Partial<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,
  /** 页面的实例 */
  instance?: MiniprogramPublicInstance
}

/**
 * show 方法对应参数的类型
 *
 * @export
 */
export type DialogShowProps = Omit<
  DialogProps,
  'visible'
> & {
  /** 点击关闭按钮或蒙层的回调函数 */
  onClose?: () => void
  /** 关闭后的回调函数 */
  onClosed?: () => void
}

const destroyFns = new Map<Function, boolean>()
 
function clear() {
  for (const [close] of destroyFns) {
    close()
    destroyFns.delete(close)
  }
}
 
function mountComponent(
  props: DialogShowProps,
  container: DialogInstance,
  statePropName: string = 'visible'
) {
  const { render, destroy, update } = usePopupStateHOC<DialogInstance>(statePropName)(container)
  const close = () => {
    if (isTrue(container[statePropName])) {
      destroy(props.onClose)
      if (destroyFns.has(close)) {
        destroyFns.delete(close)
      }
    }
  }
 
  destroyFns.set(close, true)
 
  if (isFalse(container[statePropName])) {
    render(props)
  }
 
  // rewrite close
  container.onClose = () => {
    close()
  }
  container.onClosed = () => {
    props.onClosed?.()
  }
 
  return {
    destroy: close,
    update,
  }
}
 
function show (props?: DialogShowProps, options?: DialogShowOptions): () => void
function show (props?: DialogShowProps, selector?: string, instance?: MiniprogramPublicInstance): () => void
function show (props?: DialogShowProps, selector?: DialogShowOptions | string, instance?: MiniprogramPublicInstance): () => void {
  let opts: DialogShowOptions = {
    selector: '#dora-dialog',
    instance: getCurrentPage(),
  }
  if (typeof selector === 'string') {
    opts.selector = selector as string
    if (instance) {
      opts.instance = instance
    }
  } else if (typeof selector === 'object') {
    opts = {
      ...opts,
      ...selector as DialogShowOptions,
    }
  }
  const comp = findComponentNode<DialogInstance>(opts.selector, opts.instance)
  const { destroy } = mountComponent(props, comp)
  
  return () => destroy()
}
 
/**
 * 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, instance?: MiniprogramPublicInstance): Promise<void>
function alert (props?: DialogAlertProps, selector?: DialogShowOptions | string, instance?: MiniprogramPublicInstance): 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, instance)
  })
}
 
/**
 * 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, instance?: MiniprogramPublicInstance): Promise<boolean>
function confirm (props?: DialogConfirmProps, selector?: DialogShowOptions | string, instance?: MiniprogramPublicInstance): 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, instance)
  })
}
 
export {
  show,
  alert,
  confirm,
  clear,
}