@nocobase/client-v2 2.2.0-alpha.3 → 2.2.0-alpha.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/es/RouteRepository.d.ts +8 -0
- package/es/authRedirect.d.ts +1 -0
- package/es/components/form/TypedVariableInput.d.ts +9 -1
- package/es/components/form/filter/CollectionFilter.d.ts +2 -0
- package/es/components/form/filter/CollectionFilterPanel.d.ts +2 -0
- package/es/components/form/filter/useFilterActionProps.d.ts +2 -0
- package/es/index.d.ts +2 -0
- package/es/index.mjs +146 -114
- package/es/utils/getRouteRuntimeVersion.d.ts +23 -0
- package/es/utils/index.d.ts +1 -0
- package/lib/index.js +147 -115
- package/package.json +7 -7
- package/src/RouteRepository.ts +25 -0
- package/src/__tests__/RouteRepository.test.ts +23 -0
- package/src/__tests__/authRedirect.test.ts +17 -0
- package/src/__tests__/getRouteRuntimeVersion.test.ts +68 -0
- package/src/authRedirect.ts +38 -0
- package/src/components/form/JsonTextArea.tsx +4 -0
- package/src/components/form/TypedVariableInput.tsx +58 -34
- package/src/components/form/__tests__/TypedVariableInput.test.tsx +52 -0
- package/src/components/form/filter/CollectionFilter.tsx +4 -0
- package/src/components/form/filter/CollectionFilterPanel.tsx +4 -0
- package/src/components/form/filter/__tests__/useFilterActionProps.test.tsx +95 -0
- package/src/components/form/filter/useFilterActionProps.ts +37 -6
- package/src/flow/actions/dateTimeFormat.tsx +2 -2
- package/src/flow/admin-shell/BaseLayoutModel.tsx +13 -0
- package/src/flow/admin-shell/admin-layout/AdminLayoutEntryGuard.test.tsx +177 -1
- package/src/flow/admin-shell/admin-layout/AdminLayoutEntryGuard.tsx +20 -0
- package/src/flow/admin-shell/admin-layout/__tests__/AdminLayoutModel.test.tsx +77 -0
- package/src/flow/models/blocks/table/TableColumnModel.tsx +6 -2
- package/src/flow/models/blocks/table/__tests__/TableColumnModel.test.tsx +51 -0
- package/src/flow/models/fields/AssociationFieldModel/RecordSelectFieldModel.tsx +45 -13
- package/src/flow/utils/__tests__/dateTimeFormat.test.ts +42 -0
- package/src/index.ts +2 -0
- package/src/utils/getRouteRuntimeVersion.ts +185 -0
- package/src/utils/index.tsx +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/client-v2",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.4",
|
|
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.2.0-alpha.
|
|
31
|
-
"@nocobase/flow-engine": "2.2.0-alpha.
|
|
32
|
-
"@nocobase/sdk": "2.2.0-alpha.
|
|
33
|
-
"@nocobase/shared": "2.2.0-alpha.
|
|
34
|
-
"@nocobase/utils": "2.2.0-alpha.
|
|
30
|
+
"@nocobase/evaluators": "2.2.0-alpha.4",
|
|
31
|
+
"@nocobase/flow-engine": "2.2.0-alpha.4",
|
|
32
|
+
"@nocobase/sdk": "2.2.0-alpha.4",
|
|
33
|
+
"@nocobase/shared": "2.2.0-alpha.4",
|
|
34
|
+
"@nocobase/utils": "2.2.0-alpha.4",
|
|
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": "
|
|
49
|
+
"gitHead": "81bf8733db4bccf08a6bc5e6d97f274bce349161"
|
|
50
50
|
}
|
package/src/RouteRepository.ts
CHANGED
|
@@ -268,6 +268,16 @@ export class RouteRepository {
|
|
|
268
268
|
return this.findRoute(this.listAccessible(), schemaUid);
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
+
/**
|
|
272
|
+
* 通过路由 id 反查对应路由节点。
|
|
273
|
+
*
|
|
274
|
+
* @param routeId 桌面路由主键
|
|
275
|
+
* @returns 匹配到的路由节点
|
|
276
|
+
*/
|
|
277
|
+
getRouteById(routeId: string | number): NocoBaseDesktopRoute | undefined {
|
|
278
|
+
return this.findRouteById(this.listAccessible(), routeId);
|
|
279
|
+
}
|
|
280
|
+
|
|
271
281
|
protected getAPIClient(): APIClient {
|
|
272
282
|
if (!this.ctx?.api) {
|
|
273
283
|
throw new Error('[NocoBase] RouteRepository requires context.api.');
|
|
@@ -346,4 +356,19 @@ export class RouteRepository {
|
|
|
346
356
|
}
|
|
347
357
|
return undefined;
|
|
348
358
|
}
|
|
359
|
+
|
|
360
|
+
private findRouteById(routes: NocoBaseDesktopRoute[], routeId: string | number): NocoBaseDesktopRoute | undefined {
|
|
361
|
+
for (const route of routes) {
|
|
362
|
+
if (route.id != null && String(route.id) === String(routeId)) {
|
|
363
|
+
return route;
|
|
364
|
+
}
|
|
365
|
+
if (route.children) {
|
|
366
|
+
const found = this.findRouteById(route.children, routeId);
|
|
367
|
+
if (found) {
|
|
368
|
+
return found;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
349
374
|
}
|
|
@@ -140,6 +140,29 @@ describe('RouteRepository', () => {
|
|
|
140
140
|
expect(repository.getRouteBySchemaUid('custom-page')).toBeUndefined();
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
+
it('should find nested routes by id', () => {
|
|
144
|
+
const { repository } = createRouteRepository();
|
|
145
|
+
|
|
146
|
+
repository.setRoutes([
|
|
147
|
+
{
|
|
148
|
+
id: 1,
|
|
149
|
+
schemaUid: 'group-1',
|
|
150
|
+
children: [
|
|
151
|
+
{
|
|
152
|
+
id: 11,
|
|
153
|
+
schemaUid: 'page-1',
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
},
|
|
157
|
+
] as NocoBaseDesktopRoute[]);
|
|
158
|
+
|
|
159
|
+
expect(repository.getRouteById('11')).toMatchObject({
|
|
160
|
+
id: 11,
|
|
161
|
+
schemaUid: 'page-1',
|
|
162
|
+
});
|
|
163
|
+
expect(repository.getRouteById('missing')).toBeUndefined();
|
|
164
|
+
});
|
|
165
|
+
|
|
143
166
|
it('should notify only subscribers for the changed layout cache', () => {
|
|
144
167
|
const { repository } = createRouteRepository();
|
|
145
168
|
const adminEvents: string[][] = [];
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import {
|
|
11
11
|
buildV2SigninHref,
|
|
12
12
|
getCurrentV2RedirectPath,
|
|
13
|
+
normalizeV2RedirectPath,
|
|
13
14
|
redirectToV2Signin,
|
|
14
15
|
resolveV2SigninRedirect,
|
|
15
16
|
} from '../authRedirect';
|
|
@@ -133,6 +134,22 @@ describe('auth redirect helpers', () => {
|
|
|
133
134
|
});
|
|
134
135
|
|
|
135
136
|
describe('v2 sub-app context (router basename contains /apps/<id>/)', () => {
|
|
137
|
+
it('should normalize signin redirect fallback under the current sub-app basename', () => {
|
|
138
|
+
const app = {
|
|
139
|
+
getPublicPath: () => '/v/',
|
|
140
|
+
router: {
|
|
141
|
+
getBasename: () => '/v/apps/test-app/',
|
|
142
|
+
},
|
|
143
|
+
} as any;
|
|
144
|
+
|
|
145
|
+
expect(normalizeV2RedirectPath(app, '')).toBe('/v/apps/test-app/admin/');
|
|
146
|
+
expect(normalizeV2RedirectPath(app, '/admin/?tab=overview#panel')).toBe(
|
|
147
|
+
'/v/apps/test-app/admin/?tab=overview#panel',
|
|
148
|
+
);
|
|
149
|
+
expect(normalizeV2RedirectPath(app, '/v/apps/test-app/admin/')).toBe('/v/apps/test-app/admin/');
|
|
150
|
+
expect(normalizeV2RedirectPath(app, '/v/admin/')).toBe('/v/apps/test-app/admin/');
|
|
151
|
+
});
|
|
152
|
+
|
|
136
153
|
it('should preserve sub-app segment when building current redirect path under simple public path', () => {
|
|
137
154
|
const app = {
|
|
138
155
|
getPublicPath: () => '/v2/',
|
|
@@ -0,0 +1,68 @@
|
|
|
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 { afterEach, describe, expect, it } from 'vitest';
|
|
11
|
+
import { getRouteRuntimeVersion } from '../utils/getRouteRuntimeVersion';
|
|
12
|
+
|
|
13
|
+
function replacePathname(pathname: string) {
|
|
14
|
+
Object.defineProperty(window, 'location', {
|
|
15
|
+
configurable: true,
|
|
16
|
+
value: {
|
|
17
|
+
...window.location,
|
|
18
|
+
pathname,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('getRouteRuntimeVersion', () => {
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
delete window.__nocobase_modern_client_prefix__;
|
|
26
|
+
delete window.__nocobase_public_path__;
|
|
27
|
+
replacePathname('/');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('returns modern when the current pathname is inside the modern client prefix', () => {
|
|
31
|
+
window.__nocobase_modern_client_prefix__ = 'v';
|
|
32
|
+
window.__nocobase_public_path__ = '/nocobase/v/';
|
|
33
|
+
replacePathname('/nocobase/v/admin/settings/workflow');
|
|
34
|
+
|
|
35
|
+
expect(getRouteRuntimeVersion()).toBe('modern');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns legacy when reused v2 components run under the v1 settings page', () => {
|
|
39
|
+
window.__nocobase_modern_client_prefix__ = 'v';
|
|
40
|
+
window.__nocobase_public_path__ = '/nocobase/';
|
|
41
|
+
replacePathname('/nocobase/admin/settings/workflow/workflows/123');
|
|
42
|
+
|
|
43
|
+
expect(getRouteRuntimeVersion()).toBe('legacy');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('returns legacy when root public path is "/"', () => {
|
|
47
|
+
window.__nocobase_modern_client_prefix__ = 'v';
|
|
48
|
+
window.__nocobase_public_path__ = '/';
|
|
49
|
+
replacePathname('/admin/settings/workflow/workflows/123');
|
|
50
|
+
|
|
51
|
+
expect(getRouteRuntimeVersion()).toBe('legacy');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('returns modern when root public path is "/v/"', () => {
|
|
55
|
+
window.__nocobase_modern_client_prefix__ = 'v';
|
|
56
|
+
window.__nocobase_public_path__ = '/v/';
|
|
57
|
+
replacePathname('/v/admin/workflow/workflows/123');
|
|
58
|
+
|
|
59
|
+
expect(getRouteRuntimeVersion()).toBe('modern');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('falls back to pathname matching when public path is unavailable', () => {
|
|
63
|
+
window.__nocobase_modern_client_prefix__ = 'v';
|
|
64
|
+
replacePathname('/v/admin/workflow/workflows/123');
|
|
65
|
+
|
|
66
|
+
expect(getRouteRuntimeVersion()).toBe('modern');
|
|
67
|
+
});
|
|
68
|
+
});
|
package/src/authRedirect.ts
CHANGED
|
@@ -204,6 +204,44 @@ function getDefaultV2AdminRedirectPath(app: AppLike) {
|
|
|
204
204
|
return joinRootRelativePath(getV2EffectiveBasePath(app), '/admin');
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
function isSafeRootRelativePath(value?: string | null) {
|
|
208
|
+
return !!value && value.startsWith('/') && !value.startsWith('//') && !value.startsWith('/\\');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function preserveTrailingSlash(originalPathname: string, value: string) {
|
|
212
|
+
if (originalPathname !== '/' && originalPathname.endsWith('/') && !value.endsWith('/')) {
|
|
213
|
+
return `${value}/`;
|
|
214
|
+
}
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function normalizeV2RedirectPath(app: AppLike, target?: string | null, fallbackPath = '/admin/') {
|
|
219
|
+
// In a v2 sub-app, publicPath can be `/v/` while the active router
|
|
220
|
+
// basename is `/v/apps/a/`. Redirects must resolve under the basename.
|
|
221
|
+
const basePath = trimTrailingSlashes(getV2EffectiveBasePath(app)) || '/';
|
|
222
|
+
const fallbackTarget = isSafeRootRelativePath(fallbackPath) ? fallbackPath : '/admin/';
|
|
223
|
+
const rawTarget = isSafeRootRelativePath(target) ? target : fallbackTarget;
|
|
224
|
+
let { pathname, search, hash } = splitPathLike(rawTarget);
|
|
225
|
+
let normalizedPathname = normalizePathname(pathname);
|
|
226
|
+
|
|
227
|
+
// Already under the current v2 runtime, e.g. `/v/apps/a/admin/`.
|
|
228
|
+
if (basePath === '/' || normalizedPathname === basePath || normalizedPathname.startsWith(`${basePath}/`)) {
|
|
229
|
+
return `${preserveTrailingSlash(pathname, normalizedPathname)}${normalizeSearch(search)}${normalizeHash(hash)}`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const publicPath = trimTrailingSlashes(getV2PublicPath(app)) || '/';
|
|
233
|
+
// Under v2 publicPath but outside the current basename, e.g. `/v/admin/`
|
|
234
|
+
// inside `/v/apps/a/`; use fallback so it lands on `/v/apps/a/admin/`.
|
|
235
|
+
if (publicPath !== '/' && (normalizedPathname === publicPath || normalizedPathname.startsWith(`${publicPath}/`))) {
|
|
236
|
+
({ pathname, search, hash } = splitPathLike(fallbackTarget));
|
|
237
|
+
normalizedPathname = normalizePathname(pathname);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Basename-relative target, e.g. `/admin/`, becomes `/v/apps/a/admin/`.
|
|
241
|
+
const joinedPathname = preserveTrailingSlash(pathname, joinRootRelativePath(basePath, normalizedPathname));
|
|
242
|
+
return `${joinedPathname}${normalizeSearch(search)}${normalizeHash(hash)}`;
|
|
243
|
+
}
|
|
244
|
+
|
|
207
245
|
/**
|
|
208
246
|
* 将当前 v2 页面地址转换为根相对 redirect 路径。
|
|
209
247
|
*
|
|
@@ -52,7 +52,9 @@ export interface TypedVariableInputProps {
|
|
|
52
52
|
* Allowed constant types. The `Constant` switcher entry always exposes a
|
|
53
53
|
* typed submenu (matching v1 `Variable.Input`) so users can see what type
|
|
54
54
|
* the constant is, even when only one type is permitted. Default: all four
|
|
55
|
-
* supported types.
|
|
55
|
+
* supported types. Passing `[]` switches the component into a variable-only
|
|
56
|
+
* mode: constants/null are hidden, the empty state becomes a readonly
|
|
57
|
+
* placeholder, and clearing a selected variable resets to `null`.
|
|
56
58
|
*/
|
|
57
59
|
types?: TypedConstantSpec[];
|
|
58
60
|
/**
|
|
@@ -89,6 +91,12 @@ export interface TypedVariableInputProps {
|
|
|
89
91
|
defaultToFirstConstantTypeWhenUndefined?: boolean;
|
|
90
92
|
/** Variable-token delimiters. Default `['{{', '}}']` — see `VariableInput`. */
|
|
91
93
|
delimiters?: VariableDelimiters;
|
|
94
|
+
/**
|
|
95
|
+
* Hide variable choices from the switcher. In variable-only mode this hides
|
|
96
|
+
* the selector button entirely; in mixed constant/variable mode it keeps the
|
|
97
|
+
* null/constant choices but removes variable entries.
|
|
98
|
+
*/
|
|
99
|
+
hideVariable?: boolean;
|
|
92
100
|
disabled?: boolean;
|
|
93
101
|
placeholder?: string;
|
|
94
102
|
style?: React.CSSProperties;
|
|
@@ -400,6 +408,7 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
400
408
|
nullable = true,
|
|
401
409
|
defaultToFirstConstantTypeWhenUndefined = true,
|
|
402
410
|
delimiters,
|
|
411
|
+
hideVariable = false,
|
|
403
412
|
disabled,
|
|
404
413
|
placeholder,
|
|
405
414
|
style,
|
|
@@ -418,13 +427,14 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
418
427
|
const formatVariablePath = useMemo(() => makeFormatVariablePath(delimiters), [delimiters]);
|
|
419
428
|
|
|
420
429
|
const normalizedTypes = useMemo(() => normalizeTypes(types), [types]);
|
|
430
|
+
const variableOnly = normalizedTypes.length === 0;
|
|
421
431
|
const defaultedValue = useMemo(() => {
|
|
422
|
-
if (!defaultToFirstConstantTypeWhenUndefined || value !== undefined) {
|
|
432
|
+
if (variableOnly || !defaultToFirstConstantTypeWhenUndefined || value !== undefined) {
|
|
423
433
|
return undefined;
|
|
424
434
|
}
|
|
425
435
|
const firstType = normalizedTypes[0];
|
|
426
436
|
return firstType ? defaultValueFor(firstType.type) : undefined;
|
|
427
|
-
}, [defaultToFirstConstantTypeWhenUndefined, normalizedTypes, value]);
|
|
437
|
+
}, [defaultToFirstConstantTypeWhenUndefined, normalizedTypes, value, variableOnly]);
|
|
428
438
|
const effectiveValue = value === undefined && defaultedValue !== undefined ? defaultedValue : value;
|
|
429
439
|
const detected = useMemo(() => detectMode(effectiveValue, parseVariablePath), [effectiveValue, parseVariablePath]);
|
|
430
440
|
|
|
@@ -481,13 +491,13 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
481
491
|
// `updateFlag` is read so this recomputes (with fresh option references) after a lazy load mutates the meta tree.
|
|
482
492
|
void updateFlag;
|
|
483
493
|
const items: SwitcherOption[] = [];
|
|
484
|
-
if (nullable) {
|
|
494
|
+
if (!variableOnly && nullable) {
|
|
485
495
|
items.push({ value: NULL_KEY, label: t('Null'), isLeaf: true });
|
|
486
496
|
}
|
|
487
497
|
// Always render Constant with a typed submenu — even when only one type is
|
|
488
498
|
// allowed. Matches v1 `Variable.Input`, where clicking 常量 reveals 数字 /
|
|
489
499
|
// 逻辑值 / etc. so the user can see what type the constant actually is.
|
|
490
|
-
if (normalizedTypes.length > 0) {
|
|
500
|
+
if (!variableOnly && normalizedTypes.length > 0) {
|
|
491
501
|
items.push({
|
|
492
502
|
value: CONST_KEY,
|
|
493
503
|
label: t('Constant'),
|
|
@@ -498,9 +508,11 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
498
508
|
})),
|
|
499
509
|
});
|
|
500
510
|
}
|
|
501
|
-
|
|
511
|
+
if (!hideVariable) {
|
|
512
|
+
items.push(...buildContextSelectorItems(metaTree).map(fromContextItem));
|
|
513
|
+
}
|
|
502
514
|
return items;
|
|
503
|
-
}, [
|
|
515
|
+
}, [hideVariable, metaTree, normalizedTypes, nullable, t, updateFlag, variableOnly]);
|
|
504
516
|
|
|
505
517
|
const onSwitcherChange = useCallback<NonNullable<CascaderProps<SwitcherOption>['onChange']>>(
|
|
506
518
|
(path, selectedOptions) => {
|
|
@@ -530,6 +542,10 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
530
542
|
);
|
|
531
543
|
|
|
532
544
|
const onClearVariable = useCallback(() => {
|
|
545
|
+
if (variableOnly) {
|
|
546
|
+
onChange?.(null);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
533
549
|
const first = normalizedTypes[0];
|
|
534
550
|
if (first) {
|
|
535
551
|
onChange?.(defaultValueFor(first.type));
|
|
@@ -538,7 +554,7 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
538
554
|
if (nullable) {
|
|
539
555
|
onChange?.(null);
|
|
540
556
|
}
|
|
541
|
-
}, [nullable, normalizedTypes, onChange]);
|
|
557
|
+
}, [nullable, normalizedTypes, onChange, variableOnly]);
|
|
542
558
|
|
|
543
559
|
const constantTypeForRendering: TypedConstantType = useMemo(() => {
|
|
544
560
|
const m = detected.mode;
|
|
@@ -553,6 +569,7 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
553
569
|
|
|
554
570
|
const isVariable = detected.mode === 'variable';
|
|
555
571
|
const isNull = detected.mode === 'null';
|
|
572
|
+
const showSwitcher = switcherOptions.length > 0 && !(hideVariable && isVariable);
|
|
556
573
|
|
|
557
574
|
const variableLabels = useMemo(() => {
|
|
558
575
|
// `updateFlag` is read so this recomputes after the preload effect below resolves a lazy level in the tree (same
|
|
@@ -565,11 +582,14 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
565
582
|
if (isVariable && detected.variablePath?.length) {
|
|
566
583
|
return detected.variablePath;
|
|
567
584
|
}
|
|
585
|
+
if (variableOnly) {
|
|
586
|
+
return undefined;
|
|
587
|
+
}
|
|
568
588
|
if (isNull) {
|
|
569
589
|
return [NULL_KEY];
|
|
570
590
|
}
|
|
571
591
|
return [CONST_KEY, constantTypeForRendering];
|
|
572
|
-
}, [constantTypeForRendering, detected.variablePath, isNull, isVariable]);
|
|
592
|
+
}, [constantTypeForRendering, detected.variablePath, isNull, isVariable, variableOnly]);
|
|
573
593
|
|
|
574
594
|
// Preload a saved variable's label path across lazy levels. `resolveVariableLabels` can only read already-loaded
|
|
575
595
|
// `children`; when a saved reference points below a node whose children are still a lazy thunk (e.g. a relation field
|
|
@@ -678,8 +698,8 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
678
698
|
minHeight: token.controlHeight,
|
|
679
699
|
border: `1px solid ${token.colorBorder}`,
|
|
680
700
|
borderRadius: token.borderRadius,
|
|
681
|
-
borderTopRightRadius: 0,
|
|
682
|
-
borderBottomRightRadius: 0,
|
|
701
|
+
borderTopRightRadius: showSwitcher ? 0 : token.borderRadius,
|
|
702
|
+
borderBottomRightRadius: showSwitcher ? 0 : token.borderRadius,
|
|
683
703
|
background: disabled ? token.colorBgContainerDisabled : token.colorBgContainer,
|
|
684
704
|
overflow: 'hidden',
|
|
685
705
|
}}
|
|
@@ -712,6 +732,8 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
712
732
|
/>
|
|
713
733
|
) : null}
|
|
714
734
|
</div>
|
|
735
|
+
) : variableOnly ? (
|
|
736
|
+
<Input placeholder={placeholder} readOnly disabled={disabled} style={{ width: '100%' }} />
|
|
715
737
|
) : isNull ? (
|
|
716
738
|
// v1 used the `placeholder` slot (not `value`) so the antd default placeholder colour applies — keeps the
|
|
717
739
|
// field looking visibly empty/inactive rather than holding a real text value.
|
|
@@ -726,31 +748,33 @@ export function TypedVariableInput(props: TypedVariableInputProps) {
|
|
|
726
748
|
})
|
|
727
749
|
)}
|
|
728
750
|
</div>
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
changeOnSelect
|
|
736
|
-
>
|
|
737
|
-
<Button
|
|
738
|
-
aria-label="variable-switcher"
|
|
751
|
+
{showSwitcher ? (
|
|
752
|
+
<Cascader<SwitcherOption>
|
|
753
|
+
options={switcherOptions}
|
|
754
|
+
value={switcherValue}
|
|
755
|
+
onChange={onSwitcherChange}
|
|
756
|
+
loadData={loadData}
|
|
739
757
|
disabled={disabled}
|
|
740
|
-
|
|
741
|
-
style={{
|
|
742
|
-
flexShrink: 0,
|
|
743
|
-
// `height: auto` (instead of antd's fixed control height) lets the button stretch to the value
|
|
744
|
-
// component's height under the compact row's default `align-items: stretch` — so it stays joined to a
|
|
745
|
-
// tall JSON textarea. Mirrors v1's `.ant-btn { height: auto }`.
|
|
746
|
-
height: 'auto',
|
|
747
|
-
fontStyle: 'italic',
|
|
748
|
-
fontFamily: '"New York", "Times New Roman", Times, serif',
|
|
749
|
-
}}
|
|
758
|
+
changeOnSelect
|
|
750
759
|
>
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
760
|
+
<Button
|
|
761
|
+
aria-label="variable-switcher"
|
|
762
|
+
disabled={disabled}
|
|
763
|
+
type={isVariable ? 'primary' : 'default'}
|
|
764
|
+
style={{
|
|
765
|
+
flexShrink: 0,
|
|
766
|
+
// `height: auto` (instead of antd's fixed control height) lets the button stretch to the value
|
|
767
|
+
// component's height under the compact row's default `align-items: stretch` — so it stays joined to a
|
|
768
|
+
// tall JSON textarea. Mirrors v1's `.ant-btn { height: auto }`.
|
|
769
|
+
height: 'auto',
|
|
770
|
+
fontStyle: 'italic',
|
|
771
|
+
fontFamily: '"New York", "Times New Roman", Times, serif',
|
|
772
|
+
}}
|
|
773
|
+
>
|
|
774
|
+
x
|
|
775
|
+
</Button>
|
|
776
|
+
</Cascader>
|
|
777
|
+
) : null}
|
|
754
778
|
</Space.Compact>
|
|
755
779
|
{jsonError ? (
|
|
756
780
|
<div style={{ marginTop: token.marginXXS, color: token.colorError, fontSize: token.fontSizeSM }}>
|
|
@@ -201,6 +201,58 @@ describe('TypedVariableInput - variable rendering', () => {
|
|
|
201
201
|
fireEvent.click(clear as HTMLButtonElement);
|
|
202
202
|
expect(handleChange).toHaveBeenCalledWith(0);
|
|
203
203
|
});
|
|
204
|
+
|
|
205
|
+
it('treats types=[] as variable-only mode with a readonly placeholder before selection', async () => {
|
|
206
|
+
const ctx = createContextWithEnv();
|
|
207
|
+
renderWithCtx(
|
|
208
|
+
ctx,
|
|
209
|
+
<TypedVariableInput
|
|
210
|
+
value={undefined}
|
|
211
|
+
types={[]}
|
|
212
|
+
namespaces={['$env']}
|
|
213
|
+
placeholder="Select variable"
|
|
214
|
+
onChange={() => undefined}
|
|
215
|
+
/>,
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const input = await screen.findByPlaceholderText('Select variable');
|
|
219
|
+
expect(input).toBeInTheDocument();
|
|
220
|
+
expect(input).toHaveAttribute('readonly');
|
|
221
|
+
expect(screen.queryByRole('button', { name: 'variable-switcher' })).toBeInTheDocument();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('clears a variable-only selection back to null', async () => {
|
|
225
|
+
const ctx = createContextWithEnv();
|
|
226
|
+
const handleChange = vi.fn();
|
|
227
|
+
const { container } = renderWithCtx(
|
|
228
|
+
ctx,
|
|
229
|
+
<TypedVariableInput value="{{$env.SMTP_PORT}}" types={[]} namespaces={['$env']} onChange={handleChange} />,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const clear = container.querySelector('button.clear-button') as HTMLButtonElement | null;
|
|
233
|
+
expect(clear).not.toBeNull();
|
|
234
|
+
fireEvent.click(clear as HTMLButtonElement);
|
|
235
|
+
expect(handleChange).toHaveBeenCalledWith(null);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('hides the variable switcher when hideVariable=true in variable-only mode', async () => {
|
|
239
|
+
const ctx = createContextWithEnv();
|
|
240
|
+
renderWithCtx(
|
|
241
|
+
ctx,
|
|
242
|
+
<TypedVariableInput
|
|
243
|
+
value="{{$env.SMTP_PORT}}"
|
|
244
|
+
types={[]}
|
|
245
|
+
namespaces={['$env']}
|
|
246
|
+
hideVariable
|
|
247
|
+
onChange={() => undefined}
|
|
248
|
+
/>,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
await waitFor(() => {
|
|
252
|
+
expect(screen.getByRole('button', { name: 'variable-tag' })).toBeInTheDocument();
|
|
253
|
+
});
|
|
254
|
+
expect(screen.queryByRole('button', { name: 'variable-switcher' })).toBeNull();
|
|
255
|
+
});
|
|
204
256
|
});
|
|
205
257
|
|
|
206
258
|
describe('TypedVariableInput - object / JSON constant', () => {
|
|
@@ -21,6 +21,8 @@ export interface CollectionFilterProps {
|
|
|
21
21
|
collection: Collection | undefined;
|
|
22
22
|
/** Previously compiled filter param used to seed the editable filter group. */
|
|
23
23
|
initialValue?: CompiledFilter;
|
|
24
|
+
/** Default compiled filter used both for initial empty state and Reset. */
|
|
25
|
+
defaultValue?: CompiledFilter;
|
|
24
26
|
/** Called on Submit or Reset with the compiled NocoBase filter param (`undefined` when cleared). */
|
|
25
27
|
onChange: (filter: CompiledFilter) => void;
|
|
26
28
|
/** Translator. Defaults to identity. */
|
|
@@ -58,6 +60,7 @@ export const CollectionFilter: FC<CollectionFilterProps> = (props) => {
|
|
|
58
60
|
const {
|
|
59
61
|
collection,
|
|
60
62
|
initialValue,
|
|
63
|
+
defaultValue,
|
|
61
64
|
onChange,
|
|
62
65
|
t = identity,
|
|
63
66
|
filterableFieldNames,
|
|
@@ -75,6 +78,7 @@ export const CollectionFilter: FC<CollectionFilterProps> = (props) => {
|
|
|
75
78
|
const filterAction = useFilterActionProps({
|
|
76
79
|
collection,
|
|
77
80
|
initialValue,
|
|
81
|
+
defaultValue,
|
|
78
82
|
filterableFieldNames,
|
|
79
83
|
nonfilterableFieldNames,
|
|
80
84
|
noIgnore,
|
|
@@ -30,6 +30,8 @@ export interface CollectionFilterPanelProps {
|
|
|
30
30
|
collection: Collection | undefined;
|
|
31
31
|
/** Previously compiled filter param used to seed the editable filter group. */
|
|
32
32
|
initialValue?: CompiledFilter;
|
|
33
|
+
/** Default compiled filter used both for initial empty state and Reset. */
|
|
34
|
+
defaultValue?: CompiledFilter;
|
|
33
35
|
/** Called when the condition group structure changes or `reset()` is invoked. */
|
|
34
36
|
onChange?: (filter: CompiledFilter) => void;
|
|
35
37
|
/** Translator. Defaults to identity. */
|
|
@@ -51,6 +53,7 @@ export const CollectionFilterPanel = forwardRef<CollectionFilterPanelRef, Collec
|
|
|
51
53
|
const {
|
|
52
54
|
collection,
|
|
53
55
|
initialValue,
|
|
56
|
+
defaultValue,
|
|
54
57
|
onChange,
|
|
55
58
|
t = identity,
|
|
56
59
|
filterableFieldNames,
|
|
@@ -61,6 +64,7 @@ export const CollectionFilterPanel = forwardRef<CollectionFilterPanelRef, Collec
|
|
|
61
64
|
const filterAction = useFilterActionProps({
|
|
62
65
|
collection,
|
|
63
66
|
initialValue,
|
|
67
|
+
defaultValue,
|
|
64
68
|
filterableFieldNames,
|
|
65
69
|
nonfilterableFieldNames,
|
|
66
70
|
noIgnore,
|
|
@@ -0,0 +1,95 @@
|
|
|
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 { FlowEngine, FlowEngineProvider, type Collection } from '@nocobase/flow-engine';
|
|
11
|
+
import { renderHook } from '@testing-library/react';
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
14
|
+
import { useFilterActionProps } from '../useFilterActionProps';
|
|
15
|
+
|
|
16
|
+
vi.mock('../../../flow/components/filter/useFilterOptions', () => ({
|
|
17
|
+
useFilterOptions: () => [],
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock('../CollectionFilterItem', () => ({
|
|
21
|
+
createCollectionFilterItem: () => undefined,
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
function makeCollection(): Collection {
|
|
25
|
+
return {
|
|
26
|
+
getFields: () => [{ name: 'title' }, { name: 'type' }],
|
|
27
|
+
} as unknown as Collection;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function makeWrapper() {
|
|
31
|
+
const engine = new FlowEngine();
|
|
32
|
+
const Wrapper: React.FC = ({ children }) => <FlowEngineProvider engine={engine}>{children}</FlowEngineProvider>;
|
|
33
|
+
return Wrapper;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('useFilterActionProps', () => {
|
|
37
|
+
it('seeds the editable group from defaultValue when initialValue is missing', () => {
|
|
38
|
+
const { result } = renderHook(
|
|
39
|
+
() =>
|
|
40
|
+
useFilterActionProps({
|
|
41
|
+
collection: makeCollection(),
|
|
42
|
+
defaultValue: { $and: [{ title: { $includes: '' } }, { type: { $eq: undefined } }] },
|
|
43
|
+
onApply: () => undefined,
|
|
44
|
+
}),
|
|
45
|
+
{ wrapper: makeWrapper() },
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(result.current.value).toMatchObject({
|
|
49
|
+
logic: '$and',
|
|
50
|
+
items: [
|
|
51
|
+
{ path: 'title', operator: '$includes', value: '' },
|
|
52
|
+
{ path: 'type', operator: '$eq', value: undefined },
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('reset restores defaultValue and emits its compiled filter', () => {
|
|
58
|
+
const onApply = vi.fn();
|
|
59
|
+
const { result } = renderHook(
|
|
60
|
+
() =>
|
|
61
|
+
useFilterActionProps({
|
|
62
|
+
collection: makeCollection(),
|
|
63
|
+
defaultValue: { $and: [{ title: { $includes: '' } }, { type: { $eq: undefined } }] },
|
|
64
|
+
initialValue: { $and: [{ title: { $includes: 'abc' } }] },
|
|
65
|
+
onApply,
|
|
66
|
+
}),
|
|
67
|
+
{ wrapper: makeWrapper() },
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
result.current.onReset();
|
|
71
|
+
|
|
72
|
+
expect(result.current.value).toMatchObject({
|
|
73
|
+
logic: '$and',
|
|
74
|
+
items: [
|
|
75
|
+
{ path: 'title', operator: '$includes', value: '' },
|
|
76
|
+
{ path: 'type', operator: '$eq', value: undefined },
|
|
77
|
+
],
|
|
78
|
+
});
|
|
79
|
+
expect(onApply).toHaveBeenCalledWith(undefined, 'reset');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('counts only effective compiled conditions, so empty default rows do not highlight the button', () => {
|
|
83
|
+
const { result } = renderHook(
|
|
84
|
+
() =>
|
|
85
|
+
useFilterActionProps({
|
|
86
|
+
collection: makeCollection(),
|
|
87
|
+
defaultValue: { $and: [{ title: { $includes: '' } }, { type: { $eq: undefined } }] },
|
|
88
|
+
onApply: () => undefined,
|
|
89
|
+
}),
|
|
90
|
+
{ wrapper: makeWrapper() },
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(result.current.conditionCount).toBe(0);
|
|
94
|
+
});
|
|
95
|
+
});
|