diff --git a/.vscode/settings.json b/.vscode/settings.json index d1f625aefd..f198f4a36c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { - "cSpell.words": ["Element Plus", "element-plus"], + "cSpell.words": ["Element Plus", "element-plus", "vnode"], "typescript.tsdk": "node_modules/typescript/lib", "editor.formatOnSave": true, "editor.codeActionsOnSave": { diff --git a/docs/en-US/component/notification.md b/docs/en-US/component/notification.md index 0cf8abb4c1..a65987770f 100644 --- a/docs/en-US/component/notification.md +++ b/docs/en-US/component/notification.md @@ -61,6 +61,18 @@ Although `message` property supports HTML strings, dynamically rendering arbitra ::: +## Message using functions ^(2.9.0) + +`message` can be VNode. + +After ^(2.9.0), `message` supports a function whose return value is a VNode. + +:::demo + +notification/use-vnode + +::: + ## Hide close button It is possible to hide the close button @@ -111,7 +123,7 @@ ElNotification({}, appContext) | Name | Description | Type | Default | | ------------------------ | ------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------- | --------- | | title | title | ^[string] | '' | -| message | description text | ^[string] / ^[VNode] | '' | +| message | description text | ^[string] / ^[VNode] / ^[Function]`() => VNode` | '' | | dangerouslyUseHTMLString | whether `message` is treated as HTML string | ^[boolean] | false | | type | notification type | ^[enum]`'success' \| 'warning' \| 'info' \| 'error' \| ''` | '' | | icon | custom icon component. It will be overridden by `type` | ^[string] / ^[Component] | — | diff --git a/docs/examples/notification/use-vnode.vue b/docs/examples/notification/use-vnode.vue new file mode 100644 index 0000000000..b742447341 --- /dev/null +++ b/docs/examples/notification/use-vnode.vue @@ -0,0 +1,34 @@ + + + diff --git a/packages/components/notification/src/notification.ts b/packages/components/notification/src/notification.ts index 9e0cac798d..1406d97d54 100644 --- a/packages/components/notification/src/notification.ts +++ b/packages/components/notification/src/notification.ts @@ -46,7 +46,11 @@ export const notificationProps = buildProps({ * @description description text */ message: { - type: definePropType([String, Object]), + type: definePropType VNode)>([ + String, + Object, + Function, + ]), default: '', }, /** diff --git a/packages/components/notification/src/notify.ts b/packages/components/notification/src/notify.ts index 1e7e4210e7..c40aa02af1 100644 --- a/packages/components/notification/src/notify.ts +++ b/packages/components/notification/src/notify.ts @@ -3,6 +3,7 @@ import { debugWarn, isClient, isElement, + isFunction, isString, isVNode, } from '@element-plus/utils' @@ -81,11 +82,7 @@ const notify: NotifyFn & Partial & { _context: AppContext | null } = const vm = createVNode( NotificationConstructor, props, - isVNode(props.message) - ? { - default: () => props.message, - } - : null + isFunction(props.message) ? props.message : () => props.message ) vm.appContext = context ?? notify._context