@jsb188/react 1.1.1 → 1.1.4
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 +5 -4
- package/states/appContext.ts +417 -1
- package/states/popOver.ts +76 -20
- package/types/PopOver.d.ts +89 -14
- package/utils/hooks.ts +14 -16
- package/utils/schema.ts +150 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsb188/react",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Contains react (and DOM) related utils and hooks.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "jsb188",
|
|
@@ -8,12 +8,13 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
"./states": "./states/index.ts",
|
|
10
10
|
"./hooks": "./utils/hooks.ts",
|
|
11
|
+
"./schema": "./utils/schema.ts",
|
|
11
12
|
"./types/PopOver.d": "./types/PopOver.d.ts"
|
|
12
13
|
},
|
|
13
14
|
"peerDependencies": {
|
|
14
|
-
"@jsb188/app": "1.1.
|
|
15
|
-
"jotai": "^2.12.
|
|
16
|
-
"react": "^
|
|
15
|
+
"@jsb188/app": "1.1.4",
|
|
16
|
+
"jotai": "^2.12.5",
|
|
17
|
+
"react": "^19.1.0"
|
|
17
18
|
},
|
|
18
19
|
"scripts": {
|
|
19
20
|
"test": "echo \"Error: no test specified\" && exit 1"
|
package/states/appContext.ts
CHANGED
|
@@ -1,2 +1,418 @@
|
|
|
1
|
+
import { clearAppCache, fetchLightModeSetting, isSystemDarkMode, saveAuthTokenToPlatform } from '@jsb188/app/platform';
|
|
2
|
+
import type { AccountData, AccountSettings, AuthenticationData, LightModeEnum } from '@jsb188/app/types/auth.d';
|
|
3
|
+
import { setAuthToken } from '@jsb188/app/utils/api';
|
|
4
|
+
import { guessFirstName, buildSingleText } from '@jsb188/app/utils/string';
|
|
5
|
+
import { createContext, useContext } from 'react';
|
|
1
6
|
|
|
2
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Type; Reducer action for App Context
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type AppContextAction = {
|
|
12
|
+
__type: (
|
|
13
|
+
// App initialization
|
|
14
|
+
| 'APP_INIT'
|
|
15
|
+
// Auth setup
|
|
16
|
+
| 'APP_LOGIN'
|
|
17
|
+
| 'APP_GUEST'
|
|
18
|
+
| 'APP_EDIT_AUTH'
|
|
19
|
+
| 'APP_EDIT_ACCOUNT'
|
|
20
|
+
// Account theme
|
|
21
|
+
| 'APP_THEME'
|
|
22
|
+
| 'APP_SYSTEM_LIGHT_MODE'
|
|
23
|
+
| 'APP_LIGHT_MODE'
|
|
24
|
+
// App events
|
|
25
|
+
| 'APP_KEY_DOWN'
|
|
26
|
+
);
|
|
27
|
+
data?: any;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type AppContextDispatch = (action: AppContextAction) => void;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Types
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
export type AppAppearanceType = {
|
|
37
|
+
realLightMode: LightModeEnum;
|
|
38
|
+
lightMode: LightModeEnum;
|
|
39
|
+
previewLightMode: LightModeEnum;
|
|
40
|
+
previewTheme: string | null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type AppContextData = {
|
|
44
|
+
// Required for app
|
|
45
|
+
init: 0 | 1 | 2; // 0 = not initialized,; 1 = loading; 2 = initialized
|
|
46
|
+
authorization: string; // This is token
|
|
47
|
+
networkError: boolean;
|
|
48
|
+
|
|
49
|
+
// Authentication data
|
|
50
|
+
account: AccountData | null;
|
|
51
|
+
primaryOrganizationId: string | null;
|
|
52
|
+
|
|
53
|
+
// Account settings & state
|
|
54
|
+
settings: AccountSettings;
|
|
55
|
+
activated: boolean;
|
|
56
|
+
hasPassword: boolean;
|
|
57
|
+
version: string | null;
|
|
58
|
+
|
|
59
|
+
// Preview theme overrides active theme
|
|
60
|
+
systemLightMode: 'LIGHT' | 'DARK';
|
|
61
|
+
previewLightMode: LightModeEnum | null;
|
|
62
|
+
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
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Context default values
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
export const DEFAULT_APP_CONTEXT = {
|
|
78
|
+
init: 0,
|
|
79
|
+
authorization: '',
|
|
80
|
+
networkError: false,
|
|
81
|
+
|
|
82
|
+
account: null,
|
|
83
|
+
primaryOrganizationId: null,
|
|
84
|
+
|
|
85
|
+
settings: {
|
|
86
|
+
theme: null,
|
|
87
|
+
lightMode: 'LIGHT',
|
|
88
|
+
timeZone: null,
|
|
89
|
+
showSelfAvatar: true,
|
|
90
|
+
isBubbleOther: false,
|
|
91
|
+
showOtherAvatar: false,
|
|
92
|
+
},
|
|
93
|
+
activated: false,
|
|
94
|
+
hasPassword: false,
|
|
95
|
+
version: null,
|
|
96
|
+
|
|
97
|
+
systemLightMode: 'LIGHT',
|
|
98
|
+
previewLightMode: null,
|
|
99
|
+
previewTheme: null,
|
|
100
|
+
|
|
101
|
+
keyDown: {
|
|
102
|
+
metaKey: false,
|
|
103
|
+
pressed: null,
|
|
104
|
+
alert: false,
|
|
105
|
+
modal: false,
|
|
106
|
+
}
|
|
107
|
+
} as AppContextData;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Helper for light mode
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
function ensureLightModeSetting(lightMode?: LightModeEnum): LightModeEnum {
|
|
114
|
+
return lightMode && ['LIGHT', 'DARK', 'SYSTEM'].includes(lightMode || '') ? lightMode : 'LIGHT';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* App log in with Promise
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
export function onAccountLogin(auth: any, dispatchApp: AppContextDispatch, otherCB?: (auth: any) => void) {
|
|
122
|
+
if (auth && auth?.token && auth.activated && auth.account) {
|
|
123
|
+
console.log('.. 🥝 logged into account ..');
|
|
124
|
+
|
|
125
|
+
saveAuthTokenToPlatform(auth.token, () => {
|
|
126
|
+
clearAppCache();
|
|
127
|
+
dispatchApp({
|
|
128
|
+
__type: 'APP_LOGIN',
|
|
129
|
+
data: auth,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (otherCB) {
|
|
133
|
+
otherCB(auth);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
} else {
|
|
137
|
+
console.log(auth);
|
|
138
|
+
console.warn('Developer error in onAccountLogin()');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* App guest init
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
export function onGuestInit(error: any, dispatchApp: AppContextDispatch) {
|
|
147
|
+
fetchLightModeSetting(null, (lightMode) => {
|
|
148
|
+
dispatchApp({
|
|
149
|
+
__type: 'APP_GUEST',
|
|
150
|
+
data: {
|
|
151
|
+
error,
|
|
152
|
+
lightMode,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Get active/current or preview theme
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
export function getTheme(
|
|
163
|
+
appState: AppContextData,
|
|
164
|
+
priority: 'PREVIEW' | 'ACCOUNT' | 'ACTIVE'
|
|
165
|
+
) {
|
|
166
|
+
let lightMode = appState.previewLightMode || appState.settings.lightMode;
|
|
167
|
+
if (lightMode === 'SYSTEM') {
|
|
168
|
+
lightMode = appState.systemLightMode;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const theme = (
|
|
172
|
+
(priority === 'PREVIEW' && appState.previewTheme) ||
|
|
173
|
+
appState.settings.theme ||
|
|
174
|
+
''
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
lightMode,
|
|
179
|
+
theme,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get theme; but prioritize preview theme
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
export function getPreviewAppearance(appState: AppContextData) {
|
|
188
|
+
let realLightMode = appState.previewLightMode || appState.settings.lightMode;
|
|
189
|
+
if (realLightMode === 'SYSTEM') {
|
|
190
|
+
realLightMode = appState.systemLightMode;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
realLightMode,
|
|
195
|
+
lightMode: appState.settings.lightMode,
|
|
196
|
+
previewLightMode: appState.previewLightMode,
|
|
197
|
+
previewTheme: appState.previewTheme,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Merge sign in mutation auth values to context
|
|
203
|
+
*/
|
|
204
|
+
|
|
205
|
+
export function mergeAuthToDefaultValues(
|
|
206
|
+
auth?: AuthenticationData | null,
|
|
207
|
+
authToken?: string | null,
|
|
208
|
+
) {
|
|
209
|
+
if (auth?.token && auth?.account?.id) {
|
|
210
|
+
let systemLightMode = 'LIGHT';
|
|
211
|
+
if (isSystemDarkMode()) {
|
|
212
|
+
systemLightMode = 'DARK';
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
setAuthToken(auth.token);
|
|
216
|
+
|
|
217
|
+
const authSettings = {
|
|
218
|
+
...DEFAULT_APP_CONTEXT.settings,
|
|
219
|
+
...auth.settings
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
authSettings.lightMode = ensureLightModeSetting(authSettings.lightMode);
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
...DEFAULT_APP_CONTEXT,
|
|
226
|
+
init: 1, // Always 1 because we're always using SSR for authentication
|
|
227
|
+
authorization: auth.token,
|
|
228
|
+
account: auth.account,
|
|
229
|
+
primaryOrganizationId: auth.primaryOrganizationId || null,
|
|
230
|
+
settings: authSettings,
|
|
231
|
+
hasPassword: auth.hasPassword,
|
|
232
|
+
version: auth.version,
|
|
233
|
+
|
|
234
|
+
// Theme
|
|
235
|
+
systemLightMode,
|
|
236
|
+
previewLightMode: null,
|
|
237
|
+
previewTheme: null,
|
|
238
|
+
};
|
|
239
|
+
} else if (authToken) {
|
|
240
|
+
setAuthToken(authToken);
|
|
241
|
+
return {
|
|
242
|
+
...DEFAULT_APP_CONTEXT,
|
|
243
|
+
authorization: authToken || ''
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
setAuthToken(null);
|
|
248
|
+
return DEFAULT_APP_CONTEXT;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* App reducer
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
export const appReducer = (state: AppContextData, action: AppContextAction) => {
|
|
256
|
+
const { __type, data } = action;
|
|
257
|
+
|
|
258
|
+
switch (__type) {
|
|
259
|
+
case 'APP_INIT': {
|
|
260
|
+
return {
|
|
261
|
+
...state,
|
|
262
|
+
init: state.init === 1 ? 2 : 1,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
case 'APP_LOGIN': {
|
|
266
|
+
return mergeAuthToDefaultValues(data);
|
|
267
|
+
}
|
|
268
|
+
case 'APP_GUEST': {
|
|
269
|
+
const networkError = data.error?.errorCode === 'network_error';
|
|
270
|
+
if (networkError) {
|
|
271
|
+
console.log('.. ❌ network error ..');
|
|
272
|
+
} else {
|
|
273
|
+
console.log('.. 🥝 guest entered ..');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
clearAppCache();
|
|
277
|
+
setAuthToken(null);
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
...DEFAULT_APP_CONTEXT,
|
|
281
|
+
init: 2,
|
|
282
|
+
authorization: '',
|
|
283
|
+
networkError,
|
|
284
|
+
|
|
285
|
+
// Theme
|
|
286
|
+
previewLightMode: null,
|
|
287
|
+
previewTheme: null,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
case 'APP_EDIT_AUTH': {
|
|
291
|
+
if (data.id === state.account?.id) {
|
|
292
|
+
return {
|
|
293
|
+
...state,
|
|
294
|
+
hasPassword: typeof data.hasPassword === 'boolean' ? data.hasPassword : state.hasPassword,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
return state;
|
|
298
|
+
}
|
|
299
|
+
case 'APP_EDIT_ACCOUNT': {
|
|
300
|
+
if (data.id === state.account?.id) {
|
|
301
|
+
return {
|
|
302
|
+
...state,
|
|
303
|
+
|
|
304
|
+
// Account
|
|
305
|
+
account: {
|
|
306
|
+
...state.account,
|
|
307
|
+
...data,
|
|
308
|
+
plus: !data.plus ? state.account?.plus : {
|
|
309
|
+
...state.account?.plus,
|
|
310
|
+
...data.plus,
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
settings: {
|
|
314
|
+
...DEFAULT_APP_CONTEXT.settings,
|
|
315
|
+
...data.settings,
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
return state;
|
|
320
|
+
}
|
|
321
|
+
case 'APP_THEME': {
|
|
322
|
+
if (!data?.preview) {
|
|
323
|
+
// Active theme
|
|
324
|
+
return {
|
|
325
|
+
...state,
|
|
326
|
+
previewTheme: null,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Is preview theme
|
|
331
|
+
return {
|
|
332
|
+
...state,
|
|
333
|
+
previewTheme: data.theme,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
case 'APP_LIGHT_MODE': {
|
|
337
|
+
if (!data.lightMode && !data.preview) {
|
|
338
|
+
console.warn('Developer error in setLightMode(): You cannot set active lightMode to null.');
|
|
339
|
+
return state;
|
|
340
|
+
} else if (data.preview) {
|
|
341
|
+
return {
|
|
342
|
+
...state,
|
|
343
|
+
previewLightMode: data.lightMode,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return {
|
|
348
|
+
...state,
|
|
349
|
+
previewLightMode: null,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
case 'APP_SYSTEM_LIGHT_MODE': {
|
|
353
|
+
return {
|
|
354
|
+
...state,
|
|
355
|
+
systemLightMode: data.lightMode,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
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
|
+
default:
|
|
372
|
+
throw new Error('App Reducer Error: [' + __type + ']');
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
export const AppContext = createContext({
|
|
377
|
+
appState: DEFAULT_APP_CONTEXT as AppContextData,
|
|
378
|
+
dispatchApp: (() => {
|
|
379
|
+
console.warn('AppContext has been re-initialized due to hot refresh.');
|
|
380
|
+
}) as AppContextDispatch
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Use current account
|
|
385
|
+
*/
|
|
386
|
+
|
|
387
|
+
export function useCurrentAccount() {
|
|
388
|
+
const { appState: { activated, hasPassword, version, account, settings, primaryOrganizationId } } = useContext(AppContext);
|
|
389
|
+
const hasName = !!account?.profile?.firstName || !!account?.profile?.lastName;
|
|
390
|
+
const fullName = buildSingleText([account?.profile?.firstName, account?.profile?.lastName], ' ');
|
|
391
|
+
const displayName = guessFirstName(fullName, 8);
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
// App state
|
|
395
|
+
account,
|
|
396
|
+
settings,
|
|
397
|
+
activated,
|
|
398
|
+
hasPassword,
|
|
399
|
+
version,
|
|
400
|
+
|
|
401
|
+
// Computed
|
|
402
|
+
isLoggedIn: !!account?.id,
|
|
403
|
+
hasName,
|
|
404
|
+
fullName,
|
|
405
|
+
displayName,
|
|
406
|
+
labelName: displayName || account?.email?.address || '?',
|
|
407
|
+
primaryOrganizationId
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Check if logged in
|
|
413
|
+
*/
|
|
414
|
+
|
|
415
|
+
export function useIsLoggedIn() {
|
|
416
|
+
const { appState: { account } } = useContext(AppContext);
|
|
417
|
+
return !!account?.id;
|
|
418
|
+
}
|
package/states/popOver.ts
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai';
|
|
2
|
-
import { useCallback } from 'react';
|
|
3
1
|
import {
|
|
4
2
|
ClosePopOverFn,
|
|
5
3
|
CloseTooltipFn,
|
|
6
4
|
OpenPopOverFn,
|
|
7
5
|
OpenTooltipFn,
|
|
6
|
+
PopOverGlobalStateObj,
|
|
7
|
+
PopOverGlobalStateParams,
|
|
8
8
|
PopOverHookProps,
|
|
9
9
|
PopOverProps,
|
|
10
|
+
SetPopOverStateFn,
|
|
10
11
|
TooltipHookProps,
|
|
11
12
|
TooltipProps,
|
|
12
13
|
UpdatePopOverFn,
|
|
14
|
+
UpdatePopOverParams,
|
|
13
15
|
UpdateTooltipFn
|
|
14
16
|
} from '@jsb188/react/types/PopOver.d';
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* Type; Pop overs
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
interface UpdateStateParams {
|
|
21
|
-
name: string;
|
|
22
|
-
variables: Record<string, any>;
|
|
23
|
-
}
|
|
17
|
+
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai';
|
|
18
|
+
import { useCallback } from 'react';
|
|
24
19
|
|
|
25
20
|
/**
|
|
26
21
|
* Pop over class
|
|
@@ -37,7 +32,7 @@ class PopOver {
|
|
|
37
32
|
return data;
|
|
38
33
|
}
|
|
39
34
|
|
|
40
|
-
update(data:
|
|
35
|
+
update(data: UpdatePopOverParams, prev: PopOverProps | null): PopOverProps | null {
|
|
41
36
|
const { name, ...variables } = data;
|
|
42
37
|
const nextName = name || prev?.name
|
|
43
38
|
if (!nextName) {
|
|
@@ -50,8 +45,39 @@ class PopOver {
|
|
|
50
45
|
};
|
|
51
46
|
}
|
|
52
47
|
|
|
53
|
-
close(): PopOverProps | null {
|
|
54
|
-
|
|
48
|
+
close(prev: PopOverProps | null): PopOverProps | null {
|
|
49
|
+
if (!prev || !prev.animationClassName) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let nextAnimClassName = prev.animationClassName;
|
|
54
|
+
if (/\bon_mount\b/.test(nextAnimClassName)) {
|
|
55
|
+
nextAnimClassName = nextAnimClassName.replace(/\bon_mount\b/, '').trim();
|
|
56
|
+
} else if (!/\breverse\b/.test(nextAnimClassName)) {
|
|
57
|
+
nextAnimClassName += ' reverse';
|
|
58
|
+
} else {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
...prev,
|
|
64
|
+
closing: true,
|
|
65
|
+
animationClassName: nextAnimClassName,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
setGlobalState(globalState: PopOverGlobalStateParams | null, prev: PopOverProps | null): PopOverProps | null {
|
|
70
|
+
if (!prev) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
...prev,
|
|
76
|
+
globalState: !globalState ? null : {
|
|
77
|
+
id: prev.id,
|
|
78
|
+
...globalState
|
|
79
|
+
},
|
|
80
|
+
};
|
|
55
81
|
}
|
|
56
82
|
}
|
|
57
83
|
|
|
@@ -127,18 +153,31 @@ function composeClosePopOverFn(
|
|
|
127
153
|
return;
|
|
128
154
|
}
|
|
129
155
|
setPopOverIsHover(false);
|
|
130
|
-
setPopOver(popOverClass.close());
|
|
156
|
+
setPopOver(popOverClass.close(popOver));
|
|
131
157
|
};
|
|
132
158
|
}
|
|
133
159
|
|
|
134
160
|
function composeUpdatePopOverFn( setPopOver: (value: PopOverProps | null) => void): UpdatePopOverFn {
|
|
135
|
-
return (data:
|
|
161
|
+
return (data: UpdatePopOverParams) => {
|
|
136
162
|
setPopOver((prev: PopOverProps | null) => {
|
|
137
163
|
const nextPopOverState = popOverClass.update(data, prev);
|
|
138
164
|
if (nextPopOverState) {
|
|
139
165
|
return nextPopOverState;
|
|
140
166
|
}
|
|
141
|
-
console.warn('
|
|
167
|
+
console.warn('{popOver} data should never be null when using update():', data);
|
|
168
|
+
return prev;
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function composeSetPopOverState( setPopOver: (value: PopOverProps | null) => void): SetPopOverStateFn {
|
|
174
|
+
return (data: PopOverGlobalStateParams | null) => {
|
|
175
|
+
setPopOver((prev: PopOverProps | null) => {
|
|
176
|
+
const nextPopOverState = popOverClass.setGlobalState(data, prev);
|
|
177
|
+
if (nextPopOverState) {
|
|
178
|
+
return nextPopOverState;
|
|
179
|
+
}
|
|
180
|
+
console.warn('{popOver} data should never be null when using setGlobalState():', data);
|
|
142
181
|
return prev;
|
|
143
182
|
});
|
|
144
183
|
};
|
|
@@ -154,15 +193,32 @@ export function usePopOver(): PopOverHookProps {
|
|
|
154
193
|
|
|
155
194
|
const openPopOver = useCallback( composeOpenPopOverFn(setPopOver), []);
|
|
156
195
|
const closePopOver = useCallback( composeClosePopOverFn(setPopOver, setPopOverIsHover, popOver), [popOver]);
|
|
157
|
-
|
|
158
|
-
// Test to make sure updatePopOver updates previous state even with [] deps
|
|
159
196
|
const updatePopOver = useCallback( composeUpdatePopOverFn(setPopOver), []);
|
|
197
|
+
const setPopOverState = useCallback( composeSetPopOverState(setPopOver), []);
|
|
160
198
|
|
|
161
199
|
return {
|
|
162
200
|
popOver,
|
|
163
201
|
openPopOver,
|
|
164
202
|
closePopOver,
|
|
165
203
|
updatePopOver,
|
|
204
|
+
setPopOverState
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Use pop over global state
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
export function usePopOverState() {
|
|
213
|
+
const [popOver, setPopOver] = useAtom<PopOverProps | null>(popOverClass.state);
|
|
214
|
+
const setPopOverIsHover = useSetAtom(popOverIsHoverState);
|
|
215
|
+
const updatePopOver = useCallback( composeUpdatePopOverFn(setPopOver), []);
|
|
216
|
+
const closePopOver = useCallback( composeClosePopOverFn(setPopOver, setPopOverIsHover, popOver), [popOver]);
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
popOverState: popOver?.globalState,
|
|
220
|
+
closePopOver,
|
|
221
|
+
updatePopOver
|
|
166
222
|
};
|
|
167
223
|
}
|
|
168
224
|
|
|
@@ -192,7 +248,7 @@ function composeCloseTooltipFn( setTooltip: (value: TooltipProps | null) => void
|
|
|
192
248
|
if (prev?.id !== removeId) {
|
|
193
249
|
return prev;
|
|
194
250
|
}
|
|
195
|
-
return
|
|
251
|
+
return tooltipClass.close();
|
|
196
252
|
});
|
|
197
253
|
};
|
|
198
254
|
}
|
package/types/PopOver.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ColorEnum } from '@jsb188/app/types/app.d';
|
|
1
2
|
|
|
2
3
|
/**
|
|
3
4
|
* Utility types
|
|
@@ -18,29 +19,82 @@ export interface ClientRectValues {
|
|
|
18
19
|
* Pop over interfaces
|
|
19
20
|
*/
|
|
20
21
|
|
|
21
|
-
export interface
|
|
22
|
-
__type
|
|
23
|
-
|
|
22
|
+
export interface PONListSubtitleObj {
|
|
23
|
+
__type: 'SUBTITLE';
|
|
24
|
+
hidden?: boolean;
|
|
25
|
+
text: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface POListBreakObj {
|
|
29
|
+
__type: 'BREAK';
|
|
30
|
+
hidden?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface POListItemObj {
|
|
34
|
+
__type: 'LIST_ITEM' | 'CHECK_LIST_ITEM';
|
|
35
|
+
value?: string | null;
|
|
24
36
|
text: string;
|
|
25
37
|
className?: string;
|
|
26
|
-
|
|
38
|
+
colorIndicator?: ColorEnum;
|
|
39
|
+
avatarDisplayName?: string;
|
|
27
40
|
photoUri?: string | null;
|
|
28
|
-
href?: string;
|
|
29
|
-
to?: string;
|
|
30
41
|
LinkComponent?: any;
|
|
31
42
|
allowDisabledOnClick?: boolean;
|
|
32
43
|
iconName?: string;
|
|
33
44
|
rightIconName?: string;
|
|
45
|
+
rightIconClassName?: string;
|
|
46
|
+
hidden?: boolean;
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
|
|
49
|
+
// Presets
|
|
50
|
+
preset?: 'small' | 'default';
|
|
51
|
+
|
|
52
|
+
// Web props
|
|
53
|
+
to?: string;
|
|
54
|
+
|
|
55
|
+
// Mobile props
|
|
56
|
+
// ?
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface POCheckListItemObj extends POListItemObj {
|
|
60
|
+
__type: 'CHECK_LIST_ITEM';
|
|
61
|
+
hidden?: boolean;
|
|
62
|
+
checked?: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface PONavAvatarItemObj {
|
|
66
|
+
__type: 'AVATAR_ITEM';
|
|
67
|
+
value?: string | null;
|
|
68
|
+
text: string;
|
|
69
|
+
label: string;
|
|
70
|
+
avatarDisplayName?: string;
|
|
71
|
+
photoUri?: string | null;
|
|
72
|
+
className?: string;
|
|
73
|
+
LinkComponent?: any;
|
|
74
|
+
allowDisabledOnClick?: boolean;
|
|
75
|
+
rightIconName?: string;
|
|
34
76
|
hidden?: boolean;
|
|
35
77
|
disabled?: boolean;
|
|
78
|
+
|
|
79
|
+
// Web props
|
|
80
|
+
to?: string;
|
|
81
|
+
|
|
82
|
+
// Mobile props
|
|
83
|
+
// ?
|
|
36
84
|
}
|
|
37
85
|
|
|
86
|
+
export interface PODateRangeObj {
|
|
87
|
+
__type: 'DATE_RANGE';
|
|
88
|
+
hidden?: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type PONavItemObj = PONListSubtitleObj | POListBreakObj | POListItemObj | POCheckListItemObj | PONavAvatarItemObj | PODateRangeObj;
|
|
92
|
+
|
|
38
93
|
export interface OptionsPOIface {
|
|
39
|
-
name: '
|
|
94
|
+
name: 'PO_LIST';
|
|
40
95
|
variables: {
|
|
41
96
|
notReady?: boolean;
|
|
42
|
-
|
|
43
|
-
backgroundClassName?: string;
|
|
97
|
+
designClassName?: string;
|
|
44
98
|
className?: string;
|
|
45
99
|
savingValue?: string;
|
|
46
100
|
iconName?: string;
|
|
@@ -49,14 +103,15 @@ export interface OptionsPOIface {
|
|
|
49
103
|
displayName?: string;
|
|
50
104
|
photoUri?: string | null;
|
|
51
105
|
selectedValue?: string | null;
|
|
52
|
-
options:
|
|
53
|
-
|
|
106
|
+
options: PONavItemObj[];
|
|
107
|
+
addFooterButton?: boolean;
|
|
108
|
+
footerButtonText?: string;
|
|
54
109
|
LinkComponent?: any;
|
|
55
110
|
};
|
|
56
111
|
}
|
|
57
112
|
|
|
58
113
|
export interface ImagePOIface {
|
|
59
|
-
name: '
|
|
114
|
+
name: 'PO_VIEW_IMAGE'
|
|
60
115
|
variables: {
|
|
61
116
|
imageUri: string;
|
|
62
117
|
alt?: string;
|
|
@@ -70,7 +125,7 @@ export type PopOverIface = OptionsPOIface | ImagePOIface;
|
|
|
70
125
|
* Pop overs
|
|
71
126
|
*/
|
|
72
127
|
|
|
73
|
-
export type POPosition = 'top' | 'left' | 'right' | 'bottom';
|
|
128
|
+
export type POPosition = 'top' | 'left' | 'right' | 'bottom' | 'bottom_left';
|
|
74
129
|
|
|
75
130
|
interface FixedElementProps {
|
|
76
131
|
id: string; // This ensures that the popover doesn't reset from within the same mouse pointer element
|
|
@@ -90,18 +145,38 @@ interface FixedElementProps {
|
|
|
90
145
|
}
|
|
91
146
|
|
|
92
147
|
export interface PopOverProps extends Partial<FixedElementProps> {
|
|
148
|
+
id?: string;
|
|
93
149
|
name: string; // Used to be "__type"
|
|
150
|
+
closing?: boolean; // If true, multiple close() functions are triggered to animate & close popover
|
|
151
|
+
animationClassName?: string;
|
|
94
152
|
doNotTrackHover?: boolean;
|
|
153
|
+
globalState?: Record<string, any> | null; // Use this to listen to state changes from outside the popover
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface UpdatePopOverParams {
|
|
157
|
+
name: string;
|
|
158
|
+
variables: Record<string, any>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface PopOverGlobalStateParams {
|
|
162
|
+
action: 'ITEM' | 'SUBMIT' | 'MOUNT' | 'UNMOUNT';
|
|
163
|
+
value: any;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface PopOverGlobalStateObj extends PopOverGlobalStateParams {
|
|
167
|
+
id: string;
|
|
95
168
|
}
|
|
96
169
|
|
|
97
170
|
export type OpenPopOverFn = (data: PopOverProps) => void;
|
|
98
171
|
export type ClosePopOverFn = (dismissIfName?: string) => void;
|
|
99
|
-
export type UpdatePopOverFn = (data:
|
|
172
|
+
export type UpdatePopOverFn = (data: UpdatePopOverParams) => void;
|
|
173
|
+
export type SetPopOverStateFn = (data: PopOverGlobalStateParams | null) => void;
|
|
100
174
|
|
|
101
175
|
export interface PopOverHandlerProps {
|
|
102
176
|
openPopOver: OpenPopOverFn;
|
|
103
177
|
closePopOver: ClosePopOverFn;
|
|
104
178
|
updatePopOver: UpdatePopOverFn;
|
|
179
|
+
setPopOverState: SetPopOverStateFn;
|
|
105
180
|
}
|
|
106
181
|
|
|
107
182
|
export interface PopOverHookProps extends PopOverHandlerProps {
|
package/utils/hooks.ts
CHANGED
|
@@ -194,52 +194,50 @@ export function arePropsEqualExceptChildren(pp: any, np: any) {
|
|
|
194
194
|
|
|
195
195
|
type VisibilityValue = 0 | .5 | 1 | 2;
|
|
196
196
|
|
|
197
|
-
export function useAnimationVisibility(
|
|
198
|
-
props:
|
|
197
|
+
export function useAnimationVisibility<T>(
|
|
198
|
+
props: T,
|
|
199
199
|
onHasProps?: (hasProps: boolean) => void,
|
|
200
200
|
): [
|
|
201
201
|
any,
|
|
202
202
|
VisibilityValue,
|
|
203
203
|
(VisibilityValue: VisibilityValue) => void,
|
|
204
204
|
] {
|
|
205
|
-
const [
|
|
206
|
-
const [persistedProps, setPersistedProps] = useState(
|
|
205
|
+
const [visibility, setVisibility] = useState(0 as VisibilityValue);
|
|
206
|
+
const [persistedProps, setPersistedProps] = useState<T>(props as T);
|
|
207
207
|
const hasData = !!props;
|
|
208
208
|
|
|
209
209
|
useEffect(() => {
|
|
210
210
|
if (props) {
|
|
211
|
-
setPersistedProps(
|
|
212
|
-
}
|
|
213
|
-
if (onHasProps) {
|
|
214
|
-
onHasProps(hasData);
|
|
211
|
+
setPersistedProps(props);
|
|
215
212
|
}
|
|
213
|
+
onHasProps?.(hasData);
|
|
216
214
|
}, [props]);
|
|
217
215
|
|
|
218
216
|
useEffect(() => {
|
|
219
|
-
if (
|
|
217
|
+
if (visibility === 1) {
|
|
220
218
|
if (hasData) {
|
|
221
219
|
const timer = setTimeout(() => {
|
|
222
|
-
|
|
220
|
+
setVisibility(2);
|
|
223
221
|
}, 100);
|
|
224
222
|
return () => clearTimeout(timer);
|
|
225
223
|
} else {
|
|
226
224
|
const timer = setTimeout(() => {
|
|
227
|
-
|
|
225
|
+
setVisibility(0);
|
|
228
226
|
}, 300);
|
|
229
227
|
return () => clearTimeout(timer);
|
|
230
228
|
}
|
|
231
|
-
} else if (
|
|
229
|
+
} else if (visibility === .5) {
|
|
232
230
|
// Use custom exit function if .5
|
|
233
231
|
return;
|
|
234
232
|
}
|
|
235
233
|
|
|
236
234
|
const shouldBeValue = hasData ? 2 : 0;
|
|
237
|
-
if (shouldBeValue !==
|
|
238
|
-
|
|
235
|
+
if (shouldBeValue !== visibility) {
|
|
236
|
+
setVisibility(1);
|
|
239
237
|
}
|
|
240
|
-
}, [
|
|
238
|
+
}, [visibility, hasData]);
|
|
241
239
|
|
|
242
|
-
return [persistedProps,
|
|
240
|
+
return [persistedProps, visibility, setVisibility];
|
|
243
241
|
}
|
|
244
242
|
|
|
245
243
|
/**
|
package/utils/schema.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import i18n from '@jsb188/app/i18n';
|
|
3
|
+
import type { FormItemSchemaObj, FormSchemaObj } from '@jsb188/app/types/form.d';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Types
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
interface ComputedSchema {
|
|
10
|
+
formValues: any;
|
|
11
|
+
setFormValues: (formValues: any) => void;
|
|
12
|
+
listData: (FormItemSchemaObj & { name: string })[];
|
|
13
|
+
validate?: () => any;
|
|
14
|
+
isButtonDisabled?: (formValues: any) => boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Make form values from current form values, current data, and schema
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function makeFormValues(
|
|
22
|
+
schema: any,
|
|
23
|
+
dataForSchema?: any,
|
|
24
|
+
currentData?: any,
|
|
25
|
+
) {
|
|
26
|
+
const newFormValues: Record<string, any> = {};
|
|
27
|
+
const { listData } = schema;
|
|
28
|
+
|
|
29
|
+
for (const obj of listData) {
|
|
30
|
+
const { item: { name, setter, items } } = obj;
|
|
31
|
+
|
|
32
|
+
if (currentData || dataForSchema) {
|
|
33
|
+
if (Array.isArray(items)) {
|
|
34
|
+
items.forEach((subItem: any) => {
|
|
35
|
+
if (typeof subItem.setter === 'function') {
|
|
36
|
+
newFormValues[subItem.name] = subItem.setter(
|
|
37
|
+
currentData,
|
|
38
|
+
dataForSchema,
|
|
39
|
+
);
|
|
40
|
+
} else if (currentData) {
|
|
41
|
+
newFormValues[subItem.name] = currentData[subItem.name];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
} else {
|
|
45
|
+
if (typeof setter === 'function') {
|
|
46
|
+
newFormValues[name] = setter(currentData, dataForSchema);
|
|
47
|
+
} else if (currentData) {
|
|
48
|
+
newFormValues[name] = currentData[name];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return newFormValues;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolve schema using data
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
export function useSchema(
|
|
62
|
+
schema: FormSchemaObj,
|
|
63
|
+
dataForSchema: any,
|
|
64
|
+
currentData?: any,
|
|
65
|
+
): ComputedSchema {
|
|
66
|
+
const { listData, rules, isButtonDisabled } = schema;
|
|
67
|
+
const [formValues, setFormValues] = useState(makeFormValues(schema, dataForSchema, currentData));
|
|
68
|
+
|
|
69
|
+
// Make listData from schema
|
|
70
|
+
|
|
71
|
+
const computedListData: any[] = listData.map((schemaItem: FormItemSchemaObj) => {
|
|
72
|
+
const { __type, hidden, locked, item } = schemaItem;
|
|
73
|
+
if (
|
|
74
|
+
hidden === true ||
|
|
75
|
+
(typeof hidden === 'function' && hidden(dataForSchema))
|
|
76
|
+
) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let lockedValue;
|
|
81
|
+
if (typeof locked === 'function') {
|
|
82
|
+
lockedValue = locked(dataForSchema);
|
|
83
|
+
} else if (typeof locked === 'boolean') {
|
|
84
|
+
lockedValue = locked;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const computedItem: Record<string, any> = { ...item };
|
|
88
|
+
if (computedItem) {
|
|
89
|
+
for (const key in computedItem) {
|
|
90
|
+
if (typeof computedItem[key] === 'function') {
|
|
91
|
+
computedItem[key] = computedItem[key](dataForSchema);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
__type,
|
|
98
|
+
locked: lockedValue,
|
|
99
|
+
item: computedItem,
|
|
100
|
+
};
|
|
101
|
+
}).filter(Boolean);
|
|
102
|
+
|
|
103
|
+
// Create validation function
|
|
104
|
+
|
|
105
|
+
const validate = (): any => {
|
|
106
|
+
if (rules) {
|
|
107
|
+
const __errorFields: string[] = [];
|
|
108
|
+
|
|
109
|
+
let firstError: any = null;
|
|
110
|
+
for (const ruleObj of rules) {
|
|
111
|
+
const { for: key, rule, error } = ruleObj;
|
|
112
|
+
const value = formValues[key];
|
|
113
|
+
const isValid = rule(value, formValues);
|
|
114
|
+
|
|
115
|
+
if (!isValid) {
|
|
116
|
+
if (!__errorFields.includes(key)) {
|
|
117
|
+
__errorFields.push(key);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!firstError) {
|
|
121
|
+
if (error) {
|
|
122
|
+
firstError = typeof error === 'function' ? error(value) : error;
|
|
123
|
+
} else {
|
|
124
|
+
firstError = {
|
|
125
|
+
message: i18n.t('error.unknown_error'),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
setFormValues({
|
|
133
|
+
...formValues,
|
|
134
|
+
__errorFields,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return firstError;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return null;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
formValues,
|
|
145
|
+
setFormValues,
|
|
146
|
+
listData: computedListData,
|
|
147
|
+
validate,
|
|
148
|
+
isButtonDisabled,
|
|
149
|
+
};
|
|
150
|
+
}
|