@jsb188/react 1.1.11 → 1.1.12
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/libs/README.md +75 -0
- package/libs/page-filters.helpers.ts +65 -0
- package/libs/page-filters.ts +223 -0
- package/libs/use-page-filters.ts +147 -0
- package/package.json +8 -4
- package/states/appContext.ts +15 -53
- package/states/index.ts +1 -0
- package/states/keyDown.ts +147 -51
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +158 -88
- package/states/popOver.ts +245 -154
- package/states/reactiveEvents.ts +53 -10
- package/states/uploads.ts +0 -2
- package/types/PopOver.d.ts +190 -55
- package/utils/hooks.ts +109 -29
- package/utils/lazy.ts +183 -0
- package/utils/schema.ts +0 -170
package/utils/hooks.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { areObjectsEqual } from '@jsb188/app/utils/object.ts';
|
|
2
|
+
import { releaseObjectUrl, retainObjectUrl } from '@jsb188/app/utils/url_client.ts';
|
|
2
3
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -7,12 +8,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
7
8
|
* NOTE: Do *NOT* export this function.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
|
-
function getDatabaseAction(
|
|
11
|
-
currentData: any,
|
|
12
|
-
data: any,
|
|
13
|
-
allowNull?: boolean,
|
|
14
|
-
mergeCurrentData?: boolean,
|
|
15
|
-
): any {
|
|
11
|
+
function getDatabaseAction(currentData: any, data: any, allowNull?: boolean, mergeCurrentData?: boolean): any {
|
|
16
12
|
if (!currentData) {
|
|
17
13
|
return {
|
|
18
14
|
action: 'INSERT',
|
|
@@ -24,14 +20,10 @@ function getDatabaseAction(
|
|
|
24
20
|
|
|
25
21
|
let hasChanges = false;
|
|
26
22
|
for (const key in data) {
|
|
27
|
-
if (
|
|
28
|
-
Object.prototype.hasOwnProperty.call(data, key) &&
|
|
29
|
-
data[key] !== undefined
|
|
30
|
-
) {
|
|
23
|
+
if (Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined) {
|
|
31
24
|
// trim() on all string databse fields should be OK;
|
|
32
25
|
// But if not, add an extra param.
|
|
33
|
-
const value =
|
|
34
|
-
typeof data[key] === 'string' ? data[key].trim() : data[key];
|
|
26
|
+
const value = typeof data[key] === 'string' ? data[key].trim() : data[key];
|
|
35
27
|
|
|
36
28
|
const isNotNull = value !== null || allowNull;
|
|
37
29
|
if (isNotNull) {
|
|
@@ -58,10 +50,7 @@ function getDatabaseAction(
|
|
|
58
50
|
|
|
59
51
|
if (mergeCurrentData) {
|
|
60
52
|
for (const key in currentData) {
|
|
61
|
-
if (
|
|
62
|
-
Object.prototype.hasOwnProperty.call(currentData, key) &&
|
|
63
|
-
documentData[key] === undefined
|
|
64
|
-
) {
|
|
53
|
+
if (Object.prototype.hasOwnProperty.call(currentData, key) && documentData[key] === undefined) {
|
|
65
54
|
documentData[key] = currentData[key];
|
|
66
55
|
}
|
|
67
56
|
}
|
|
@@ -79,10 +68,7 @@ function getDatabaseAction(
|
|
|
79
68
|
* Basic diff check
|
|
80
69
|
*/
|
|
81
70
|
|
|
82
|
-
export function formValuesAreDiff(
|
|
83
|
-
currentValues: any,
|
|
84
|
-
formValues: any,
|
|
85
|
-
): boolean {
|
|
71
|
+
export function formValuesAreDiff(currentValues: any, formValues: any): boolean {
|
|
86
72
|
if (!formValues && formValues !== 0) {
|
|
87
73
|
// If formValues are null, it's not set yet
|
|
88
74
|
return false;
|
|
@@ -100,7 +86,7 @@ export function formValuesAreDiff(
|
|
|
100
86
|
// }
|
|
101
87
|
// }
|
|
102
88
|
|
|
103
|
-
const result = getDatabaseAction(currentValues, formValues);
|
|
89
|
+
const result = getDatabaseAction(currentValues, formValues, true);
|
|
104
90
|
return result.action !== 'NONE';
|
|
105
91
|
}
|
|
106
92
|
|
|
@@ -133,9 +119,7 @@ export function useMounted() {
|
|
|
133
119
|
let cachedComputerTimeZone: string | null = null;
|
|
134
120
|
|
|
135
121
|
export function useComputerTimeZone() {
|
|
136
|
-
const [timeZone, setTimeZone] = useState<string | null>(
|
|
137
|
-
cachedComputerTimeZone,
|
|
138
|
-
);
|
|
122
|
+
const [timeZone, setTimeZone] = useState<string | null>(cachedComputerTimeZone);
|
|
139
123
|
|
|
140
124
|
useEffect(() => {
|
|
141
125
|
cachedComputerTimeZone ??= Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
@@ -186,11 +170,7 @@ export function checkPropsDiff(pp: any, np: any) {
|
|
|
186
170
|
}
|
|
187
171
|
|
|
188
172
|
for (const k in np) {
|
|
189
|
-
if (
|
|
190
|
-
Object.prototype.hasOwnProperty.call(np, k) &&
|
|
191
|
-
!Object.prototype.hasOwnProperty.call(pp, k) &&
|
|
192
|
-
np[k] !== pp[k]
|
|
193
|
-
) {
|
|
173
|
+
if (Object.prototype.hasOwnProperty.call(np, k) && !Object.prototype.hasOwnProperty.call(pp, k) && np[k] !== pp[k]) {
|
|
194
174
|
console.log('New Prop:', k);
|
|
195
175
|
isSame = false;
|
|
196
176
|
}
|
|
@@ -351,6 +331,27 @@ export function useImagesLoadStatus(
|
|
|
351
331
|
return loaded;
|
|
352
332
|
}
|
|
353
333
|
|
|
334
|
+
/*
|
|
335
|
+
* Hold local object URLs (e.g. image previews) while mounted, so they outlive the component that created them.
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
export function useRetainedObjectUrls(urls: (string | null | undefined)[]) {
|
|
339
|
+
const urlsKey = urls.filter(Boolean).join('\n');
|
|
340
|
+
|
|
341
|
+
useEffect(() => {
|
|
342
|
+
const retainedUrls = urlsKey ? urlsKey.split('\n') : [];
|
|
343
|
+
for (const url of retainedUrls) {
|
|
344
|
+
retainObjectUrl(url);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return () => {
|
|
348
|
+
for (const url of retainedUrls) {
|
|
349
|
+
releaseObjectUrl(url);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
}, [urlsKey]);
|
|
353
|
+
}
|
|
354
|
+
|
|
354
355
|
/*
|
|
355
356
|
* Track whether a referenced div's scroll position has passed a pixel offset.
|
|
356
357
|
*/
|
|
@@ -383,3 +384,82 @@ export function useScrollPastPosition(pixels: number) {
|
|
|
383
384
|
|
|
384
385
|
return [divRef, hasScrolledPast] as const;
|
|
385
386
|
}
|
|
387
|
+
|
|
388
|
+
export type ScrollNavState = 'STATIC' | 'HIDDEN' | 'SHOWN';
|
|
389
|
+
|
|
390
|
+
interface ScrollNavValue {
|
|
391
|
+
navState: ScrollNavState;
|
|
392
|
+
// Whether the page is scrolled past `staticOffset` (independent of scroll direction)
|
|
393
|
+
pastOffset: boolean;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/*
|
|
397
|
+
* Track the state of a hide-on-scroll window nav.
|
|
398
|
+
* 'STATIC' from page load until the page scrolls down past `staticOffset` (the nav sits in the page flow),
|
|
399
|
+
* then 'HIDDEN' while scrolling down and 'SHOWN' as soon as the page scrolls back up. Once shown, the nav
|
|
400
|
+
* stays fixed all the way up and only returns to 'STATIC' at the very top, where the two line up.
|
|
401
|
+
* Scroll deltas smaller than `minDelta` are ignored so trackpad jitter does not flip the direction.
|
|
402
|
+
*/
|
|
403
|
+
|
|
404
|
+
export function useScrollNavState(staticOffset = 0, minDelta = 4) {
|
|
405
|
+
const [value, setValue] = useState<ScrollNavValue>({ navState: 'STATIC', pastOffset: false });
|
|
406
|
+
|
|
407
|
+
useEffect(() => {
|
|
408
|
+
let lastScrollY = window.scrollY;
|
|
409
|
+
let frameRequested = false;
|
|
410
|
+
|
|
411
|
+
const update = () => {
|
|
412
|
+
frameRequested = false;
|
|
413
|
+
const scrollY = window.scrollY;
|
|
414
|
+
const delta = scrollY - lastScrollY;
|
|
415
|
+
const isDirectionChange = Math.abs(delta) >= minDelta;
|
|
416
|
+
|
|
417
|
+
if (isDirectionChange) {
|
|
418
|
+
lastScrollY = scrollY;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const pastOffset = scrollY > staticOffset;
|
|
422
|
+
|
|
423
|
+
const getNavState = (currentValue: ScrollNavState): ScrollNavState => {
|
|
424
|
+
// Negative scrollY happens during rubber-band overscroll; it counts as the top.
|
|
425
|
+
if (scrollY <= 0) {
|
|
426
|
+
return 'STATIC';
|
|
427
|
+
}
|
|
428
|
+
if (isDirectionChange) {
|
|
429
|
+
if (delta > 0) {
|
|
430
|
+
return pastOffset ? 'HIDDEN' : currentValue;
|
|
431
|
+
}
|
|
432
|
+
return currentValue === 'STATIC' ? 'STATIC' : 'SHOWN';
|
|
433
|
+
}
|
|
434
|
+
// Landed past the offset without a real scroll (e.g. restored scroll position on load)
|
|
435
|
+
return currentValue === 'STATIC' && pastOffset ? 'SHOWN' : currentValue;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
setValue((currentValue) => {
|
|
439
|
+
const navState = getNavState(currentValue.navState);
|
|
440
|
+
// Keep the same object when nothing changed so scroll frames do not re-render the nav
|
|
441
|
+
if (navState === currentValue.navState && pastOffset === currentValue.pastOffset) {
|
|
442
|
+
return currentValue;
|
|
443
|
+
}
|
|
444
|
+
return { navState, pastOffset };
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// Coalesce bursts of scroll events into one read per frame
|
|
449
|
+
const handleScroll = () => {
|
|
450
|
+
if (!frameRequested) {
|
|
451
|
+
frameRequested = true;
|
|
452
|
+
window.requestAnimationFrame(update);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
window.addEventListener('scroll', handleScroll, { passive: true });
|
|
457
|
+
update();
|
|
458
|
+
|
|
459
|
+
return () => {
|
|
460
|
+
window.removeEventListener('scroll', handleScroll);
|
|
461
|
+
};
|
|
462
|
+
}, [staticOffset, minDelta]);
|
|
463
|
+
|
|
464
|
+
return value;
|
|
465
|
+
}
|
package/utils/lazy.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Component, type ComponentType, createElement, type ErrorInfo, type ReactNode, use } from 'react';
|
|
2
|
+
|
|
3
|
+
export interface ComponentModule<P> {
|
|
4
|
+
default: ComponentType<P>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface PreloadableComponent<P> {
|
|
8
|
+
// Render inside <Suspense>; suspends only while the code is still downloading
|
|
9
|
+
Component: ComponentType<P>;
|
|
10
|
+
// Start (or reuse) the download; resolves null on failure and never rejects
|
|
11
|
+
preload: () => Promise<ComponentModule<P> | null>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// A promise carrying the settled-state fields React's use() reads to skip suspending
|
|
15
|
+
type TrackedPromise<T> = Promise<T> & {
|
|
16
|
+
status?: 'pending' | 'fulfilled' | 'rejected';
|
|
17
|
+
value?: T;
|
|
18
|
+
reason?: unknown;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
interface LazyWithPreloadOptions<P> {
|
|
22
|
+
// Rendered instead of the component when its code fails to download (defaults to rendering nothing)
|
|
23
|
+
fallback?: ComponentType<P>;
|
|
24
|
+
// Called when a render needed the code and it failed to download
|
|
25
|
+
onError?: (error: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* Error thrown when a lazy component's code failed to download.
|
|
30
|
+
*/
|
|
31
|
+
export class LazyLoadError extends Error {
|
|
32
|
+
constructor(cause: unknown) {
|
|
33
|
+
super('Failed to load lazy component code', { cause });
|
|
34
|
+
this.name = 'LazyLoadError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* Catch only code download failures (LazyLoadError) and render a fallback;
|
|
40
|
+
* every other error is rethrown to the next error boundary as before.
|
|
41
|
+
*/
|
|
42
|
+
class LazyLoadErrorBoundary extends Component<
|
|
43
|
+
{
|
|
44
|
+
children: ReactNode;
|
|
45
|
+
renderFallback: () => ReactNode;
|
|
46
|
+
onLoadError: (error: LazyLoadError) => void;
|
|
47
|
+
},
|
|
48
|
+
{ error: unknown }
|
|
49
|
+
> {
|
|
50
|
+
state = { error: null as unknown };
|
|
51
|
+
|
|
52
|
+
static getDerivedStateFromError(error: unknown) {
|
|
53
|
+
return { error };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
componentDidCatch(error: unknown, _errorInfo: ErrorInfo) {
|
|
57
|
+
if (error instanceof LazyLoadError) {
|
|
58
|
+
this.props.onLoadError(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
render() {
|
|
63
|
+
const { error } = this.state;
|
|
64
|
+
if (!error) {
|
|
65
|
+
return this.props.children;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (error instanceof LazyLoadError) {
|
|
69
|
+
return this.props.renderFallback();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Not a download failure; let the app's error handling deal with it
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/*
|
|
78
|
+
* Create a component whose code downloads on first render, or earlier through
|
|
79
|
+
* preload() (idle or hover prefetch). Unlike React.lazy, a component whose code
|
|
80
|
+
* was already preloaded renders synchronously without suspending, so prefetched
|
|
81
|
+
* content never flashes a Suspense fallback. The download is shared by
|
|
82
|
+
* preload() and every render. A failed download renders `fallback` instead of
|
|
83
|
+
* throwing to the app's error boundary. A later render or preload() starts a new
|
|
84
|
+
* download, though some browsers (Chromium) keep failing a dynamic import that
|
|
85
|
+
* failed once until the page reloads, so `fallback` should offer a reload.
|
|
86
|
+
*/
|
|
87
|
+
export function lazyWithPreload<P>(
|
|
88
|
+
load: () => Promise<ComponentModule<P>>,
|
|
89
|
+
options: LazyWithPreloadOptions<P> = {},
|
|
90
|
+
): PreloadableComponent<P> {
|
|
91
|
+
let modulePromise: TrackedPromise<ComponentModule<P>> | null = null;
|
|
92
|
+
|
|
93
|
+
const loadOnce = () => {
|
|
94
|
+
if (!modulePromise) {
|
|
95
|
+
const promise: TrackedPromise<ComponentModule<P>> = load().catch((error) => {
|
|
96
|
+
throw new LazyLoadError(error);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Record the settled state in the fields React's use() reads, so a loaded module
|
|
100
|
+
// is returned synchronously (no suspending) on every later render
|
|
101
|
+
promise.status = 'pending';
|
|
102
|
+
promise.then(
|
|
103
|
+
(module) => {
|
|
104
|
+
promise.status = 'fulfilled';
|
|
105
|
+
promise.value = module;
|
|
106
|
+
},
|
|
107
|
+
(error) => {
|
|
108
|
+
promise.status = 'rejected';
|
|
109
|
+
promise.reason = error;
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
modulePromise = promise;
|
|
113
|
+
}
|
|
114
|
+
return modulePromise;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// Forget a failed download so the next attempt fetches again (the same promise is kept
|
|
118
|
+
// while a render is still retrying, so React's use() sees one stable promise)
|
|
119
|
+
const forgetFailedLoad = () => {
|
|
120
|
+
if (modulePromise?.status === 'rejected') {
|
|
121
|
+
modulePromise = null;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
function LoadedComponent(props: P) {
|
|
126
|
+
const Loaded = use(loadOnce() as Promise<ComponentModule<P>>).default;
|
|
127
|
+
return createElement(Loaded as ComponentType<any>, props as any);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function PreloadableComponent(props: P) {
|
|
131
|
+
return createElement(LazyLoadErrorBoundary, {
|
|
132
|
+
onLoadError: (error: LazyLoadError) => {
|
|
133
|
+
forgetFailedLoad();
|
|
134
|
+
options.onError?.(error.cause);
|
|
135
|
+
},
|
|
136
|
+
renderFallback: () => (options.fallback ? createElement(options.fallback as ComponentType<any>, props as any) : null),
|
|
137
|
+
children: createElement(LoadedComponent as ComponentType<any>, props as any),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
Component: PreloadableComponent,
|
|
143
|
+
preload: () =>
|
|
144
|
+
loadOnce().catch(() => {
|
|
145
|
+
forgetFailedLoad();
|
|
146
|
+
return null;
|
|
147
|
+
}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
* Wrap an already imported component in the same shape as lazyWithPreload(),
|
|
153
|
+
* for registries that mix lazy and static entries (e.g. prompts that must open instantly).
|
|
154
|
+
*/
|
|
155
|
+
export function preloadedComponent<P>(Loaded: ComponentType<P>): PreloadableComponent<P> {
|
|
156
|
+
const module = { default: Loaded };
|
|
157
|
+
return {
|
|
158
|
+
Component: Loaded,
|
|
159
|
+
preload: () => Promise.resolve(module),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/*
|
|
164
|
+
* Reload the page after a code chunk failed to load (e.g. a tab still running
|
|
165
|
+
* an old deploy), at most once per `windowMs` so a persistent failure can't
|
|
166
|
+
* cause a reload loop. Returns true when a reload was started.
|
|
167
|
+
*/
|
|
168
|
+
export function reloadOnceAfterChunkError(storageKey: string, windowMs = 60000) {
|
|
169
|
+
try {
|
|
170
|
+
const lastReloadAt = Number(sessionStorage.getItem(storageKey) || 0);
|
|
171
|
+
if (Date.now() - lastReloadAt < windowMs) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
sessionStorage.setItem(storageKey, String(Date.now()));
|
|
176
|
+
} catch {
|
|
177
|
+
// Without sessionStorage there's no loop guard; don't reload
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
globalThis.location.reload();
|
|
182
|
+
return true;
|
|
183
|
+
}
|
package/utils/schema.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
2
|
-
import i18n from '@jsb188/app/i18n/index.ts';
|
|
3
|
-
import type {
|
|
4
|
-
FormItemSchemaObj,
|
|
5
|
-
FormSchemaObj,
|
|
6
|
-
} from '@jsb188/app/types/form.d.ts';
|
|
7
|
-
import { getObject, setObject } from '@jsb188/app/utils/object.ts';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Types
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
interface ComputedSchema {
|
|
14
|
-
formValues: any;
|
|
15
|
-
setFormValues: (formValues: any) => void;
|
|
16
|
-
listData: (FormItemSchemaObj & { name: string })[];
|
|
17
|
-
validate?: () => any;
|
|
18
|
-
isButtonDisabled?: (formValues: any) => boolean;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Make form values from current form values, current data, and schema
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
export function makeFormValues(
|
|
26
|
-
schema: any,
|
|
27
|
-
dataForSchema?: any,
|
|
28
|
-
currentData?: any,
|
|
29
|
-
) {
|
|
30
|
-
const newFormValues: Record<string, any> = {};
|
|
31
|
-
const { listData } = schema;
|
|
32
|
-
|
|
33
|
-
for (const obj of listData) {
|
|
34
|
-
const item = obj?.item || {};
|
|
35
|
-
const { name, setter, items } = item;
|
|
36
|
-
|
|
37
|
-
if (!name && !Array.isArray(items)) {
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (currentData || dataForSchema) {
|
|
42
|
-
if (Array.isArray(items)) {
|
|
43
|
-
items.forEach(({ item: subItem }: any) => {
|
|
44
|
-
if (typeof subItem.setter === 'function') {
|
|
45
|
-
setObject(
|
|
46
|
-
newFormValues,
|
|
47
|
-
subItem.name,
|
|
48
|
-
subItem.setter(currentData, dataForSchema),
|
|
49
|
-
);
|
|
50
|
-
} else if (currentData) {
|
|
51
|
-
setObject(
|
|
52
|
-
newFormValues,
|
|
53
|
-
subItem.name,
|
|
54
|
-
getObject(currentData, subItem.name),
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
} else if (typeof setter === 'function') {
|
|
59
|
-
setObject(newFormValues, name, setter(currentData, dataForSchema));
|
|
60
|
-
} else if (currentData) {
|
|
61
|
-
setObject(newFormValues, name, getObject(currentData, name));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return newFormValues;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Resolve schema using data
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
export function useSchema(
|
|
74
|
-
schema: FormSchemaObj,
|
|
75
|
-
dataForSchema: any,
|
|
76
|
-
currentData?: any,
|
|
77
|
-
): ComputedSchema {
|
|
78
|
-
const { listData, rules, isButtonDisabled } = schema;
|
|
79
|
-
const [formValues, setFormValues] = useState(
|
|
80
|
-
makeFormValues(schema, dataForSchema, currentData),
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
// Make listData from schema
|
|
84
|
-
|
|
85
|
-
const computedListData: any[] = listData
|
|
86
|
-
.map((schemaItem: FormItemSchemaObj) => {
|
|
87
|
-
const { __type, hidden, locked, item } = schemaItem;
|
|
88
|
-
if (
|
|
89
|
-
hidden === true ||
|
|
90
|
-
(typeof hidden === 'function' && hidden(dataForSchema))
|
|
91
|
-
) {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
let lockedValue;
|
|
96
|
-
if (typeof locked === 'function') {
|
|
97
|
-
lockedValue = locked(dataForSchema);
|
|
98
|
-
} else if (typeof locked === 'boolean') {
|
|
99
|
-
lockedValue = locked;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const computedItem: Record<string, any> = { ...item };
|
|
103
|
-
if (computedItem) {
|
|
104
|
-
for (const key in computedItem) {
|
|
105
|
-
if (
|
|
106
|
-
typeof computedItem[key] === 'function' &&
|
|
107
|
-
!key.startsWith('onClick') &&
|
|
108
|
-
!['getter', 'setter'].includes(key)
|
|
109
|
-
) {
|
|
110
|
-
computedItem[key] = computedItem[key](dataForSchema);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
__type,
|
|
117
|
-
locked: lockedValue,
|
|
118
|
-
item: computedItem,
|
|
119
|
-
};
|
|
120
|
-
})
|
|
121
|
-
.filter(Boolean);
|
|
122
|
-
|
|
123
|
-
// Create validation function
|
|
124
|
-
|
|
125
|
-
const validate = (): any => {
|
|
126
|
-
if (rules) {
|
|
127
|
-
const __errorFields: string[] = [];
|
|
128
|
-
|
|
129
|
-
let firstError: any = null;
|
|
130
|
-
for (const ruleObj of rules) {
|
|
131
|
-
const { for: key, rule, error } = ruleObj;
|
|
132
|
-
const value = formValues[key];
|
|
133
|
-
const isValid = rule(value, formValues);
|
|
134
|
-
|
|
135
|
-
if (!isValid) {
|
|
136
|
-
if (!__errorFields.includes(key)) {
|
|
137
|
-
__errorFields.push(key);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (!firstError) {
|
|
141
|
-
if (error) {
|
|
142
|
-
firstError = typeof error === 'function' ? error(value) : error;
|
|
143
|
-
} else {
|
|
144
|
-
firstError = {
|
|
145
|
-
message: i18n.t('error.unknown_error_msg'),
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
setFormValues({
|
|
153
|
-
...formValues,
|
|
154
|
-
__errorFields,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
return firstError;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return null;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
formValues,
|
|
165
|
-
setFormValues,
|
|
166
|
-
listData: computedListData,
|
|
167
|
-
validate,
|
|
168
|
-
isButtonDisabled,
|
|
169
|
-
};
|
|
170
|
-
}
|