Skip to content

Commit 4b7d8de

Browse files
authored
feat: LocaleNotification supports to trigger reload by pressing the Enter key (#643)
* feat: improve localeNotification * test: update localeNotification.test.tsx * feat: respond to pressing Enter by focusing the button
1 parent 2590217 commit 4b7d8de

File tree

3 files changed

+114
-32
lines changed

3 files changed

+114
-32
lines changed

src/workbench/notification/__tests__/__snapshots__/localeNotification.test.tsx.snap

+39-17
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,57 @@ exports[`The LocaleNotification Component Match Snapshot 1`] = `
55
style={
66
Object {
77
"lineHeight": "1.5",
8-
"textAlign": "left",
9-
"width": 350,
8+
"textAlign": "right",
9+
"width": 420,
1010
}
1111
}
1212
>
13-
<p>
14-
The current locale has changed to
15-
chinese
16-
, click the button to reload the Page and applying the changes.
17-
</p>
18-
<p
13+
<div
1914
style={
2015
Object {
21-
"fontWeight": "bold",
16+
"direction": "ltr",
17+
"textAlign": "left",
18+
"whiteSpace": "normal",
2219
}
2320
}
2421
>
25-
Notice: Reload the Page could lose the data, Please confirm you have saved before.
26-
</p>
27-
<a
28-
className="mo-btn mo-btn--normal"
29-
onClick={[Function]}
22+
<p>
23+
The current locale has changed to
24+
chinese
25+
, click the button to reload the Page and applying the changes.
26+
</p>
27+
<p
28+
style={
29+
Object {
30+
"fontWeight": "bold",
31+
}
32+
}
33+
>
34+
Notice: Reload the Page could lose the data, Please confirm you have saved before.
35+
</p>
36+
</div>
37+
<div
38+
onKeyDown={[Function]}
39+
onKeyUp={[Function]}
3040
style={
3141
Object {
32-
"width": 150,
42+
"display": "inline-block",
43+
"marginBottom": 2,
3344
}
3445
}
46+
tabIndex={0}
3547
>
36-
Confirm Reload
37-
</a>
48+
<a
49+
className="mo-btn mo-btn--normal"
50+
onClick={[Function]}
51+
style={
52+
Object {
53+
"width": 150,
54+
}
55+
}
56+
>
57+
Confirm Reload
58+
</a>
59+
</div>
3860
</div>
3961
`;

src/workbench/notification/__tests__/localeNotification.test.tsx

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, render } from '@testing-library/react';
1+
import { fireEvent, render, waitFor } from '@testing-library/react';
22
import React from 'react';
33
import { create } from 'react-test-renderer';
44
import LocaleNotification from '../notificationPane/localeNotification';
@@ -26,4 +26,26 @@ describe('The LocaleNotification Component', () => {
2626

2727
window.location.reload = originalFunction;
2828
});
29+
30+
test('Should support to reload by pressing the Enter key.', async () => {
31+
const originalFunction = window.location.reload;
32+
const mockFn = jest.fn();
33+
Reflect.deleteProperty(window, 'location');
34+
Object.defineProperty(window, 'location', {
35+
writable: true,
36+
value: { reload: mockFn },
37+
});
38+
const { getByText } = render(<LocaleNotification locale="zh-CN" />);
39+
const elem = getByText('Confirm Reload');
40+
41+
await waitFor(() => {
42+
fireEvent.keyDown(elem, { key: 'Enter', code: 'Enter' });
43+
fireEvent.keyUp(elem, { key: 'Enter', code: 'Enter' });
44+
45+
expect(jest.isMockFunction(window.location.reload)).toBeTruthy();
46+
expect(mockFn).toBeCalled();
47+
});
48+
49+
window.location.reload = originalFunction;
50+
});
2951
});

src/workbench/notification/notificationPane/localeNotification.tsx

+52-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Button } from 'mo/components';
2-
import React, { useCallback } from 'react';
2+
import React, { useCallback, useEffect, useRef } from 'react';
33

44
interface ILocaleNotificationProps {
55
locale: string;
@@ -12,25 +12,63 @@ export function LocaleNotification(props: ILocaleNotificationProps) {
1212
window.location.reload();
1313
}, []);
1414

15+
const wrapperRef = useRef<HTMLDivElement>(null);
16+
17+
useEffect(() => {
18+
// Delay execution to ensure focus on element
19+
setTimeout(() => wrapperRef.current?.focus());
20+
}, []);
21+
22+
let isOkToReload = false;
23+
24+
const handleKeyDown = (e) => {
25+
if (e.key === 'Enter') {
26+
isOkToReload = true;
27+
}
28+
};
29+
30+
const handleKeyUp = (e) => {
31+
if (e.key === 'Enter' && isOkToReload) {
32+
reload();
33+
}
34+
};
35+
1536
return (
1637
<div
1738
style={{
1839
lineHeight: '1.5',
19-
width: 350,
20-
textAlign: 'left',
40+
width: 420,
41+
textAlign: 'right',
2142
}}
2243
>
23-
<p>
24-
The current locale has changed to {locale}, click the button to
25-
reload the Page and applying the changes.
26-
</p>
27-
<p style={{ fontWeight: 'bold' }}>
28-
Notice: Reload the Page could lose the data, Please confirm you
29-
have saved before.
30-
</p>
31-
<Button style={{ width: 150 }} onClick={reload}>
32-
Confirm Reload
33-
</Button>
44+
<div
45+
style={{
46+
direction: 'ltr',
47+
whiteSpace: 'normal',
48+
textAlign: 'left',
49+
}}
50+
>
51+
<p>
52+
The current locale has changed to {locale}, click the button
53+
to reload the Page and applying the changes.
54+
</p>
55+
<p style={{ fontWeight: 'bold' }}>
56+
Notice: Reload the Page could lose the data, Please confirm
57+
you have saved before.
58+
</p>
59+
</div>
60+
<div
61+
ref={wrapperRef}
62+
// make it focusable
63+
tabIndex={0}
64+
onKeyDown={handleKeyDown}
65+
onKeyUp={handleKeyUp}
66+
style={{ display: 'inline-block', marginBottom: 2 }}
67+
>
68+
<Button style={{ width: 150 }} onClick={reload}>
69+
Confirm Reload
70+
</Button>
71+
</div>
3472
</div>
3573
);
3674
}

0 commit comments

Comments
 (0)