@nocobase/client-v2 2.1.0-beta.36 → 2.1.0-beta.38
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/es/Application.d.ts +1 -0
- package/es/BaseApplication.d.ts +4 -0
- package/es/RouterManager.d.ts +1 -0
- package/es/components/KeepAlive.d.ts +22 -0
- package/es/components/RouterBridge.d.ts +9 -0
- package/es/components/form/DialogFormLayout.d.ts +5 -29
- package/es/components/form/filter/CollectionFilter.d.ts +41 -0
- package/es/components/form/filter/CollectionFilterItem.d.ts +41 -0
- package/es/components/form/filter/DateFilterDynamicComponent.d.ts +57 -0
- package/es/components/form/filter/FilterValueInput.d.ts +29 -0
- package/es/components/form/filter/index.d.ts +11 -0
- package/es/components/form/filter/useFilterActionProps.d.ts +96 -0
- package/es/components/form/index.d.ts +1 -0
- package/es/data-source/ExtendCollectionsProvider.d.ts +50 -0
- package/es/data-source/index.d.ts +9 -0
- package/es/flow/FlowPage.d.ts +2 -1
- package/es/flow/admin-shell/AdminLayoutRouteCoordinator.d.ts +8 -40
- package/es/flow/admin-shell/BaseLayoutModel.d.ts +89 -0
- package/es/flow/admin-shell/BaseLayoutRouteCoordinator.d.ts +74 -0
- package/es/flow/admin-shell/admin-layout/AdminLayoutEntryGuard.d.ts +12 -0
- package/es/flow/admin-shell/admin-layout/AdminLayoutModel.d.ts +7 -92
- package/es/flow/admin-shell/admin-layout/index.d.ts +2 -0
- package/es/flow/admin-shell/useAdminLayoutRoutePage.d.ts +2 -2
- package/es/flow/admin-shell/useLayoutRoutePage.d.ts +23 -0
- package/es/flow/components/FlowRoute.d.ts +10 -1
- package/es/flow/components/filter/index.d.ts +2 -0
- package/es/flow/components/filter/useFilterOptions.d.ts +54 -0
- package/es/flow/index.d.ts +4 -0
- package/es/flow/models/base/PageModel/PageModel.d.ts +3 -1
- package/es/flow/models/blocks/form/FormActionGroupModel.d.ts +1 -0
- package/es/flow/models/blocks/table/TableBlockModel.d.ts +10 -0
- package/es/flow/models/fields/AssociationFieldModel/SubTableFieldModel/index.d.ts +1 -1
- package/es/flow-compat/passwordUtils.d.ts +1 -1
- package/es/index.d.ts +2 -0
- package/es/index.mjs +491 -439
- package/es/layout-manager/LayoutContentRoute.d.ts +14 -0
- package/es/layout-manager/LayoutManager.d.ts +22 -0
- package/es/layout-manager/LayoutRoute.d.ts +14 -0
- package/es/layout-manager/index.d.ts +13 -0
- package/es/layout-manager/types.d.ts +20 -0
- package/es/layout-manager/utils.d.ts +14 -0
- package/es/nocobase-buildin-plugin/index.d.ts +3 -10
- package/es/settings-center/index.d.ts +1 -1
- package/es/settings-center/plugin-manager/BulkEnableButton.d.ts +15 -0
- package/es/settings-center/plugin-manager/PluginCard.d.ts +15 -0
- package/es/settings-center/plugin-manager/PluginDetail.d.ts +16 -0
- package/es/settings-center/{PluginManagerPage.d.ts → plugin-manager/index.d.ts} +1 -7
- package/es/settings-center/plugin-manager/types.d.ts +34 -0
- package/lib/index.js +491 -439
- package/package.json +8 -7
- package/src/Application.tsx +27 -12
- package/src/BaseApplication.tsx +19 -0
- package/src/PluginSettingsManager.ts +1 -1
- package/src/RouterManager.tsx +17 -1
- package/src/__tests__/PluginSettingsManager.test.ts +41 -2
- package/src/__tests__/app.test.tsx +17 -1
- package/src/__tests__/globalDeps.test.ts +1 -0
- package/src/__tests__/nocobase-buildin-plugin-auth.test.tsx +45 -2
- package/src/__tests__/plugin-manager.test.tsx +177 -0
- package/src/__tests__/settings-center.test.tsx +24 -2
- package/src/components/KeepAlive.tsx +131 -0
- package/src/components/README.md +89 -6
- package/src/components/README.zh-CN.md +89 -7
- package/src/components/RouterBridge.tsx +28 -4
- package/src/components/__tests__/KeepAlive.test.tsx +63 -0
- package/src/components/__tests__/RouterBridge.test.tsx +27 -0
- package/src/components/form/DialogFormLayout.tsx +5 -29
- package/src/components/form/filter/CollectionFilter.tsx +101 -0
- package/src/components/form/filter/CollectionFilterItem.tsx +176 -0
- package/src/components/form/filter/DateFilterDynamicComponent.tsx +283 -0
- package/src/components/form/filter/FilterValueInput.tsx +198 -0
- package/src/components/form/filter/__tests__/CollectionFilterItem.test.tsx +205 -0
- package/src/components/form/filter/__tests__/DateFilterDynamicComponent.test.tsx +148 -0
- package/src/components/form/filter/__tests__/FilterValueInput.test.tsx +243 -0
- package/src/components/form/filter/__tests__/compileFilterGroup.test.ts +146 -0
- package/src/components/form/filter/index.ts +13 -0
- package/src/components/form/filter/useFilterActionProps.ts +200 -0
- package/src/components/form/index.tsx +1 -0
- package/src/data-source/ExtendCollectionsProvider.tsx +144 -0
- package/src/data-source/__tests__/ExtendCollectionsProvider.test.tsx +264 -0
- package/src/data-source/index.ts +10 -0
- package/src/flow/FlowPage.tsx +35 -7
- package/src/flow/__tests__/FlowPage.test.tsx +79 -0
- package/src/flow/__tests__/FlowRoute.test.tsx +529 -2
- package/src/flow/actions/__tests__/linkageRules.subFormSetFieldProps.test.ts +191 -0
- package/src/flow/actions/__tests__/openView.subModelKey.test.tsx +33 -0
- package/src/flow/actions/aclCheck.tsx +4 -0
- package/src/flow/actions/aclCheckRefresh.tsx +4 -0
- package/src/flow/actions/dateTimeFormat.tsx +12 -8
- package/src/flow/actions/linkageRules.tsx +122 -0
- package/src/flow/actions/openView.tsx +28 -4
- package/src/flow/admin-shell/AdminLayoutRouteCoordinator.ts +11 -329
- package/src/flow/admin-shell/BaseLayoutModel.tsx +455 -0
- package/src/flow/admin-shell/BaseLayoutRouteCoordinator.ts +502 -0
- package/src/flow/admin-shell/__tests__/AdminLayoutRouteCoordinator.test.ts +547 -3
- package/src/flow/admin-shell/admin-layout/AdminLayoutComponent.tsx +4 -4
- package/src/flow/admin-shell/admin-layout/AdminLayoutEntryGuard.tsx +160 -0
- package/src/flow/admin-shell/admin-layout/AdminLayoutMenuModels.tsx +0 -12
- package/src/flow/admin-shell/admin-layout/AdminLayoutModel.tsx +28 -201
- package/src/flow/admin-shell/admin-layout/AdminLayoutSlotModels.tsx +11 -2
- package/src/flow/admin-shell/admin-layout/__tests__/AdminLayoutMenuModels.test.ts +1 -26
- package/src/flow/admin-shell/admin-layout/__tests__/AdminLayoutModel.test.tsx +149 -27
- package/src/flow/admin-shell/admin-layout/index.ts +2 -0
- package/src/flow/admin-shell/useAdminLayoutRoutePage.ts +10 -26
- package/src/flow/admin-shell/useLayoutRoutePage.ts +61 -0
- package/src/flow/components/AdminLayout.tsx +4 -154
- package/src/flow/components/FlowRoute.tsx +105 -15
- package/src/flow/components/filter/index.ts +3 -0
- package/src/flow/components/filter/useFilterOptions.ts +80 -0
- package/src/flow/index.ts +4 -0
- package/src/flow/models/base/ActionModel.tsx +8 -1
- package/src/flow/models/base/PageModel/PageModel.tsx +51 -18
- package/src/flow/models/base/PageModel/RootPageModel.tsx +6 -13
- package/src/flow/models/base/PageModel/__tests__/PageModel.test.ts +102 -1
- package/src/flow/models/base/RouteModel.tsx +1 -1
- package/src/flow/models/blocks/form/FormActionGroupModel.tsx +14 -0
- package/src/flow/models/blocks/form/FormItemModel.tsx +8 -1
- package/src/flow/models/blocks/form/__tests__/FormActionGroupModel.test.ts +46 -0
- package/src/flow/models/blocks/form/submitValues.ts +4 -1
- package/src/flow/models/blocks/table/TableBlockModel.tsx +118 -16
- package/src/flow/models/blocks/table/__tests__/TableBlockModel.rowSelection.test.tsx +114 -0
- package/src/flow/models/fields/AssociationFieldModel/SubFormFieldModel.tsx +7 -1
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableField.tsx +1 -1
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/index.tsx +6 -5
- package/src/flow/models/fields/ClickableFieldModel.tsx +9 -1
- package/src/flow/models/fields/DisplayTimeFieldModel.tsx +1 -1
- package/src/flow/models/fields/TimeFieldModel.tsx +1 -1
- package/src/flow/models/fields/__tests__/TimeFieldModel.test.tsx +61 -0
- package/src/flow/models/fields/mobile-components/MobileDatePicker.tsx +19 -3
- package/src/flow/models/fields/mobile-components/__tests__/MobileDatePicker.test.tsx +94 -0
- package/src/flow/models/topbar/TopbarActionModel.tsx +1 -1
- package/src/flow/utils/__tests__/dateTimeFormat.test.ts +91 -0
- package/src/index.ts +2 -0
- package/src/layout-manager/LayoutContentRoute.tsx +90 -0
- package/src/layout-manager/LayoutManager.tsx +185 -0
- package/src/layout-manager/LayoutRoute.tsx +138 -0
- package/src/layout-manager/__tests__/LayoutManager.test.tsx +335 -0
- package/src/layout-manager/__tests__/LayoutRoute.test.tsx +473 -0
- package/src/layout-manager/index.ts +14 -0
- package/src/layout-manager/types.ts +22 -0
- package/src/layout-manager/utils.ts +37 -0
- package/src/nocobase-buildin-plugin/index.tsx +69 -67
- package/src/nocobase-buildin-plugin/plugins/LocalePlugin.ts +1 -0
- package/src/settings-center/index.ts +1 -1
- package/src/settings-center/plugin-manager/BulkEnableButton.tsx +111 -0
- package/src/settings-center/plugin-manager/PluginCard.tsx +270 -0
- package/src/settings-center/plugin-manager/PluginDetail.tsx +195 -0
- package/src/settings-center/plugin-manager/index.tsx +254 -0
- package/src/settings-center/plugin-manager/types.ts +35 -0
- package/src/settings-center/utils.tsx +8 -1
- package/src/theme/__tests__/globalStyles.test.ts +24 -0
- package/src/theme/globalStyles.ts +10 -0
- package/src/utils/globalDeps.ts +2 -0
- package/src/settings-center/PluginManagerPage.tsx +0 -162
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { CollectionOptions } from '@nocobase/flow-engine';
|
|
11
|
+
import React, { FC, ReactNode, useEffect, useRef } from 'react';
|
|
12
|
+
import { useApp } from '../hooks';
|
|
13
|
+
|
|
14
|
+
export interface ExtendCollectionsProviderProps {
|
|
15
|
+
/** Data source key to extend. Defaults to `'main'`. */
|
|
16
|
+
dataSource?: string;
|
|
17
|
+
/** Collections to surface for the lifetime of this provider's subtree. */
|
|
18
|
+
collections: CollectionOptions[];
|
|
19
|
+
/**
|
|
20
|
+
* When `true`, re-sync the data source whenever the `collections` prop
|
|
21
|
+
* reference changes after mount: add entries newly present in the prop and
|
|
22
|
+
* remove entries no longer present (only those this provider registered).
|
|
23
|
+
* The diff runs in the same render as the prop change so children see the
|
|
24
|
+
* new state on their first render — at the cost of one observable mutation
|
|
25
|
+
* per change.
|
|
26
|
+
*
|
|
27
|
+
* Defaults to `false`. Most pages pass a stable (often module-level)
|
|
28
|
+
* `collections` list and don't need this; leaving it off avoids accidental
|
|
29
|
+
* re-registration when callers forget to memoize. Enable only when your
|
|
30
|
+
* collection list legitimately varies during the provider's lifetime.
|
|
31
|
+
*/
|
|
32
|
+
syncOnChange?: boolean;
|
|
33
|
+
children?: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Mount-scoped collection injector. Adds the given `collections` to the target
|
|
38
|
+
* data source on first render — synchronously, so children can read
|
|
39
|
+
* `getCollection(name)` on their own first render — and removes them on
|
|
40
|
+
* unmount. Survives mid-session data-source reloads via the
|
|
41
|
+
* `dataSource:loaded` event by re-registering only the names this provider
|
|
42
|
+
* owns.
|
|
43
|
+
*
|
|
44
|
+
* Use this for client-only collections — e.g. a `schema-only` server
|
|
45
|
+
* collection that isn't auto-published to the v2 data source, or a pure
|
|
46
|
+
* UI-side mirror — so downstream components (like `<CollectionFilter>`) can
|
|
47
|
+
* resolve the collection by name.
|
|
48
|
+
*
|
|
49
|
+
* Default behavior is "static-at-mount": subsequent changes to the
|
|
50
|
+
* `collections` prop are ignored. Pass `syncOnChange` to opt into diffing on
|
|
51
|
+
* prop change.
|
|
52
|
+
*/
|
|
53
|
+
export const ExtendCollectionsProvider: FC<ExtendCollectionsProviderProps> = ({
|
|
54
|
+
dataSource = 'main',
|
|
55
|
+
collections,
|
|
56
|
+
syncOnChange = false,
|
|
57
|
+
children,
|
|
58
|
+
}) => {
|
|
59
|
+
const app = useApp();
|
|
60
|
+
// Lazy-ref init guard. `ownedRef.current === null` only on the first render;
|
|
61
|
+
// once populated, StrictMode dev's second render and any subsequent
|
|
62
|
+
// re-render see a non-null ref and skip re-registering. React docs bless
|
|
63
|
+
// this idiom for "init exactly once on mount" — see "Avoiding recreating
|
|
64
|
+
// the ref contents".
|
|
65
|
+
const ownedRef = useRef<CollectionOptions[] | null>(null);
|
|
66
|
+
// Identity of the `collections` reference we last reacted to; gates the
|
|
67
|
+
// opt-in diff so StrictMode's double-render doesn't diff twice.
|
|
68
|
+
const lastCollectionsRef = useRef<CollectionOptions[] | null>(null);
|
|
69
|
+
|
|
70
|
+
if (ownedRef.current === null) {
|
|
71
|
+
const ds = app.dataSourceManager?.getDataSource?.(dataSource);
|
|
72
|
+
const owned: CollectionOptions[] = [];
|
|
73
|
+
if (ds) {
|
|
74
|
+
for (const c of collections) {
|
|
75
|
+
if (ds.getCollection?.(c.name)) continue;
|
|
76
|
+
ds.addCollection?.(c);
|
|
77
|
+
owned.push(c);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
ownedRef.current = owned;
|
|
81
|
+
lastCollectionsRef.current = collections;
|
|
82
|
+
} else if (syncOnChange && lastCollectionsRef.current !== collections) {
|
|
83
|
+
const ds = app.dataSourceManager?.getDataSource?.(dataSource);
|
|
84
|
+
if (ds) {
|
|
85
|
+
const nextNames = new Set(collections.map((c) => c.name));
|
|
86
|
+
const prevOwned = new Map(ownedRef.current.map((c) => [c.name, c]));
|
|
87
|
+
for (const name of prevOwned.keys()) {
|
|
88
|
+
if (!nextNames.has(name)) ds.removeCollection?.(name);
|
|
89
|
+
}
|
|
90
|
+
const nextOwned: CollectionOptions[] = [];
|
|
91
|
+
for (const c of collections) {
|
|
92
|
+
const previous = prevOwned.get(c.name);
|
|
93
|
+
if (previous) {
|
|
94
|
+
// First-registered wins: keep the existing options object, mirroring
|
|
95
|
+
// the original behavior where re-adding a present name was a no-op.
|
|
96
|
+
// Callers who need to update a collection should remount the
|
|
97
|
+
// provider (e.g. `key={signature}`).
|
|
98
|
+
nextOwned.push(previous);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (ds.getCollection?.(c.name)) continue;
|
|
102
|
+
ds.addCollection?.(c);
|
|
103
|
+
nextOwned.push(c);
|
|
104
|
+
}
|
|
105
|
+
ownedRef.current = nextOwned;
|
|
106
|
+
}
|
|
107
|
+
lastCollectionsRef.current = collections;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
const onLoaded = (event: Event) => {
|
|
112
|
+
const key = (event as CustomEvent<{ dataSourceKey: string }>).detail?.dataSourceKey;
|
|
113
|
+
if (key !== dataSource && key !== '*') return;
|
|
114
|
+
const ds = app.dataSourceManager?.getDataSource?.(dataSource);
|
|
115
|
+
if (!ds || !ownedRef.current) return;
|
|
116
|
+
// dataSource was just reloaded from the server — our owned client-only
|
|
117
|
+
// entries got wiped. Re-add only the ones we own, using the snapshot in
|
|
118
|
+
// the ref so we don't accidentally seize names this provider never
|
|
119
|
+
// registered.
|
|
120
|
+
for (const c of ownedRef.current) {
|
|
121
|
+
if (ds.getCollection?.(c.name)) continue;
|
|
122
|
+
ds.addCollection?.(c);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
app.eventBus?.addEventListener('dataSource:loaded', onLoaded);
|
|
126
|
+
return () => {
|
|
127
|
+
app.eventBus?.removeEventListener('dataSource:loaded', onLoaded);
|
|
128
|
+
const ds = app.dataSourceManager?.getDataSource?.(dataSource);
|
|
129
|
+
const owned = ownedRef.current ?? [];
|
|
130
|
+
ownedRef.current = null;
|
|
131
|
+
lastCollectionsRef.current = null;
|
|
132
|
+
if (!ds) return;
|
|
133
|
+
for (const c of owned) ds.removeCollection?.(c.name);
|
|
134
|
+
};
|
|
135
|
+
// `collections` / `syncOnChange` intentionally excluded — they drive the
|
|
136
|
+
// render-phase init/diff above, not this effect. The listener reads
|
|
137
|
+
// `ownedRef` each time it fires (async, never during render).
|
|
138
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
139
|
+
}, [app, dataSource]);
|
|
140
|
+
|
|
141
|
+
return <>{children}</>;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export default ExtendCollectionsProvider;
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { CollectionOptions } from '@nocobase/flow-engine';
|
|
11
|
+
import { FlowEngineProvider } from '@nocobase/flow-engine';
|
|
12
|
+
import { act, render } from '@testing-library/react';
|
|
13
|
+
import React, { useState } from 'react';
|
|
14
|
+
import { describe, expect, it } from 'vitest';
|
|
15
|
+
import { createMockClient } from '../../MockApplication';
|
|
16
|
+
import { ExtendCollectionsProvider } from '../ExtendCollectionsProvider';
|
|
17
|
+
|
|
18
|
+
// `MockApplication` isn't exported as a named type, so infer from the factory.
|
|
19
|
+
type AppInstance = ReturnType<typeof createMockClient>;
|
|
20
|
+
|
|
21
|
+
function makeApp(): AppInstance {
|
|
22
|
+
const app = createMockClient();
|
|
23
|
+
// The mock client wires a lazy `appInfo` getter to `GET app:getInfo`; any
|
|
24
|
+
// proxy iteration of the FlowEngineContext can trip it. Pre-stub so a 404
|
|
25
|
+
// doesn't surface as an unhandled axios rejection during the test run.
|
|
26
|
+
app.apiMock.onGet('app:getInfo').reply(200, { data: { version: 'test' } });
|
|
27
|
+
return app;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const LOCKED: CollectionOptions = {
|
|
31
|
+
name: 'lockedUsers',
|
|
32
|
+
fields: [{ name: 'id', type: 'integer', interface: 'integer' }],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const USERS: CollectionOptions = {
|
|
36
|
+
name: 'users',
|
|
37
|
+
fields: [{ name: 'username', type: 'string', interface: 'input' }],
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const POSTS: CollectionOptions = {
|
|
41
|
+
name: 'posts',
|
|
42
|
+
fields: [{ name: 'title', type: 'string', interface: 'input' }],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function mountWith(app: AppInstance, node: React.ReactNode) {
|
|
46
|
+
return render(<FlowEngineProvider engine={app.flowEngine}>{node}</FlowEngineProvider>);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getMain(app: AppInstance) {
|
|
50
|
+
return app.dataSourceManager.getDataSource('main');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('ExtendCollectionsProvider', () => {
|
|
54
|
+
// The defining contract: a page-inner that reads `getCollection(name)` in its
|
|
55
|
+
// own render body (the LockedUsersPage pattern at LockedUsersPage.tsx:156-157)
|
|
56
|
+
// must see the registered collection on its FIRST render, with no extra tick.
|
|
57
|
+
it('lets children read the registered collection during their first render', () => {
|
|
58
|
+
const app = makeApp();
|
|
59
|
+
let firstRenderResult: { name?: string } | undefined;
|
|
60
|
+
|
|
61
|
+
const Child: React.FC = () => {
|
|
62
|
+
// Read synchronously during render — this is the contract that forces
|
|
63
|
+
// the provider to register collections in render-phase rather than in
|
|
64
|
+
// an effect.
|
|
65
|
+
const found = getMain(app)?.getCollection?.(LOCKED.name);
|
|
66
|
+
firstRenderResult = found ? { name: found.name } : undefined;
|
|
67
|
+
return <div>child</div>;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
mountWith(
|
|
71
|
+
app,
|
|
72
|
+
<ExtendCollectionsProvider collections={[LOCKED]}>
|
|
73
|
+
<Child />
|
|
74
|
+
</ExtendCollectionsProvider>,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(firstRenderResult).toEqual({ name: LOCKED.name });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('removes only the collections it added when unmounted', () => {
|
|
81
|
+
const app = makeApp();
|
|
82
|
+
// Pre-existing collection in the data source — provider must not touch it.
|
|
83
|
+
getMain(app).addCollection(USERS);
|
|
84
|
+
|
|
85
|
+
const { unmount } = mountWith(
|
|
86
|
+
app,
|
|
87
|
+
<ExtendCollectionsProvider collections={[LOCKED]}>
|
|
88
|
+
<span>inside</span>
|
|
89
|
+
</ExtendCollectionsProvider>,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
93
|
+
expect(getMain(app).getCollection(USERS.name)?.name).toBe(USERS.name);
|
|
94
|
+
|
|
95
|
+
unmount();
|
|
96
|
+
|
|
97
|
+
expect(getMain(app).getCollection(LOCKED.name)).toBeUndefined();
|
|
98
|
+
// The provider didn't add USERS, so it must leave it alone.
|
|
99
|
+
expect(getMain(app).getCollection(USERS.name)?.name).toBe(USERS.name);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Direct regression for the lazy-ref idempotency: even when the provider
|
|
103
|
+
// re-renders many times (which is what StrictMode dev's double-render and
|
|
104
|
+
// any caller-driven re-render look like to the provider), it must not
|
|
105
|
+
// re-register the same collection or accumulate duplicate ownership.
|
|
106
|
+
it('only calls addCollection once across many re-renders of the same mount', () => {
|
|
107
|
+
const app = makeApp();
|
|
108
|
+
let addCount = 0;
|
|
109
|
+
const origAdd = getMain(app).addCollection.bind(getMain(app));
|
|
110
|
+
getMain(app).addCollection = (c: CollectionOptions) => {
|
|
111
|
+
addCount += 1;
|
|
112
|
+
origAdd(c);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const Host: React.FC = () => {
|
|
116
|
+
const [, setTick] = useState(0);
|
|
117
|
+
// Force a render burst on mount — covers StrictMode dev's second render
|
|
118
|
+
// and any other parent-driven re-renders. The lazy-ref guard in the
|
|
119
|
+
// provider must hold across all of them.
|
|
120
|
+
React.useEffect(() => {
|
|
121
|
+
setTick((n) => n + 1);
|
|
122
|
+
setTick((n) => n + 1);
|
|
123
|
+
}, []);
|
|
124
|
+
return (
|
|
125
|
+
<ExtendCollectionsProvider collections={[LOCKED]}>
|
|
126
|
+
<span>inside</span>
|
|
127
|
+
</ExtendCollectionsProvider>
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const { unmount } = mountWith(app, <Host />);
|
|
132
|
+
|
|
133
|
+
expect(addCount).toBe(1);
|
|
134
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
135
|
+
|
|
136
|
+
unmount();
|
|
137
|
+
expect(getMain(app).getCollection(LOCKED.name)).toBeUndefined();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Default semantics: "static-at-mount". Caller's prop changes are ignored.
|
|
141
|
+
describe('with syncOnChange={false} (default)', () => {
|
|
142
|
+
it('ignores a `collections` prop reference change after mount', () => {
|
|
143
|
+
const app = makeApp();
|
|
144
|
+
|
|
145
|
+
const Host: React.FC = () => {
|
|
146
|
+
const [list, setList] = useState<CollectionOptions[]>([LOCKED]);
|
|
147
|
+
return (
|
|
148
|
+
<>
|
|
149
|
+
<button type="button" onClick={() => setList([LOCKED, POSTS])}>
|
|
150
|
+
add posts
|
|
151
|
+
</button>
|
|
152
|
+
<ExtendCollectionsProvider collections={list}>
|
|
153
|
+
<span>inside</span>
|
|
154
|
+
</ExtendCollectionsProvider>
|
|
155
|
+
</>
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const { getByText } = mountWith(app, <Host />);
|
|
160
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
161
|
+
expect(getMain(app).getCollection(POSTS.name)).toBeUndefined();
|
|
162
|
+
|
|
163
|
+
act(() => {
|
|
164
|
+
getByText('add posts').click();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// POSTS must NOT have been registered — syncOnChange is off.
|
|
168
|
+
expect(getMain(app).getCollection(POSTS.name)).toBeUndefined();
|
|
169
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe('with syncOnChange={true}', () => {
|
|
174
|
+
it('adds collections newly added to the prop and removes ones dropped from it', () => {
|
|
175
|
+
const app = makeApp();
|
|
176
|
+
|
|
177
|
+
const Host: React.FC = () => {
|
|
178
|
+
const [list, setList] = useState<CollectionOptions[]>([LOCKED, POSTS]);
|
|
179
|
+
return (
|
|
180
|
+
<>
|
|
181
|
+
<button type="button" onClick={() => setList([POSTS, USERS])}>
|
|
182
|
+
swap
|
|
183
|
+
</button>
|
|
184
|
+
<ExtendCollectionsProvider collections={list} syncOnChange>
|
|
185
|
+
<span>inside</span>
|
|
186
|
+
</ExtendCollectionsProvider>
|
|
187
|
+
</>
|
|
188
|
+
);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const { getByText } = mountWith(app, <Host />);
|
|
192
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
193
|
+
expect(getMain(app).getCollection(POSTS.name)?.name).toBe(POSTS.name);
|
|
194
|
+
expect(getMain(app).getCollection(USERS.name)).toBeUndefined();
|
|
195
|
+
|
|
196
|
+
act(() => {
|
|
197
|
+
getByText('swap').click();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// LOCKED was dropped from the prop → removed.
|
|
201
|
+
expect(getMain(app).getCollection(LOCKED.name)).toBeUndefined();
|
|
202
|
+
// POSTS was in both lists → kept.
|
|
203
|
+
expect(getMain(app).getCollection(POSTS.name)?.name).toBe(POSTS.name);
|
|
204
|
+
// USERS is new → added.
|
|
205
|
+
expect(getMain(app).getCollection(USERS.name)?.name).toBe(USERS.name);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('still leaves pre-existing collections it never owned alone after diff', () => {
|
|
209
|
+
const app = makeApp();
|
|
210
|
+
// Pre-existing in the data source before the provider mounts.
|
|
211
|
+
getMain(app).addCollection(USERS);
|
|
212
|
+
|
|
213
|
+
const Host: React.FC = () => {
|
|
214
|
+
const [list, setList] = useState<CollectionOptions[]>([LOCKED, USERS]);
|
|
215
|
+
return (
|
|
216
|
+
<>
|
|
217
|
+
<button type="button" onClick={() => setList([LOCKED])}>
|
|
218
|
+
drop users
|
|
219
|
+
</button>
|
|
220
|
+
<ExtendCollectionsProvider collections={list} syncOnChange>
|
|
221
|
+
<span>inside</span>
|
|
222
|
+
</ExtendCollectionsProvider>
|
|
223
|
+
</>
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const { getByText } = mountWith(app, <Host />);
|
|
228
|
+
// USERS was already there, so the provider never owned it.
|
|
229
|
+
expect(getMain(app).getCollection(USERS.name)?.name).toBe(USERS.name);
|
|
230
|
+
|
|
231
|
+
act(() => {
|
|
232
|
+
getByText('drop users').click();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// The prop dropped USERS, but the provider doesn't own it — must not
|
|
236
|
+
// accidentally remove it.
|
|
237
|
+
expect(getMain(app).getCollection(USERS.name)?.name).toBe(USERS.name);
|
|
238
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// Mid-session reload: the data source manager wipes everything and reloads
|
|
243
|
+
// from the server; client-only entries this provider registered would be
|
|
244
|
+
// gone. The `dataSource:loaded` event handler re-adds owned entries.
|
|
245
|
+
it('re-registers owned collections after a dataSource:loaded event', () => {
|
|
246
|
+
const app = makeApp();
|
|
247
|
+
|
|
248
|
+
mountWith(
|
|
249
|
+
app,
|
|
250
|
+
<ExtendCollectionsProvider collections={[LOCKED]}>
|
|
251
|
+
<span>inside</span>
|
|
252
|
+
</ExtendCollectionsProvider>,
|
|
253
|
+
);
|
|
254
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
255
|
+
|
|
256
|
+
// Simulate the data source manager wiping then re-broadcasting the event.
|
|
257
|
+
act(() => {
|
|
258
|
+
getMain(app).removeCollection(LOCKED.name);
|
|
259
|
+
app.eventBus.dispatchEvent(new CustomEvent('dataSource:loaded', { detail: { dataSourceKey: 'main' } }));
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
expect(getMain(app).getCollection(LOCKED.name)?.name).toBe(LOCKED.name);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export * from './ExtendCollectionsProvider';
|
package/src/flow/FlowPage.tsx
CHANGED
|
@@ -7,15 +7,33 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import {
|
|
11
|
+
FlowModelRenderer,
|
|
12
|
+
isInheritedFrom,
|
|
13
|
+
useFlowEngine,
|
|
14
|
+
useFlowModelById,
|
|
15
|
+
useFlowViewContext,
|
|
16
|
+
} from '@nocobase/flow-engine';
|
|
17
|
+
import type { FlowModel, FlowModelRendererProps, ModelConstructor } from '@nocobase/flow-engine';
|
|
12
18
|
import { useRequest } from 'ahooks';
|
|
13
19
|
import React from 'react';
|
|
14
20
|
import FlowRoute from './components/FlowRoute';
|
|
15
21
|
import { SkeletonFallback } from './components/SkeletonFallback';
|
|
22
|
+
import { ChildPageModel } from './models';
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
const defaultPageFlowSettings: FlowModelRendererProps['showFlowSettings'] = {
|
|
25
|
+
showBackground: false,
|
|
26
|
+
showBorder: false,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function InternalFlowPage({
|
|
30
|
+
uid,
|
|
31
|
+
showFlowSettings,
|
|
32
|
+
...props
|
|
33
|
+
}: { uid: string; showFlowSettings?: FlowModelRendererProps['showFlowSettings'] } & Record<string, unknown>) {
|
|
18
34
|
const model = useFlowModelById(uid);
|
|
35
|
+
const resolvedShowFlowSettings = showFlowSettings ?? model?.props?.showFlowSettings ?? defaultPageFlowSettings;
|
|
36
|
+
|
|
19
37
|
return (
|
|
20
38
|
<FlowModelRenderer
|
|
21
39
|
model={model}
|
|
@@ -25,7 +43,7 @@ function InternalFlowPage({ uid, ...props }) {
|
|
|
25
43
|
/>
|
|
26
44
|
}
|
|
27
45
|
hideRemoveInSettings
|
|
28
|
-
showFlowSettings={
|
|
46
|
+
showFlowSettings={resolvedShowFlowSettings}
|
|
29
47
|
{...props}
|
|
30
48
|
/>
|
|
31
49
|
);
|
|
@@ -36,14 +54,21 @@ type FlowPageProps = {
|
|
|
36
54
|
parentId?: string;
|
|
37
55
|
onModelLoaded?: (uid: string, model: FlowModel) => void;
|
|
38
56
|
defaultTabTitle?: string;
|
|
57
|
+
showFlowSettings?: FlowModelRendererProps['showFlowSettings'];
|
|
39
58
|
};
|
|
40
59
|
|
|
41
60
|
export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknown>) => {
|
|
42
61
|
const { pageModelClass = 'ChildPageModel', parentId, onModelLoaded, defaultTabTitle, ...rest } = props;
|
|
43
62
|
const flowEngine = useFlowEngine();
|
|
44
63
|
const ctx = useFlowViewContext();
|
|
45
|
-
const { loading, data } = useRequest(
|
|
64
|
+
const { loading, data, error } = useRequest(
|
|
46
65
|
async () => {
|
|
66
|
+
const ModelClass = await flowEngine.getModelClassAsync(pageModelClass);
|
|
67
|
+
if (!ModelClass) {
|
|
68
|
+
throw new Error(`[NocoBase] Page model class '${pageModelClass}' is not registered.`);
|
|
69
|
+
}
|
|
70
|
+
const shouldInjectDefaultChildTab =
|
|
71
|
+
ModelClass === ChildPageModel || isInheritedFrom(ModelClass as ModelConstructor, ChildPageModel);
|
|
47
72
|
const options = {
|
|
48
73
|
async: true,
|
|
49
74
|
parentId,
|
|
@@ -51,7 +76,7 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
|
|
|
51
76
|
subType: 'object',
|
|
52
77
|
use: pageModelClass,
|
|
53
78
|
};
|
|
54
|
-
if (
|
|
79
|
+
if (shouldInjectDefaultChildTab) {
|
|
55
80
|
const tabTitle = defaultTabTitle || flowEngine.translate?.('Details');
|
|
56
81
|
options['subModels'] = {
|
|
57
82
|
tabs: [
|
|
@@ -86,9 +111,12 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
|
|
|
86
111
|
return data;
|
|
87
112
|
},
|
|
88
113
|
{
|
|
89
|
-
refreshDeps: [parentId],
|
|
114
|
+
refreshDeps: [parentId, pageModelClass, defaultTabTitle],
|
|
90
115
|
},
|
|
91
116
|
);
|
|
117
|
+
if (error) {
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
92
120
|
if (loading || !data?.uid) {
|
|
93
121
|
return <SkeletonFallback style={{ margin: ctx?.isMobileLayout ? 8 : ctx?.themeToken.marginBlock }} />;
|
|
94
122
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { render } from '@testing-library/react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
13
|
+
import { FlowPage } from '../FlowPage';
|
|
14
|
+
|
|
15
|
+
const mocks = vi.hoisted(() => ({
|
|
16
|
+
rendererProps: undefined as any,
|
|
17
|
+
model: {
|
|
18
|
+
uid: 'public-form-page',
|
|
19
|
+
props: {
|
|
20
|
+
showFlowSettings: false,
|
|
21
|
+
},
|
|
22
|
+
context: {
|
|
23
|
+
isMobileLayout: false,
|
|
24
|
+
themeToken: {
|
|
25
|
+
marginBlock: 16,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
vi.mock('@nocobase/flow-engine', async () => {
|
|
32
|
+
const actual = await vi.importActual<any>('@nocobase/flow-engine');
|
|
33
|
+
const ReactModule = await import('react');
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
...actual,
|
|
37
|
+
FlowModelRenderer: vi.fn((props: any) => {
|
|
38
|
+
mocks.rendererProps = props;
|
|
39
|
+
return ReactModule.createElement('div', { 'data-testid': 'flow-model-renderer' });
|
|
40
|
+
}),
|
|
41
|
+
useFlowEngine: vi.fn(() => ({
|
|
42
|
+
getModelClassAsync: vi.fn(),
|
|
43
|
+
loadOrCreateModel: vi.fn(),
|
|
44
|
+
context: {},
|
|
45
|
+
})),
|
|
46
|
+
useFlowModelById: vi.fn(() => mocks.model),
|
|
47
|
+
useFlowViewContext: vi.fn(() => ({
|
|
48
|
+
themeToken: {
|
|
49
|
+
marginBlock: 16,
|
|
50
|
+
},
|
|
51
|
+
})),
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
vi.mock('ahooks', async () => {
|
|
56
|
+
const actual = await vi.importActual<any>('ahooks');
|
|
57
|
+
return {
|
|
58
|
+
...actual,
|
|
59
|
+
useRequest: vi.fn(() => ({
|
|
60
|
+
loading: false,
|
|
61
|
+
data: {
|
|
62
|
+
uid: 'public-form-page',
|
|
63
|
+
},
|
|
64
|
+
error: null,
|
|
65
|
+
})),
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('FlowPage', () => {
|
|
70
|
+
afterEach(() => {
|
|
71
|
+
mocks.rendererProps = undefined;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('uses showFlowSettings from page model props', () => {
|
|
75
|
+
render(<FlowPage />);
|
|
76
|
+
|
|
77
|
+
expect(mocks.rendererProps?.showFlowSettings).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
});
|