Skip to content

Commit feffe70

Browse files
committed
refactor: useNotification #6527
1 parent 02ed988 commit feffe70

File tree

8 files changed

+151
-197
lines changed

8 files changed

+151
-197
lines changed

components/message/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Key, VueNode } from '../_util/type';
44
export type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
55

66
export interface ConfigOptions {
7-
top?: number;
7+
top?: number | string;
88
duration?: number;
99
prefixCls?: string;
1010
getContainer?: () => HTMLElement;

components/message/useMessage.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ const Holder = defineComponent({
5656
const [, hashId] = useStyle(prefixCls);
5757

5858
// =============================== Style ===============================
59-
const getStyles = () => ({
60-
left: '50%',
61-
transform: 'translateX(-50%)',
62-
top: `${top ?? DEFAULT_OFFSET}px`,
63-
});
59+
const getStyles = () => {
60+
const top = props.top ?? DEFAULT_OFFSET;
61+
return {
62+
left: '50%',
63+
transform: 'translateX(-50%)',
64+
top: typeof top === 'number' ? `${top}px` : top,
65+
};
66+
};
6467
const getClassName = () => classNames(hashId.value, props.rtl ? `${prefixCls.value}-rtl` : '');
6568

6669
// ============================== Motion ===============================

components/notification/PurePanel.tsx

+53-40
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { computed } from 'vue';
1+
import { computed, defineComponent } from 'vue';
22
import useStyle from './style';
33
import useConfigInject from '../config-provider/hooks/useConfigInject';
44
import type { IconType } from './interface';
55
import Notice from '../vc-notification/Notice';
66
import classNames from '../_util/classNames';
77
import type { NoticeProps } from '../vc-notification/Notice';
88
import type { VueNode } from '../_util/type';
9-
import {
10-
CheckCircleOutlined,
11-
CloseCircleOutlined,
12-
CloseOutlined,
13-
ExclamationCircleOutlined,
14-
InfoCircleOutlined,
15-
} from '@ant-design/icons-vue';
9+
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
10+
import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled';
11+
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
12+
import CheckCircleFilled from '@ant-design/icons-vue/CheckCircleFilled';
13+
import InfoCircleFilled from '@ant-design/icons-vue/InfoCircleFilled';
14+
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
1615
import { renderHelper } from '../_util/util';
1716

1817
export function getCloseIcon(prefixCls: string, closeIcon?: VueNode) {
@@ -34,11 +33,19 @@ export interface PureContentProps {
3433
type?: IconType;
3534
}
3635

36+
export const TypeIcon = {
37+
info: <InfoCircleFilled />,
38+
success: <CheckCircleFilled />,
39+
error: <CloseCircleFilled />,
40+
warning: <ExclamationCircleFilled />,
41+
loading: <LoadingOutlined />,
42+
};
43+
3744
const typeToIcon = {
38-
success: CheckCircleOutlined,
39-
info: InfoCircleOutlined,
40-
error: CloseCircleOutlined,
41-
warning: ExclamationCircleOutlined,
45+
success: CheckCircleFilled,
46+
info: InfoCircleFilled,
47+
error: CloseCircleFilled,
48+
warning: ExclamationCircleFilled,
4249
};
4350

4451
export function PureContent({
@@ -74,36 +81,42 @@ export function PureContent({
7481

7582
export interface PurePanelProps
7683
extends Omit<NoticeProps, 'prefixCls' | 'eventKey'>,
77-
Omit<PureContentProps, 'prefixCls' | 'children'> {
84+
Omit<PureContentProps, 'prefixCls'> {
7885
prefixCls?: string;
7986
}
8087

8188
/** @private Internal Component. Do not use in your production. */
82-
export default function PurePanel(props: PurePanelProps) {
83-
const { getPrefixCls } = useConfigInject('notification', props);
84-
const prefixCls = computed(() => props.prefixCls || getPrefixCls('notification'));
85-
const noticePrefixCls = `${prefixCls.value}-notice`;
89+
export default defineComponent<PurePanelProps>({
90+
name: 'PurePanel',
91+
inheritAttrs: false,
92+
props: ['prefixCls', 'icon', 'type', 'message', 'description', 'btn', 'closeIcon'] as any,
93+
setup(props) {
94+
const { getPrefixCls } = useConfigInject('notification', props);
95+
const prefixCls = computed(() => props.prefixCls || getPrefixCls('notification'));
96+
const noticePrefixCls = computed(() => `${prefixCls.value}-notice`);
8697

87-
const [, hashId] = useStyle(prefixCls);
88-
89-
return (
90-
<Notice
91-
{...props}
92-
prefixCls={prefixCls.value}
93-
class={classNames(hashId.value, `${noticePrefixCls}-pure-panel`)}
94-
noticeKey="pure"
95-
duration={null}
96-
closable={props.closable}
97-
closeIcon={getCloseIcon(prefixCls.value, props.closeIcon)}
98-
>
99-
<PureContent
100-
prefixCls={noticePrefixCls}
101-
icon={props.icon}
102-
type={props.type}
103-
message={props.message}
104-
description={props.description}
105-
btn={props.btn}
106-
/>
107-
</Notice>
108-
);
109-
}
98+
const [, hashId] = useStyle(prefixCls);
99+
return () => {
100+
return (
101+
<Notice
102+
{...props}
103+
prefixCls={prefixCls.value}
104+
class={classNames(hashId.value, `${noticePrefixCls.value}-pure-panel`)}
105+
noticeKey="pure"
106+
duration={null}
107+
closable={props.closable}
108+
closeIcon={getCloseIcon(prefixCls.value, props.closeIcon)}
109+
>
110+
<PureContent
111+
prefixCls={noticePrefixCls.value}
112+
icon={props.icon}
113+
type={props.type}
114+
message={props.message}
115+
description={props.description}
116+
btn={props.btn}
117+
/>
118+
</Notice>
119+
);
120+
};
121+
},
122+
});

components/notification/index.tsx

+4-60
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { NotificationInstance as VCNotificationInstance } from '../vc-notif
1212
import classNames from '../_util/classNames';
1313
import useStyle from './style';
1414
import useNotification from './useNotification';
15+
import { getPlacementStyle } from './util';
1516

1617
export type NotificationPlacement =
1718
| 'top'
@@ -77,63 +78,6 @@ function setNotificationConfig(options: ConfigProps) {
7778
}
7879
}
7980

80-
function getPlacementStyle(
81-
placement: NotificationPlacement,
82-
top: string = defaultTop,
83-
bottom: string = defaultBottom,
84-
) {
85-
let style: CSSProperties;
86-
switch (placement) {
87-
case 'top':
88-
style = {
89-
left: '50%',
90-
transform: 'translateX(-50%)',
91-
right: 'auto',
92-
top,
93-
bottom: 'auto',
94-
};
95-
break;
96-
case 'topLeft':
97-
style = {
98-
left: '0px',
99-
top,
100-
bottom: 'auto',
101-
};
102-
break;
103-
case 'topRight':
104-
style = {
105-
right: '0px',
106-
top,
107-
bottom: 'auto',
108-
};
109-
break;
110-
case 'bottom':
111-
style = {
112-
left: '50%',
113-
transform: 'translateX(-50%)',
114-
right: 'auto',
115-
top: 'auto',
116-
bottom,
117-
};
118-
break;
119-
case 'bottomLeft':
120-
style = {
121-
left: '0px',
122-
top: 'auto',
123-
bottom,
124-
};
125-
break;
126-
default:
127-
style = {
128-
right: '0px',
129-
top: 'auto',
130-
bottom,
131-
};
132-
break;
133-
}
134-
return style;
135-
}
136-
13781
function getNotificationInstance(
13882
{
13983
prefixCls: customizePrefixCls,
@@ -167,7 +111,7 @@ function getNotificationInstance(
167111
prefixCls: customizePrefixCls || defaultPrefixCls,
168112
useStyle,
169113
class: notificationClass,
170-
style: getPlacementStyle(placement, top, bottom),
114+
style: getPlacementStyle(placement, top ?? defaultTop, bottom ?? defaultBottom),
171115
appContext,
172116
getContainer,
173117
closeIcon: ({ prefixCls }) => {
@@ -210,8 +154,8 @@ export interface NotificationArgsProps {
210154
class?: string;
211155
readonly type?: IconType;
212156
onClick?: () => void;
213-
top?: string;
214-
bottom?: string;
157+
top?: string | number;
158+
bottom?: string | number;
215159
getContainer?: () => HTMLElement;
216160
closeIcon?: VueNode | (() => VueNode);
217161
appContext?: any;

components/notification/interface.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ export type NotificationPlacement =
1212
export type IconType = 'success' | 'info' | 'error' | 'warning';
1313

1414
export interface ArgsProps {
15-
message: VueNode;
16-
description?: VueNode;
17-
btn?: VueNode;
15+
message: (() => VueNode) | VueNode;
16+
description?: (() => VueNode) | VueNode;
17+
btn?: (() => VueNode) | VueNode;
1818
key?: Key;
1919
onClose?: () => void;
2020
duration?: number | null;
21-
icon?: VueNode;
21+
icon?: (() => VueNode) | VueNode;
2222
placement?: NotificationPlacement;
2323
style?: CSSProperties;
24-
className?: string;
24+
class?: string;
2525
readonly type?: IconType;
2626
onClick?: () => void;
27-
closeIcon?: VueNode;
27+
closeIcon?: (() => VueNode) | VueNode;
2828
}
2929

3030
type StaticFn = (args: ArgsProps) => void;
@@ -39,20 +39,20 @@ export interface NotificationInstance {
3939
}
4040

4141
export interface GlobalConfigProps {
42-
top?: number;
43-
bottom?: number;
42+
top?: number | string;
43+
bottom?: number | string;
4444
duration?: number;
4545
prefixCls?: string;
4646
getContainer?: () => HTMLElement;
4747
placement?: NotificationPlacement;
48-
closeIcon?: VueNode;
48+
closeIcon?: (() => VueNode) | VueNode;
4949
rtl?: boolean;
5050
maxCount?: number;
5151
}
5252

5353
export interface NotificationConfig {
54-
top?: number;
55-
bottom?: number;
54+
top?: number | string;
55+
bottom?: number | string;
5656
prefixCls?: string;
5757
getContainer?: () => HTMLElement;
5858
placement?: NotificationPlacement;

0 commit comments

Comments
 (0)