@jsb188/react 1.1.3 → 1.1.5
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/package.json +4 -4
- package/states/appContext.ts +20 -40
- package/states/index.ts +1 -0
- package/states/keyDown.ts +66 -0
- package/states/modal.ts +22 -6
- package/states/popOver.ts +1 -3
- package/types/PopOver.d.ts +69 -13
- package/utils/hooks.ts +77 -59
- package/utils/schema.ts +14 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsb188/react",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "Contains react (and DOM) related utils and hooks.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "jsb188",
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
"./types/PopOver.d": "./types/PopOver.d.ts"
|
|
13
13
|
},
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@jsb188/app": "1.1.
|
|
16
|
-
"jotai": "^2.
|
|
17
|
-
"react": "^19.1.
|
|
15
|
+
"@jsb188/app": "1.1.5",
|
|
16
|
+
"jotai": "^2.13.1",
|
|
17
|
+
"react": "^19.1.1"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"test": "echo \"Error: no test specified\" && exit 1"
|
package/states/appContext.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { clearAppCache, fetchLightModeSetting, isSystemDarkMode, saveAuthTokenToPlatform } from '@jsb188/app/platform';
|
|
2
2
|
import type { AccountData, AccountSettings, AuthenticationData, LightModeEnum } from '@jsb188/app/types/auth.d';
|
|
3
3
|
import { setAuthToken } from '@jsb188/app/utils/api';
|
|
4
|
-
import {
|
|
4
|
+
import { buildSingleText, guessFirstName } from '@jsb188/app/utils/string';
|
|
5
5
|
import { createContext, useContext } from 'react';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -21,14 +21,14 @@ type AppContextAction = {
|
|
|
21
21
|
| 'APP_THEME'
|
|
22
22
|
| 'APP_SYSTEM_LIGHT_MODE'
|
|
23
23
|
| 'APP_LIGHT_MODE'
|
|
24
|
-
// App events
|
|
25
|
-
| 'APP_KEY_DOWN'
|
|
26
24
|
);
|
|
27
25
|
data?: any;
|
|
28
26
|
};
|
|
29
27
|
|
|
30
28
|
export type AppContextDispatch = (action: AppContextAction) => void;
|
|
31
29
|
|
|
30
|
+
export type CurrentAccountObj = ReturnType<typeof useCurrentAccount>;
|
|
31
|
+
|
|
32
32
|
/**
|
|
33
33
|
* Types
|
|
34
34
|
*/
|
|
@@ -54,20 +54,13 @@ export type AppContextData = {
|
|
|
54
54
|
settings: AccountSettings;
|
|
55
55
|
activated: boolean;
|
|
56
56
|
hasPassword: boolean;
|
|
57
|
-
|
|
57
|
+
webVersion: string | null;
|
|
58
|
+
alertUpdatesOnMount: boolean;
|
|
58
59
|
|
|
59
60
|
// Preview theme overrides active theme
|
|
60
61
|
systemLightMode: 'LIGHT' | 'DARK';
|
|
61
62
|
previewLightMode: LightModeEnum | null;
|
|
62
63
|
previewTheme: string | null;
|
|
63
|
-
|
|
64
|
-
// This needs to get migrated to Jotai
|
|
65
|
-
keyDown: {
|
|
66
|
-
metaKey: boolean;
|
|
67
|
-
pressed: string | null;
|
|
68
|
-
alert: boolean;
|
|
69
|
-
modal: boolean;
|
|
70
|
-
};
|
|
71
64
|
};
|
|
72
65
|
|
|
73
66
|
/**
|
|
@@ -92,18 +85,12 @@ export const DEFAULT_APP_CONTEXT = {
|
|
|
92
85
|
},
|
|
93
86
|
activated: false,
|
|
94
87
|
hasPassword: false,
|
|
95
|
-
|
|
88
|
+
webVersion: null,
|
|
89
|
+
alertUpdatesOnMount: false,
|
|
96
90
|
|
|
97
91
|
systemLightMode: 'LIGHT',
|
|
98
92
|
previewLightMode: null,
|
|
99
93
|
previewTheme: null,
|
|
100
|
-
|
|
101
|
-
keyDown: {
|
|
102
|
-
metaKey: false,
|
|
103
|
-
pressed: null,
|
|
104
|
-
alert: false,
|
|
105
|
-
modal: false,
|
|
106
|
-
}
|
|
107
94
|
} as AppContextData;
|
|
108
95
|
|
|
109
96
|
/**
|
|
@@ -216,7 +203,7 @@ export function mergeAuthToDefaultValues(
|
|
|
216
203
|
|
|
217
204
|
const authSettings = {
|
|
218
205
|
...DEFAULT_APP_CONTEXT.settings,
|
|
219
|
-
...auth.settings
|
|
206
|
+
...auth.account.settings
|
|
220
207
|
};
|
|
221
208
|
|
|
222
209
|
authSettings.lightMode = ensureLightModeSetting(authSettings.lightMode);
|
|
@@ -227,9 +214,11 @@ export function mergeAuthToDefaultValues(
|
|
|
227
214
|
authorization: auth.token,
|
|
228
215
|
account: auth.account,
|
|
229
216
|
primaryOrganizationId: auth.primaryOrganizationId || null,
|
|
230
|
-
settings: authSettings
|
|
217
|
+
settings: authSettings,
|
|
218
|
+
|
|
219
|
+
alertUpdatesOnMount: auth.alertUpdatesOnMount,
|
|
231
220
|
hasPassword: auth.hasPassword,
|
|
232
|
-
|
|
221
|
+
webVersion: auth.webVersion,
|
|
233
222
|
|
|
234
223
|
// Theme
|
|
235
224
|
systemLightMode,
|
|
@@ -252,7 +241,7 @@ export function mergeAuthToDefaultValues(
|
|
|
252
241
|
* App reducer
|
|
253
242
|
*/
|
|
254
243
|
|
|
255
|
-
export const appReducer = (state:
|
|
244
|
+
export const appReducer = (state: AppContextData, action: AppContextAction) => {
|
|
256
245
|
const { __type, data } = action;
|
|
257
246
|
|
|
258
247
|
switch (__type) {
|
|
@@ -355,19 +344,6 @@ export const appReducer = (state: Partial<AppContextData>, action: AppContextAct
|
|
|
355
344
|
systemLightMode: data.lightMode,
|
|
356
345
|
};
|
|
357
346
|
}
|
|
358
|
-
case 'APP_KEY_DOWN': {
|
|
359
|
-
if (typeof data.pressed !=='string' && data.pressed !== null) {
|
|
360
|
-
return state;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
return {
|
|
364
|
-
...state,
|
|
365
|
-
keyDown: {
|
|
366
|
-
...state.keyDown,
|
|
367
|
-
...data,
|
|
368
|
-
}
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
347
|
default:
|
|
372
348
|
throw new Error('App Reducer Error: [' + __type + ']');
|
|
373
349
|
}
|
|
@@ -385,21 +361,25 @@ export const AppContext = createContext({
|
|
|
385
361
|
*/
|
|
386
362
|
|
|
387
363
|
export function useCurrentAccount() {
|
|
388
|
-
const { appState: { activated, hasPassword,
|
|
364
|
+
const { appState: { activated, hasPassword, webVersion, alertUpdatesOnMount, account, settings, primaryOrganizationId }, dispatchApp } = useContext(AppContext);
|
|
389
365
|
const hasName = !!account?.profile?.firstName || !!account?.profile?.lastName;
|
|
390
366
|
const fullName = buildSingleText([account?.profile?.firstName, account?.profile?.lastName], ' ');
|
|
391
367
|
const displayName = guessFirstName(fullName, 8);
|
|
392
368
|
|
|
393
369
|
return {
|
|
370
|
+
// Dispatch
|
|
371
|
+
dispatchApp,
|
|
372
|
+
|
|
394
373
|
// App state
|
|
395
374
|
account,
|
|
396
375
|
settings,
|
|
397
376
|
activated,
|
|
398
377
|
hasPassword,
|
|
399
|
-
|
|
378
|
+
webVersion,
|
|
379
|
+
alertUpdatesOnMount,
|
|
400
380
|
|
|
401
381
|
// Computed
|
|
402
|
-
|
|
382
|
+
isLoggedIn: !!account?.id,
|
|
403
383
|
hasName,
|
|
404
384
|
fullName,
|
|
405
385
|
displayName,
|
package/states/index.ts
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { atom, useAtom, useSetAtom } from 'jotai';
|
|
2
|
+
import { useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Types
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
type KeyDownObserverObj = {
|
|
9
|
+
metaKey: boolean;
|
|
10
|
+
pressed: null | string;
|
|
11
|
+
alert: boolean;
|
|
12
|
+
modal: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Observe reactivity for key down events
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const keyDownState = atom<KeyDownObserverObj>({
|
|
20
|
+
metaKey: false,
|
|
21
|
+
pressed: null,
|
|
22
|
+
alert: false,
|
|
23
|
+
modal: false,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Compose set key down function
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
function composeSetKeyDown(setter: (data: KeyDownObserverObj | ((prev: KeyDownObserverObj) => KeyDownObserverObj)) => void) {
|
|
31
|
+
return (data: Partial<KeyDownObserverObj>) => {
|
|
32
|
+
if (typeof data.pressed !== 'string' && data.pressed !== null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setter((prev: KeyDownObserverObj) => {
|
|
37
|
+
return {
|
|
38
|
+
...prev,
|
|
39
|
+
...data,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Export; get key down setter
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
export function useSetKeyDown() {
|
|
50
|
+
const setValue = useSetAtom(keyDownState);
|
|
51
|
+
const setKeyDown = useCallback( composeSetKeyDown(setValue), []);
|
|
52
|
+
|
|
53
|
+
return setKeyDown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Export; use key down
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
export function useKeyDown() {
|
|
62
|
+
const [value, setValue] = useAtom(keyDownState);
|
|
63
|
+
const setKeyDown = useCallback( composeSetKeyDown(setValue), []);
|
|
64
|
+
|
|
65
|
+
return [value, setKeyDown] as const;
|
|
66
|
+
}
|
package/states/modal.ts
CHANGED
|
@@ -19,6 +19,8 @@ export interface ModalProps {
|
|
|
19
19
|
[key: string]: any;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
type ModalPropsFn = (prev: ModalProps | null) => ModalProps | null;
|
|
23
|
+
|
|
22
24
|
export type OpenModalScreenFn = (data: ModalRequestParams) => void;
|
|
23
25
|
|
|
24
26
|
export type OpenModalPopUpFn = (data: ModalRequestParams | null, err?: ServerErrorObj) => void;
|
|
@@ -226,9 +228,12 @@ const popUpClass = new ModalPopUp('popup', POPUP_DEFAULT_VALUES);
|
|
|
226
228
|
* Create handler functions
|
|
227
229
|
*/
|
|
228
230
|
|
|
229
|
-
function composeOpenModalScreenFn(
|
|
230
|
-
|
|
231
|
-
|
|
231
|
+
function composeOpenModalScreenFn(
|
|
232
|
+
setScreen: (value: ModalProps | null) => void,
|
|
233
|
+
prevScreen: ModalProps | null
|
|
234
|
+
) {
|
|
235
|
+
return (data: ModalRequestParams | ModalPropsFn) => {
|
|
236
|
+
const nextScreenState = typeof data === 'function' ? data(prevScreen) : screenClass.open(data);
|
|
232
237
|
if (nextScreenState) {
|
|
233
238
|
setScreen(nextScreenState);
|
|
234
239
|
} else {
|
|
@@ -266,7 +271,7 @@ function composeCloseModalPopUpFn(setPopUp: (value: ModalProps | null) => void)
|
|
|
266
271
|
|
|
267
272
|
export function useModalScreen(): ModalScreenProps {
|
|
268
273
|
const [screen, setScreen] = useAtom(screenClass.state);
|
|
269
|
-
const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
|
|
274
|
+
const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen, screen), [screen]);
|
|
270
275
|
const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
|
|
271
276
|
|
|
272
277
|
return {
|
|
@@ -276,6 +281,17 @@ export function useModalScreen(): ModalScreenProps {
|
|
|
276
281
|
};
|
|
277
282
|
}
|
|
278
283
|
|
|
284
|
+
/**
|
|
285
|
+
* Use screen open function
|
|
286
|
+
*/
|
|
287
|
+
|
|
288
|
+
export function useOpenModalScreen(): OpenModalScreenFn {
|
|
289
|
+
const [screen, setScreen] = useAtom(screenClass.state);
|
|
290
|
+
const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen, screen), [screen]);
|
|
291
|
+
|
|
292
|
+
return openModalScreen;
|
|
293
|
+
}
|
|
294
|
+
|
|
279
295
|
/**
|
|
280
296
|
* Use modal popup hook
|
|
281
297
|
*/
|
|
@@ -297,10 +313,10 @@ export function useModalPopUp(): ModalPopUpProps {
|
|
|
297
313
|
*/
|
|
298
314
|
|
|
299
315
|
export function useModalHandlers(): ModalHandlerProps {
|
|
300
|
-
const setScreen =
|
|
316
|
+
const [screen, setScreen] = useAtom(screenClass.state);
|
|
301
317
|
const setPopUp = useSetAtom(popUpClass.state);
|
|
302
318
|
|
|
303
|
-
const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen), []);
|
|
319
|
+
const openModalScreen = useCallback( composeOpenModalScreenFn(setScreen, screen), [screen]);
|
|
304
320
|
const closeModalScreen = useCallback( composeCloseModalScreenFn(setScreen), []);
|
|
305
321
|
const openModalPopUp = useCallback( composeOpenModalPopUpFn(setPopUp), []);
|
|
306
322
|
const closeModalPopUp = useCallback( composeCloseModalPopUpFn(setPopUp), []);
|
package/states/popOver.ts
CHANGED
|
@@ -3,9 +3,7 @@ import {
|
|
|
3
3
|
CloseTooltipFn,
|
|
4
4
|
OpenPopOverFn,
|
|
5
5
|
OpenTooltipFn,
|
|
6
|
-
PopOverGlobalStateObj,
|
|
7
6
|
PopOverGlobalStateParams,
|
|
8
|
-
PopOverHookProps,
|
|
9
7
|
PopOverProps,
|
|
10
8
|
SetPopOverStateFn,
|
|
11
9
|
TooltipHookProps,
|
|
@@ -187,7 +185,7 @@ function composeSetPopOverState( setPopOver: (value: PopOverProps | null) => voi
|
|
|
187
185
|
* Use pop over
|
|
188
186
|
*/
|
|
189
187
|
|
|
190
|
-
export function usePopOver()
|
|
188
|
+
export function usePopOver() {
|
|
191
189
|
const [popOver, setPopOver] = useAtom<PopOverProps | null>(popOverClass.state);
|
|
192
190
|
const setPopOverIsHover = useSetAtom(popOverIsHoverState);
|
|
193
191
|
|
package/types/PopOver.d.ts
CHANGED
|
@@ -20,8 +20,11 @@ export interface ClientRectValues {
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
export interface PONListSubtitleObj {
|
|
23
|
-
__type: '
|
|
23
|
+
__type: 'LIST_SUBTITLE';
|
|
24
|
+
hidden?: boolean;
|
|
24
25
|
text: string;
|
|
26
|
+
textClassName?: string;
|
|
27
|
+
colorIndicator?: ColorEnum;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
export interface POListBreakObj {
|
|
@@ -30,10 +33,12 @@ export interface POListBreakObj {
|
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
export interface POListItemObj {
|
|
33
|
-
__type: 'LIST_ITEM' | 'CHECK_LIST_ITEM';
|
|
34
|
-
|
|
36
|
+
__type: 'LIST_ITEM' | 'CHECK_LIST_ITEM' | 'LIST_ITEM_POPUP';
|
|
37
|
+
name?: string; // Name for form, for formValues object; if not set, index will be used
|
|
38
|
+
value?: string | boolean | null;
|
|
35
39
|
text: string;
|
|
36
40
|
className?: string;
|
|
41
|
+
textClassName?: string;
|
|
37
42
|
colorIndicator?: ColorEnum;
|
|
38
43
|
avatarDisplayName?: string;
|
|
39
44
|
photoUri?: string | null;
|
|
@@ -42,8 +47,10 @@ export interface POListItemObj {
|
|
|
42
47
|
iconName?: string;
|
|
43
48
|
rightIconName?: string;
|
|
44
49
|
rightIconClassName?: string;
|
|
50
|
+
selected?: boolean;
|
|
45
51
|
hidden?: boolean;
|
|
46
52
|
disabled?: boolean;
|
|
53
|
+
onClick?: (name: string | null, value?: any) => void; // optional, if you want to bypass globalState handlers
|
|
47
54
|
|
|
48
55
|
// Presets
|
|
49
56
|
preset?: 'small' | 'default';
|
|
@@ -55,13 +62,20 @@ export interface POListItemObj {
|
|
|
55
62
|
// ?
|
|
56
63
|
}
|
|
57
64
|
|
|
65
|
+
export interface POModalItemObj extends POListItemObj {
|
|
66
|
+
__type: 'LIST_ITEM_POPUP' | 'LIST_ITEM_MODAL';
|
|
67
|
+
variables: any; // Variables for the pop up
|
|
68
|
+
}
|
|
69
|
+
|
|
58
70
|
export interface POCheckListItemObj extends POListItemObj {
|
|
59
71
|
__type: 'CHECK_LIST_ITEM';
|
|
60
|
-
|
|
72
|
+
name?: string; // Name for form, for formValues object; if not set, index will be used
|
|
73
|
+
hidden?: boolean;
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
export interface PONavAvatarItemObj {
|
|
64
77
|
__type: 'AVATAR_ITEM';
|
|
78
|
+
name?: string; // Name for form, for formValues object; if not set, index will be used
|
|
65
79
|
value?: string | null;
|
|
66
80
|
text: string;
|
|
67
81
|
label: string;
|
|
@@ -72,6 +86,7 @@ export interface PONavAvatarItemObj {
|
|
|
72
86
|
allowDisabledOnClick?: boolean;
|
|
73
87
|
rightIconName?: string;
|
|
74
88
|
hidden?: boolean;
|
|
89
|
+
square?: boolean;
|
|
75
90
|
disabled?: boolean;
|
|
76
91
|
|
|
77
92
|
// Web props
|
|
@@ -81,13 +96,30 @@ export interface PONavAvatarItemObj {
|
|
|
81
96
|
// ?
|
|
82
97
|
}
|
|
83
98
|
|
|
84
|
-
export interface
|
|
99
|
+
export interface PODatePickerObj {
|
|
100
|
+
__type: 'DATE_PICKER';
|
|
101
|
+
hidden?: boolean;
|
|
102
|
+
name?: string; // Name for form, for formValues object; if not set, index will be used
|
|
103
|
+
minDate?: Date; // Optional minimum date
|
|
104
|
+
maxDate?: Date; // Optional maximum date
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface PODateRangeObj extends PODatePickerObj {
|
|
85
108
|
__type: 'DATE_RANGE';
|
|
86
109
|
}
|
|
87
110
|
|
|
88
|
-
export
|
|
111
|
+
export interface POTextObj {
|
|
112
|
+
__type: 'TEXT';
|
|
113
|
+
hidden?: boolean;
|
|
114
|
+
text: string;
|
|
115
|
+
className?: string;
|
|
116
|
+
designClassName?: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export type POListIfaceItem = PONavAvatarItemObj | PONListSubtitleObj | POListBreakObj | POListItemObj | POCheckListItemObj | POModalItemObj | PODatePickerObj | PODateRangeObj | POTextObj;
|
|
120
|
+
export type POCheckListIfaceItem = PONListSubtitleObj | POListBreakObj | POCheckListItemObj;
|
|
89
121
|
|
|
90
|
-
export interface
|
|
122
|
+
export interface POListIface {
|
|
91
123
|
name: 'PO_LIST';
|
|
92
124
|
variables: {
|
|
93
125
|
notReady?: boolean;
|
|
@@ -99,15 +131,34 @@ export interface OptionsPOIface {
|
|
|
99
131
|
description?: string;
|
|
100
132
|
displayName?: string;
|
|
101
133
|
photoUri?: string | null;
|
|
102
|
-
|
|
103
|
-
options:
|
|
134
|
+
initialState?: Record<string, any>;
|
|
135
|
+
options: POListIfaceItem[];
|
|
104
136
|
addFooterButton?: boolean;
|
|
105
137
|
footerButtonText?: string;
|
|
106
138
|
LinkComponent?: any;
|
|
107
139
|
};
|
|
108
140
|
}
|
|
109
141
|
|
|
110
|
-
export interface
|
|
142
|
+
export interface POCheckListIface {
|
|
143
|
+
name: 'PO_CHECK_LIST';
|
|
144
|
+
variables: {
|
|
145
|
+
notReady?: boolean;
|
|
146
|
+
className?: string;
|
|
147
|
+
designClassName?: string;
|
|
148
|
+
savingValue?: string;
|
|
149
|
+
iconName?: string;
|
|
150
|
+
title?: string;
|
|
151
|
+
description?: string;
|
|
152
|
+
displayName?: string;
|
|
153
|
+
photoUri?: string | null;
|
|
154
|
+
options: POCheckListIfaceItem[];
|
|
155
|
+
initialState?: (string | null)[];
|
|
156
|
+
addFooterButton?: boolean;
|
|
157
|
+
footerButtonText?: string;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface POImageIface {
|
|
111
162
|
name: 'PO_VIEW_IMAGE'
|
|
112
163
|
variables: {
|
|
113
164
|
imageUri: string;
|
|
@@ -116,13 +167,13 @@ export interface ImagePOIface {
|
|
|
116
167
|
}
|
|
117
168
|
}
|
|
118
169
|
|
|
119
|
-
export type PopOverIface =
|
|
170
|
+
export type PopOverIface = POListIface | POCheckListIface | POImageIface;
|
|
120
171
|
|
|
121
172
|
/**
|
|
122
173
|
* Pop overs
|
|
123
174
|
*/
|
|
124
175
|
|
|
125
|
-
export type POPosition = 'top' | 'left' | 'right' | 'bottom' | 'bottom_left';
|
|
176
|
+
export type POPosition = 'top' | 'left' | 'right' | 'bottom' | 'bottom_left' | 'bottom_right';
|
|
126
177
|
|
|
127
178
|
interface FixedElementProps {
|
|
128
179
|
id: string; // This ensures that the popover doesn't reset from within the same mouse pointer element
|
|
@@ -145,8 +196,11 @@ export interface PopOverProps extends Partial<FixedElementProps> {
|
|
|
145
196
|
id?: string;
|
|
146
197
|
name: string; // Used to be "__type"
|
|
147
198
|
closing?: boolean; // If true, multiple close() functions are triggered to animate & close popover
|
|
199
|
+
zClassName?: string; // Class name for z-index, e.g. 'z6', 'z7', etc.
|
|
148
200
|
animationClassName?: string;
|
|
149
201
|
doNotTrackHover?: boolean;
|
|
202
|
+
doNotRemoveOnPageEvents?: boolean; // If true, popover will be removed when the user scrolls or navigation events
|
|
203
|
+
scrollAreaDOMId?: string | null; // Some pages have different parts as the main body, this is the scroll element that need to be tracked
|
|
150
204
|
globalState?: Record<string, any> | null; // Use this to listen to state changes from outside the popover
|
|
151
205
|
}
|
|
152
206
|
|
|
@@ -156,7 +210,8 @@ export interface UpdatePopOverParams {
|
|
|
156
210
|
}
|
|
157
211
|
|
|
158
212
|
export interface PopOverGlobalStateParams {
|
|
159
|
-
action: 'ITEM' | 'MOUNT' | 'UNMOUNT';
|
|
213
|
+
action: 'ITEM' | 'ITEM_AUTO' | 'SUBMIT' | 'MOUNT' | 'UNMOUNT';
|
|
214
|
+
name?: string | null; // Name of form item
|
|
160
215
|
value: any;
|
|
161
216
|
}
|
|
162
217
|
|
|
@@ -177,6 +232,7 @@ export interface PopOverHandlerProps {
|
|
|
177
232
|
}
|
|
178
233
|
|
|
179
234
|
export interface PopOverHookProps extends PopOverHandlerProps {
|
|
235
|
+
pathname?: string;
|
|
180
236
|
popOver: PopOverProps | null;
|
|
181
237
|
}
|
|
182
238
|
|
package/utils/hooks.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { areObjectsEqual } from '@jsb188/app/utils/object';
|
|
1
2
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -28,17 +29,21 @@ function getDatabaseAction(
|
|
|
28
29
|
// But if not, add an extra param.
|
|
29
30
|
const value = typeof data[key] === 'string' ? data[key].trim() : data[key];
|
|
30
31
|
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
32
|
+
const isNotNull = value !== null || allowNull;
|
|
33
|
+
if (isNotNull) {
|
|
33
34
|
documentData[key] = value;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
if (
|
|
37
38
|
// Doing loose != check instead of !== type check
|
|
38
39
|
currentData[key] != value &&
|
|
39
|
-
|
|
40
|
+
isNotNull
|
|
40
41
|
) {
|
|
41
|
-
|
|
42
|
+
if (value && typeof value === 'object') {
|
|
43
|
+
hasChanges = !areObjectsEqual(currentData[key], value);
|
|
44
|
+
} else {
|
|
45
|
+
hasChanges = true;
|
|
46
|
+
}
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
}
|
|
@@ -194,100 +199,113 @@ export function arePropsEqualExceptChildren(pp: any, np: any) {
|
|
|
194
199
|
|
|
195
200
|
type VisibilityValue = 0 | .5 | 1 | 2;
|
|
196
201
|
|
|
197
|
-
export function useAnimationVisibility(
|
|
198
|
-
props:
|
|
202
|
+
export function useAnimationVisibility<T>(
|
|
203
|
+
props: T,
|
|
199
204
|
onHasProps?: (hasProps: boolean) => void,
|
|
200
205
|
): [
|
|
201
206
|
any,
|
|
202
207
|
VisibilityValue,
|
|
203
208
|
(VisibilityValue: VisibilityValue) => void,
|
|
204
209
|
] {
|
|
205
|
-
const [
|
|
206
|
-
const [persistedProps, setPersistedProps] = useState(
|
|
210
|
+
const [visibility, setVisibility] = useState(0 as VisibilityValue);
|
|
211
|
+
const [persistedProps, setPersistedProps] = useState<T>(props as T);
|
|
207
212
|
const hasData = !!props;
|
|
208
213
|
|
|
209
214
|
useEffect(() => {
|
|
210
215
|
if (props) {
|
|
211
|
-
setPersistedProps(
|
|
212
|
-
}
|
|
213
|
-
if (onHasProps) {
|
|
214
|
-
onHasProps(hasData);
|
|
216
|
+
setPersistedProps(props);
|
|
215
217
|
}
|
|
218
|
+
onHasProps?.(hasData);
|
|
216
219
|
}, [props]);
|
|
217
220
|
|
|
218
221
|
useEffect(() => {
|
|
219
|
-
if (
|
|
222
|
+
if (visibility === 1) {
|
|
220
223
|
if (hasData) {
|
|
221
224
|
const timer = setTimeout(() => {
|
|
222
|
-
|
|
225
|
+
setVisibility(2);
|
|
223
226
|
}, 100);
|
|
224
227
|
return () => clearTimeout(timer);
|
|
225
228
|
} else {
|
|
226
229
|
const timer = setTimeout(() => {
|
|
227
|
-
|
|
230
|
+
setVisibility(0);
|
|
228
231
|
}, 300);
|
|
229
232
|
return () => clearTimeout(timer);
|
|
230
233
|
}
|
|
231
|
-
} else if (
|
|
232
|
-
|
|
233
|
-
|
|
234
|
+
} else if (visibility === .5) {
|
|
235
|
+
const timer = setTimeout(() => {
|
|
236
|
+
setVisibility(0);
|
|
237
|
+
}, 300);
|
|
238
|
+
return () => clearTimeout(timer);
|
|
234
239
|
}
|
|
235
240
|
|
|
236
241
|
const shouldBeValue = hasData ? 2 : 0;
|
|
237
|
-
if (shouldBeValue !==
|
|
238
|
-
|
|
242
|
+
if (shouldBeValue !== visibility) {
|
|
243
|
+
setVisibility(hasData ? 1 : .5);
|
|
239
244
|
}
|
|
240
|
-
}, [
|
|
245
|
+
}, [visibility, hasData]);
|
|
241
246
|
|
|
242
|
-
return [persistedProps,
|
|
247
|
+
return [persistedProps, visibility, setVisibility];
|
|
243
248
|
}
|
|
244
249
|
|
|
245
250
|
/**
|
|
246
|
-
*
|
|
251
|
+
* Check if image are loaded
|
|
252
|
+
* NOTE: This only checks on mount
|
|
247
253
|
*/
|
|
248
254
|
|
|
249
|
-
export function
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
):
|
|
253
|
-
|
|
254
|
-
(val: 0 | .5 | 1 | 2) => void
|
|
255
|
-
] {
|
|
256
|
-
|
|
257
|
-
const [visible, setVisible] = useState(0 as 0 | .5 | 1 | 2);
|
|
258
|
-
const allowOpen = useRef(openOnMount);
|
|
255
|
+
export function useImagesLoadStatus(
|
|
256
|
+
imageUrls: string[],
|
|
257
|
+
timeoutMS = 10000 // default timeout miliseconds
|
|
258
|
+
): boolean {
|
|
259
|
+
const [loaded, setLoaded] = useState(false);
|
|
259
260
|
|
|
260
261
|
useEffect(() => {
|
|
261
|
-
if (
|
|
262
|
-
|
|
262
|
+
if (!imageUrls || imageUrls.length === 0) {
|
|
263
|
+
setLoaded(true);
|
|
264
|
+
return;
|
|
263
265
|
}
|
|
264
266
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
267
|
+
let isCancelled = false;
|
|
268
|
+
let loadedCount = 0;
|
|
269
|
+
const timeout = setTimeout(() => {
|
|
270
|
+
if (!isCancelled) {
|
|
271
|
+
setLoaded(true);
|
|
272
|
+
}
|
|
273
|
+
}, timeoutMS);
|
|
271
274
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
setVisible(0);
|
|
278
|
-
}, 100);
|
|
279
|
-
return () => { clearTimeout(timer); };
|
|
275
|
+
const handleLoad = () => {
|
|
276
|
+
loadedCount += 1;
|
|
277
|
+
if (loadedCount === imageUrls.length && !isCancelled) {
|
|
278
|
+
clearTimeout(timeout);
|
|
279
|
+
setLoaded(true);
|
|
280
280
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const handleError = () => {
|
|
284
|
+
loadedCount += 1; // count errors too, so they don’t block loading
|
|
285
|
+
if (loadedCount === imageUrls.length && !isCancelled) {
|
|
286
|
+
clearTimeout(timeout);
|
|
287
|
+
setLoaded(true);
|
|
286
288
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const imageElements = imageUrls.map((url) => {
|
|
292
|
+
const img = new Image();
|
|
293
|
+
img.onload = handleLoad;
|
|
294
|
+
img.onerror = handleError;
|
|
295
|
+
img.src = url;
|
|
296
|
+
return img;
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
return () => {
|
|
300
|
+
isCancelled = true;
|
|
301
|
+
clearTimeout(timeout);
|
|
302
|
+
// Optional: clear handlers to prevent leaks
|
|
303
|
+
imageElements.forEach((img) => {
|
|
304
|
+
img.onload = null;
|
|
305
|
+
img.onerror = null;
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
}, []);
|
|
291
309
|
|
|
292
|
-
return
|
|
310
|
+
return loaded;
|
|
293
311
|
}
|
package/utils/schema.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
2
|
import i18n from '@jsb188/app/i18n';
|
|
3
3
|
import type { FormItemSchemaObj, FormSchemaObj } from '@jsb188/app/types/form.d';
|
|
4
|
+
import { getObject, setObject } from '@jsb188/app/utils/object';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Types
|
|
@@ -31,22 +32,20 @@ export function makeFormValues(
|
|
|
31
32
|
|
|
32
33
|
if (currentData || dataForSchema) {
|
|
33
34
|
if (Array.isArray(items)) {
|
|
34
|
-
items.forEach((subItem: any) => {
|
|
35
|
+
items.forEach(({ item: subItem }: any) => {
|
|
35
36
|
if (typeof subItem.setter === 'function') {
|
|
36
|
-
newFormValues
|
|
37
|
+
setObject(newFormValues, subItem.name, subItem.setter(
|
|
37
38
|
currentData,
|
|
38
39
|
dataForSchema,
|
|
39
|
-
);
|
|
40
|
+
));
|
|
40
41
|
} else if (currentData) {
|
|
41
|
-
newFormValues
|
|
42
|
+
setObject(newFormValues, subItem.name, getObject(currentData, subItem.name));
|
|
42
43
|
}
|
|
43
44
|
});
|
|
44
|
-
} else {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
newFormValues[name] = currentData[name];
|
|
49
|
-
}
|
|
45
|
+
} else if (typeof setter === 'function') {
|
|
46
|
+
setObject(newFormValues, name, setter(currentData, dataForSchema));
|
|
47
|
+
} else if (currentData) {
|
|
48
|
+
setObject(newFormValues, name, getObject(currentData, name));
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
51
|
}
|
|
@@ -87,7 +86,11 @@ export function useSchema(
|
|
|
87
86
|
const computedItem: Record<string, any> = { ...item };
|
|
88
87
|
if (computedItem) {
|
|
89
88
|
for (const key in computedItem) {
|
|
90
|
-
if (
|
|
89
|
+
if (
|
|
90
|
+
typeof computedItem[key] === 'function' &&
|
|
91
|
+
!key.startsWith('onClick') &&
|
|
92
|
+
!['getter', 'setter'].includes(key)
|
|
93
|
+
) {
|
|
91
94
|
computedItem[key] = computedItem[key](dataForSchema);
|
|
92
95
|
}
|
|
93
96
|
}
|