@etsoo/react 1.5.99 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/EventWatcher.tsx +27 -0
- package/lib/app/EventWatcher.d.ts +35 -0
- package/lib/app/EventWatcher.js +43 -0
- package/lib/components/DynamicRouter.d.ts +13 -0
- package/lib/components/DynamicRouter.js +19 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
- package/src/app/EventWatcher.ts +66 -0
- package/src/components/DynamicRouter.tsx +35 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { EventWatcher } from '../src/app/EventWatcher';
|
|
4
|
+
|
|
5
|
+
function App(props: { callback: () => void }) {
|
|
6
|
+
const { callback } = props;
|
|
7
|
+
const watcher = new EventWatcher();
|
|
8
|
+
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
watcher.add({
|
|
11
|
+
type: 'click',
|
|
12
|
+
action: (_event) => callback(),
|
|
13
|
+
once: true
|
|
14
|
+
});
|
|
15
|
+
}, []);
|
|
16
|
+
return <button onClick={(event) => watcher.do(event)}></button>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('Tests for EventWatcher', () => {
|
|
20
|
+
const callback = jest.fn();
|
|
21
|
+
render(<App callback={callback} />);
|
|
22
|
+
const button = screen.getByRole<HTMLButtonElement>('button');
|
|
23
|
+
button.click();
|
|
24
|
+
expect(callback).toBeCalled();
|
|
25
|
+
button.click();
|
|
26
|
+
expect(callback).toBeCalledTimes(1);
|
|
27
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Event watcher action
|
|
4
|
+
*/
|
|
5
|
+
export interface EventWatcherAction {
|
|
6
|
+
/**
|
|
7
|
+
* Event type
|
|
8
|
+
*/
|
|
9
|
+
type?: string | string[];
|
|
10
|
+
/**
|
|
11
|
+
* Once action or not
|
|
12
|
+
*/
|
|
13
|
+
once?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Action
|
|
16
|
+
*/
|
|
17
|
+
action: (event: React.BaseSyntheticEvent | string) => void | PromiseLike<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Event watcher
|
|
21
|
+
*/
|
|
22
|
+
export declare class EventWatcher {
|
|
23
|
+
private actions;
|
|
24
|
+
/**
|
|
25
|
+
* Add action
|
|
26
|
+
* @param action Action
|
|
27
|
+
*/
|
|
28
|
+
add(action: EventWatcherAction): void;
|
|
29
|
+
/**
|
|
30
|
+
* Do the event
|
|
31
|
+
* @param event Event
|
|
32
|
+
*/
|
|
33
|
+
do(event: React.BaseSyntheticEvent | string): void;
|
|
34
|
+
private isMatch;
|
|
35
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event watcher
|
|
3
|
+
*/
|
|
4
|
+
export class EventWatcher {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.actions = [];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Add action
|
|
10
|
+
* @param action Action
|
|
11
|
+
*/
|
|
12
|
+
add(action) {
|
|
13
|
+
this.actions.push(action);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Do the event
|
|
17
|
+
* @param event Event
|
|
18
|
+
*/
|
|
19
|
+
do(event) {
|
|
20
|
+
// Type
|
|
21
|
+
const type = typeof event === 'string' ? event : event.type;
|
|
22
|
+
// Execute
|
|
23
|
+
const removeIndices = [];
|
|
24
|
+
this.actions.forEach((item, index) => {
|
|
25
|
+
if (!this.isMatch(item, type))
|
|
26
|
+
return;
|
|
27
|
+
item.action(event);
|
|
28
|
+
if (item.once)
|
|
29
|
+
removeIndices.push(index);
|
|
30
|
+
});
|
|
31
|
+
// Remove all once actions
|
|
32
|
+
removeIndices
|
|
33
|
+
.reverse()
|
|
34
|
+
.forEach((index) => this.actions.splice(index, 1));
|
|
35
|
+
}
|
|
36
|
+
isMatch(action, type) {
|
|
37
|
+
if (action.type == null)
|
|
38
|
+
return true;
|
|
39
|
+
if (typeof action.type === 'string')
|
|
40
|
+
return action.type === type;
|
|
41
|
+
return action.type.includes(type);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Dynamic router props
|
|
4
|
+
*/
|
|
5
|
+
export declare type DynamicRouterProps = {
|
|
6
|
+
basename: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Dynamic router
|
|
10
|
+
* @param props Props
|
|
11
|
+
* @returns Component
|
|
12
|
+
*/
|
|
13
|
+
export declare function DynamicRouter(props: React.PropsWithChildren<DynamicRouterProps>): JSX.Element;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BridgeUtils } from '@etsoo/appscript';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { BrowserRouter, MemoryRouter } from 'react-router-dom';
|
|
4
|
+
function getEntries(host) {
|
|
5
|
+
const startUrl = host.getStartUrl();
|
|
6
|
+
return startUrl == null ? undefined : [startUrl];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Dynamic router
|
|
10
|
+
* @param props Props
|
|
11
|
+
* @returns Component
|
|
12
|
+
*/
|
|
13
|
+
export function DynamicRouter(props) {
|
|
14
|
+
// Destruct
|
|
15
|
+
const { basename, children } = props;
|
|
16
|
+
// Layout
|
|
17
|
+
const host = BridgeUtils.host;
|
|
18
|
+
return host == null ? (React.createElement(BrowserRouter, { basename: basename }, children)) : (React.createElement(MemoryRouter, { basename: basename, initialEntries: getEntries(host) }, children));
|
|
19
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from './app/CoreConstants';
|
|
2
|
+
export * from './app/EventWatcher';
|
|
2
3
|
export * from './app/InputDialogProps';
|
|
3
4
|
export * from './app/ReactUtils';
|
|
4
5
|
export * from './app/RefreshTokenRQ';
|
|
5
6
|
export * from './components/DnDList';
|
|
7
|
+
export * from './components/DynamicRouter';
|
|
6
8
|
export * from './components/GridColumn';
|
|
7
9
|
export * from './components/GridLoader';
|
|
8
10
|
export * from './components/GridMethodRef';
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// app
|
|
2
2
|
export * from './app/CoreConstants';
|
|
3
|
+
export * from './app/EventWatcher';
|
|
3
4
|
export * from './app/InputDialogProps';
|
|
4
5
|
export * from './app/ReactUtils';
|
|
5
6
|
export * from './app/RefreshTokenRQ';
|
|
6
7
|
// components
|
|
7
8
|
export * from './components/DnDList';
|
|
9
|
+
export * from './components/DynamicRouter';
|
|
8
10
|
export * from './components/GridColumn';
|
|
9
11
|
export * from './components/GridLoader';
|
|
10
12
|
export * from './components/GridMethodRef';
|
package/package.json
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Event watcher action
|
|
5
|
+
*/
|
|
6
|
+
export interface EventWatcherAction {
|
|
7
|
+
/**
|
|
8
|
+
* Event type
|
|
9
|
+
*/
|
|
10
|
+
type?: string | string[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Once action or not
|
|
14
|
+
*/
|
|
15
|
+
once?: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Action
|
|
19
|
+
*/
|
|
20
|
+
action: (
|
|
21
|
+
event: React.BaseSyntheticEvent | string
|
|
22
|
+
) => void | PromiseLike<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Event watcher
|
|
27
|
+
*/
|
|
28
|
+
export class EventWatcher {
|
|
29
|
+
private actions: EventWatcherAction[] = [];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Add action
|
|
33
|
+
* @param action Action
|
|
34
|
+
*/
|
|
35
|
+
add(action: EventWatcherAction) {
|
|
36
|
+
this.actions.push(action);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Do the event
|
|
41
|
+
* @param event Event
|
|
42
|
+
*/
|
|
43
|
+
do(event: React.BaseSyntheticEvent | string) {
|
|
44
|
+
// Type
|
|
45
|
+
const type = typeof event === 'string' ? event : event.type;
|
|
46
|
+
|
|
47
|
+
// Execute
|
|
48
|
+
const removeIndices: number[] = [];
|
|
49
|
+
this.actions.forEach((item, index) => {
|
|
50
|
+
if (!this.isMatch(item, type)) return;
|
|
51
|
+
item.action(event);
|
|
52
|
+
if (item.once) removeIndices.push(index);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Remove all once actions
|
|
56
|
+
removeIndices
|
|
57
|
+
.reverse()
|
|
58
|
+
.forEach((index) => this.actions.splice(index, 1));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private isMatch(action: EventWatcherAction, type: string) {
|
|
62
|
+
if (action.type == null) return true;
|
|
63
|
+
if (typeof action.type === 'string') return action.type === type;
|
|
64
|
+
return action.type.includes(type);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { BridgeUtils, IBridgeHost } from '@etsoo/appscript';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { BrowserRouter, MemoryRouter } from 'react-router-dom';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Dynamic router props
|
|
7
|
+
*/
|
|
8
|
+
export type DynamicRouterProps = { basename: string };
|
|
9
|
+
|
|
10
|
+
function getEntries(host: IBridgeHost) {
|
|
11
|
+
const startUrl = host.getStartUrl();
|
|
12
|
+
return startUrl == null ? undefined : [startUrl];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Dynamic router
|
|
17
|
+
* @param props Props
|
|
18
|
+
* @returns Component
|
|
19
|
+
*/
|
|
20
|
+
export function DynamicRouter(
|
|
21
|
+
props: React.PropsWithChildren<DynamicRouterProps>
|
|
22
|
+
) {
|
|
23
|
+
// Destruct
|
|
24
|
+
const { basename, children } = props;
|
|
25
|
+
|
|
26
|
+
// Layout
|
|
27
|
+
const host = BridgeUtils.host;
|
|
28
|
+
return host == null ? (
|
|
29
|
+
<BrowserRouter basename={basename}>{children}</BrowserRouter>
|
|
30
|
+
) : (
|
|
31
|
+
<MemoryRouter basename={basename} initialEntries={getEntries(host)}>
|
|
32
|
+
{children}
|
|
33
|
+
</MemoryRouter>
|
|
34
|
+
);
|
|
35
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// app
|
|
2
2
|
export * from './app/CoreConstants';
|
|
3
|
+
export * from './app/EventWatcher';
|
|
3
4
|
export * from './app/InputDialogProps';
|
|
4
5
|
export * from './app/ReactUtils';
|
|
5
6
|
export * from './app/RefreshTokenRQ';
|
|
6
7
|
|
|
7
8
|
// components
|
|
8
9
|
export * from './components/DnDList';
|
|
10
|
+
export * from './components/DynamicRouter';
|
|
9
11
|
export * from './components/GridColumn';
|
|
10
12
|
export * from './components/GridLoader';
|
|
11
13
|
export * from './components/GridMethodRef';
|