@nocobase/client-v2 2.1.15 → 2.1.18

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/client-v2",
3
- "version": "2.1.15",
3
+ "version": "2.1.18",
4
4
  "license": "Apache-2.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.mjs",
@@ -27,11 +27,11 @@
27
27
  "@formily/antd-v5": "1.2.3",
28
28
  "@formily/react": "^2.2.27",
29
29
  "@formily/shared": "^2.2.27",
30
- "@nocobase/evaluators": "2.1.15",
31
- "@nocobase/flow-engine": "2.1.15",
32
- "@nocobase/sdk": "2.1.15",
33
- "@nocobase/shared": "2.1.15",
34
- "@nocobase/utils": "2.1.15",
30
+ "@nocobase/evaluators": "2.1.18",
31
+ "@nocobase/flow-engine": "2.1.18",
32
+ "@nocobase/sdk": "2.1.18",
33
+ "@nocobase/shared": "2.1.18",
34
+ "@nocobase/utils": "2.1.18",
35
35
  "ahooks": "^3.7.2",
36
36
  "antd": "5.24.2",
37
37
  "antd-style": "3.7.1",
@@ -46,5 +46,5 @@
46
46
  "react-i18next": "^11.15.1",
47
47
  "react-router-dom": "^6.30.1"
48
48
  },
49
- "gitHead": "1219585bbb4f5ebb6378be99fa2ff04b3376e6a2"
49
+ "gitHead": "2234b42b21c96d22a4b65a1f16a65ae61f434779"
50
50
  }
@@ -14,7 +14,7 @@ import {
14
14
  useFlowModelById,
15
15
  useFlowViewContext,
16
16
  } from '@nocobase/flow-engine';
17
- import type { FlowModel, FlowModelRendererProps, ModelConstructor } from '@nocobase/flow-engine';
17
+ import type { FlowEngineContext, FlowModel, FlowModelRendererProps, ModelConstructor } from '@nocobase/flow-engine';
18
18
  import { useRequest } from 'ahooks';
19
19
  import React from 'react';
20
20
  import FlowRoute from './components/FlowRoute';
@@ -57,10 +57,36 @@ type FlowPageProps = {
57
57
  showFlowSettings?: FlowModelRendererProps['showFlowSettings'];
58
58
  };
59
59
 
