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

feat(comp:notification): support contentProps (#1014) #1046

Merged
merged 1 commit into from
Jul 29, 2022
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
25 changes: 25 additions & 0 deletions packages/components/notification/docs/Index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ export interface NotificationRef {
}
```

### 拿到 content 实例的引用

当 content 为 VNode 时,可以通过 contentProps 传入一个 ref 引用。

```html
<template>
<IxButton @click="openNotification">Open</IxButton>
</template>

<script setup lang="ts">
import { h, ref } from 'vue'
import { useNotification } from '@idux/components/notification'

const { open } = useNotification()

const contentRef = ref()

const openNotification = () => open({
title: 'Basic Notification',
content: h('div', 'Some contents...'),
contentProps: { ref: contentRef }
})
</script>
```

<!--- insert less variable begin --->
## 主题变量

Expand Down
13 changes: 9 additions & 4 deletions packages/components/notification/src/NotificationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import type {
} from './types'
import type { VKey } from '@idux/cdk/utils'
import type { CommonConfig, NotificationConfig } from '@idux/components/config'
import type { ComputedRef, Ref } from 'vue'

import { TransitionGroup, computed, defineComponent, provide, ref } from 'vue'
import { ComputedRef, Ref, TransitionGroup, cloneVNode, computed, defineComponent, isVNode, provide, ref } from 'vue'

import { isArray, isUndefined, pickBy } from 'lodash-es'

Expand Down Expand Up @@ -57,9 +56,15 @@ export default defineComponent({
return () => {
const getChild = (notifications: NotificationOptions[]) => {
return notifications.map(item => {
const { key, visible = true } = item
const { key, visible = true, content, contentProps, ...restProps } = item
const onClose = () => destroy(key!)
return <Notification {...item} onClose={onClose} visible={visible}></Notification>
const mergedProps = { key, visible, onClose }
const contentNode = isVNode(content) ? cloneVNode(content, contentProps, true) : content
return (
<Notification {...mergedProps} {...restProps}>
{contentNode}
</Notification>
)
})
}

Expand Down
3 changes: 2 additions & 1 deletion packages/components/notification/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { PortalTargetType } from '@idux/cdk/portal'
import type { ExtractInnerPropTypes, ExtractPublicPropTypes, MaybeArray, VKey } from '@idux/cdk/utils'
import type { ButtonProps } from '@idux/components/button'
import type { DefineComponent, HTMLAttributes, PropType, VNode } from 'vue'
import type { DefineComponent, HTMLAttributes, PropType, VNode, VNodeProps } from 'vue'

// 挑出部分必填的属性
type PickRequire<T, U extends keyof T> = Pick<T, Exclude<keyof T, U>> & Required<Pick<T, U>>
Expand Down Expand Up @@ -71,6 +71,7 @@ export type NotificationInstance = InstanceType<DefineComponent<NotificationProp

// 通过useNotification的配置
export interface NotificationOptions<K = VKey> extends PickRequire<NotificationProps, 'title' | 'content'> {
contentProps?: Record<string, unknown> | VNodeProps
onDestroy?: (key: K) => void
}
export type NotificationPlacementMap = Record<NotificationPlacement, NotificationOptions[]>
Expand Down