From 9189a1f8748ef66e2ff2406640247c4168545811 Mon Sep 17 00:00:00 2001 From: wuchao Date: Wed, 18 Dec 2024 11:02:12 +0800 Subject: [PATCH] feat: Pass porps to your custom compoonent --- docs/docs/.vitepress/config.ts | 4 + .../usage/pass-props-to-custom-component.md | 114 ++++++++++++++++++ playground/src/components/constomCompo.vue | 17 ++- playground/src/pages/index.vue | 5 +- src/components/ToastItem.tsx | 85 ++++--------- src/components/toastify-container/prop.ts | 5 + 6 files changed, 166 insertions(+), 64 deletions(-) create mode 100644 docs/docs/usage/pass-props-to-custom-component.md diff --git a/docs/docs/.vitepress/config.ts b/docs/docs/.vitepress/config.ts index 09b2cb0..94fc160 100644 --- a/docs/docs/.vitepress/config.ts +++ b/docs/docs/.vitepress/config.ts @@ -148,6 +148,10 @@ function sidebarConfig() { // text: 'Use a controlled progress bar', // link: '/usage/use-a-controlled-progress-bar', // }, + { + text: 'Pass props to custom component', + link: '/usage/pass-props-to-custom-component', + }, { text: 'How to style', link: '/usage/how-to-style', diff --git a/docs/docs/usage/pass-props-to-custom-component.md b/docs/docs/usage/pass-props-to-custom-component.md new file mode 100644 index 0000000..917df10 --- /dev/null +++ b/docs/docs/usage/pass-props-to-custom-component.md @@ -0,0 +1,114 @@ +# Pass porps to your custom compoonent. + +> `expandCustomProps` default is false. + +- `expandCustomProps`: true + +::: sandbox +```vue /src/App.vue + + + +``` + +```vue /src/CustomComp.vue + + + +``` +::: + +- disable globally + +```ts +app.use( + Vue3Toasity, + { + expandCustomProps: true, // default is false + } as ToastContainerOptions, +); +``` + +- `expandCustomProps`: false + +::: sandbox +```vue /src/App.vue + + + +``` + +```vue /src/CustomComp.vue + + + +``` +::: +``` + + diff --git a/playground/src/components/constomCompo.vue b/playground/src/components/constomCompo.vue index 89d0cc1..aeff79a 100644 --- a/playground/src/components/constomCompo.vue +++ b/playground/src/components/constomCompo.vue @@ -1,10 +1,15 @@ diff --git a/playground/src/pages/index.vue b/playground/src/pages/index.vue index 9a4f99d..268730d 100644 --- a/playground/src/pages/index.vue +++ b/playground/src/pages/index.vue @@ -18,9 +18,12 @@ const onOptionsChange = (opts: ToastOptions) => { function showToast() { toast(constomCompo, { - contentProps: { title: 'narges1' }, type: 'info', ...options.value, + contentProps: { + title: 'narges1', + color: '#ff0', + }, }); } diff --git a/src/components/ToastItem.tsx b/src/components/ToastItem.tsx index 981685a..1c1e763 100644 --- a/src/components/ToastItem.tsx +++ b/src/components/ToastItem.tsx @@ -51,13 +51,13 @@ const ToastItem = defineComponent({ toastRef, loading, done: () => { - ToastActions.remove(item.toastId); + ToastActions.remove(item.toastId); }, ...getDefaultTransition(item.transition as ToastTransition, item.disabledEnterTransition), ...item, }); - return () => + return () =>
{/* icon */} { - toastIcon.value != null && + toastIcon.value != null &&
- + } {/* content */} { - item.contentProps - ? -
- {h( - toRaw(item.content) as any, { contentProps: item.contentProps }, - )} -
- : -
- { - isComponent(item.content as Content) - ? - h( +
+ { + isComponent(item.content as Content) + ? + h( toRaw(item.content) as any, { toastProps: toRaw(item), closeToast: hideToast, data: item.data, + ...item.expandCustomProps ? item.contentProps : { contentProps: item.contentProps || {} }, }, - ) - : isFn(item.content) - ? (item.content as Function)({ - toastProps: toRaw(item), - closeToast: hideToast, - data: item.data, - }) - : - item.dangerouslyHTMLString - ? h('div', { innerHTML: item.content as string }) - : item.content - - } -
- } + ) + : isFn(item.content) + ? (item.content as Function)({ + toastProps: toRaw(item), + closeToast: hideToast, + data: item.data, + }) + : + item.dangerouslyHTMLString + ? h('div', { innerHTML: item.content as string }) + : item.content - {/*
- { - isComponent(item.content as Content) - ? - h( - toRaw(item.content) as any, - { - toastProps: toRaw(item), - closeToast: hideToast, - data: item.data, - }, - ) - : isFn(item.content) - ? (item.content as Function)({ - toastProps: toRaw(item), - closeToast: hideToast, - data: item.data, - }) - : - item.dangerouslyHTMLString - ? h('div', { innerHTML: item.content as string }) - : item.content - - } -
*/} + } +
+ }
{/* close button */} { - (item.closeButton === undefined || item.closeButton === true) && + (item.closeButton === undefined || item.closeButton === true) && { @@ -187,7 +154,7 @@ const ToastItem = defineComponent({ hideToast(); }} /> - + } { diff --git a/src/components/toastify-container/prop.ts b/src/components/toastify-container/prop.ts index d228cf8..70b816f 100644 --- a/src/components/toastify-container/prop.ts +++ b/src/components/toastify-container/prop.ts @@ -184,6 +184,11 @@ const props = { required: false, default: null, }, + expandCustomProps: { + type: Boolean, + required: false, + default: false, + }, }; export default props;