Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Toast、Notify]: 修复 close 事件不生效 #865

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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