60
+ type FlowPageViewContext = FlowEngineContext & {
61
+ view?: {
62
+ inputArgs?: {
63
+ isMobileLayout?: unknown;
64
+ };
65
+ };
66
+ };
67
+
68
+ const bindViewLayoutState = (model: FlowModel, ctx?: FlowPageViewContext | null) => {
69
+ const hasViewMobileLayout = typeof ctx?.view?.inputArgs?.isMobileLayout === 'boolean';
70
+ const hasContextMobileLayout = typeof ctx?.isMobileLayout === 'boolean';
71
+ if (!hasViewMobileLayout && !hasContextMobileLayout) {
72
+ return;
73
+ }
74
+
75
+ model.context.defineProperty('isMobileLayout', {
76
+ get: () => {
77
+ if (typeof ctx?.isMobileLayout === 'boolean') {
78
+ return ctx.isMobileLayout;
79
+ }
80
+ return !!ctx?.view?.inputArgs?.isMobileLayout;
81
+ },
82
+ cache: false,
83
+ });
84
+ };
85
+
60
86
  export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknown>) => {
61
87
  const { pageModelClass = 'ChildPageModel', parentId, onModelLoaded, defaultTabTitle, ...rest } = props;
62
88
  const flowEngine = useFlowEngine();
63
- const ctx = useFlowViewContext();
89
+ const ctx = useFlowViewContext<FlowPageViewContext>();
64
90
  const { loading, data, error } = useRequest(
65
91
  async () => {
66
92
  const ModelClass = await flowEngine.getModelClassAsync(pageModelClass);
@@ -105,6 +131,7 @@ export const FlowPage = React.memo((props: FlowPageProps & Record<string, unknow
105
131
  const data = await flowEngine.loadOrCreateModel(options, { skipSave: !flowEngine.context.flowSettingsEnabled });
106
132
  if (data?.uid && onModelLoaded) {
107
133
  data.context.addDelegate(ctx);
134
+ bindViewLayoutState(data, ctx);
108
135
  data.removeParentDelegate();
109
136
  onModelLoaded(data.uid, data);
110
137
  }
@@ -7,44 +7,105 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- import { render } from '@testing-library/react';
10
+ import { render, waitFor } from '@testing-library/react';
11
11
  import React from 'react';
12
12
  import { afterEach, describe, expect, it, vi } from 'vitest';
13
13
  import { FlowPage } from '../FlowPage';
14
14
 
15
- const mocks = vi.hoisted(() => ({
16
- rendererProps: undefined as any,
17
- model: {
15
+ type TestContextProperty = {
16
+ value?: unknown;
17
+ get?: () => unknown;
18
+ };
19
+
20
+ type TestModelContext = {
21
+ isMobileLayout: boolean;
22
+ themeToken: {
23
+ marginBlock: number;
24
+ };
25
+ addDelegate: ReturnType<typeof vi.fn>;
26
+ defineProperty: ReturnType<typeof vi.fn>;
27
+ };
28
+
29
+ type TestPageModel = {
30
+ uid: string;
31
+ props: {
32
+ showFlowSettings: boolean;
33
+ };
34
+ context: TestModelContext;
35
+ removeParentDelegate: ReturnType<typeof vi.fn>;
36
+ };
37
+
38
+ type RendererProps = {
39
+ model?: TestPageModel;
40
+ showFlowSettings?: boolean;
41
+ } & Record<string, unknown>;
42
+
43
+ const defineContextProperty = (context: TestModelContext, key: string, property: TestContextProperty) => {
44
+ Object.defineProperty(context, key, {
45
+ configurable: true,
46
+ enumerable: true,
47
+ get: property.get || (() => property.value),
48
+ });
49
+ };
50
+
51
+ const createPageModel = (): TestPageModel => {
52
+ const context = {
53
+ isMobileLayout: false,
54
+ themeToken: {
55
+ marginBlock: 16,
56
+ },
57
+ addDelegate: vi.fn(),
58
+ defineProperty: vi.fn((key: string, property: TestContextProperty) => {
59
+ defineContextProperty(context, key, property);
60
+ }),
61
+ };
62
+
63
+ return {
18
64
  uid: 'public-form-page',
19
65
  props: {
20
66
  showFlowSettings: false,
21
67
  },
22
- context: {
23
- isMobileLayout: false,
24
- themeToken: {
25
- marginBlock: 16,
26
- },
27
- },
28
- },
68
+ context,
69
+ removeParentDelegate: vi.fn(),
70
+ };
71
+ };
72
+
73
+ const mocks = vi.hoisted(() => ({
74
+ rendererMobileStates: [] as boolean[],
75
+ rendererProps: undefined as RendererProps | undefined,
76
+ model: undefined as TestPageModel | undefined,
77
+ viewInputMobileLayout: true as boolean | undefined,
78
+ contextMobileLayout: undefined as boolean | undefined,
29
79
  }));
30
80
 
31
81
  vi.mock('@nocobase/flow-engine', async () => {
32
- const actual = await vi.importActual<any>('@nocobase/flow-engine');
82
+ const actual = await vi.importActual<typeof import('@nocobase/flow-engine')>('@nocobase/flow-engine');
33
83
  const ReactModule = await import('react');
84
+ class TestPageModelClass {}
34
85
 
35
86
  return {
36
87
  ...actual,
37
- FlowModelRenderer: vi.fn((props: any) => {
88
+ FlowModelRenderer: vi.fn((props: RendererProps) => {
38
89
  mocks.rendererProps = props;
90
+ if (typeof props.model?.context.isMobileLayout === 'boolean') {
91
+ mocks.rendererMobileStates.push(props.model.context.isMobileLayout);
92
+ }
39
93
  return ReactModule.createElement('div', { 'data-testid': 'flow-model-renderer' });
40
94
  }),
41
95
  useFlowEngine: vi.fn(() => ({
42
- getModelClassAsync: vi.fn(),
43
- loadOrCreateModel: vi.fn(),
96
+ getModelClassAsync: vi.fn(async () => TestPageModelClass),
97
+ loadOrCreateModel: vi.fn(async () => mocks.model),
44
98
  context: {},
99
+ translate: vi.fn((value: string) => value),
45
100
  })),
46
101
  useFlowModelById: vi.fn(() => mocks.model),
47
102
  useFlowViewContext: vi.fn(() => ({
103
+ view: {
104
+ inputArgs: {
105
+ ...(typeof mocks.viewInputMobileLayout === 'boolean' ? { isMobileLayout: mocks.viewInputMobileLayout } : {}),
106
+ },
107
+ },
108
+ ...(typeof mocks.contextMobileLayout === 'boolean' ? { isMobileLayout: mocks.contextMobileLayout } : {}),
48
109
  themeToken: {
49
110
  marginBlock: 16,
50
111
  },
@@ -53,27 +114,93 @@ vi.mock('@nocobase/flow-engine', async () => {
53
114
  });
54
115
 
55
116
  vi.mock('ahooks', async () => {
56
- const actual = await vi.importActual<any>('ahooks');
117
+ const actual = await vi.importActual<typeof import('ahooks')>('ahooks');
118
+ const ReactModule = await import('react');
57
119
  return {
58
120
  ...actual,
59
- useRequest: vi.fn(() => ({
60
- loading: false,
61
- data: {
62
- uid: 'public-form-page',
63
- },
64
- error: null,
65
- })),
121
+ useRequest: vi.fn((service: () => Promise<unknown>) => {
122
+ const serviceRef = ReactModule.useRef(service);
123
+ const [state, setState] = ReactModule.useState<{
124
+ loading: boolean;
125
+ data?: unknown;
126
+ error?: unknown;
127
+ }>({
128
+ loading: true,
129
+ });
130
+
131
+ ReactModule.useEffect(() => {
132
+ let active = true;
133
+ serviceRef
134
+ .current()
135
+ .then((data) => {
136
+ if (active) {
137
+ setState({ loading: false, data, error: null });
138
+ }
139
+ })
140
+ .catch((error) => {
141
+ if (active) {
142
+ setState({ loading: false, data: undefined, error });
143
+ }
144
+ });
145
+
146
+ return () => {
147
+ active = false;
148
+ };
149
+ }, []);
150
+
151
+ return state;
152
+ }),
66
153
  };
67
154
  });
68
155
 
69
156
  describe('FlowPage', () => {
70
157
  afterEach(() => {
158
+ mocks.rendererMobileStates = [];
71
159
  mocks.rendererProps = undefined;
160
+ mocks.model = undefined;
161
+ mocks.viewInputMobileLayout = true;
162
+ mocks.contextMobileLayout = undefined;
72
163
  });
73
164
 
74
- it('uses showFlowSettings from page model props', () => {
165
+ it('uses showFlowSettings from page model props', async () => {
166
+ mocks.model = createPageModel();
75
167
  render(<FlowPage />);
76
168
 
77
- expect(mocks.rendererProps?.showFlowSettings).toBe(false);
169
+ await waitFor(() => {
170
+ expect((mocks.rendererProps as { showFlowSettings?: boolean } | undefined)?.showFlowSettings).toBe(false);
171
+ });
172
+ });
173
+
174
+ it('exposes mobile layout state from route-managed view inputArgs before first render', async () => {
175
+ mocks.model = createPageModel();
176
+ render(<FlowPage onModelLoaded={vi.fn()} />);
177
+
178
+ await waitFor(() => {
179
+ expect(mocks.rendererMobileStates.length).toBeGreaterThan(0);
180
+ });
181
+ expect(mocks.rendererMobileStates[0]).toBe(true);
78
182
  });
183
+
184
+ it.each([
185
+ { viewInputMobileLayout: true, contextMobileLayout: false, expected: false },
186
+ { viewInputMobileLayout: false, contextMobileLayout: true, expected: true },
187
+ { viewInputMobileLayout: undefined, contextMobileLayout: true, expected: true },
188
+ { viewInputMobileLayout: undefined, contextMobileLayout: false, expected: false },
189
+ { viewInputMobileLayout: true, contextMobileLayout: undefined, expected: true },
190
+ { viewInputMobileLayout: false, contextMobileLayout: undefined, expected: false },
191
+ ])(
192
+ 'resolves mobile layout state from view input args and layout context %#',
193
+ async ({ viewInputMobileLayout, contextMobileLayout, expected }) => {
194
+ mocks.viewInputMobileLayout = viewInputMobileLayout;
195
+ mocks.contextMobileLayout = contextMobileLayout;
196
+ mocks.model = createPageModel();
197
+
198
+ render(<FlowPage onModelLoaded={vi.fn()} />);
199
+
200
+ await waitFor(() => {
201
+ expect(mocks.rendererMobileStates.length).toBeGreaterThan(0);
202
+ });
203
+ expect(mocks.rendererMobileStates[0]).toBe(expected);
204
+ },
205
+ );
79
206
  });
@@ -111,6 +111,15 @@ const getDefaultBasePathnameFromRoutePath = (routePath?: string) => {
111
111
 
112
112
  const isKnownViewParamName = (segment: string) => ['tab', 'filterbytk', 'sourceid'].includes(segment);
113
113
 
114
+ const isLegacyLayoutContentRouteName = (routeName: string, targetRouteName?: string) => {
115
+ return (
116
+ !!targetRouteName &&
117
+ ['page', 'page.tabs', 'page.tabs.popups', 'page.tab', 'page.view', 'page.tab.view'].some(
118
+ (legacyName) => targetRouteName === `${routeName}.${legacyName}`,
119
+ )
120
+ );
121
+ };
122
+
114
123
  const isStandardLayoutRelativePath = (relativePath: string) => {
115
124
  if (!relativePath) {
116
125
  return true;
@@ -184,6 +193,7 @@ export class BaseLayoutModel<
184
193
 
185
194
  registerRoutePage(pageUid: string, meta: RoutePageMeta) {
186
195
  this.routePageMetaMap.set(pageUid, meta);
196
+ this.restoreCurrentLayoutRouteFromRouterContext(pageUid);
187
197
  const routeModel = this.getCoordinator().registerPage(pageUid, meta);
188
198
  this.getCoordinator().syncRoute(this.getCurrentCoordinatorRouteLike());
189
199
  return routeModel;
@@ -364,6 +374,10 @@ export class BaseLayoutModel<
364
374
  return;
365
375
  }
366
376
 
377
+ if (this.shouldIgnoreStaleLayoutRouteCleanup(routeLike)) {
378
+ return;
379
+ }
380
+
367
381
  this.currentLayoutRoute = null;
368
382
  this.activePageUid = '';
369
383
  this.routeCoordinator?.syncRoute({});
@@ -440,6 +454,98 @@ export class BaseLayoutModel<
440
454
  }
441
455
  return {};
442
456
  }
457
+
458
+ private restoreCurrentLayoutRouteFromRouterContext(pageUid: string) {
459
+ if (this.currentLayoutRoute?.type === 'page') {
460
+ return;
461
+ }
462
+
463
+ const routeLike = this.getRouterContextRouteLike();
464
+ if (!routeLike) {
465
+ return;
466
+ }
467
+
468
+ if (!this.isRouteLikeOwnedByCurrentLayout(routeLike)) {
469
+ return;
470
+ }
471
+
472
+ const layoutRoute = this.resolveLayoutRoute({
473
+ ...routeLike,
474
+ layoutRouteName: routeLike.layoutRouteName || this.layout.routeName,
475
+ });
476
+ if (layoutRoute.type !== 'page' || layoutRoute.pageUid !== pageUid) {
477
+ return;
478
+ }
479
+
480
+ this.currentLayoutRoute = layoutRoute;
481
+ this.activePageUid = layoutRoute.pageUid;
482
+ }
483
+
484
+ private shouldIgnoreStaleLayoutRouteCleanup(routeLike?: LayoutRouteLike) {
485
+ if (!routeLike?.pathname || this.currentLayoutRoute?.type !== 'page') {
486
+ return false;
487
+ }
488
+
489
+ const currentLayoutRoute = this.currentLayoutRoute;
490
+ if (!this.routePageMetaMap.has(currentLayoutRoute.pageUid)) {
491
+ return false;
492
+ }
493
+
494
+ const routeLikePathname = normalizePathname(routeLike.pathname);
495
+ if (routeLikePathname !== currentLayoutRoute.pathname) {
496
+ return false;
497
+ }
498
+
499
+ const routerRouteLike = this.getRouterContextRouteLike();
500
+ if (!routerRouteLike) {
501
+ return false;
502
+ }
503
+
504
+ if (!this.isRouteLikeOwnedByCurrentLayout(routerRouteLike)) {
505
+ return false;
506
+ }
507
+
508
+ const routerLayoutRoute = this.resolveLayoutRoute({
509
+ ...routerRouteLike,
510
+ layoutRouteName: routerRouteLike.layoutRouteName || this.layout.routeName,
511
+ });
512
+
513
+ return (
514
+ routerLayoutRoute.type === 'page' &&
515
+ routerLayoutRoute.pageUid === currentLayoutRoute.pageUid &&
516
+ routerLayoutRoute.pathname === currentLayoutRoute.pathname
517
+ );
518
+ }
519
+
520
+ private getRouterContextRouteLike(): LayoutRouteLike | null {
521
+ const route = this.flowEngine.context.route as LayoutRouteLike | undefined;
522
+ if (!route?.pathname) {
523
+ return null;
524
+ }
525
+
526
+ return {
527
+ id: route.id,
528
+ name: route.name || route.id,
529
+ pathname: route.pathname,
530
+ params: route.params,
531
+ layoutRouteName: route.layoutRouteName,
532
+ layoutBasePathname: route.layoutBasePathname,
533
+ };
534
+ }
535
+
536
+ private isRouteLikeOwnedByCurrentLayout(routeLike: LayoutRouteLike) {
537
+ if (routeLike.layoutRouteName) {
538
+ return routeLike.layoutRouteName === this.layout.routeName;
539
+ }
540
+
541
+ const layoutBasePathname = getDefaultBasePathnameFromRoutePath(this.layout.routePath);
542
+ if (routeLike.layoutBasePathname && layoutBasePathname) {
543
+ return normalizeBasePathname(routeLike.layoutBasePathname) === normalizeBasePathname(layoutBasePathname);
544
+ }
545
+
546
+ const routeName = routeLike.name || routeLike.id;
547
+ return this.isLayoutContentRoute(routeLike) || isLegacyLayoutContentRouteName(this.layout.routeName, routeName);
548
+ }
443
549
  }
444
550
 
445
551
  /**