@nocobase/client-v2 2.1.32 → 2.1.34
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/index.mjs +19 -19
- package/lib/index.js +34 -34
- package/package.json +7 -7
- package/src/flow/models/base/CollectionBlockModel.tsx +9 -0
- package/src/flow/models/base/__tests__/CollectionBlockModel.initialBeforeRenderRefresh.test.ts +60 -0
- package/src/flow/models/fields/JsonFieldModel.tsx +31 -8
- package/src/flow/models/fields/__tests__/JsonFieldModel.test.ts +82 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/client-v2",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.34",
|
|
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.
|
|
31
|
-
"@nocobase/flow-engine": "2.1.
|
|
32
|
-
"@nocobase/sdk": "2.1.
|
|
33
|
-
"@nocobase/shared": "2.1.
|
|
34
|
-
"@nocobase/utils": "2.1.
|
|
30
|
+
"@nocobase/evaluators": "2.1.34",
|
|
31
|
+
"@nocobase/flow-engine": "2.1.34",
|
|
32
|
+
"@nocobase/sdk": "2.1.34",
|
|
33
|
+
"@nocobase/shared": "2.1.34",
|
|
34
|
+
"@nocobase/utils": "2.1.34",
|
|
35
35
|
"ahooks": "^3.7.2",
|
|
36
36
|
"antd": "5.24.2",
|
|
37
37
|
"antd-style": "3.7.1",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"react-i18next": "^11.15.1",
|
|
49
49
|
"react-router-dom": "^6.30.1"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "6d4dfdfbde69698ed1c2ed54108a8f6736e56e10"
|
|
52
52
|
}
|
|
@@ -82,6 +82,15 @@ export class CollectionBlockModel<T = DefaultStructure> extends DataBlockModel<T
|
|
|
82
82
|
const engine = this.context.engine as FlowEngine;
|
|
83
83
|
const currentVersion = this.getDirtyTrackingVersion(engine, dataSourceKey, resource, params);
|
|
84
84
|
|
|
85
|
+
if (resource instanceof MultiRecordResource && this.getDataLoadingMode() === 'manual' && !this.hasActiveFilters()) {
|
|
86
|
+
resource.setData([]);
|
|
87
|
+
resource.setMeta({ count: 0, hasNext: false });
|
|
88
|
+
resource.setPage(1);
|
|
89
|
+
resource.loading = false;
|
|
90
|
+
this.lastSeenDirtyVersion = currentVersion;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
85
94
|
if (forceRefresh) {
|
|
86
95
|
if (this.dirtyRefreshing) return;
|
|
87
96
|
this.dirtyRefreshing = true;
|
package/src/flow/models/base/__tests__/CollectionBlockModel.initialBeforeRenderRefresh.test.ts
CHANGED
|
@@ -128,4 +128,64 @@ describe('CollectionBlockModel initial beforeRender refresh', () => {
|
|
|
128
128
|
expect(resource.getData()).toEqual([]);
|
|
129
129
|
expect(resource.getMeta('count')).toBe(0);
|
|
130
130
|
});
|
|
131
|
+
|
|
132
|
+
it('skips forced active refresh in manual mode when filters are empty', async () => {
|
|
133
|
+
const { model, resource } = setupModelWithManualMode();
|
|
134
|
+
const refreshSpy = vi.spyOn(resource, 'refresh');
|
|
135
|
+
|
|
136
|
+
resource.setData([{ id: 2, name: 'Stale' }]);
|
|
137
|
+
resource.setMeta({ count: 1, hasNext: true });
|
|
138
|
+
resource.setPage(2);
|
|
139
|
+
resource.loading = true;
|
|
140
|
+
|
|
141
|
+
model.onActive(true);
|
|
142
|
+
await Promise.resolve();
|
|
143
|
+
await Promise.resolve();
|
|
144
|
+
|
|
145
|
+
expect(refreshSpy).not.toHaveBeenCalled();
|
|
146
|
+
expect(resource.getData()).toEqual([]);
|
|
147
|
+
expect(resource.getMeta('count')).toBe(0);
|
|
148
|
+
expect(resource.getMeta('hasNext')).toBe(false);
|
|
149
|
+
expect(resource.getMeta('page')).toBe(1);
|
|
150
|
+
expect(resource.getRequestParameter('page')).toBe(1);
|
|
151
|
+
expect(resource.loading).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('keeps forced active refresh in manual mode when filters are active', async () => {
|
|
155
|
+
const { model, resource } = setupModelWithManualMode();
|
|
156
|
+
const refreshSpy = vi.spyOn(resource, 'refresh').mockResolvedValue();
|
|
157
|
+
|
|
158
|
+
resource.setData([{ id: 2, name: 'Stale' }]);
|
|
159
|
+
resource.setMeta({ count: 1, hasNext: true, page: 2 });
|
|
160
|
+
model.setFilterActive('filter-form-item', true);
|
|
161
|
+
|
|
162
|
+
model.onActive(true);
|
|
163
|
+
await Promise.resolve();
|
|
164
|
+
|
|
165
|
+
expect(refreshSpy).toHaveBeenCalledTimes(1);
|
|
166
|
+
expect(resource.getData()).toEqual([{ id: 2, name: 'Stale' }]);
|
|
167
|
+
expect(resource.getMeta('count')).toBe(1);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('skips forced active refresh in manual mode when only resource filters are active', async () => {
|
|
171
|
+
const { model, resource } = setupModelWithManualMode();
|
|
172
|
+
const refreshSpy = vi.spyOn(resource, 'refresh');
|
|
173
|
+
|
|
174
|
+
resource.setData([{ id: 2, name: 'Stale' }]);
|
|
175
|
+
resource.setMeta({ count: 1, hasNext: true });
|
|
176
|
+
resource.setPage(2);
|
|
177
|
+
resource.loading = true;
|
|
178
|
+
resource.addFilterGroup('data-scope', { status: { $eq: 'active' } });
|
|
179
|
+
|
|
180
|
+
model.onActive(true);
|
|
181
|
+
await Promise.resolve();
|
|
182
|
+
|
|
183
|
+
expect(refreshSpy).not.toHaveBeenCalled();
|
|
184
|
+
expect(resource.getData()).toEqual([]);
|
|
185
|
+
expect(resource.getMeta('count')).toBe(0);
|
|
186
|
+
expect(resource.getMeta('hasNext')).toBe(false);
|
|
187
|
+
expect(resource.getMeta('page')).toBe(1);
|
|
188
|
+
expect(resource.getRequestParameter('page')).toBe(1);
|
|
189
|
+
expect(resource.loading).toBe(false);
|
|
190
|
+
});
|
|
131
191
|
});
|
|
@@ -13,6 +13,22 @@ import JSON5 from 'json5';
|
|
|
13
13
|
import React, { useState, useEffect, useRef } from 'react';
|
|
14
14
|
import { FieldModel } from '../base/FieldModel';
|
|
15
15
|
|
|
16
|
+
const jsonValidationRuleMarker = '__jsonSyntaxValidation';
|
|
17
|
+
|
|
18
|
+
interface JsonValidationRule {
|
|
19
|
+
[jsonValidationRuleMarker]: true;
|
|
20
|
+
type: 'any';
|
|
21
|
+
validator?: (_rule: unknown, value: unknown) => Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isRuleObject(rule: unknown): rule is Record<string, unknown> {
|
|
25
|
+
return typeof rule === 'object' && rule !== null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isJsonValidationRule(rule: unknown): rule is JsonValidationRule {
|
|
29
|
+
return isRuleObject(rule) && rule[jsonValidationRuleMarker] === true;
|
|
30
|
+
}
|
|
31
|
+
|
|
16
32
|
const jsonCss = css`
|
|
17
33
|
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
|
18
34
|
font-size: 14px;
|
|
@@ -40,7 +56,7 @@ export const JsonInput = observer((props: any) => {
|
|
|
40
56
|
}
|
|
41
57
|
|
|
42
58
|
internalChange.current = false;
|
|
43
|
-
}, [value]);
|
|
59
|
+
}, [_JSON, space, value]);
|
|
44
60
|
|
|
45
61
|
// 用户输入时
|
|
46
62
|
const handleChange = (ev) => {
|
|
@@ -91,23 +107,30 @@ JsonFieldModel.registerFlow({
|
|
|
91
107
|
key: 'jsonInitSetting',
|
|
92
108
|
steps: {
|
|
93
109
|
initValidation: {
|
|
94
|
-
handler(ctx
|
|
95
|
-
const
|
|
110
|
+
handler(ctx) {
|
|
111
|
+
const currentRules = Array.isArray(ctx.model.parent.props.rules) ? ctx.model.parent.props.rules : [];
|
|
112
|
+
const rules = currentRules.filter((rule: unknown) => {
|
|
113
|
+
return (!isRuleObject(rule) || Object.keys(rule).length > 0) && !isJsonValidationRule(rule);
|
|
114
|
+
});
|
|
96
115
|
// 添加 JSON 语法校验规则
|
|
97
116
|
rules.push({
|
|
98
|
-
validator
|
|
117
|
+
// validator 函数序列化后会被丢弃;保留 any 类型可避免持久化规则退化为默认字符串校验。
|
|
118
|
+
type: 'any',
|
|
119
|
+
[jsonValidationRuleMarker]: true,
|
|
120
|
+
validator: (_rule: unknown, value: unknown) => {
|
|
99
121
|
// 允许空
|
|
100
122
|
if (value === null || value === undefined || value === '') return Promise.resolve();
|
|
101
123
|
// 如果已经是对象/数组,直接通过
|
|
102
124
|
if (typeof value === 'object') return Promise.resolve();
|
|
103
125
|
try {
|
|
104
|
-
JSON.parse(value);
|
|
126
|
+
JSON.parse(String(value));
|
|
105
127
|
return Promise.resolve();
|
|
106
|
-
} catch (
|
|
107
|
-
|
|
128
|
+
} catch (error) {
|
|
129
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
130
|
+
return Promise.reject(ctx.t('Invalid JSON format') + ': ' + message);
|
|
108
131
|
}
|
|
109
132
|
},
|
|
110
|
-
});
|
|
133
|
+
} satisfies JsonValidationRule);
|
|
111
134
|
ctx.model.parent.setProps({
|
|
112
135
|
rules,
|
|
113
136
|
});
|
|
@@ -0,0 +1,82 @@
|
|
|
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, FlowModel } from '@nocobase/flow-engine';
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
import { JsonFieldModel } from '../JsonFieldModel';
|
|
13
|
+
|
|
14
|
+
interface ValidationRule {
|
|
15
|
+
required?: boolean;
|
|
16
|
+
type?: string;
|
|
17
|
+
validator?: (_rule: unknown, value: unknown) => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function createModels(rules: ValidationRule[]) {
|
|
21
|
+
const engine = new FlowEngine();
|
|
22
|
+
engine.registerModels({ JsonFieldModel });
|
|
23
|
+
const parent = engine.createModel<FlowModel<{ subModels: { field: JsonFieldModel } }>>({
|
|
24
|
+
use: FlowModel,
|
|
25
|
+
uid: 'json-form-item',
|
|
26
|
+
props: { rules },
|
|
27
|
+
subModels: {
|
|
28
|
+
field: {
|
|
29
|
+
use: JsonFieldModel,
|
|
30
|
+
uid: 'json-field',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return { field: parent.subModels.field, parent };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function initializeJsonValidation(field: JsonFieldModel) {
|
|
39
|
+
const step = field.getFlow('jsonInitSetting')?.steps.initValidation;
|
|
40
|
+
if (!step) {
|
|
41
|
+
throw new Error('JSON validation step is not registered');
|
|
42
|
+
}
|
|
43
|
+
step.handler(field.context, {});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('JsonFieldModel validation', () => {
|
|
47
|
+
it('replaces legacy empty rules with one serializable JSON validation rule', async () => {
|
|
48
|
+
const { field, parent } = createModels([{}, { required: true }]);
|
|
49
|
+
|
|
50
|
+
initializeJsonValidation(field);
|
|
51
|
+
|
|
52
|
+
const rules = parent.props.rules as ValidationRule[];
|
|
53
|
+
expect(rules).toHaveLength(2);
|
|
54
|
+
expect(rules[0]).toEqual({ required: true });
|
|
55
|
+
expect(rules[1]).toMatchObject({ type: 'any' });
|
|
56
|
+
expect(rules[1].validator).toBeTypeOf('function');
|
|
57
|
+
if (!rules[1].validator) {
|
|
58
|
+
throw new Error('JSON validator is missing');
|
|
59
|
+
}
|
|
60
|
+
await expect(rules[1].validator({}, { name: 'NocoBase' })).resolves.toBeUndefined();
|
|
61
|
+
await expect(rules[1].validator({}, '{"name":')).rejects.toContain('Invalid JSON format');
|
|
62
|
+
|
|
63
|
+
const persistedRules = JSON.parse(JSON.stringify(parent.serialize())).props.rules as ValidationRule[];
|
|
64
|
+
expect(persistedRules).toHaveLength(2);
|
|
65
|
+
expect(persistedRules[1]).toMatchObject({ type: 'any' });
|
|
66
|
+
expect(persistedRules[1].validator).toBeUndefined();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('re-registers the runtime validator without duplicating its persisted placeholder', () => {
|
|
70
|
+
const { field, parent } = createModels([{}]);
|
|
71
|
+
initializeJsonValidation(field);
|
|
72
|
+
const persistedRules = JSON.parse(JSON.stringify(parent.serialize())).props.rules as ValidationRule[];
|
|
73
|
+
parent.setProps({ rules: persistedRules });
|
|
74
|
+
|
|
75
|
+
initializeJsonValidation(field);
|
|
76
|
+
|
|
77
|
+
const rules = parent.props.rules as ValidationRule[];
|
|
78
|
+
expect(rules).toHaveLength(1);
|
|
79
|
+
expect(rules[0]).toMatchObject({ type: 'any' });
|
|
80
|
+
expect(rules[0].validator).toBeTypeOf('function');
|
|
81
|
+
});
|
|
82
|
+
});
|