@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/client-v2",
|
|
3
|
-
"version": "2.4.0-alpha.
|
|
3
|
+
"version": "2.4.0-alpha.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.4.0-alpha.
|
|
31
|
-
"@nocobase/flow-engine": "2.4.0-alpha.
|
|
32
|
-
"@nocobase/sdk": "2.4.0-alpha.
|
|
33
|
-
"@nocobase/shared": "2.4.0-alpha.
|
|
34
|
-
"@nocobase/utils": "2.4.0-alpha.
|
|
30
|
+
"@nocobase/evaluators": "2.4.0-alpha.18",
|
|
31
|
+
"@nocobase/flow-engine": "2.4.0-alpha.18",
|
|
32
|
+
"@nocobase/sdk": "2.4.0-alpha.18",
|
|
33
|
+
"@nocobase/shared": "2.4.0-alpha.18",
|
|
34
|
+
"@nocobase/utils": "2.4.0-alpha.18",
|
|
35
35
|
"ahooks": "^3.7.2",
|
|
36
36
|
"antd": "5.24.2",
|
|
37
37
|
"antd-style": "3.7.1",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"react-router-dom": "^6.30.1",
|
|
50
50
|
"zxing-wasm": "^3.1.3"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "ae799cda3345b2bcb9f47567303b2dccce91d90c"
|
|
53
53
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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 { createContext } from 'react';
|
|
11
|
+
|
|
12
|
+
// Internal callback for dismissing the mobile topbar after a nested menu action completes.
|
|
13
|
+
export const MobileTopbarActionsContext = createContext<(() => void) | undefined>(undefined);
|
|
@@ -11,13 +11,14 @@ import { EllipsisOutlined } from '@ant-design/icons';
|
|
|
11
11
|
import { css } from '@emotion/css';
|
|
12
12
|
import { FlowModelRenderer } from '@nocobase/flow-engine';
|
|
13
13
|
import { ConfigProvider, Divider, Popover, theme } from 'antd';
|
|
14
|
-
import React, { useCallback, useMemo, useState } from 'react';
|
|
14
|
+
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
15
15
|
import { ErrorBoundary } from 'react-error-boundary';
|
|
16
16
|
import { useAclSnippets } from '../../../acl/useAclSnippets';
|
|
17
17
|
import type { CustomToken } from '../../../theme';
|
|
18
18
|
import { TopbarActionModel } from '../../models/topbar/TopbarActionModel';
|
|
19
19
|
import { USER_CENTER_ACTION_ID } from '../../models/topbar/UserCenterTopbarActionModel';
|
|
20
20
|
import { HelpLite } from './HelpLite';
|
|
21
|
+
import { MobileTopbarActionsContext } from './MobileTopbarActionsContext';
|
|
21
22
|
|
|
22
23
|
const topbarActionsBarClassName = css`
|
|
23
24
|
display: inline-flex;
|
|
@@ -206,11 +207,21 @@ export const TopbarActionsBar = React.memo((props: { actions: TopbarActionModel[
|
|
|
206
207
|
setOpen(false);
|
|
207
208
|
}, []);
|
|
208
209
|
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
if (!props.isMobile) {
|
|
212
|
+
setOpen(false);
|
|
213
|
+
}
|
|
214
|
+
}, [props.isMobile]);
|
|
215
|
+
|
|
209
216
|
if (props.isMobile) {
|
|
210
217
|
return (
|
|
211
218
|
<Popover
|
|
212
219
|
rootClassName={popoverStyle}
|
|
213
|
-
content={
|
|
220
|
+
content={
|
|
221
|
+
<MobileTopbarActionsContext.Provider value={handleContentClick}>
|
|
222
|
+
<TopbarActionsContent actions={props.actions} onActionClick={handleContentClick} />
|
|
223
|
+
</MobileTopbarActionsContext.Provider>
|
|
224
|
+
}
|
|
214
225
|
color={customToken.colorBgHeader}
|
|
215
226
|
trigger="click"
|
|
216
227
|
open={open}
|
|
@@ -0,0 +1,259 @@
|
|
|
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 { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
11
|
+
import { Drawer, Input } from 'antd';
|
|
12
|
+
import React, { useState } from 'react';
|
|
13
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
14
|
+
|
|
15
|
+
vi.mock('@nocobase/flow-engine', () => ({
|
|
16
|
+
FlowModel: class {
|
|
17
|
+
uid: string;
|
|
18
|
+
context = { t: (value: string) => value };
|
|
19
|
+
|
|
20
|
+
constructor(options: { uid: string }) {
|
|
21
|
+
this.uid = options.uid;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
FlowModelRenderer: ({ model }: { model: { render: () => React.ReactNode } }) => model.render(),
|
|
25
|
+
observer: (component: React.ComponentType) => component,
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
vi.mock('../../../models/topbar/TopbarActionModel', async () => {
|
|
29
|
+
const { FlowModel } = await import('@nocobase/flow-engine');
|
|
30
|
+
return {
|
|
31
|
+
TopbarActionModel: class extends FlowModel {
|
|
32
|
+
isHidden() {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getActionId() {
|
|
37
|
+
return 'user-center';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
renderWrapper(node: React.ReactNode) {
|
|
41
|
+
return node;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
renderButton() {
|
|
45
|
+
return <button data-testid="user-center-button">User center</button>;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
vi.mock('../../../../acl/useAclSnippets', () => ({
|
|
52
|
+
useAclSnippets: () => ({ allow: () => true }),
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
vi.mock('../HelpLite', () => ({ HelpLite: () => null }));
|
|
56
|
+
|
|
57
|
+
import {
|
|
58
|
+
UserCenterActionItemModel,
|
|
59
|
+
UserCenterSelectItemModel,
|
|
60
|
+
UserCenterTopbarActionModel,
|
|
61
|
+
} from '../../../models/topbar/UserCenterTopbarActionModel';
|
|
62
|
+
import { TopbarActionsBar } from '../TopbarActionsBar';
|
|
63
|
+
|
|
64
|
+
afterEach(cleanup);
|
|
65
|
+
|
|
66
|
+
function createUserCenter(onClick: () => Promise<void>, closeOnClick = true) {
|
|
67
|
+
const item = new UserCenterActionItemModel({ uid: 'test-edit-profile', use: 'UserCenterActionItemModel' });
|
|
68
|
+
item.label = 'Edit profile';
|
|
69
|
+
item.onClick = onClick;
|
|
70
|
+
item.closeOnClick = closeOnClick;
|
|
71
|
+
const model = new UserCenterTopbarActionModel({ uid: 'test-user-center', use: 'UserCenterTopbarActionModel' });
|
|
72
|
+
vi.spyOn(model, 'getPreparedItems').mockImplementation(async (closeDropdown) => {
|
|
73
|
+
item.setCloseDropdown(closeDropdown);
|
|
74
|
+
return [item];
|
|
75
|
+
});
|
|
76
|
+
return model;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function openUserMenu(isMobile: boolean) {
|
|
80
|
+
if (isMobile) {
|
|
81
|
+
fireEvent.click(screen.getByRole('img', { name: 'ellipsis' }));
|
|
82
|
+
}
|
|
83
|
+
fireEvent.mouseEnter(screen.getByTestId('user-center-button'));
|
|
84
|
+
const action = await screen.findByRole('button', { name: 'Edit profile' });
|
|
85
|
+
await waitFor(() => expect(action).toBeVisible());
|
|
86
|
+
return action;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
describe('mobile user center popover', () => {
|
|
90
|
+
it('closes both menus after selecting an action and keeps the opened drawer mounted', async () => {
|
|
91
|
+
const model = createUserCenter(vi.fn());
|
|
92
|
+
const Test = () => {
|
|
93
|
+
const [open, setOpen] = useState(false);
|
|
94
|
+
model.getPreparedItems = async (closeDropdown) => {
|
|
95
|
+
const item = new UserCenterActionItemModel({ uid: 'profile', use: 'UserCenterActionItemModel' });
|
|
96
|
+
item.label = 'Edit profile';
|
|
97
|
+
item.onClick = async () => setOpen(true);
|
|
98
|
+
item.setCloseDropdown(closeDropdown);
|
|
99
|
+
return [item];
|
|
100
|
+
};
|
|
101
|
+
return (
|
|
102
|
+
<>
|
|
103
|
+
<TopbarActionsBar actions={[model]} isMobile />
|
|
104
|
+
<Drawer open={open} title="Edit profile">
|
|
105
|
+
<Input aria-label="Nickname" />
|
|
106
|
+
</Drawer>
|
|
107
|
+
</>
|
|
108
|
+
);
|
|
109
|
+
};
|
|
110
|
+
render(<Test />);
|
|
111
|
+
|
|
112
|
+
fireEvent.click(await openUserMenu(true));
|
|
113
|
+
|
|
114
|
+
await waitFor(() => expect(screen.queryByRole('tooltip')).not.toBeInTheDocument());
|
|
115
|
+
expect(screen.getByRole('textbox', { name: 'Nickname' })).toBeVisible();
|
|
116
|
+
expect(screen.queryByRole('button', { name: 'Edit profile' })).not.toBeInTheDocument();
|
|
117
|
+
|
|
118
|
+
fireEvent.click(screen.getByRole('img', { name: 'ellipsis' }));
|
|
119
|
+
fireEvent.mouseEnter(screen.getByTestId('user-center-button'));
|
|
120
|
+
await waitFor(() => expect(screen.getByRole('button', { name: 'Edit profile' })).toBeVisible());
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('respects actions that keep the menu open', async () => {
|
|
124
|
+
const onClick = vi.fn(async () => {});
|
|
125
|
+
render(<TopbarActionsBar actions={[createUserCenter(onClick, false)]} isMobile />);
|
|
126
|
+
|
|
127
|
+
fireEvent.click(await openUserMenu(true));
|
|
128
|
+
|
|
129
|
+
await waitFor(() => expect(onClick).toHaveBeenCalledOnce());
|
|
130
|
+
expect(screen.getByRole('tooltip')).toBeVisible();
|
|
131
|
+
expect(screen.getByRole('button', { name: 'Edit profile' })).toBeVisible();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('keeps the topbar open when the user only leaves the nested menu', async () => {
|
|
135
|
+
render(<TopbarActionsBar actions={[createUserCenter(vi.fn())]} isMobile />);
|
|
136
|
+
const action = await openUserMenu(true);
|
|
137
|
+
|
|
138
|
+
const dropdown = action.closest('.ant-dropdown');
|
|
139
|
+
expect(dropdown).not.toBeNull();
|
|
140
|
+
if (dropdown) {
|
|
141
|
+
fireEvent.mouseLeave(dropdown);
|
|
142
|
+
}
|
|
143
|
+
fireEvent.mouseLeave(screen.getByTestId('user-center-button'));
|
|
144
|
+
|
|
145
|
+
await waitFor(() => expect(screen.queryByRole('button', { name: 'Edit profile' })).not.toBeInTheDocument());
|
|
146
|
+
expect(screen.getByRole('tooltip')).toBeVisible();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('keeps desktop actions working without a mobile popover', async () => {
|
|
150
|
+
const onClick = vi.fn(async () => {});
|
|
151
|
+
render(<TopbarActionsBar actions={[createUserCenter(onClick)]} />);
|
|
152
|
+
|
|
153
|
+
fireEvent.click(await openUserMenu(false));
|
|
154
|
+
|
|
155
|
+
await waitFor(() => expect(screen.queryByRole('button', { name: 'Edit profile' })).not.toBeInTheDocument());
|
|
156
|
+
expect(onClick).toHaveBeenCalledOnce();
|
|
157
|
+
expect(screen.queryByRole('img', { name: 'ellipsis' })).not.toBeInTheDocument();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('shows profile while checking availability and ignores a result from an earlier opening', async () => {
|
|
161
|
+
let resolveFirst!: (visible: boolean) => void;
|
|
162
|
+
const firstCheck = new Promise<boolean>((resolve) => {
|
|
163
|
+
resolveFirst = resolve;
|
|
164
|
+
});
|
|
165
|
+
const check = vi.fn().mockReturnValueOnce(firstCheck).mockResolvedValueOnce(false).mockResolvedValueOnce(true);
|
|
166
|
+
const prepareProfile = vi.fn(async () => {});
|
|
167
|
+
class Profile extends UserCenterActionItemModel {
|
|
168
|
+
static itemId = 'profile';
|
|
169
|
+
prepare = prepareProfile;
|
|
170
|
+
label = 'Edit profile';
|
|
171
|
+
}
|
|
172
|
+
class Verification extends UserCenterActionItemModel {
|
|
173
|
+
static itemId = 'verification';
|
|
174
|
+
label = 'Verification';
|
|
175
|
+
checkAvailability = check;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const model = new UserCenterTopbarActionModel({ uid: 'menu', use: 'UserCenterTopbarActionModel' });
|
|
179
|
+
Object.defineProperty(model, 'flowEngine', {
|
|
180
|
+
value: {
|
|
181
|
+
getSubclassesOfAsync: () =>
|
|
182
|
+
new Map<string, typeof UserCenterActionItemModel>([
|
|
183
|
+
['profile', Profile],
|
|
184
|
+
['verification', Verification],
|
|
185
|
+
]),
|
|
186
|
+
createModel: ({ use, uid }: { use: typeof UserCenterActionItemModel; uid: string }) =>
|
|
187
|
+
new use({ uid, use: use.name }),
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
render(<TopbarActionsBar actions={[model]} />);
|
|
191
|
+
|
|
192
|
+
const closeMenu = async () => {
|
|
193
|
+
fireEvent.click(screen.getByRole('button', { name: 'Edit profile' }));
|
|
194
|
+
await waitFor(() => expect(screen.queryByRole('button', { name: 'Edit profile' })).not.toBeInTheDocument());
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
await openUserMenu(false);
|
|
198
|
+
expect(check).toHaveBeenCalledTimes(1);
|
|
199
|
+
expect(screen.queryByRole('button', { name: 'Verification' })).not.toBeInTheDocument();
|
|
200
|
+
await closeMenu();
|
|
201
|
+
|
|
202
|
+
await openUserMenu(false);
|
|
203
|
+
expect(check).toHaveBeenCalledTimes(2);
|
|
204
|
+
await act(async () => resolveFirst(true));
|
|
205
|
+
expect(screen.queryByRole('button', { name: 'Verification' })).not.toBeInTheDocument();
|
|
206
|
+
await closeMenu();
|
|
207
|
+
|
|
208
|
+
await openUserMenu(false);
|
|
209
|
+
expect(await screen.findByRole('button', { name: 'Verification' })).toBeVisible();
|
|
210
|
+
expect(check).toHaveBeenCalledTimes(3);
|
|
211
|
+
expect(prepareProfile).toHaveBeenCalledOnce();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it.each([true, false])('respects closeOnChange=%s for preference selectors', async (closeOnChange) => {
|
|
215
|
+
const onChange = vi.fn(async () => {});
|
|
216
|
+
const item = new UserCenterSelectItemModel({ uid: 'language', use: 'UserCenterSelectItemModel' });
|
|
217
|
+
item.label = 'Language';
|
|
218
|
+
item.value = 'en';
|
|
219
|
+
item.options = [
|
|
220
|
+
{ label: 'English', value: 'en' },
|
|
221
|
+
{ label: 'Chinese', value: 'zh' },
|
|
222
|
+
];
|
|
223
|
+
item.onChange = onChange;
|
|
224
|
+
item.closeOnChange = closeOnChange;
|
|
225
|
+
const model = createUserCenter(vi.fn());
|
|
226
|
+
model.getPreparedItems = async (closeDropdown) => {
|
|
227
|
+
item.setCloseDropdown(closeDropdown);
|
|
228
|
+
return [item];
|
|
229
|
+
};
|
|
230
|
+
render(<TopbarActionsBar actions={[model]} isMobile />);
|
|
231
|
+
fireEvent.click(screen.getByRole('img', { name: 'ellipsis' }));
|
|
232
|
+
fireEvent.mouseEnter(screen.getByTestId('user-center-button'));
|
|
233
|
+
const select = await screen.findByRole('combobox');
|
|
234
|
+
await waitFor(() => expect(select.closest('.ant-select')).toBeVisible());
|
|
235
|
+
|
|
236
|
+
fireEvent.mouseDown(select);
|
|
237
|
+
fireEvent.click(await screen.findByText('Chinese'));
|
|
238
|
+
|
|
239
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledWith('zh'));
|
|
240
|
+
if (closeOnChange) {
|
|
241
|
+
await waitFor(() => expect(screen.queryByRole('tooltip')).not.toBeInTheDocument());
|
|
242
|
+
} else {
|
|
243
|
+
expect(screen.getByRole('tooltip')).toBeVisible();
|
|
244
|
+
expect(select.closest('.ant-select')).toBeVisible();
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('does not reopen the previous popover after switching to desktop and back', async () => {
|
|
249
|
+
const actions = [createUserCenter(vi.fn())];
|
|
250
|
+
const { rerender } = render(<TopbarActionsBar actions={actions} isMobile />);
|
|
251
|
+
fireEvent.click(screen.getByRole('img', { name: 'ellipsis' }));
|
|
252
|
+
await waitFor(() => expect(screen.getByRole('tooltip')).toBeVisible());
|
|
253
|
+
|
|
254
|
+
rerender(<TopbarActionsBar actions={actions} isMobile={false} />);
|
|
255
|
+
rerender(<TopbarActionsBar actions={actions} isMobile />);
|
|
256
|
+
|
|
257
|
+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import type { FlowModelContext } from '@nocobase/flow-engine';
|
|
11
|
+
|
|
10
12
|
type CloseGuardContext = {
|
|
11
13
|
inputArgs?: {
|
|
12
14
|
dirty?: {
|
|
@@ -23,25 +25,24 @@ type CloseGuardContext = {
|
|
|
23
25
|
content: string;
|
|
24
26
|
okText: string;
|
|
25
27
|
cancelText: string;
|
|
26
|
-
}) => boolean | Promise<boolean>;
|
|
28
|
+
}) => boolean | Promise<boolean> | ReturnType<FlowModelContext['modal']['confirm']>;
|
|
27
29
|
};
|
|
28
30
|
t: (value: string) => string;
|
|
29
31
|
exitAll: () => void;
|
|
30
32
|
};
|
|
31
33
|
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const confirmed = await ctx.modal.confirm({
|
|
34
|
+
export function confirmDiscardChanges(ctx: Pick<CloseGuardContext, 'modal' | 't'>) {
|
|
35
|
+
return ctx.modal.confirm({
|
|
38
36
|
title: ctx.t('Unsaved changes'),
|
|
39
37
|
content: ctx.t("Are you sure you don't want to save?"),
|
|
40
38
|
okText: ctx.t('Confirm'),
|
|
41
39
|
cancelText: ctx.t('Cancel'),
|
|
42
40
|
});
|
|
41
|
+
}
|
|
43
42
|
|
|
44
|
-
|
|
43
|
+
export async function confirmUnsavedChangesHandler(ctx: CloseGuardContext) {
|
|
44
|
+
if (!ctx.inputArgs?.dirty?.hasDirtyForms) return;
|
|
45
|
+
if (!(await confirmDiscardChanges(ctx))) {
|
|
45
46
|
ctx.inputArgs?.controller?.prevent?.();
|
|
46
47
|
ctx.exitAll();
|
|
47
48
|
}
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
FlowModelRenderer,
|
|
17
17
|
MemoFlowModelRenderer,
|
|
18
18
|
MultiRecordResource,
|
|
19
|
+
observable,
|
|
19
20
|
SingleRecordResource,
|
|
20
21
|
} from '@nocobase/flow-engine';
|
|
21
22
|
import { Pagination, Space } from 'antd';
|
|
@@ -23,12 +24,14 @@ import { isEqual } from 'lodash';
|
|
|
23
24
|
import React from 'react';
|
|
24
25
|
import { BlockSceneEnum } from '../../base/BlockModel';
|
|
25
26
|
import { FormBlockContent, FormBlockModel } from './FormBlockModel';
|
|
27
|
+
import { confirmDiscardChanges } from '../../base/PageModel/closeGuard';
|
|
26
28
|
import { dispatchEventDeep } from '../../../utils';
|
|
27
29
|
|
|
28
30
|
export class EditFormModel extends FormBlockModel {
|
|
29
31
|
static scene = BlockSceneEnum.oam;
|
|
30
32
|
static blockCapabilityActionName = 'update';
|
|
31
33
|
|
|
34
|
+
private readonly confirmingPageChange = observable.ref(false);
|
|
32
35
|
private actionFlowSettings = { showBackground: false, showBorder: false, toolbarPosition: 'above' as const };
|
|
33
36
|
private actionExtraToolbarItems = [
|
|
34
37
|
{
|
|
@@ -71,9 +74,26 @@ export class EditFormModel extends FormBlockModel {
|
|
|
71
74
|
getAclActionName() {
|
|
72
75
|
return 'update';
|
|
73
76
|
}
|
|
77
|
+
|
|
78
|
+
private hasUnsavedChanges = () => {
|
|
79
|
+
const record = this.getCurrentRecord();
|
|
80
|
+
return Array.from(this.getUserModifiedFields()).some(
|
|
81
|
+
(field) => !isEqual(this.form.getFieldValue(field), record?.[field]),
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
74
85
|
handlePageChange = async (page: number) => {
|
|
75
86
|
if (this.resource instanceof MultiRecordResource) {
|
|
76
87
|
const multiResource = this.resource as MultiRecordResource;
|
|
88
|
+
if (page === multiResource.getPage() || this.confirmingPageChange.value) return;
|
|
89
|
+
if (this.hasUnsavedChanges()) {
|
|
90
|
+
this.confirmingPageChange.value = true;
|
|
91
|
+
try {
|
|
92
|
+
if (!(await confirmDiscardChanges(this.context))) return;
|
|
93
|
+
} finally {
|
|
94
|
+
this.confirmingPageChange.value = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
77
97
|
multiResource.setPage(page);
|
|
78
98
|
multiResource.loading = true;
|
|
79
99
|
await multiResource.refresh();
|
|
@@ -116,10 +136,12 @@ export class EditFormModel extends FormBlockModel {
|
|
|
116
136
|
}}
|
|
117
137
|
>
|
|
118
138
|
<Pagination
|
|
139
|
+
// Simple pagination keeps its own editable page number. Reset it while confirming the switch.
|
|
140
|
+
key={this.confirmingPageChange.value ? 'confirming' : 'editing'}
|
|
119
141
|
simple
|
|
120
142
|
pageSize={1}
|
|
121
143
|
showSizeChanger={false}
|
|
122
|
-
|
|
144
|
+
current={(this.resource as MultiRecordResource).getPage()}
|
|
123
145
|
total={(this.resource as MultiRecordResource).getTotalPage()}
|
|
124
146
|
onChange={this.handlePageChange}
|
|
125
147
|
style={{ display: 'inline-block' }}
|
|
@@ -0,0 +1,77 @@
|
|
|
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, MultiRecordResource, observer } from '@nocobase/flow-engine';
|
|
11
|
+
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
|
|
12
|
+
import { Form, Input, Modal } from 'antd';
|
|
13
|
+
import React from 'react';
|
|
14
|
+
import { expect, it, vi } from 'vitest';
|
|
15
|
+
import { EditFormModel } from '../EditFormModel';
|
|
16
|
+
|
|
17
|
+
it('keeps the visible page and edits on cancel and loads the next record on confirm', async () => {
|
|
18
|
+
const records = [{ name: 'Record B' }, { name: 'Record A' }];
|
|
19
|
+
const model = new EditFormModel({ uid: 'edit', flowEngine: new FlowEngine() });
|
|
20
|
+
const resource = new MultiRecordResource(model.context);
|
|
21
|
+
resource.setPage(1).setPageSize(1).setMeta({ count: 2, totalPage: 2 }).setData([records[0]]);
|
|
22
|
+
model.context.defineProperty('resource', { value: resource });
|
|
23
|
+
model.context.defineProperty('record', { get: () => model.getCurrentRecord() });
|
|
24
|
+
const refresh = vi.spyOn(resource, 'refresh').mockImplementation(async () => {
|
|
25
|
+
const record = records[resource.getPage() - 1];
|
|
26
|
+
resource.setData([record]);
|
|
27
|
+
model.form.setFieldsValue(record);
|
|
28
|
+
model.resetUserModifiedFields();
|
|
29
|
+
resource.loading = false;
|
|
30
|
+
});
|
|
31
|
+
const Editor = observer(() => {
|
|
32
|
+
const [form] = Form.useForm();
|
|
33
|
+
const [modal, dialogs] = Modal.useModal();
|
|
34
|
+
model.context.defineProperty('form', { value: form });
|
|
35
|
+
model.context.defineProperty('modal', { value: modal });
|
|
36
|
+
const block = model.renderComponent();
|
|
37
|
+
return (
|
|
38
|
+
<>
|
|
39
|
+
{dialogs}
|
|
40
|
+
{React.cloneElement(block, {
|
|
41
|
+
grid: (
|
|
42
|
+
<Form.Item name="name" label="Name">
|
|
43
|
+
<Input />
|
|
44
|
+
</Form.Item>
|
|
45
|
+
),
|
|
46
|
+
actions: null,
|
|
47
|
+
})}
|
|
48
|
+
</>
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const { container } = render(<Editor />);
|
|
53
|
+
const field = screen.getByRole('textbox', { name: 'Name' });
|
|
54
|
+
const pageInput = () => screen.getByRole('textbox', { name: 'Go to' });
|
|
55
|
+
const nextPage = () => fireEvent.click(container.querySelector('.ant-pagination-next button'));
|
|
56
|
+
expect(field).toHaveValue('Record B');
|
|
57
|
+
fireEvent.change(field, { target: { value: 'unsaved draft' } });
|
|
58
|
+
nextPage();
|
|
59
|
+
const dialog = await screen.findByRole('dialog');
|
|
60
|
+
expect(dialog).toHaveTextContent('Unsaved changes');
|
|
61
|
+
expect(pageInput()).toHaveValue('1');
|
|
62
|
+
fireEvent.click(within(dialog).getByText('Cancel'));
|
|
63
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
|
|
64
|
+
expect(pageInput()).toHaveValue('1');
|
|
65
|
+
expect(field).toHaveValue('unsaved draft');
|
|
66
|
+
expect(refresh).not.toHaveBeenCalled();
|
|
67
|
+
|
|
68
|
+
nextPage();
|
|
69
|
+
fireEvent.click(within(await screen.findByRole('dialog')).getByText('Confirm'));
|
|
70
|
+
await waitFor(() => expect(field).toHaveValue('Record A'));
|
|
71
|
+
expect(pageInput()).toHaveValue('2');
|
|
72
|
+
expect(refresh).toHaveBeenCalledTimes(1);
|
|
73
|
+
|
|
74
|
+
fireEvent.click(container.querySelector('.ant-pagination-prev button'));
|
|
75
|
+
await waitFor(() => expect(field).toHaveValue('Record B'));
|
|
76
|
+
expect(pageInput()).toHaveValue('1');
|
|
77
|
+
});
|
|
@@ -45,7 +45,7 @@ import {
|
|
|
45
45
|
getRowKey,
|
|
46
46
|
useBlockHeight,
|
|
47
47
|
} from './utils';
|
|
48
|
-
import {
|
|
48
|
+
import { getTableSorterOrder } from './sortUtils';
|
|
49
49
|
import { commonConditionHandler, ConditionBuilder } from '../../../components/ConditionBuilder';
|
|
50
50
|
import { BulkDeleteActionModel } from '../../actions/BulkDeleteActionModel';
|
|
51
51
|
import {
|
|
@@ -1038,15 +1038,9 @@ const HighPerformanceTable = React.memo(
|
|
|
1038
1038
|
const mergedColumns = leftAuxiliaryColumn ? [leftAuxiliaryColumn, ...columns] : columns;
|
|
1039
1039
|
|
|
1040
1040
|
const handleChange = useCallback(
|
|
1041
|
-
async (pagination, filters, sorter) => {
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
const currentSort = resourceSort?.length ? resourceSort : globalSort;
|
|
1045
|
-
const sorterField = resolveTableSorterField(sorter);
|
|
1046
|
-
const sort =
|
|
1047
|
-
sorter.order && sorterField ? (sorter.order === `ascend` ? [sorterField] : [`-${sorterField}`]) : currentSort;
|
|
1048
|
-
if (sorter) {
|
|
1049
|
-
model.resource.setSort(sort);
|
|
1041
|
+
async (pagination, filters, sorter, extra) => {
|
|
1042
|
+
if (extra?.action === 'sort') {
|
|
1043
|
+
model.resource.setSort(getTableSorterOrder(sorter, model.getDragSortFieldName(), model.props.globalSort));
|
|
1050
1044
|
}
|
|
1051
1045
|
if (model.resource.getPageSize() !== pagination.pageSize) {
|
|
1052
1046
|
model.resource.setPage(1);
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { describe, expect, it } from 'vitest';
|
|
11
11
|
|
|
12
|
-
import { getTableColumnSortField, resolveTableSorterField } from '../sortUtils';
|
|
12
|
+
import { getTableColumnSortField, getTableSorterOrder, resolveTableSorterField } from '../sortUtils';
|
|
13
13
|
|
|
14
14
|
describe('table sort utils', () => {
|
|
15
15
|
it('builds a relation sort path from the selected association title field', () => {
|
|
@@ -56,3 +56,13 @@ describe('table sort utils', () => {
|
|
|
56
56
|
).toBe('org_oho.orgname');
|
|
57
57
|
});
|
|
58
58
|
});
|
|
59
|
+
|
|
60
|
+
it('restores configured sorting after both directions of manual association sorting', () => {
|
|
61
|
+
const column = { sortField: 'team.title' };
|
|
62
|
+
const defaults = ['-createdAt', 'id'];
|
|
63
|
+
expect(getTableSorterOrder({ column, order: 'ascend' }, undefined, defaults)).toEqual(['team.title']);
|
|
64
|
+
expect(getTableSorterOrder({ column, order: 'descend' }, undefined, defaults)).toEqual(['-team.title']);
|
|
65
|
+
expect(getTableSorterOrder({ column }, undefined, defaults)).toEqual(defaults);
|
|
66
|
+
expect(getTableSorterOrder({ column })).toEqual([]);
|
|
67
|
+
expect(getTableSorterOrder({ column }, 'position', defaults)).toEqual(['position']);
|
|
68
|
+
});
|
|
@@ -50,3 +50,13 @@ export function resolveTableSorterField(sorter: {
|
|
|
50
50
|
}) {
|
|
51
51
|
return sorter?.column?.sortField || sorter?.field;
|
|
52
52
|
}
|
|
53
|
+
|
|
54
|
+
export function getTableSorterOrder(
|
|
55
|
+
sorter: Parameters<typeof resolveTableSorterField>[0] & { order?: string },
|
|
56
|
+
dragSortField?: string,
|
|
57
|
+
defaultSort: string[] = [],
|
|
58
|
+
) {
|
|
59
|
+
const field = resolveTableSorterField(sorter);
|
|
60
|
+
if (sorter?.order && field) return [sorter.order === 'ascend' ? field : `-${field}`];
|
|
61
|
+
return dragSortField ? [dragSortField] : defaultSort;
|
|
62
|
+
}
|
|
@@ -751,7 +751,10 @@ async function originalHandler(ctx, params) {
|
|
|
751
751
|
|
|
752
752
|
const targetInterface = targetLabelField.getInterfaceOptions();
|
|
753
753
|
|
|
754
|
-
const operator =
|
|
754
|
+
const operator =
|
|
755
|
+
targetLabelField.type === 'string' || targetLabelField.dataType === 'string'
|
|
756
|
+
? '$includes'
|
|
757
|
+
: targetInterface?.filterable?.operators?.[0]?.value || '$includes';
|
|
755
758
|
|
|
756
759
|
const searchText = ctx.inputArgs.searchText?.trim();
|
|
757
760
|
const resource = ctx.model.resource;
|
|
@@ -590,7 +590,10 @@ async function originalHandler(ctx, params) {
|
|
|
590
590
|
|
|
591
591
|
const targetInterface = targetLabelField.getInterfaceOptions();
|
|
592
592
|
|
|
593
|
-
const operator =
|
|
593
|
+
const operator =
|
|
594
|
+
targetLabelField.type === 'string' || targetLabelField.dataType === 'string'
|
|
595
|
+
? '$includes'
|
|
596
|
+
: targetInterface?.filterable?.operators?.[0]?.value || '$includes';
|
|
594
597
|
|
|
595
598
|
const searchText = ctx.inputArgs.searchText?.trim();
|
|
596
599
|
|