@nocobase/client-v2 2.3.0-alpha.1 → 2.3.0-beta.11
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/APIClient.d.ts +12 -0
- package/es/components/form/ScanInput/useCodeScanner.d.ts +12 -2
- package/es/components/form/ScanInput/zxingWasmDecoder.d.ts +9 -0
- package/es/flow/actions/linkageRules.d.ts +25 -0
- package/es/flow/components/FieldAssignValueInput.d.ts +6 -30
- package/es/flow/components/FlowRoute.d.ts +2 -2
- package/es/flow/components/field-value-variable/DateVariableEditor.d.ts +24 -0
- package/es/flow/components/field-value-variable/FieldValueVariableInput.d.ts +32 -0
- package/es/flow/components/field-value-variable/dateValue.d.ts +36 -0
- package/es/flow/components/field-value-variable/index.d.ts +11 -0
- package/es/flow/components/filter/VariableFilterItem.d.ts +7 -0
- package/es/flow/components/placeholders/BlockPlaceholder.d.ts +1 -0
- package/es/flow/internal/registerDeviceTypeContext.d.ts +10 -0
- package/es/flow/internal/utils/operatorSchemaHelper.d.ts +2 -2
- package/es/flow/models/actions/UpdateRecordActionUtils.d.ts +8 -2
- package/es/flow/models/base/ActionModelCore.d.ts +4 -2
- package/es/flow/models/base/BlockGridModel.d.ts +3 -0
- package/es/flow/models/base/CollectionBlockModel.d.ts +3 -0
- package/es/flow/models/blocks/details/DetailsBlockModel.d.ts +3 -0
- package/es/flow/models/blocks/filter-form/FilterFormGridModel.d.ts +1 -0
- package/es/flow/models/blocks/filter-form/FilterFormSubmitActionModel.d.ts +3 -1
- package/es/flow/models/blocks/form/FormBlockModel.d.ts +7 -0
- package/es/flow/models/blocks/form/FormGridModel.d.ts +6 -0
- package/es/flow/models/blocks/form/value-runtime/rules.d.ts +1 -0
- package/es/flow/models/blocks/table/JSColumnModel.d.ts +2 -0
- package/es/flow/models/blocks/table/TableBlockModel.d.ts +1 -0
- package/es/flow/models/blocks/table/TableColumnModel.d.ts +12 -0
- package/es/flow/models/blocks/table/utils.d.ts +1 -0
- package/es/flow/models/fields/AssociationFieldModel/RecordPickerFieldModel.d.ts +2 -1
- package/es/flow/models/fields/AssociationFieldModel/RecordSelectFieldModel.d.ts +0 -17
- package/es/flow/models/fields/AssociationFieldModel/recordSelectShared.d.ts +41 -0
- package/es/flow/models/fields/DateTimeFieldModel/dateLimit.d.ts +15 -7
- package/es/flow-compat/passwordUtils.d.ts +1 -1
- package/es/index.d.ts +1 -0
- package/es/index.mjs +148 -145
- package/es/ui-operation/index.d.ts +15 -0
- package/es/ui-operation/ui-operation-codec.d.ts +10 -0
- package/lib/index.js +170 -167
- package/lib/locale/languageCodes.js +1 -0
- package/package.json +10 -8
- package/src/APIClient.ts +92 -0
- package/src/Application.tsx +3 -1
- package/src/__tests__/APIClient.test.tsx +58 -0
- package/src/__tests__/nocobase-buildin-plugin-auth.test.tsx +7 -2
- package/src/__tests__/settings-center.test.tsx +270 -6
- package/src/acl/ACLProvider.tsx +2 -0
- package/src/acl/__tests__/ACLProvider.test.tsx +92 -0
- package/src/collection-manager/__tests__/field-configure.test.ts +52 -0
- package/src/collection-manager/field-configure.ts +2 -2
- package/src/components/form/ScanInput/CodeScanner.tsx +64 -35
- package/src/components/form/ScanInput/__tests__/CodeScanner.test.tsx +107 -0
- package/src/components/form/ScanInput/__tests__/useCodeScanner.test.tsx +294 -24
- package/src/components/form/ScanInput/__tests__/zxingWasmDecoder.test.ts +78 -0
- package/src/components/form/ScanInput/useCodeScanner.ts +281 -23
- package/src/components/form/ScanInput/zxingWasmDecoder.ts +64 -0
- package/src/components/form/filter/CollectionFilterItem.tsx +4 -2
- package/src/components/form/filter/__tests__/CollectionFilterItem.test.tsx +17 -0
- package/src/components/form/table/Table.tsx +81 -6
- package/src/components/form/table/__tests__/Table.columnWidth.test.tsx +433 -0
- package/src/flow/__tests__/FlowRoute.test.tsx +126 -8
- package/src/flow/__tests__/PluginFlowEngine.test.ts +58 -0
- package/src/flow/actions/__tests__/actionLinkageRules.forkProps.test.ts +314 -0
- package/src/flow/actions/__tests__/dataScopeFormValueClear.test.ts +102 -0
- package/src/flow/actions/__tests__/linkageRules.actionStates.test.ts +33 -0
- package/src/flow/actions/__tests__/linkageRules.subFormSetFieldProps.test.ts +72 -0
- package/src/flow/actions/__tests__/validation.test.ts +48 -0
- package/src/flow/actions/linkageRules.tsx +59 -16
- package/src/flow/admin-shell/admin-layout/AdminLayoutMenuUtils.tsx +78 -59
- package/src/flow/admin-shell/admin-layout/__tests__/AdminLayoutMenuModels.test.ts +228 -0
- package/src/flow/admin-shell/admin-layout/__tests__/TopbarActionsBar.test.tsx +42 -23
- package/src/flow/common/Markdown/style.ts +4 -0
- package/src/flow/components/BlockItemCard.tsx +2 -2
- package/src/flow/components/FieldAssignValueInput.tsx +42 -621
- package/src/flow/components/FlowRoute.tsx +71 -29
- package/src/flow/components/field-value-variable/DateVariableEditor.tsx +181 -0
- package/src/flow/components/field-value-variable/FieldValueVariableInput.tsx +335 -0
- package/src/flow/components/field-value-variable/__tests__/FieldValueVariableInput.test.tsx +389 -0
- package/src/flow/components/field-value-variable/dateValue.ts +223 -0
- package/src/flow/components/field-value-variable/index.ts +12 -0
- package/src/flow/components/filter/VariableFilterItem.tsx +23 -6
- package/src/flow/components/filter/__tests__/VariableFilterItem.rightMetaTree.test.tsx +158 -0
- package/src/flow/components/filter/__tests__/VariableFilterItem.test.tsx +125 -0
- package/src/flow/components/placeholders/BlockPlaceholder.tsx +70 -23
- package/src/flow/components/placeholders/__tests__/BlockPlaceholder.test.tsx +57 -7
- package/src/flow/index.ts +2 -0
- package/src/flow/internal/components/Markdown/DisplayMarkdown.tsx +14 -9
- package/src/flow/internal/components/Markdown/__tests__/DisplayMarkdown.test.tsx +46 -0
- package/src/flow/internal/registerDeviceTypeContext.ts +39 -0
- package/src/flow/internal/utils/operatorSchemaHelper.ts +3 -2
- package/src/flow/models/actions/UpdateRecordActionModel.tsx +18 -1
- package/src/flow/models/actions/UpdateRecordActionUtils.ts +18 -6
- package/src/flow/models/actions/__tests__/UpdateRecordActionModel.test.ts +76 -4
- package/src/flow/models/base/ActionModelCore.tsx +11 -2
- package/src/flow/models/base/BlockGridModel.tsx +26 -0
- package/src/flow/models/base/CollectionBlockModel.tsx +38 -3
- package/src/flow/models/base/GridModel.tsx +0 -1
- package/src/flow/models/base/PageModel/PageTabModel.tsx +67 -17
- package/src/flow/models/base/PageModel/__tests__/PageTabModel.test.ts +509 -12
- package/src/flow/models/base/__tests__/ActionModelCore.render.test.tsx +49 -0
- package/src/flow/models/base/__tests__/BlockGridModel.selectSceneActivation.test.ts +124 -0
- package/src/flow/models/base/__tests__/CollectionBlockModel.addAppends.test.ts +41 -0
- package/src/flow/models/base/__tests__/CollectionBlockModel.initialBeforeRenderRefresh.test.ts +125 -9
- package/src/flow/models/blocks/assign-form/AssignFormGridModel.tsx +22 -1
- package/src/flow/models/blocks/assign-form/AssignFormItemModel.tsx +36 -50
- package/src/flow/models/blocks/assign-form/__tests__/assignFieldValuesFlow.editor.test.tsx +140 -0
- package/src/flow/models/blocks/details/DetailsBlockModel.tsx +8 -3
- package/src/flow/models/blocks/details/__tests__/DetailsBlockModel.initialPaginationChangeRefresh.test.ts +32 -1
- package/src/flow/models/blocks/filter-form/FilterFormBlockModel.tsx +27 -3
- package/src/flow/models/blocks/filter-form/FilterFormGridModel.tsx +36 -5
- package/src/flow/models/blocks/filter-form/FilterFormItemModel.tsx +129 -14
- package/src/flow/models/blocks/filter-form/FilterFormSubmitActionModel.tsx +57 -4
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormGridModel.onModelCreated.test.ts +174 -1
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormGridModel.toggleFormFieldsCollapse.test.ts +29 -0
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormItemModel.defineChildren.test.ts +359 -1
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormItemModel.getFilterValue.test.ts +72 -0
- package/src/flow/models/blocks/filter-form/__tests__/FilterFormSubmitActionModel.lifecycle.test.ts +90 -0
- package/src/flow/models/blocks/filter-form/__tests__/defaultValues.wiring.test.ts +32 -4
- package/src/flow/models/blocks/filter-form/fields/FieldComponentProps.tsx +1 -1
- package/src/flow/models/blocks/filter-form/fields/__tests__/FieldComponentProps.options.test.tsx +61 -0
- package/src/flow/models/blocks/filter-manager/FilterManager.ts +5 -0
- package/src/flow/models/blocks/filter-manager/__tests__/FilterManager.test.ts +40 -0
- package/src/flow/models/blocks/form/FormBlockModel.tsx +73 -5
- package/src/flow/models/blocks/form/FormGridModel.tsx +4 -0
- package/src/flow/models/blocks/form/__tests__/FormBlockModel.test.tsx +153 -2
- package/src/flow/models/blocks/form/__tests__/popupLinkage.test.tsx +175 -0
- package/src/flow/models/blocks/form/__tests__/runJsFormSubmit.test.ts +131 -0
- package/src/flow/models/blocks/form/value-runtime/__tests__/runtime.test.ts +435 -0
- package/src/flow/models/blocks/form/value-runtime/rules.ts +67 -14
- package/src/flow/models/blocks/form/value-runtime/runtime.ts +43 -19
- package/src/flow/models/blocks/js-block/JSBlock.tsx +1 -1
- package/src/flow/models/blocks/table/JSColumnModel.tsx +10 -1
- package/src/flow/models/blocks/table/TableBlockModel.tsx +28 -2
- package/src/flow/models/blocks/table/TableColumnModel.tsx +48 -9
- package/src/flow/models/blocks/table/__tests__/JSColumnModel.test.tsx +52 -5
- package/src/flow/models/blocks/table/__tests__/TableBlockModel.filterReset.test.ts +78 -0
- package/src/flow/models/blocks/table/__tests__/TableBlockModel.quickEditRefresh.test.ts +12 -0
- package/src/flow/models/blocks/table/__tests__/TableColumnModel.test.tsx +33 -0
- package/src/flow/models/blocks/table/utils.ts +7 -0
- package/src/flow/models/fields/AssociationFieldModel/CascadeSelectFieldModel.tsx +33 -1
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/PopupSubTableFieldModel.tsx +12 -3
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/__tests__/popupContext.test.ts +120 -0
- package/src/flow/models/fields/AssociationFieldModel/PopupSubTableFieldModel/actions/PopupSubTableEditActionModel.tsx +10 -2
- package/src/flow/models/fields/AssociationFieldModel/RecordPickerFieldModel.tsx +29 -3
- package/src/flow/models/fields/AssociationFieldModel/RecordSelectFieldModel.tsx +9 -136
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableField.tsx +5 -1
- package/src/flow/models/fields/AssociationFieldModel/__tests__/RecordPickerFieldModel.itemContext.test.ts +191 -3
- package/src/flow/models/fields/AssociationFieldModel/recordSelectShared.tsx +156 -0
- package/src/flow/models/fields/DateTimeFieldModel/__tests__/DateTimeNoTzFieldModel.dateLimit.test.tsx +149 -0
- package/src/flow/models/fields/DateTimeFieldModel/dateLimit.ts +149 -113
- package/src/flow/models/fields/DisplayAssociationField/DisplaySubTableFieldModel.tsx +42 -7
- package/src/flow/models/fields/DisplayAssociationField/__tests__/DisplaySubTableFieldModel.test.tsx +351 -0
- package/src/flow/models/fields/DisplayHtmlFieldModel.tsx +2 -1
- package/src/flow/models/fields/DisplayNumberFieldModel.tsx +1 -1
- package/src/flow/models/fields/JsonFieldModel.tsx +31 -8
- package/src/flow/models/fields/NumberFieldModel.tsx +1 -1
- package/src/flow/models/fields/RichTextFieldModel/__tests__/RichTextFieldModel.test.tsx +74 -0
- package/src/flow/models/fields/RichTextFieldModel/index.tsx +27 -6
- package/src/flow/models/fields/__tests__/DisplayNumberFieldModel.test.ts +33 -0
- package/src/flow/models/fields/__tests__/JsonFieldModel.test.ts +82 -0
- package/src/flow/models/fields/__tests__/NumberFieldModel.test.tsx +47 -0
- package/src/flow/models/fields/mobile-components/MobileLazySelect.tsx +6 -0
- package/src/flow/models/fields/mobile-components/MobileSelect.tsx +27 -2
- package/src/flow/models/fields/mobile-components/__tests__/MobileSelect.test.tsx +139 -13
- package/src/flow/models/topbar/TopbarActionModel.tsx +5 -5
- package/src/flow/utils/dataScopeFormValueClear.ts +4 -3
- package/src/index.ts +1 -0
- package/src/layout-manager/LayoutContentRoute.tsx +2 -1
- package/src/layout-manager/LayoutRoute.tsx +2 -1
- package/src/layout-manager/__tests__/LayoutRoute.test.tsx +87 -1
- package/src/locale/languageCodes.ts +1 -0
- package/src/settings-center/AdminSettingsLayout.tsx +23 -2
- package/src/ui-operation/index.ts +33 -0
- package/src/ui-operation/ui-operation-codec.ts +54 -0
|
@@ -119,6 +119,162 @@ export interface LazySelectProps extends Omit<SelectProps<any>, 'mode' | 'option
|
|
|
119
119
|
allowEdit?: boolean;
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
type AssociationHydrationSetterOptions = {
|
|
123
|
+
source?: string;
|
|
124
|
+
markExplicit?: boolean;
|
|
125
|
+
triggerEvent?: boolean;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
type AssociationHydrationSetterContext = {
|
|
129
|
+
setFormValue?: (
|
|
130
|
+
namePath: unknown,
|
|
131
|
+
value: unknown,
|
|
132
|
+
options?: AssociationHydrationSetterOptions,
|
|
133
|
+
) => Promise<unknown> | void;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export type AssociationHydrationModel = {
|
|
137
|
+
resource?: {
|
|
138
|
+
get?: (tk: unknown) => Promise<unknown> | unknown;
|
|
139
|
+
};
|
|
140
|
+
context?: unknown;
|
|
141
|
+
props?: {
|
|
142
|
+
name?: unknown;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
type AssociationHydrationStatus = 'pending' | 'done';
|
|
147
|
+
|
|
148
|
+
type AssociationHydrationCandidate = {
|
|
149
|
+
item: AssociationOption;
|
|
150
|
+
tk: unknown;
|
|
151
|
+
tkKey: string;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export function isAssociationRecord(value: unknown): value is AssociationOption {
|
|
155
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function collectAssociationHydrationCandidates(options: {
|
|
159
|
+
value: LazySelectProps['value'];
|
|
160
|
+
isMultiple: boolean;
|
|
161
|
+
valueKey: string;
|
|
162
|
+
labelKey: string;
|
|
163
|
+
statusMap: Map<string, AssociationHydrationStatus>;
|
|
164
|
+
}): AssociationHydrationCandidate[] {
|
|
165
|
+
const { value, isMultiple, valueKey, labelKey, statusMap } = options;
|
|
166
|
+
const list = isMultiple ? (Array.isArray(value) ? value : []) : value != null ? [value] : [];
|
|
167
|
+
const activeTkKeys = new Set<string>();
|
|
168
|
+
|
|
169
|
+
for (const item of list) {
|
|
170
|
+
if (!isAssociationRecord(item)) continue;
|
|
171
|
+
const tk = item[valueKey];
|
|
172
|
+
if (tk == null) continue;
|
|
173
|
+
const tkKey = typeof tk === 'object' ? JSON.stringify(tk) : String(tk);
|
|
174
|
+
if (tkKey) activeTkKeys.add(tkKey);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const key of Array.from(statusMap.keys())) {
|
|
178
|
+
if (!activeTkKeys.has(key)) statusMap.delete(key);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const candidates: AssociationHydrationCandidate[] = [];
|
|
182
|
+
for (const item of list) {
|
|
183
|
+
if (!isAssociationRecord(item)) continue;
|
|
184
|
+
const tk = item[valueKey];
|
|
185
|
+
if (tk == null || item[labelKey] != null) continue;
|
|
186
|
+
|
|
187
|
+
const tkKey = typeof tk === 'object' ? JSON.stringify(tk) : String(tk);
|
|
188
|
+
if (!tkKey || statusMap.has(tkKey)) continue;
|
|
189
|
+
|
|
190
|
+
statusMap.set(tkKey, 'pending');
|
|
191
|
+
candidates.push({ item, tk, tkKey });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return candidates;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function hasAssociationHydrationSetter(value: unknown): value is AssociationHydrationSetterContext {
|
|
198
|
+
return isAssociationRecord(value) && typeof value.setFormValue === 'function';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function getAssociationHydrationNamePath(model: AssociationHydrationModel) {
|
|
202
|
+
const context = isAssociationRecord(model.context) ? model.context : undefined;
|
|
203
|
+
return context?.fieldPathArray ?? context?.fieldPath ?? model.props?.name;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function getAssociationHydrationSetterContext(model: AssociationHydrationModel) {
|
|
207
|
+
if (hasAssociationHydrationSetter(model.context)) return model.context;
|
|
208
|
+
|
|
209
|
+
const context = isAssociationRecord(model.context) ? model.context : undefined;
|
|
210
|
+
const blockModel = isAssociationRecord(context?.blockModel) ? context.blockModel : undefined;
|
|
211
|
+
return hasAssociationHydrationSetter(blockModel?.context) ? blockModel.context : undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function useAssociationValueHydration(options: {
|
|
215
|
+
model: AssociationHydrationModel;
|
|
216
|
+
value: LazySelectProps['value'];
|
|
217
|
+
isMultiple: boolean;
|
|
218
|
+
fieldNames: AssociationFieldNames;
|
|
219
|
+
onChange: LazySelectProps['onChange'];
|
|
220
|
+
}) {
|
|
221
|
+
const { model, value, isMultiple, fieldNames, onChange } = options;
|
|
222
|
+
const hydrateStatusRef = React.useRef<Map<string, AssociationHydrationStatus>>(new Map());
|
|
223
|
+
|
|
224
|
+
React.useEffect(() => {
|
|
225
|
+
const resource = model.resource;
|
|
226
|
+
if (typeof resource?.get !== 'function' || !fieldNames.value || !fieldNames.label) return;
|
|
227
|
+
|
|
228
|
+
const current = value;
|
|
229
|
+
const candidates = collectAssociationHydrationCandidates({
|
|
230
|
+
value: current,
|
|
231
|
+
isMultiple,
|
|
232
|
+
valueKey: fieldNames.value,
|
|
233
|
+
labelKey: fieldNames.label,
|
|
234
|
+
statusMap: hydrateStatusRef.current,
|
|
235
|
+
});
|
|
236
|
+
if (!candidates.length) return;
|
|
237
|
+
|
|
238
|
+
const namePath = getAssociationHydrationNamePath(model);
|
|
239
|
+
const setterContext = getAssociationHydrationSetterContext(model);
|
|
240
|
+
|
|
241
|
+
const hydrateCandidate = async ({ item, tk, tkKey }: AssociationHydrationCandidate) => {
|
|
242
|
+
try {
|
|
243
|
+
const record = await resource.get?.(tk);
|
|
244
|
+
if (!isAssociationRecord(record)) return;
|
|
245
|
+
|
|
246
|
+
const merged = { ...item, ...record };
|
|
247
|
+
if (merged[fieldNames.label] == null) return;
|
|
248
|
+
|
|
249
|
+
const nextValue = isMultiple
|
|
250
|
+
? (Array.isArray(current) ? current : []).map((entry) =>
|
|
251
|
+
isAssociationRecord(entry) && entry[fieldNames.value] === tk ? merged : entry,
|
|
252
|
+
)
|
|
253
|
+
: merged;
|
|
254
|
+
|
|
255
|
+
if (typeof setterContext?.setFormValue === 'function' && namePath != null) {
|
|
256
|
+
await setterContext.setFormValue(namePath, nextValue, {
|
|
257
|
+
source: 'default',
|
|
258
|
+
markExplicit: false,
|
|
259
|
+
triggerEvent: false,
|
|
260
|
+
});
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
onChange(nextValue);
|
|
265
|
+
} catch {
|
|
266
|
+
// Keep the original ID-only value when the related record cannot be loaded.
|
|
267
|
+
} finally {
|
|
268
|
+
hydrateStatusRef.current.set(tkKey, 'done');
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
candidates.forEach((candidate) => {
|
|
273
|
+
hydrateCandidate(candidate);
|
|
274
|
+
});
|
|
275
|
+
}, [fieldNames.label, fieldNames.value, isMultiple, model, onChange, value]);
|
|
276
|
+
}
|
|
277
|
+
|
|
122
278
|
export interface LabelByFieldProps {
|
|
123
279
|
option: AssociationOption;
|
|
124
280
|
fieldNames: AssociationFieldNames;
|
|
@@ -17,6 +17,8 @@ import { DateTimeNoTzPicker } from '../DateTimeNoTzFieldModel';
|
|
|
17
17
|
let capturedDatePickerProps: any;
|
|
18
18
|
let currentForm: any;
|
|
19
19
|
const mockResolveJsonTemplate = vi.fn();
|
|
20
|
+
const mockRunjs = vi.fn();
|
|
21
|
+
const mockLoggerWarn = vi.fn();
|
|
20
22
|
|
|
21
23
|
vi.mock('@nocobase/flow-engine', async (importOriginal) => {
|
|
22
24
|
const actual = await importOriginal<typeof import('@nocobase/flow-engine')>();
|
|
@@ -32,6 +34,10 @@ vi.mock('@nocobase/flow-engine', async (importOriginal) => {
|
|
|
32
34
|
}),
|
|
33
35
|
useFlowContext: () => ({
|
|
34
36
|
resolveJsonTemplate: mockResolveJsonTemplate,
|
|
37
|
+
runjs: mockRunjs,
|
|
38
|
+
logger: {
|
|
39
|
+
warn: mockLoggerWarn,
|
|
40
|
+
},
|
|
35
41
|
}),
|
|
36
42
|
};
|
|
37
43
|
});
|
|
@@ -64,6 +70,9 @@ describe('DateTimeNoTzPicker date range limit', () => {
|
|
|
64
70
|
currentForm = undefined;
|
|
65
71
|
capturedDatePickerProps = undefined;
|
|
66
72
|
mockResolveJsonTemplate.mockReset();
|
|
73
|
+
mockRunjs.mockReset();
|
|
74
|
+
mockLoggerWarn.mockReset();
|
|
75
|
+
mockRunjs.mockResolvedValue({ success: true, value: undefined });
|
|
67
76
|
mockResolveJsonTemplate.mockImplementation(async (params) => ({
|
|
68
77
|
...params,
|
|
69
78
|
_maxDate: currentForm?.getFieldValue?.('b'),
|
|
@@ -239,4 +248,144 @@ describe('DateTimeNoTzPicker date range limit', () => {
|
|
|
239
248
|
expect(timeConfig?.disabledMinutes?.(12)).toEqual([]);
|
|
240
249
|
expect(timeConfig?.disabledSeconds?.(12, 34)).toEqual([]);
|
|
241
250
|
});
|
|
251
|
+
|
|
252
|
+
it('evaluates RunJS before applying minDate', async () => {
|
|
253
|
+
const code = 'return new Date("2026-05-10T08:09:10.000Z").toISOString();';
|
|
254
|
+
mockRunjs.mockResolvedValue({ success: true, value: '2026-05-10T08:09:10.000Z' });
|
|
255
|
+
mockResolveJsonTemplate.mockImplementation(async (params) => params);
|
|
256
|
+
|
|
257
|
+
render(<TestWrapper picker="date" showTime _minDate={{ code, version: 'v2' }} onChange={vi.fn()} value={null} />);
|
|
258
|
+
|
|
259
|
+
await waitFor(() => {
|
|
260
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-09 00:00:00'))).toBe(true);
|
|
261
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-10 00:00:00'))).toBe(false);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(mockRunjs.mock.calls[0]?.[0]).toBe(code);
|
|
265
|
+
expect(mockRunjs.mock.calls[0]?.[2]).toEqual({ version: 'v2' });
|
|
266
|
+
expect(mockResolveJsonTemplate).toHaveBeenCalledWith({
|
|
267
|
+
_minDate: '2026-05-10T08:09:10.000Z',
|
|
268
|
+
_maxDate: undefined,
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('supports a RunJS minDate together with a variable maxDate', async () => {
|
|
273
|
+
mockRunjs.mockResolvedValue({ success: true, value: '2026-05-10 08:00:00' });
|
|
274
|
+
mockResolveJsonTemplate.mockImplementation(async (params) => ({
|
|
275
|
+
...params,
|
|
276
|
+
_maxDate: '2026-05-12 18:30:40',
|
|
277
|
+
}));
|
|
278
|
+
|
|
279
|
+
render(
|
|
280
|
+
<TestWrapper
|
|
281
|
+
picker="date"
|
|
282
|
+
showTime
|
|
283
|
+
_minDate={{ code: 'return "2026-05-10 08:00:00";', version: 'v2' }}
|
|
284
|
+
_maxDate={'{{ $nForm.max }}'}
|
|
285
|
+
onChange={vi.fn()}
|
|
286
|
+
value={null}
|
|
287
|
+
/>,
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
await waitFor(() => {
|
|
291
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-09 00:00:00'))).toBe(true);
|
|
292
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-13 00:00:00'))).toBe(true);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-11 00:00:00'))).toBe(false);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('clears stale restrictions when RunJS execution fails', async () => {
|
|
299
|
+
mockResolveJsonTemplate.mockImplementation(async (params) => params);
|
|
300
|
+
mockRunjs.mockImplementation(async (code) => {
|
|
301
|
+
if (code === 'throw new Error("failed");') {
|
|
302
|
+
return { success: false, error: new Error('failed') };
|
|
303
|
+
}
|
|
304
|
+
return { success: true, value: '2026-05-10 08:00:00' };
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const view = render(
|
|
308
|
+
<TestWrapper
|
|
309
|
+
picker="date"
|
|
310
|
+
showTime
|
|
311
|
+
_minDate={{ code: 'return "2026-05-10 08:00:00";', version: 'v2' }}
|
|
312
|
+
onChange={vi.fn()}
|
|
313
|
+
value={null}
|
|
314
|
+
/>,
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
await waitFor(() => {
|
|
318
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-09 00:00:00'))).toBe(true);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
view.rerender(
|
|
322
|
+
<TestWrapper
|
|
323
|
+
picker="date"
|
|
324
|
+
showTime
|
|
325
|
+
_minDate={{ code: 'throw new Error("failed");', version: 'v2' }}
|
|
326
|
+
onChange={vi.fn()}
|
|
327
|
+
value={null}
|
|
328
|
+
/>,
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
await waitFor(() => {
|
|
332
|
+
expect(mockLoggerWarn).toHaveBeenCalled();
|
|
333
|
+
expect(capturedDatePickerProps?.minDate).toBeNull();
|
|
334
|
+
expect(capturedDatePickerProps?.disabledDate).toBeNull();
|
|
335
|
+
expect(capturedDatePickerProps?.disabledTime).toBeNull();
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('keeps the latest RunJS result when an earlier execution finishes later', async () => {
|
|
340
|
+
const slowResolvers: Array<(result: { success: boolean; value: string }) => void> = [];
|
|
341
|
+
mockResolveJsonTemplate.mockImplementation(async (params) => params);
|
|
342
|
+
mockRunjs.mockImplementation((code) => {
|
|
343
|
+
if (code === 'return "slow";') {
|
|
344
|
+
return new Promise((resolve) => {
|
|
345
|
+
slowResolvers.push(resolve);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
return Promise.resolve({ success: true, value: '2026-06-01 00:00:00' });
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const view = render(
|
|
352
|
+
<TestWrapper
|
|
353
|
+
picker="date"
|
|
354
|
+
showTime
|
|
355
|
+
_minDate={{ code: 'return "slow";', version: 'v2' }}
|
|
356
|
+
onChange={vi.fn()}
|
|
357
|
+
value={null}
|
|
358
|
+
/>,
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
await waitFor(() => {
|
|
362
|
+
expect(slowResolvers.length).toBeGreaterThan(0);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
view.rerender(
|
|
366
|
+
<TestWrapper
|
|
367
|
+
picker="date"
|
|
368
|
+
showTime
|
|
369
|
+
_minDate={{ code: 'return "fast";', version: 'v2' }}
|
|
370
|
+
onChange={vi.fn()}
|
|
371
|
+
value={null}
|
|
372
|
+
/>,
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
await waitFor(() => {
|
|
376
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-05-31 00:00:00'))).toBe(true);
|
|
377
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-07-01 00:00:00'))).toBe(false);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
slowResolvers.forEach((resolve) => resolve({ success: true, value: '2026-12-01 00:00:00' }));
|
|
381
|
+
|
|
382
|
+
await waitFor(() => {
|
|
383
|
+
expect(mockResolveJsonTemplate).toHaveBeenCalledWith({
|
|
384
|
+
_minDate: '2026-12-01 00:00:00',
|
|
385
|
+
_maxDate: undefined,
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
expect(capturedDatePickerProps?.disabledDate?.(dayjs('2026-07-01 00:00:00'))).toBe(false);
|
|
390
|
+
});
|
|
242
391
|
});
|
|
@@ -8,140 +8,176 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { autorun } from '@formily/reactive';
|
|
11
|
-
import { Form } from 'antd';
|
|
12
|
-
import { useFlowContext } from '@nocobase/flow-engine';
|
|
11
|
+
import { Form, type FormInstance } from 'antd';
|
|
12
|
+
import { resolveRunJSObjectValues, useFlowContext } from '@nocobase/flow-engine';
|
|
13
13
|
import { dayjs } from '@nocobase/utils/client';
|
|
14
|
+
import type { ConfigType, Dayjs } from 'dayjs';
|
|
14
15
|
import { first, last } from 'lodash';
|
|
15
|
-
import
|
|
16
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
16
17
|
|
|
17
18
|
type DateLimitProps = {
|
|
18
|
-
_minDate?:
|
|
19
|
-
_maxDate?:
|
|
20
|
-
currentForm?:
|
|
19
|
+
_minDate?: unknown;
|
|
20
|
+
_maxDate?: unknown;
|
|
21
|
+
currentForm?: unknown;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const [maxDate, setMaxDate] = useState<dayjs.Dayjs | null>(null);
|
|
29
|
-
const [disabledDate, setDisabledDate] = useState<any>(null);
|
|
30
|
-
const [disabledTime, setDisabledTime] = useState<any>(null);
|
|
31
|
-
const disposeRef = useRef<any>(null);
|
|
24
|
+
type DisabledTimeConfig = {
|
|
25
|
+
disabledHours: () => number[];
|
|
26
|
+
disabledMinutes: (selectedHour: number) => number[];
|
|
27
|
+
disabledSeconds: (selectedHour: number, selectedMinute: number) => number[];
|
|
28
|
+
};
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
disposeRef.current();
|
|
36
|
-
}
|
|
30
|
+
type DisabledDate = (current: Dayjs) => boolean;
|
|
31
|
+
type DisabledTime = (current: Dayjs | null) => DisabledTimeConfig;
|
|
37
32
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
function isAntdFormInstance(value: unknown): value is FormInstance {
|
|
34
|
+
if (!value || typeof value !== 'object') return false;
|
|
35
|
+
return typeof (value as Partial<FormInstance>).getFieldsValue === 'function';
|
|
36
|
+
}
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
function parseDateLimitValue(value: unknown): Dayjs | null {
|
|
39
|
+
if (!value) return null;
|
|
40
|
+
const parsed = dayjs(value as ConfigType);
|
|
41
|
+
return parsed.isValid() ? parsed : null;
|
|
42
|
+
}
|
|
46
43
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
export function useDateLimit(props: DateLimitProps) {
|
|
45
|
+
const ctx = useFlowContext();
|
|
46
|
+
const currentForm = isAntdFormInstance(props.currentForm) ? props.currentForm : undefined;
|
|
47
|
+
const currentFormValues = Form.useWatch([], currentForm);
|
|
48
|
+
const [minDate, setMinDate] = useState<Dayjs | null>(null);
|
|
49
|
+
const [maxDate, setMaxDate] = useState<Dayjs | null>(null);
|
|
50
|
+
const [disabledDate, setDisabledDate] = useState<DisabledDate | null>(null);
|
|
51
|
+
const [disabledTime, setDisabledTime] = useState<DisabledTime | null>(null);
|
|
52
|
+
const evaluationIdRef = useRef(0);
|
|
53
|
+
|
|
54
|
+
const limitDate = useCallback(async () => {
|
|
55
|
+
const evaluationId = ++evaluationIdRef.current;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const runJSResolvedParams = await resolveRunJSObjectValues(ctx, {
|
|
59
|
+
_minDate: props._minDate,
|
|
60
|
+
_maxDate: props._maxDate,
|
|
61
|
+
});
|
|
62
|
+
const resolvedParams = (await ctx.resolveJsonTemplate({
|
|
63
|
+
_minDate: runJSResolvedParams._minDate,
|
|
64
|
+
_maxDate: runJSResolvedParams._maxDate,
|
|
65
|
+
})) as { _minDate?: unknown; _maxDate?: unknown };
|
|
66
|
+
|
|
67
|
+
if (evaluationId !== evaluationIdRef.current) return;
|
|
68
|
+
|
|
69
|
+
const nextMinRaw = Array.isArray(resolvedParams?._minDate)
|
|
70
|
+
? first(resolvedParams._minDate)
|
|
71
|
+
: resolvedParams?._minDate;
|
|
72
|
+
const nextMaxRaw = Array.isArray(resolvedParams?._maxDate)
|
|
73
|
+
? last(resolvedParams._maxDate)
|
|
74
|
+
: resolvedParams?._maxDate;
|
|
75
|
+
const nextMinDate = parseDateLimitValue(nextMinRaw);
|
|
76
|
+
const nextMaxDate = parseDateLimitValue(nextMaxRaw);
|
|
77
|
+
|
|
78
|
+
setMinDate(nextMinDate);
|
|
79
|
+
setMaxDate(nextMaxDate);
|
|
80
|
+
|
|
81
|
+
const fullTimeArr = Array.from({ length: 60 }, (_, i) => i);
|
|
82
|
+
|
|
83
|
+
const nextDisabledDate: DisabledDate = (current) => {
|
|
84
|
+
if (!dayjs.isDayjs(current)) return false;
|
|
85
|
+
|
|
86
|
+
const min = nextMinDate?.startOf('day') || null;
|
|
87
|
+
const max = nextMaxDate?.endOf('day') || null;
|
|
88
|
+
|
|
89
|
+
if (min && current.startOf('day').isBefore(min)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if (max && current.startOf('day').isAfter(max)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
};
|
|
52
97
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
? last(resolvedParams._maxDate)
|
|
58
|
-
: resolvedParams?._maxDate;
|
|
59
|
-
const nextMinDate = nextMinRaw ? dayjs(nextMinRaw) : null;
|
|
60
|
-
const nextMaxDate = nextMaxRaw ? dayjs(nextMaxRaw) : null;
|
|
61
|
-
|
|
62
|
-
setMinDate(nextMinDate?.isValid?.() ? nextMinDate : null);
|
|
63
|
-
setMaxDate(nextMaxDate?.isValid?.() ? nextMaxDate : null);
|
|
64
|
-
|
|
65
|
-
const fullTimeArr = Array.from({ length: 60 }, (_, i) => i);
|
|
66
|
-
|
|
67
|
-
const nextDisabledDate = (current: dayjs.Dayjs) => {
|
|
68
|
-
if (!dayjs.isDayjs(current)) return false;
|
|
69
|
-
|
|
70
|
-
const min = nextMinDate?.isValid?.() ? nextMinDate.startOf('day') : null;
|
|
71
|
-
const max = nextMaxDate?.isValid?.() ? nextMaxDate.endOf('day') : null;
|
|
72
|
-
|
|
73
|
-
if (min && current.startOf('day').isBefore(min)) {
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
if (max && current.startOf('day').isAfter(max)) {
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
return false;
|
|
80
|
-
};
|
|
98
|
+
const nextDisabledTime: DisabledTime = (current) => {
|
|
99
|
+
if (!current || (!nextMinDate && !nextMaxDate)) {
|
|
100
|
+
return { disabledHours: () => [], disabledMinutes: () => [], disabledSeconds: () => [] };
|
|
101
|
+
}
|
|
81
102
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return { disabledHours: () => [], disabledMinutes: () => [], disabledSeconds: () => [] };
|
|
85
|
-
}
|
|
103
|
+
const isCurrentMinDay = !!nextMinDate && current.isSame(nextMinDate, 'day');
|
|
104
|
+
const isCurrentMaxDay = !!nextMaxDate && current.isSame(nextMaxDate, 'day');
|
|
86
105
|
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
const disabledHours = () => {
|
|
107
|
+
const hours = [];
|
|
108
|
+
if (isCurrentMinDay && nextMinDate) {
|
|
109
|
+
for (let hour = 0; hour < nextMinDate.hour(); hour++) {
|
|
110
|
+
hours.push(hour);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (isCurrentMaxDay && nextMaxDate) {
|
|
114
|
+
for (let hour = nextMaxDate.hour() + 1; hour < 24; hour++) {
|
|
115
|
+
hours.push(hour);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return hours;
|
|
119
|
+
};
|
|
89
120
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
for (let hour = 0; hour < nextMinDate.hour(); hour++) {
|
|
94
|
-
hours.push(hour);
|
|
121
|
+
const disabledMinutes = (selectedHour: number) => {
|
|
122
|
+
if (isCurrentMinDay && nextMinDate && selectedHour === nextMinDate.hour()) {
|
|
123
|
+
return fullTimeArr.filter((minute) => minute < nextMinDate.minute());
|
|
95
124
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
for (let hour = nextMaxDate.hour() + 1; hour < 24; hour++) {
|
|
99
|
-
hours.push(hour);
|
|
125
|
+
if (isCurrentMaxDay && nextMaxDate && selectedHour === nextMaxDate.hour()) {
|
|
126
|
+
return fullTimeArr.filter((minute) => minute > nextMaxDate.minute());
|
|
100
127
|
}
|
|
101
|
-
|
|
102
|
-
|
|
128
|
+
return [];
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const disabledSeconds = (selectedHour: number, selectedMinute: number) => {
|
|
132
|
+
if (
|
|
133
|
+
isCurrentMinDay &&
|
|
134
|
+
nextMinDate &&
|
|
135
|
+
selectedHour === nextMinDate.hour() &&
|
|
136
|
+
selectedMinute === nextMinDate.minute()
|
|
137
|
+
) {
|
|
138
|
+
return fullTimeArr.filter((second) => second < nextMinDate.second());
|
|
139
|
+
}
|
|
140
|
+
if (
|
|
141
|
+
isCurrentMaxDay &&
|
|
142
|
+
nextMaxDate &&
|
|
143
|
+
selectedHour === nextMaxDate.hour() &&
|
|
144
|
+
selectedMinute === nextMaxDate.minute()
|
|
145
|
+
) {
|
|
146
|
+
return fullTimeArr.filter((second) => second > nextMaxDate.second());
|
|
147
|
+
}
|
|
148
|
+
return [];
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
disabledHours,
|
|
153
|
+
disabledMinutes,
|
|
154
|
+
disabledSeconds,
|
|
155
|
+
};
|
|
103
156
|
};
|
|
104
157
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (isCurrentMaxDay && nextMaxDate && selectedHour === nextMaxDate.hour()) {
|
|
110
|
-
return fullTimeArr.filter((minute) => minute > nextMaxDate.minute());
|
|
111
|
-
}
|
|
112
|
-
return [];
|
|
113
|
-
};
|
|
158
|
+
setDisabledDate(() => nextDisabledDate);
|
|
159
|
+
setDisabledTime(() => nextDisabledTime);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (evaluationId !== evaluationIdRef.current) return;
|
|
114
162
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return fullTimeArr.filter((second) => second < nextMinDate.second());
|
|
123
|
-
}
|
|
124
|
-
if (
|
|
125
|
-
isCurrentMaxDay &&
|
|
126
|
-
nextMaxDate &&
|
|
127
|
-
selectedHour === nextMaxDate.hour() &&
|
|
128
|
-
selectedMinute === nextMaxDate.minute()
|
|
129
|
-
) {
|
|
130
|
-
return fullTimeArr.filter((second) => second > nextMaxDate.second());
|
|
131
|
-
}
|
|
132
|
-
return [];
|
|
133
|
-
};
|
|
163
|
+
ctx.logger?.warn?.({ err: error }, 'Failed to resolve date range limit');
|
|
164
|
+
setMinDate(null);
|
|
165
|
+
setMaxDate(null);
|
|
166
|
+
setDisabledDate(null);
|
|
167
|
+
setDisabledTime(null);
|
|
168
|
+
}
|
|
169
|
+
}, [ctx, props._maxDate, props._minDate]);
|
|
134
170
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
};
|
|
140
|
-
};
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
const dispose = autorun(() => {
|
|
173
|
+
limitDate();
|
|
174
|
+
});
|
|
141
175
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
176
|
+
return () => {
|
|
177
|
+
evaluationIdRef.current += 1;
|
|
178
|
+
dispose();
|
|
179
|
+
};
|
|
180
|
+
}, [currentFormValues, limitDate]);
|
|
145
181
|
|
|
146
182
|
return {
|
|
147
183
|
minDate,
|