Skip to content

Commit bdc9763

Browse files
EmilyyyLiu刘欢
and
刘欢
authored
refactor: replace notification close btn a with button (#367)
* 关闭按钮修改从a标签修改为button并调整样式和之前a标签一致 * 格式化恢复 * 无 * 关闭button删除tabIndex={0} * 补充单测 * 删除closeIcon,icon从closable中取用 * 单测配置duration=0 * 修改closableObj?.closeIcon为?? --------- Co-authored-by: 刘欢 <[email protected]>
1 parent 3b1e34b commit bdc9763

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

Diff for: assets/index.less

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
cursor: pointer;
8080
opacity: 0.2;
8181
filter: alpha(opacity=20);
82+
border: 0;
83+
background-color: #fff;
8284

8385
&-x:after {
8486
content: '×';

Diff for: src/Notice.tsx

+5-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
2727
eventKey,
2828
content,
2929
closable,
30-
closeIcon = 'x',
3130
props: divProps,
3231

3332
onClick,
@@ -46,7 +45,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
4645
onNoticeClose(eventKey);
4746
};
4847

49-
const onCloseKeyDown: React.KeyboardEventHandler<HTMLAnchorElement> = (e) => {
48+
const onCloseKeyDown: React.KeyboardEventHandler<HTMLButtonElement> = (e) => {
5049
if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) {
5150
onInternalClose();
5251
}
@@ -106,11 +105,8 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
106105
if (typeof closable === 'object' && closable !== null) {
107106
return closable;
108107
}
109-
if (closable) {
110-
return { closeIcon };
111-
}
112108
return {};
113-
}, [closable, closeIcon]);
109+
}, [closable]);
114110

115111
const ariaProps = pickAttrs(closableObj, true);
116112

@@ -143,8 +139,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
143139

144140
{/* Close Icon */}
145141
{closable && (
146-
<a
147-
tabIndex={0}
142+
<button
148143
className={`${noticePrefixCls}-close`}
149144
onKeyDown={onCloseKeyDown}
150145
aria-label="Close"
@@ -155,8 +150,8 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
155150
onInternalClose();
156151
}}
157152
>
158-
{closableObj.closeIcon}
159-
</a>
153+
{closableObj.closeIcon ?? 'x'}
154+
</button>
160155
)}
161156

162157
{/* Progress Bar */}

Diff for: src/hooks/useNotification.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface NotificationConfig {
1414
/** Customize container. It will repeat call which means you should return same container element. */
1515
getContainer?: () => HTMLElement | ShadowRoot;
1616
motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps);
17-
closeIcon?: React.ReactNode;
17+
1818
closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes);
1919
maxCount?: number;
2020
duration?: number;

Diff for: src/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface NoticeConfig {
99
duration?: number | null;
1010
showProgress?: boolean;
1111
pauseOnHover?: boolean;
12-
closeIcon?: React.ReactNode;
12+
1313
closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes);
1414
className?: string;
1515
style?: React.CSSProperties;

Diff for: tests/index.test.tsx

+32-4
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ describe('Notification.Basic', () => {
5252
});
5353

5454
it('works with custom close icon', () => {
55-
const { instance } = renderDemo({
56-
closeIcon: <span className="test-icon">test-close-icon</span>,
57-
});
55+
const { instance } = renderDemo();
5856

5957
act(() => {
6058
instance.open({
6159
content: <p className="test">1</p>,
62-
closable: true,
60+
closable: {
61+
closeIcon: <span className="test-icon">test-close-icon</span>,
62+
},
6363
duration: 0,
6464
});
6565
});
@@ -892,4 +892,32 @@ describe('Notification.Basic', () => {
892892
expect(document.querySelectorAll('.rc-notification-notice').length).toBe(1);
893893
});
894894
});
895+
it('notification close node ', () => {
896+
const Demo = () => {
897+
const [duration] = React.useState(0);
898+
const [api, holder] = useNotification({ duration });
899+
return (
900+
<>
901+
<button
902+
data-testid="show-notification"
903+
onClick={() => {
904+
api.open({
905+
content: `Test Notification`,
906+
closable: { 'aria-label': 'xxx' },
907+
});
908+
}}
909+
>
910+
show notification
911+
</button>
912+
{holder}
913+
</>
914+
);
915+
};
916+
const { getByTestId } = render(<Demo />);
917+
fireEvent.click(getByTestId('show-notification'));
918+
expect(document.querySelector('button.rc-notification-notice-close')).toHaveAttribute(
919+
'aria-label',
920+
'xxx',
921+
);
922+
});
895923
});

0 commit comments

Comments
 (0)