Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 22 additions & 3 deletions src/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export default class Notice extends Component<NoticeProps> {
this.clearCloseTimer();
}

keyDown = (e: React.KeyboardEvent<HTMLAnchorElement>) => {
if (e.key === 'Enter' || e.code === 'Enter' || e.charCode === 13) {
this.close();
}
};

close = (e?: React.MouseEvent<HTMLAnchorElement>) => {
if (e) {
e.stopPropagation();
Expand Down Expand Up @@ -89,8 +95,16 @@ export default class Notice extends Component<NoticeProps> {
}

render() {
const { prefixCls, className, closable, closeIcon, style, onClick, children, holder } =
this.props;
const {
prefixCls,
className,
closable,
closeIcon,
style,
onClick,
children,
holder,
} = this.props;
const componentClass = `${prefixCls}-notice`;
const dataOrAriaAttributeProps = Object.keys(this.props).reduce(
(acc: Record<string, string>, key: string) => {
Expand All @@ -114,7 +128,12 @@ export default class Notice extends Component<NoticeProps> {
>
<div className={`${componentClass}-content`}>{children}</div>
{closable ? (
<a tabIndex={0} onClick={this.close} className={`${componentClass}-close`}>
<a
tabIndex={0}
onClick={this.close}
onKeyDown={this.keyDown}
className={`${componentClass}-close`}
>
{closeIcon || <span className={`${componentClass}-close-x`} />}
</a>
) : null}
Expand Down
36 changes: 36 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,40 @@ describe('Notification.Basic', () => {
},
);
});

it('closes via keyboard Enter key', (done) => {
let container;

Notification.newInstance(
{
TEST_RENDER: (node) => {
({ container } = render(<div>{node}</div>));
},
},
(notification) => {
notification.notice({
content: <p className="test">1</p>,
closable: true,
duration: null,
});

setTimeout(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use waitFakeTimers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yoyo837, all the tests use setTimeout in this test suite, and this test is no different. Not sure this PR is the right place to refactor.

expect(container.querySelectorAll('.test')).toHaveLength(1);
expect(container.querySelector('a')).toBeTruthy();

setTimeout(() => {
fireEvent.keyDown(container.querySelector('a'), {
key: 'Enter',
code: 'Enter',
charCode: 13,
});

expect(container.querySelectorAll('.test')).toHaveLength(0);
notification.destroy();
done();
}, 1000);
}, 10);
},
);
});
});