Skip to content

Commit

Permalink
feat: add option to take props for a component as props
Browse files Browse the repository at this point in the history
  • Loading branch information
elias.khodaparast committed Oct 27, 2024
1 parent 00a8acb commit e0d4a29
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
26 changes: 26 additions & 0 deletions playground/src/components/constomCompo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<div>
{{ contentProps.title }}
</div>
<button>Click me</button>
</template>
<script setup>
const props = defineProps({
contentProps: {
type: Object,
default: () => ({ title: String })
,
},
});
console.log(props);
</script>

<style>
button{
background-color: orange;
padding: 3px;
border: 1px solid red
}
</style>
13 changes: 8 additions & 5 deletions playground/src/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<script setup lang="ts">
import { h, ref } from 'vue';
import { h, reactive, ref } from 'vue';
import { Divider } from 'ant-design-vue';
import { toast, ToastOptions } from 'vue3-toastify';
import Conditions from '../components/Conditions.vue';
import ToastCode from '../components/ToastCode.vue';
import 'ant-design-vue/es/button/style/index.css';
import 'ant-design-vue/es/divider/style/index.css';
import constomCompo from '@/components/constomCompo.vue';
const options = ref({} as ToastOptions);
const onOptionsChange = (opts: ToastOptions) => {
options.value = opts;
};
function showToast() {
toast(
`Hello!\nWow so easy!&nbsp;<strong>${parseInt(String(Math.random() * 1000), 10)}</strong>`,
options.value,
);
toast(constomCompo, {
contentProps: { title: 'narges1' },
type: 'info',
...options.value,
});
}
function showLoadToast() {
Expand Down
40 changes: 38 additions & 2 deletions src/components/ToastItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,43 @@ const ToastItem = defineComponent({
}

{/* content */}
<div data-testid="toast-content">
{
item.contentProps
?
<div data-testid="toast-content">
{h(
toRaw(item.content) as any, { contentProps: item.contentProps },
)}
</div>
:
<div data-testid="toast-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

}
</div>
}

{/* <div data-testid="toast-content">
{
isComponent(item.content as Content)
?
Expand All @@ -137,7 +173,7 @@ const ToastItem = defineComponent({
: item.content
}
</div>
</div> */}
</div>

{/* close button */}
Expand Down
5 changes: 5 additions & 0 deletions src/components/toastify-container/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ const props = {
required: false,
default: '',
},
contentProps: {
type: Object,
required: false,
default: null,
},
};

export default props;
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type Content =
| string
| VNode
| ((props: ToastContentProps) => VNode)
| DefineComponent<{}, {}, any>;
| DefineComponent<{}, {}, any>
|any;

export type ToastFunc = {(content: Content, options?: ToastOptions): void};

Expand Down Expand Up @@ -261,6 +262,8 @@ export interface ToastOptions<Data = {}> extends Options {

/** Only available when using `toast.loading' */
isLoading?: boolean;

contentProps?: Object;
}

/**
Expand Down

0 comments on commit e0d4a29

Please sign in to comment.