Skip to content

Commit 98e365e

Browse files
shiqiWang0beier
andauthored
refactor: switchWindow to refactor as hooks (#327)
* refactor: switchWindow to refactor as hooks * fix: change hooks to use && make sure event unique * fix: fix test unit --------- Co-authored-by: beier <[email protected]>
1 parent e6b4028 commit 98e365e

File tree

5 files changed

+71
-68
lines changed

5 files changed

+71
-68
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export { default as Resize } from './resize';
2222
export { default as SlidePane } from './slidePane';
2323
export { default as SpreadSheet } from './spreadSheet';
2424
export { default as StatusTag } from './statusTag';
25-
export { default as SwitchWindow } from './switchWindow';
25+
export { default as useWindowSwitchListener } from './switchWindow';
2626
export { default as useCookieListener } from './cookies';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import useWindowSwitchListener from '../index';
2+
import { cleanup, renderHook } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
4+
5+
describe('test useWindowSwitchListener hooks', () => {
6+
afterEach(() => {
7+
cleanup();
8+
jest.resetAllMocks();
9+
});
10+
test('test callBack to called', () => {
11+
const fn = jest.fn();
12+
renderHook(() =>
13+
useWindowSwitchListener(() => {
14+
fn();
15+
})
16+
);
17+
window.dispatchEvent(new Event('focus'));
18+
expect(fn).toHaveBeenCalled();
19+
expect(fn).toHaveBeenCalledTimes(1);
20+
});
21+
});

src/switchWindow/demos/basic.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useState } from 'react';
2+
import { useWindowSwitchListener } from 'dt-react-component';
3+
4+
export default () => {
5+
const [msg, setMsg] = useState('hello world');
6+
const handleSwitch = () => {
7+
setMsg('window listener');
8+
console.log('window listener');
9+
};
10+
useWindowSwitchListener(() => {
11+
handleSwitch();
12+
});
13+
14+
return (
15+
<div
16+
style={{
17+
height: '100%',
18+
width: '100%',
19+
}}
20+
>
21+
{msg}
22+
</div>
23+
);
24+
};

src/switchWindow/index.md

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,20 @@
11
---
2-
title: SwitchWindow 窗口切换事件监听
2+
title: useWindowSwitchListener 监听窗口触发焦点事件
33
group: 组件
44
toc: content
55
demo:
66
cols: 2
77
---
88

9-
# SwitchWindow 窗口切换事件监听
9+
# useWindowSwitchListener 监听窗口触发焦点事件
1010

1111
## 何时使用
1212

13-
窗口切换事件监听
13+
监听当前窗口获取焦点时,执行某个回调时使用
1414

1515
## 示例
1616

17-
```jsx
18-
/**
19-
* title: "基础使用"
20-
*/
21-
import React, { useState } from 'react';
22-
import { SwitchWindow } from 'dt-react-component';
23-
24-
export default () => {
25-
const [msg, setMsg] = useState('hello world');
26-
27-
return (
28-
<>
29-
<SwitchWindow
30-
onSwitch={() => {
31-
setMsg('window listener');
32-
console.log('window listener');
33-
}}
34-
>
35-
<div
36-
id="box"
37-
style={{
38-
height: '100%',
39-
width: '100%',
40-
}}
41-
>
42-
{msg}
43-
</div>
44-
</SwitchWindow>
45-
</>
46-
);
47-
};
48-
```
17+
<code src='./demos/basic.tsx' title='基础使用'></code>
4918

5019
## API
5120

src/switchWindow/index.tsx

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
import React from 'react';
2-
export interface SwitchWindowProps {
3-
onSwitch?: (evt) => void;
4-
style?: React.CSSProperties;
5-
children?: React.ReactNode;
6-
}
7-
/**
8-
* 窗口切换事件监听,
9-
* 用法:
10-
* <SwitchWindow onSwitch={}></SwitchWindow>
11-
*/
12-
class SwitchWindow extends React.Component<SwitchWindowProps, any> {
13-
componentDidMount() {
14-
this.initEvent();
15-
}
1+
import { useCallback, useEffect } from 'react';
162

17-
listener = (e: any) => {
18-
const { onSwitch } = this.props;
19-
console.log('switch window is focusing!', window.location);
20-
if (onSwitch) onSwitch(e);
3+
const useWindowSwitchListener = (onSwitch: (evt: FocusEvent) => void) => {
4+
useEffect(() => {
5+
const eventListener = (e: FocusEvent) => listener(e);
6+
handleAddListener(eventListener);
7+
return () => {
8+
handleRemoveListener(eventListener);
9+
};
10+
}, []);
11+
const listener = useCallback(
12+
(e: FocusEvent) => {
13+
return onSwitch(e);
14+
},
15+
[onSwitch]
16+
);
17+
const handleAddListener = (eventListener: (e: FocusEvent) => void) => {
18+
window.addEventListener('focus', eventListener);
2119
};
22-
23-
componentWillUnmount() {
24-
window.removeEventListener('focus', this.listener);
25-
}
26-
27-
initEvent = () => {
28-
window.addEventListener('focus', this.listener);
20+
const handleRemoveListener = (eventListener: (e: FocusEvent) => void) => {
21+
window.removeEventListener('focus', eventListener);
2922
};
23+
};
3024

31-
render() {
32-
return <React.Fragment>{this.props.children}</React.Fragment>;
33-
}
34-
}
35-
36-
export default SwitchWindow;
25+
export default useWindowSwitchListener;

0 commit comments

Comments
 (0)