@etsoo/react 1.5.16 → 1.5.20

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.
@@ -1,6 +1,5 @@
1
1
  import { ActionResultError, BridgeUtils, CoreApp, createClient } from '@etsoo/appscript';
2
2
  import { WindowStorage } from '@etsoo/shared';
3
- import { navigate } from '@reach/router';
4
3
  import React from 'react';
5
4
  import { NotifierMU } from '../mu/NotifierMU';
6
5
  import { ProgressCount } from '../mu/ProgressCount';
@@ -199,7 +198,8 @@ export class ReactApp extends CoreApp {
199
198
  * @param url Url
200
199
  */
201
200
  redirectTo(url) {
202
- (this.history == null ? navigate : this.history.navigate)(url);
201
+ const navigate = ReactUtils.getNavigateFn();
202
+ navigate(url);
203
203
  }
204
204
  /**
205
205
  * Set page data
@@ -1,9 +1,21 @@
1
- import { History } from '@reach/router';
1
+ import { History, HistorySource } from '@reach/router';
2
2
  import React from 'react';
3
+ declare type MemoryHistorySource = HistorySource & {
4
+ history: {
5
+ get entries(): any;
6
+ get index(): number;
7
+ go(to: number): void;
8
+ };
9
+ };
3
10
  /**
4
11
  * React utils
5
12
  */
6
13
  export declare namespace ReactUtils {
14
+ /**
15
+ * Cretae memory source to fix go back bug
16
+ * @param initialPath Initial path
17
+ */
18
+ function createMemorySource(initialPath?: string): MemoryHistorySource;
7
19
  /**
8
20
  * Format input value
9
21
  * @param value Input value
@@ -35,3 +47,4 @@ export declare namespace ReactUtils {
35
47
  */
36
48
  function triggerChange(input: HTMLInputElement, value: string, cancelable: boolean): void;
37
49
  }
50
+ export {};
@@ -1,4 +1,4 @@
1
- import { createHistory, createMemorySource, navigate } from '@reach/router';
1
+ import { createHistory, navigate } from '@reach/router';
2
2
  /**
3
3
  * React utils
4
4
  */
@@ -7,6 +7,71 @@ export var ReactUtils;
7
7
  // Memory history
8
8
  // https://github.com/reach/router/issues/225
9
9
  let memoryHistory;
10
+ /**
11
+ * Cretae memory source to fix go back bug
12
+ * @param initialPath Initial path
13
+ */
14
+ function createMemorySource(initialPath = '/') {
15
+ const searchIndex = initialPath.indexOf('?');
16
+ const pathname = searchIndex > -1
17
+ ? initialPath.substring(0, searchIndex)
18
+ : initialPath;
19
+ const initialLocation = {
20
+ pathname,
21
+ search: searchIndex > -1 ? initialPath.substring(searchIndex) : ''
22
+ };
23
+ let index = 0;
24
+ const stack = [initialLocation];
25
+ const states = [null];
26
+ return {
27
+ get location() {
28
+ return stack[index];
29
+ },
30
+ addEventListener(name, fn) { },
31
+ removeEventListener(name, fn) { },
32
+ history: {
33
+ get entries() {
34
+ return stack;
35
+ },
36
+ get index() {
37
+ return index;
38
+ },
39
+ get state() {
40
+ return states[index];
41
+ },
42
+ pushState(state, _, uri) {
43
+ const [pathname, search = ''] = uri.split('?');
44
+ index++;
45
+ // View a, index = 0
46
+ // View b, index = 1
47
+ // Go(-1), a, index = 0
48
+ // View c, index = 1, directly push is wrong
49
+ if (index < stack.length) {
50
+ stack.splice(index, stack.length - index);
51
+ states.splice(index, stack.length - index);
52
+ }
53
+ stack.push({
54
+ pathname,
55
+ search: search.length ? `?${search}` : search
56
+ });
57
+ states.push(state);
58
+ },
59
+ replaceState(state, _, uri) {
60
+ const [pathname, search = ''] = uri.split('?');
61
+ stack[index] = { pathname, search };
62
+ states[index] = state;
63
+ },
64
+ go(to) {
65
+ const newIndex = index + to;
66
+ if (newIndex < 0 || newIndex > states.length - 1) {
67
+ return;
68
+ }
69
+ index = newIndex;
70
+ }
71
+ }
72
+ };
73
+ }
74
+ ReactUtils.createMemorySource = createMemorySource;
10
75
  /**
11
76
  * Format input value
12
77
  * @param value Input value
@@ -10,8 +10,6 @@ import { ReactUtils } from '../app/ReactUtils';
10
10
  export function BackButton(props) {
11
11
  // Destruct
12
12
  const { color = 'primary', size = 'small', onClick, ...rest } = props;
13
- // Navigate
14
- const navigate = ReactUtils.getNavigateFn();
15
13
  // Theme
16
14
  const theme = useTheme();
17
15
  // Color
@@ -19,10 +17,12 @@ export function BackButton(props) {
19
17
  ? theme.palette[color]
20
18
  : theme.palette.primary;
21
19
  // Click handler
22
- const onClickLocal = (event) => {
20
+ const onClickLocal = async (event) => {
23
21
  if (onClick)
24
22
  onClick(event);
25
- navigate(-1);
23
+ // Navigate
24
+ const navigate = ReactUtils.getNavigateFn();
25
+ await navigate(-1);
26
26
  };
27
27
  return (React.createElement(IconButton, { "aria-label": "Back", color: color, size: size, onClick: onClickLocal, sx: {
28
28
  backgroundColor: pColor.contrastText,
@@ -56,6 +56,8 @@ export function ListMoreDisplay(props) {
56
56
  ref.lastLoadedItems = newItems;
57
57
  ref.isNextPageLoading = false;
58
58
  ref.hasNextPage = hasNextPage;
59
+ // Next page
60
+ ref.currentPage = currentPage + 1;
59
61
  // Update rows
60
62
  if (states.items == null || reset)
61
63
  setStates({ items, completed: !hasNextPage });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.5.16",
3
+ "version": "1.5.20",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -83,9 +83,9 @@
83
83
  "@babel/runtime-corejs3": "^7.17.2",
84
84
  "@types/jest": "^27.4.0",
85
85
  "@types/react-test-renderer": "^17.0.1",
86
- "@typescript-eslint/eslint-plugin": "^5.11.0",
87
- "@typescript-eslint/parser": "^5.11.0",
88
- "eslint": "^8.8.0",
86
+ "@typescript-eslint/eslint-plugin": "^5.12.0",
87
+ "@typescript-eslint/parser": "^5.12.0",
88
+ "eslint": "^8.9.0",
89
89
  "eslint-config-airbnb-base": "^15.0.0",
90
90
  "eslint-plugin-import": "^2.25.4",
91
91
  "eslint-plugin-react": "^7.28.0",
@@ -14,7 +14,7 @@ import {
14
14
  NotificationReturn
15
15
  } from '@etsoo/notificationbase';
16
16
  import { DataTypes, WindowStorage } from '@etsoo/shared';
17
- import { History, navigate } from '@reach/router';
17
+ import { History } from '@reach/router';
18
18
  import React from 'react';
19
19
  import { NotifierMU } from '../mu/NotifierMU';
20
20
  import { ProgressCount } from '../mu/ProgressCount';
@@ -370,7 +370,8 @@ export class ReactApp<
370
370
  * @param url Url
371
371
  */
372
372
  override redirectTo(url: string) {
373
- (this.history == null ? navigate : this.history.navigate)(url);
373
+ const navigate = ReactUtils.getNavigateFn();
374
+ navigate(url);
374
375
  }
375
376
 
376
377
  /**
@@ -1,11 +1,14 @@
1
- import {
2
- createHistory,
3
- createMemorySource,
4
- History,
5
- navigate
6
- } from '@reach/router';
1
+ import { createHistory, History, HistorySource, navigate } from '@reach/router';
7
2
  import React from 'react';
8
3
 
4
+ type MemoryHistorySource = HistorySource & {
5
+ history: {
6
+ get entries(): any;
7
+ get index(): number;
8
+ go(to: number): void;
9
+ };
10
+ };
11
+
9
12
  /**
10
13
  * React utils
11
14
  */
@@ -14,6 +17,82 @@ export namespace ReactUtils {
14
17
  // https://github.com/reach/router/issues/225
15
18
  let memoryHistory: History | null;
16
19
 
20
+ /**
21
+ * Cretae memory source to fix go back bug
22
+ * @param initialPath Initial path
23
+ */
24
+ export function createMemorySource(
25
+ initialPath: string = '/'
26
+ ): MemoryHistorySource {
27
+ const searchIndex = initialPath.indexOf('?');
28
+ const pathname =
29
+ searchIndex > -1
30
+ ? initialPath.substring(0, searchIndex)
31
+ : initialPath;
32
+
33
+ const initialLocation: any = {
34
+ pathname,
35
+ search: searchIndex > -1 ? initialPath.substring(searchIndex) : ''
36
+ };
37
+
38
+ let index = 0;
39
+
40
+ const stack = [initialLocation];
41
+ const states = [null];
42
+
43
+ return {
44
+ get location() {
45
+ return stack[index];
46
+ },
47
+ addEventListener(name, fn) {},
48
+ removeEventListener(name, fn) {},
49
+ history: {
50
+ get entries() {
51
+ return stack;
52
+ },
53
+ get index() {
54
+ return index;
55
+ },
56
+ get state() {
57
+ return states[index];
58
+ },
59
+ pushState(state, _, uri) {
60
+ const [pathname, search = ''] = uri.split('?');
61
+ index++;
62
+
63
+ // View a, index = 0
64
+ // View b, index = 1
65
+ // Go(-1), a, index = 0
66
+ // View c, index = 1, directly push is wrong
67
+ if (index < stack.length) {
68
+ stack.splice(index, stack.length - index);
69
+ states.splice(index, stack.length - index);
70
+ }
71
+
72
+ stack.push({
73
+ pathname,
74
+ search: search.length ? `?${search}` : search
75
+ });
76
+ states.push(state);
77
+ },
78
+ replaceState(state, _, uri) {
79
+ const [pathname, search = ''] = uri.split('?');
80
+ stack[index] = { pathname, search };
81
+ states[index] = state;
82
+ },
83
+ go(to: number) {
84
+ const newIndex = index + to;
85
+
86
+ if (newIndex < 0 || newIndex > states.length - 1) {
87
+ return;
88
+ }
89
+
90
+ index = newIndex;
91
+ }
92
+ }
93
+ };
94
+ }
95
+
17
96
  /**
18
97
  * Format input value
19
98
  * @param value Input value
@@ -17,9 +17,6 @@ export function BackButton(props: BackButtonProps) {
17
17
  // Destruct
18
18
  const { color = 'primary', size = 'small', onClick, ...rest } = props;
19
19
 
20
- // Navigate
21
- const navigate = ReactUtils.getNavigateFn();
22
-
23
20
  // Theme
24
21
  const theme = useTheme();
25
22
 
@@ -30,9 +27,12 @@ export function BackButton(props: BackButtonProps) {
30
27
  : theme.palette.primary;
31
28
 
32
29
  // Click handler
33
- const onClickLocal = (event: React.MouseEvent<HTMLButtonElement>) => {
30
+ const onClickLocal = async (event: React.MouseEvent<HTMLButtonElement>) => {
34
31
  if (onClick) onClick(event);
35
- navigate(-1);
32
+
33
+ // Navigate
34
+ const navigate = ReactUtils.getNavigateFn();
35
+ await navigate(-1);
36
36
  };
37
37
 
38
38
  return (
@@ -137,6 +137,9 @@ export function ListMoreDisplay<
137
137
  ref.isNextPageLoading = false;
138
138
  ref.hasNextPage = hasNextPage;
139
139
 
140
+ // Next page
141
+ ref.currentPage = currentPage + 1;
142
+
140
143
  // Update rows
141
144
  if (states.items == null || reset)
142
145
  setStates({ items, completed: !hasNextPage });