Skip to content

Commit

Permalink
fix(toast、notify): 修复 close 事件不生效
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilotager committed Dec 24, 2024
1 parent c027bf3 commit d1692d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/notify/notify.imperative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const initialNotifyOptions: NotifyOptions = {
}

const DEFAULT_NOTIFY_SELECTOR = "#notify"
const DEFAULT_NOTIFY_SELECTOR_CREATE = "notify"

const defaultNotifyOptions: NotifyOptions = {}

Expand Down Expand Up @@ -51,8 +52,13 @@ export function openNotify(args: ReactNode | NotifyOptions) {
unmountPortal(notifyView)
}
mountPortal(
createElement(Notify, { ...restOptions, children: restOptions.message, defaultOpen: true }) as unknown as TaroNode,
notifyView
createElement(Notify, {
...restOptions,
children: restOptions.message,
defaultOpen: true,
id: selector === DEFAULT_NOTIFY_SELECTOR ? DEFAULT_NOTIFY_SELECTOR_CREATE : selector,
}) as unknown as TaroNode,
notifyView,
)
}
}
Expand All @@ -66,5 +72,5 @@ export function createNotify(color: NotifyColor) {
}

export function closeNotify(selector?: string) {
notifyEvents.trigger("close", selector ?? defaultNotifyOptions.selector)
notifyEvents.trigger("close", selector ? `#${selector}` : defaultNotifyOptions.selector)
}
12 changes: 9 additions & 3 deletions packages/core/src/toast/toast.imperative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const initialToastOptions: ToastOptions = {
}

const DEFAULT_TOAST_SELECTOR = "#toast"
const DEFAULT_TOAST_SELECTOR_CREATE = "toast"

const defaultToastOptions: ToastOptions = {}

Expand Down Expand Up @@ -54,8 +55,13 @@ export function openToast(args: ReactNode | ToastOptions) {
unmountPortal(toastView)
}
mountPortal(
createElement(Toast, { ...restOptions, children: restOptions.message, defaultOpen: true }) as unknown as TaroNode,
toastView
createElement(Toast, {
...restOptions,
children: restOptions.message,
defaultOpen: true,
id: selector === DEFAULT_TOAST_SELECTOR ? DEFAULT_TOAST_SELECTOR_CREATE : selector,
}) as unknown as TaroNode,
toastView,
)
}
}
Expand All @@ -69,5 +75,5 @@ export function createToast(type: ToastType) {
}

export function closeToast(selector?: string) {
toastEvents.trigger("close", selector ?? defaultToastOptions.selector)
toastEvents.trigger("close", selector ? `#${selector}` : defaultToastOptions.selector)
}
11 changes: 8 additions & 3 deletions packages/core/src/toast/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import {
} from "../utils/dom/element"
import { useObject, useToRef } from "../utils/state"
import { isElementOf } from "../utils/validate"
import { ToastPosition, ToastOptions, ToastType, toastEvents, toastSelectorSet } from "./toast.shared"
import {
ToastPosition,
ToastOptions,
ToastType,
toastEvents,
toastSelectorSet,
} from "./toast.shared"

const TOAST_PRESET_TYPES = ["text", "loading", "success", "fail", "html"]

Expand All @@ -54,7 +60,6 @@ function useToastClose(cb: (selector: string) => void) {
}, [])
}


function defaultToastIcon(icon?: ReactNode, type?: ToastType): ReactNode {
if (icon) {
return icon
Expand Down Expand Up @@ -166,7 +171,7 @@ export default function Toast(props: ToastProps) {
// eslint-disable-next-line react-hooks/exhaustive-deps
rootSelectorRef.current && toastSelectorSet.delete(rootSelectorRef.current)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

useEffect(() => {
Expand Down

0 comments on commit d1692d5

Please sign in to comment.