@nocobase/client-v2 2.4.0-alpha.16 → 2.4.0-alpha.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/es/components/form/VariableInput.d.ts +1 -0
- package/es/flow/admin-shell/admin-layout/MobileTopbarActionsContext.d.ts +10 -0
- package/es/flow/models/base/PageModel/closeGuard.d.ts +8 -1
- package/es/flow/models/blocks/form/EditFormModel.d.ts +2 -0
- package/es/flow/models/blocks/table/sortUtils.d.ts +3 -0
- package/es/flow/models/topbar/UserCenterTopbarActionModel.d.ts +2 -0
- package/es/index.mjs +90 -90
- package/lib/index.js +92 -92
- package/package.json +7 -7
- package/src/components/form/VariableInput.tsx +1 -0
- package/src/flow/admin-shell/admin-layout/MobileTopbarActionsContext.ts +13 -0
- package/src/flow/admin-shell/admin-layout/TopbarActionsBar.tsx +13 -2
- package/src/flow/admin-shell/admin-layout/__tests__/TopbarActionsBar.mobile.test.tsx +259 -0
- package/src/flow/models/base/PageModel/closeGuard.ts +9 -8
- package/src/flow/models/blocks/form/EditFormModel.tsx +23 -1
- package/src/flow/models/blocks/form/__tests__/EditFormModel.pagination.test.tsx +77 -0
- package/src/flow/models/blocks/table/TableBlockModel.tsx +4 -10
- package/src/flow/models/blocks/table/__tests__/sortUtils.test.ts +11 -1
- package/src/flow/models/blocks/table/sortUtils.ts +10 -0
- package/src/flow/models/fields/AssociationFieldModel/CascadeSelectFieldModel.tsx +4 -1
- package/src/flow/models/fields/AssociationFieldModel/RecordSelectFieldModel.tsx +4 -1
- package/src/flow/models/fields/AssociationFieldModel/__tests__/associationSelect.search.test.ts +113 -0
- package/src/flow/models/topbar/UserCenterTopbarActionModel.tsx +64 -30
package/src/flow/models/fields/AssociationFieldModel/__tests__/associationSelect.search.test.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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 { CollectionField } from '@nocobase/flow-engine';
|
|
11
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
12
|
+
import '../../../index';
|
|
13
|
+
import { createTypedFilterable } from '../../../../../collection-manager/filter-operators';
|
|
14
|
+
import { CascadeSelectInnerFieldModel } from '../CascadeSelectFieldModel';
|
|
15
|
+
import { RecordSelectFieldModel } from '../RecordSelectFieldModel';
|
|
16
|
+
|
|
17
|
+
function createContext(fieldOptions: Record<string, unknown>, searchText = ' 3 ') {
|
|
18
|
+
const field = new CollectionField({ name: 'display_name', ...fieldOptions });
|
|
19
|
+
const filterable = createTypedFilterable<{ dataType?: string }>(
|
|
20
|
+
[
|
|
21
|
+
{ types: ['boolean'], operators: 'boolean' },
|
|
22
|
+
{ types: ['string'], operators: 'string' },
|
|
23
|
+
{ types: ['integer'], operators: 'number' },
|
|
24
|
+
],
|
|
25
|
+
(meta) => meta.dataType || 'string',
|
|
26
|
+
);
|
|
27
|
+
vi.spyOn(field, 'getInterfaceOptions').mockReturnValue({ filterable });
|
|
28
|
+
const records = [{ id: 3, display_name: '3 Jackson Hughes' }];
|
|
29
|
+
const resource = {
|
|
30
|
+
setPage: vi.fn(),
|
|
31
|
+
addFilterGroup: vi.fn(),
|
|
32
|
+
removeFilterGroup: vi.fn(),
|
|
33
|
+
refresh: vi.fn().mockResolvedValue(undefined),
|
|
34
|
+
getData: vi.fn(() => records),
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
field,
|
|
38
|
+
inputArgs: { searchText },
|
|
39
|
+
model: {
|
|
40
|
+
collectionField: { targetCollection: { getField: vi.fn(() => field) } },
|
|
41
|
+
props: { fieldNames: { label: 'display_name', value: 'id' } },
|
|
42
|
+
resource,
|
|
43
|
+
setDataSource: vi.fn(),
|
|
44
|
+
setProps: vi.fn(),
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe.each([
|
|
50
|
+
{ name: 'RecordSelect', model: RecordSelectFieldModel, searchGroup: '__search__display_name' },
|
|
51
|
+
{ name: 'CascadeSelect', model: CascadeSelectInnerFieldModel, searchGroup: 'display_name' },
|
|
52
|
+
])('$name search', ({ model, searchGroup }) => {
|
|
53
|
+
beforeEach(() => vi.useFakeTimers());
|
|
54
|
+
afterEach(() => {
|
|
55
|
+
vi.clearAllTimers();
|
|
56
|
+
vi.useRealTimers();
|
|
57
|
+
vi.restoreAllMocks();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
async function search(ctx: ReturnType<typeof createContext>) {
|
|
61
|
+
const handler = model.globalFlowRegistry.getFlow('searchSettings').getStep('step1').serialize().handler as (
|
|
62
|
+
ctx: ReturnType<typeof createContext>,
|
|
63
|
+
params: Record<string, unknown>,
|
|
64
|
+
) => unknown;
|
|
65
|
+
handler(ctx, {});
|
|
66
|
+
await vi.advanceTimersByTimeAsync(500);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
it.each([{ type: 'formula', dataType: 'string' }, { type: 'string' }])(
|
|
70
|
+
'uses partial text matching for $type labels',
|
|
71
|
+
async (options) => {
|
|
72
|
+
const ctx = createContext(options);
|
|
73
|
+
|
|
74
|
+
await search(ctx);
|
|
75
|
+
|
|
76
|
+
expect(ctx.model.resource.addFilterGroup).toHaveBeenCalledWith(searchGroup, { 'display_name.$includes': '3' });
|
|
77
|
+
expect(ctx.model.resource.setPage).toHaveBeenCalledWith(1);
|
|
78
|
+
expect(ctx.model.resource.refresh).toHaveBeenCalledOnce();
|
|
79
|
+
expect(ctx.model.setDataSource).toHaveBeenCalledWith([
|
|
80
|
+
expect.objectContaining({ display_name: '3 Jackson Hughes' }),
|
|
81
|
+
]);
|
|
82
|
+
expect(ctx.model.setProps).toHaveBeenCalledWith({ searchText: '3' });
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
it('preserves the interface operator for non-string labels', async () => {
|
|
87
|
+
const ctx = createContext({ type: 'integer' });
|
|
88
|
+
vi.spyOn(ctx.field, 'getInterfaceOptions').mockReturnValue({ filterable: { operators: [{ value: '$eq' }] } });
|
|
89
|
+
|
|
90
|
+
await search(ctx);
|
|
91
|
+
|
|
92
|
+
expect(ctx.model.resource.addFilterGroup).toHaveBeenCalledWith(searchGroup, { 'display_name.$eq': '3' });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('falls back to partial text matching without an interface', async () => {
|
|
96
|
+
const ctx = createContext({ type: 'string' });
|
|
97
|
+
vi.spyOn(ctx.field, 'getInterfaceOptions').mockReturnValue(undefined);
|
|
98
|
+
|
|
99
|
+
await search(ctx);
|
|
100
|
+
|
|
101
|
+
expect(ctx.model.resource.addFilterGroup).toHaveBeenCalledWith(searchGroup, { 'display_name.$includes': '3' });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('removes the search filter when the search is cleared', async () => {
|
|
105
|
+
const ctx = createContext({ type: 'formula', dataType: 'string' }, ' ');
|
|
106
|
+
|
|
107
|
+
await search(ctx);
|
|
108
|
+
|
|
109
|
+
expect(ctx.model.resource.removeFilterGroup).toHaveBeenCalledWith(searchGroup);
|
|
110
|
+
expect(ctx.model.resource.addFilterGroup).not.toHaveBeenCalled();
|
|
111
|
+
expect(ctx.model.resource.refresh).toHaveBeenCalledOnce();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -11,9 +11,10 @@ import { UserOutlined } from '@ant-design/icons';
|
|
|
11
11
|
import { css, cx } from '@emotion/css';
|
|
12
12
|
import { FlowModel, FlowModelRenderer, observer } from '@nocobase/flow-engine';
|
|
13
13
|
import { Divider, Dropdown, Empty, Select, Spin, theme } from 'antd';
|
|
14
|
-
import React, { useEffect, useState } from 'react';
|
|
14
|
+
import React, { useCallback, useContext, useEffect, useLayoutEffect, useState } from 'react';
|
|
15
15
|
import { ErrorBoundary } from 'react-error-boundary';
|
|
16
16
|
import { createAclSnippetAllow } from '../../../acl/createAclSnippetAllow';
|
|
17
|
+
import { MobileTopbarActionsContext } from '../../admin-shell/admin-layout/MobileTopbarActionsContext';
|
|
17
18
|
import { TopbarActionModel } from './TopbarActionModel';
|
|
18
19
|
|
|
19
20
|
export const USER_CENTER_ACTION_ID = 'user-center';
|
|
@@ -239,6 +240,9 @@ export class UserCenterItemModel extends FlowModel {
|
|
|
239
240
|
|
|
240
241
|
async prepare() {}
|
|
241
242
|
|
|
243
|
+
/** Recheck this item on each opening without delaying other menu items. */
|
|
244
|
+
checkAvailability?(): Promise<boolean>;
|
|
245
|
+
|
|
242
246
|
render() {
|
|
243
247
|
return <UserCenterTextItemView model={this as UserCenterTextItemModel} />;
|
|
244
248
|
}
|
|
@@ -372,6 +376,9 @@ const UserCenterSelectItemView = observer(
|
|
|
372
376
|
{ displayName: 'UserCenterSelectItemView' },
|
|
373
377
|
);
|
|
374
378
|
|
|
379
|
+
// Ant Design caches the hidden dropdown. Context lets availability checks observe every opening.
|
|
380
|
+
const UserCenterOpenContext = React.createContext(false);
|
|
381
|
+
|
|
375
382
|
const UserCenterDropdownPanel = (props: {
|
|
376
383
|
model: UserCenterTopbarActionModel;
|
|
377
384
|
open: boolean;
|
|
@@ -380,7 +387,9 @@ const UserCenterDropdownPanel = (props: {
|
|
|
380
387
|
const { model, open, closeDropdown } = props;
|
|
381
388
|
const { token } = theme.useToken();
|
|
382
389
|
const [loading, setLoading] = useState(false);
|
|
383
|
-
const [
|
|
390
|
+
const [preparedItems, setPreparedItems] = useState<UserCenterItemModel[]>([]);
|
|
391
|
+
const menuOpen = useContext(UserCenterOpenContext);
|
|
392
|
+
const [availability, setAvailability] = useState(new Map<UserCenterItemModel, boolean>());
|
|
384
393
|
|
|
385
394
|
useEffect(() => {
|
|
386
395
|
if (!open) {
|
|
@@ -389,37 +398,57 @@ const UserCenterDropdownPanel = (props: {
|
|
|
389
398
|
|
|
390
399
|
let cancelled = false;
|
|
391
400
|
setLoading(true);
|
|
392
|
-
|
|
401
|
+
setPreparedItems([]);
|
|
393
402
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
if (!cancelled)
|
|
398
|
-
|
|
399
|
-
}
|
|
400
|
-
})
|
|
401
|
-
.catch((error) => {
|
|
403
|
+
async function prepareItems() {
|
|
404
|
+
try {
|
|
405
|
+
const items = await model.getPreparedItems(closeDropdown);
|
|
406
|
+
if (!cancelled) setPreparedItems(items);
|
|
407
|
+
} catch (error) {
|
|
402
408
|
console.error('[NocoBase] User center prepare failed.', error);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
});
|
|
409
|
+
} finally {
|
|
410
|
+
if (!cancelled) setLoading(false);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
prepareItems();
|
|
409
414
|
|
|
410
415
|
return () => {
|
|
411
416
|
cancelled = true;
|
|
412
417
|
};
|
|
413
418
|
}, [closeDropdown, model, open]);
|
|
414
419
|
|
|
420
|
+
useLayoutEffect(() => {
|
|
421
|
+
// Keep the overlay stable during its exit animation. Clear old availability before the next opening paints.
|
|
422
|
+
if (!menuOpen) return;
|
|
423
|
+
let cancelled = false;
|
|
424
|
+
setAvailability(new Map());
|
|
425
|
+
async function checkItem(item: UserCenterItemModel) {
|
|
426
|
+
if (!item.checkAvailability) return;
|
|
427
|
+
let available = false;
|
|
428
|
+
try {
|
|
429
|
+
available = await item.checkAvailability();
|
|
430
|
+
} catch (error) {
|
|
431
|
+
console.error(`[NocoBase] User center item '${item.getItemId()}' availability check failed.`, error);
|
|
432
|
+
}
|
|
433
|
+
if (!cancelled) setAvailability((previous) => new Map(previous).set(item, available));
|
|
434
|
+
}
|
|
435
|
+
preparedItems.forEach(checkItem);
|
|
436
|
+
return () => {
|
|
437
|
+
cancelled = true;
|
|
438
|
+
};
|
|
439
|
+
}, [menuOpen, preparedItems]);
|
|
440
|
+
|
|
441
|
+
const items = preparedItems.filter((item) => !item.checkAvailability || availability.get(item));
|
|
442
|
+
const waiting = loading || (!items.length && preparedItems.some((item) => !availability.has(item)));
|
|
443
|
+
|
|
415
444
|
return (
|
|
416
445
|
<div className={userCenterDropdownClassName} style={getUserCenterDropdownVars(token)}>
|
|
417
|
-
{
|
|
446
|
+
{waiting ? (
|
|
418
447
|
<div className="nb-user-center-loading">
|
|
419
448
|
<Spin size="small" />
|
|
420
449
|
</div>
|
|
421
450
|
) : null}
|
|
422
|
-
{!
|
|
451
|
+
{!waiting && !items.length ? (
|
|
423
452
|
<div className="nb-user-center-empty">
|
|
424
453
|
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description={model.context.t('No data')} />
|
|
425
454
|
</div>
|
|
@@ -452,19 +481,24 @@ const UserCenterTopbarActionView = observer(
|
|
|
452
481
|
(props: { model: UserCenterTopbarActionModel }) => {
|
|
453
482
|
const { model } = props;
|
|
454
483
|
const [open, setOpen] = useState(false);
|
|
484
|
+
const closeMobileTopbar = useContext(MobileTopbarActionsContext);
|
|
485
|
+
const closeDropdown = useCallback(() => {
|
|
486
|
+
setOpen(false);
|
|
487
|
+
closeMobileTopbar?.();
|
|
488
|
+
}, [closeMobileTopbar]);
|
|
455
489
|
|
|
456
490
|
return model.renderWrapper(
|
|
457
|
-
<
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
<UserCenterDropdownPanel model={model} open={open} closeDropdown={
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
</
|
|
491
|
+
<UserCenterOpenContext.Provider value={open}>
|
|
492
|
+
<Dropdown
|
|
493
|
+
trigger={['hover']}
|
|
494
|
+
placement="bottomRight"
|
|
495
|
+
open={open}
|
|
496
|
+
onOpenChange={setOpen}
|
|
497
|
+
dropdownRender={() => <UserCenterDropdownPanel model={model} open={open} closeDropdown={closeDropdown} />}
|
|
498
|
+
>
|
|
499
|
+
<span className={userCenterTriggerClassName}>{model.renderButton()}</span>
|
|
500
|
+
</Dropdown>
|
|
501
|
+
</UserCenterOpenContext.Provider>,
|
|
468
502
|
);
|
|
469
503
|
},
|
|
470
504
|
{ displayName: 'UserCenterTopbarActionView' },
|