@nocobase/client-v2 2.1.0-beta.27 → 2.1.0-beta.29
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/JsonTextArea.d.ts +18 -0
- package/es/components/index.d.ts +1 -0
- package/es/flow/actions/dateRangeLimit.d.ts +9 -0
- package/es/flow/actions/index.d.ts +1 -0
- package/es/flow/models/base/PageModel/PageModel.d.ts +4 -0
- package/es/flow/models/base/PageModel/RootPageModel.d.ts +9 -0
- package/es/flow/models/blocks/form/value-runtime/runtime.d.ts +7 -0
- package/es/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableColumnModel.d.ts +2 -0
- package/es/flow/models/fields/DateTimeFieldModel/dateLimit.d.ts +20 -0
- package/es/flow/models/fields/JSEditableFieldModel.d.ts +4 -0
- package/es/index.mjs +72 -65
- package/lib/index.js +61 -54
- package/package.json +6 -5
- package/src/components/form/JsonTextArea.tsx +129 -0
- package/src/components/index.ts +1 -0
- package/src/flow/actions/__tests__/fieldLinkageRules.scopeDepth.test.ts +478 -0
- package/src/flow/actions/__tests__/pattern.test.ts +190 -0
- package/src/flow/actions/dateRangeLimit.tsx +66 -0
- package/src/flow/actions/index.ts +1 -0
- package/src/flow/actions/linkageRules.tsx +117 -19
- package/src/flow/actions/openView.tsx +2 -1
- package/src/flow/actions/pattern.tsx +25 -2
- package/src/flow/admin-shell/AdminLayoutRouteCoordinator.ts +7 -1
- package/src/flow/admin-shell/__tests__/AdminLayoutRouteCoordinator.test.ts +117 -0
- package/src/flow/models/base/PageModel/PageModel.tsx +15 -3
- package/src/flow/models/base/PageModel/RootPageModel.tsx +37 -2
- package/src/flow/models/base/PageModel/__tests__/PageModel.test.ts +73 -0
- package/src/flow/models/base/PageModel/__tests__/RootPageModel.test.ts +116 -0
- package/src/flow/models/blocks/form/value-runtime/__tests__/runtime.test.ts +167 -1
- package/src/flow/models/blocks/form/value-runtime/runtime.ts +103 -11
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableColumnModel.tsx +27 -3
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableField.tsx +47 -0
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/__tests__/SubTableColumnModel.rowRecord.test.ts +42 -0
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/__tests__/SubTableField.refresh.test.tsx +122 -0
- package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/index.tsx +2 -0
- package/src/flow/models/fields/ClickableFieldModel.tsx +21 -9
- package/src/flow/models/fields/DateTimeFieldModel/DateOnlyFieldModel.tsx +9 -0
- package/src/flow/models/fields/DateTimeFieldModel/DateTimeFieldModel.tsx +4 -0
- package/src/flow/models/fields/DateTimeFieldModel/DateTimeNoTzFieldModel.tsx +9 -0
- package/src/flow/models/fields/DateTimeFieldModel/DateTimeTzFieldModel.tsx +9 -0
- package/src/flow/models/fields/DateTimeFieldModel/__tests__/DateTimeNoTzFieldModel.dateLimit.test.tsx +242 -0
- package/src/flow/models/fields/DateTimeFieldModel/dateLimit.ts +152 -0
- package/src/flow/models/fields/JSEditableFieldModel.tsx +110 -14
- package/src/flow/models/fields/__tests__/ClickableFieldModel.test.ts +87 -0
- package/src/flow/models/fields/__tests__/JSEditableFieldModel.test.tsx +210 -0
|
@@ -137,6 +137,42 @@ export class FormValueRuntime {
|
|
|
137
137
|
return this.getForm().getFieldsValue(true);
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
canApplyDefaultValuePatch(namePath: NamePath, resolved: any) {
|
|
141
|
+
if (!namePath?.length) return false;
|
|
142
|
+
if (typeof resolved === 'undefined') return false;
|
|
143
|
+
|
|
144
|
+
const pathKey = namePathToPathKey(namePath);
|
|
145
|
+
const current = this.getFormValueAtPath(namePath);
|
|
146
|
+
const last = this.lastDefaultValueByPathKey.get(pathKey);
|
|
147
|
+
const nextSnapshot = isObservable(resolved) ? toJS(resolved) : resolved;
|
|
148
|
+
const currentSnapshot = isObservable(current) ? toJS(current) : current;
|
|
149
|
+
const currentEqualsLastDefault = typeof last !== 'undefined' && _.isEqual(currentSnapshot, last);
|
|
150
|
+
const explicitHit = this.findExplicitHit(pathKey);
|
|
151
|
+
|
|
152
|
+
if (explicitHit && !currentEqualsLastDefault) return false;
|
|
153
|
+
|
|
154
|
+
const canOverwrite = isEmptyValue(current) || currentEqualsLastDefault;
|
|
155
|
+
if (!canOverwrite && _.isEqual(current, nextSnapshot)) {
|
|
156
|
+
this.lastDefaultValueByPathKey.set(pathKey, nextSnapshot);
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return canOverwrite;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
recordDefaultValuePatch(namePath: NamePath, value?: any) {
|
|
164
|
+
if (!namePath?.length) return;
|
|
165
|
+
const pathKey = namePathToPathKey(namePath);
|
|
166
|
+
const snapshot =
|
|
167
|
+
arguments.length >= 2 ? (isObservable(value) ? toJS(value) : value) : this.getFormValueAtPath(namePath);
|
|
168
|
+
this.lastDefaultValueByPathKey.set(pathKey, snapshot);
|
|
169
|
+
const current = this.getFormValueAtPath(namePath);
|
|
170
|
+
const currentSnapshot = isObservable(current) ? toJS(current) : current;
|
|
171
|
+
if (_.isEqual(currentSnapshot, snapshot)) {
|
|
172
|
+
this.clearExplicitForDefaultPatch(pathKey);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
140
176
|
private getFormValueAtPath(namePath: NamePath) {
|
|
141
177
|
const form: any = this.getForm?.();
|
|
142
178
|
if (form && typeof form.getFieldValue === 'function') {
|
|
@@ -247,11 +283,7 @@ export class FormValueRuntime {
|
|
|
247
283
|
let bumpedWriteSeq = false;
|
|
248
284
|
for (const field of changedFields || []) {
|
|
249
285
|
const name = field?.name;
|
|
250
|
-
const namePath
|
|
251
|
-
? (name as NamePath)
|
|
252
|
-
: typeof name === 'string' || typeof name === 'number'
|
|
253
|
-
? ([name] as NamePath)
|
|
254
|
-
: null;
|
|
286
|
+
const namePath = this.normalizeObservedNamePath(name);
|
|
255
287
|
if (!namePath?.length) continue;
|
|
256
288
|
const nextValue = form.getFieldValue(namePath as any);
|
|
257
289
|
const prevValue = _.get(this.valuesMirror, namePath);
|
|
@@ -265,8 +297,11 @@ export class FormValueRuntime {
|
|
|
265
297
|
const isMeaningfulTouched =
|
|
266
298
|
field?.touched === true && !this.shouldIgnoreSyntheticTouchedInit(namePath, prevValue, nextValue);
|
|
267
299
|
if (isMeaningfulTouched) {
|
|
268
|
-
|
|
269
|
-
|
|
300
|
+
const pathKey = namePathToPathKey(namePath);
|
|
301
|
+
if (!this.shouldKeepDefaultPathValueEnabled(namePath, nextValue)) {
|
|
302
|
+
touchedChangedPathKeys.add(pathKey);
|
|
303
|
+
hasMeaningfulTouchedChange = true;
|
|
304
|
+
}
|
|
270
305
|
}
|
|
271
306
|
}
|
|
272
307
|
|
|
@@ -526,6 +561,10 @@ export class FormValueRuntime {
|
|
|
526
561
|
}
|
|
527
562
|
|
|
528
563
|
private getObservedChangedValue(path: NamePath, changedValues: any, snapshot: any) {
|
|
564
|
+
if (_.has(snapshot, path as any)) {
|
|
565
|
+
return _.get(snapshot, path as any);
|
|
566
|
+
}
|
|
567
|
+
|
|
529
568
|
if (path?.length === 1) {
|
|
530
569
|
const key = path[0];
|
|
531
570
|
if (
|
|
@@ -546,6 +585,7 @@ export class FormValueRuntime {
|
|
|
546
585
|
|
|
547
586
|
let observed = snapshot;
|
|
548
587
|
for (const key of Object.keys(changedValues)) {
|
|
588
|
+
if (_.has(observed, [key])) continue;
|
|
549
589
|
if (observed === snapshot) {
|
|
550
590
|
observed = Array.isArray(snapshot) ? [...snapshot] : { ...snapshot };
|
|
551
591
|
}
|
|
@@ -573,7 +613,15 @@ export class FormValueRuntime {
|
|
|
573
613
|
this.lastObservedChangedPaths = null;
|
|
574
614
|
this.lastObservedSource = null;
|
|
575
615
|
|
|
576
|
-
|
|
616
|
+
// 子表格的 changedValues 可能只包含局部行对象,diff 必须以 form 当前完整快照为准,
|
|
617
|
+
// 否则缺失的 sibling 字段会被误判为用户清空。
|
|
618
|
+
const formSnapshot = this.getFormValuesSnapshot();
|
|
619
|
+
const rawSnapshot =
|
|
620
|
+
formSnapshot && typeof formSnapshot === 'object'
|
|
621
|
+
? formSnapshot
|
|
622
|
+
: allValues && typeof allValues === 'object'
|
|
623
|
+
? allValues
|
|
624
|
+
: changedValues;
|
|
577
625
|
const snapshot = this.getObservedSnapshot(changedValues, rawSnapshot);
|
|
578
626
|
this.reconcileArrayItemState([...rawChangedPaths, ...changedValuePaths], changedValues, snapshot);
|
|
579
627
|
this.pruneDeletedArrayItemState(snapshot);
|
|
@@ -602,8 +650,16 @@ export class FormValueRuntime {
|
|
|
602
650
|
this.bumpChangeTick();
|
|
603
651
|
}
|
|
604
652
|
|
|
605
|
-
|
|
653
|
+
const explicitPathsToMark: NamePath[] = [];
|
|
606
654
|
for (const p of explicitPaths) {
|
|
655
|
+
if (this.shouldKeepDefaultPathEnabled(p, snapshot)) {
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
explicitPathsToMark.push(p);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// 非 default 来源写入:需要使默认值永久失效(explicit)
|
|
662
|
+
for (const p of explicitPathsToMark) {
|
|
607
663
|
this.markExplicit(namePathToPathKey(p));
|
|
608
664
|
}
|
|
609
665
|
|
|
@@ -733,7 +789,7 @@ export class FormValueRuntime {
|
|
|
733
789
|
const source: ValueSource = options?.source ?? 'system';
|
|
734
790
|
const triggerEvent = options?.triggerEvent !== false;
|
|
735
791
|
const txId = options?.txId ?? createTxId();
|
|
736
|
-
const markExplicit = options?.markExplicit ?? source !== 'default';
|
|
792
|
+
const markExplicit = options?.markExplicit ?? (source !== 'default' && source !== 'linkage');
|
|
737
793
|
const ownsTxId = typeof options?.txId === 'undefined';
|
|
738
794
|
|
|
739
795
|
const linkageScopeDepth =
|
|
@@ -1071,10 +1127,12 @@ export class FormValueRuntime {
|
|
|
1071
1127
|
private markExplicit(pathKey: string) {
|
|
1072
1128
|
if (this.explicitSet.has(pathKey)) return;
|
|
1073
1129
|
this.explicitSet.add(pathKey);
|
|
1130
|
+
const preserveDescendantDefaults = this.shouldPreserveDescendantDefaultsOnExplicit(pathKey);
|
|
1074
1131
|
|
|
1075
1132
|
// explicit 后默认值永远失效:清理该路径及其子路径的 lastDefault 记录,避免误判“仍是默认值”
|
|
1076
1133
|
for (const k of Array.from(this.lastDefaultValueByPathKey.keys())) {
|
|
1077
|
-
|
|
1134
|
+
const isDescendant = k.startsWith(`${pathKey}.`) || k.startsWith(`${pathKey}[`);
|
|
1135
|
+
if (k === pathKey || (!preserveDescendantDefaults && isDescendant)) {
|
|
1078
1136
|
this.lastDefaultValueByPathKey.delete(k);
|
|
1079
1137
|
}
|
|
1080
1138
|
}
|
|
@@ -1087,6 +1145,40 @@ export class FormValueRuntime {
|
|
|
1087
1145
|
}
|
|
1088
1146
|
}
|
|
1089
1147
|
|
|
1148
|
+
private normalizeObservedNamePath(name: NamePath | string | number | undefined): NamePath | null {
|
|
1149
|
+
if (Array.isArray(name)) return name as NamePath;
|
|
1150
|
+
if (typeof name === 'number') return [name];
|
|
1151
|
+
if (typeof name !== 'string' || !name) return null;
|
|
1152
|
+
const parsed = pathKeyToNamePath(name);
|
|
1153
|
+
return parsed.length ? parsed : [name];
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
private clearExplicitForDefaultPatch(pathKey: string) {
|
|
1157
|
+
for (const key of Array.from(this.explicitSet)) {
|
|
1158
|
+
if (key === pathKey || key.startsWith(`${pathKey}.`) || key.startsWith(`${pathKey}[`)) {
|
|
1159
|
+
this.explicitSet.delete(key);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
private shouldPreserveDescendantDefaultsOnExplicit(pathKey: string) {
|
|
1165
|
+
const value = this.getFormValueAtPath(pathKeyToNamePath(pathKey));
|
|
1166
|
+
return Array.isArray(value);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
private shouldKeepDefaultPathEnabled(namePath: NamePath, snapshot: any) {
|
|
1170
|
+
return this.shouldKeepDefaultPathValueEnabled(namePath, _.get(snapshot, namePath as any));
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
private shouldKeepDefaultPathValueEnabled(namePath: NamePath, value: any) {
|
|
1174
|
+
const pathKey = namePathToPathKey(namePath);
|
|
1175
|
+
const last = this.lastDefaultValueByPathKey.get(pathKey);
|
|
1176
|
+
if (typeof last === 'undefined') return false;
|
|
1177
|
+
|
|
1178
|
+
const nextSnapshot = isObservable(value) ? toJS(value) : value;
|
|
1179
|
+
return _.isEqual(nextSnapshot, last);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1090
1182
|
private isExplicit(pathKey: string) {
|
|
1091
1183
|
return !!this.findExplicitHit(pathKey);
|
|
1092
1184
|
}
|
package/src/flow/models/fields/AssociationFieldModel/SubTableFieldModel/SubTableColumnModel.tsx
CHANGED
|
@@ -177,6 +177,25 @@ const MemoFieldRenderer = React.memo(FieldModelRenderer, (prev, next) => {
|
|
|
177
177
|
return prev.value === next.value && prev.model === next.model;
|
|
178
178
|
});
|
|
179
179
|
|
|
180
|
+
export function buildRowPathFromFieldIndex(fieldIndex: unknown): Array<string | number> | null {
|
|
181
|
+
if (!Array.isArray(fieldIndex) || !fieldIndex.length) return null;
|
|
182
|
+
const out: Array<string | number> = [];
|
|
183
|
+
for (const entry of fieldIndex) {
|
|
184
|
+
if (typeof entry !== 'string') continue;
|
|
185
|
+
const [fieldName, indexStr] = entry.split(':');
|
|
186
|
+
const index = Number(indexStr);
|
|
187
|
+
if (!fieldName || !Number.isFinite(index)) continue;
|
|
188
|
+
out.push(fieldName, index);
|
|
189
|
+
}
|
|
190
|
+
return out.length ? out : null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function getLatestSubTableRowRecord(form: any, fieldIndex: unknown, fallbackRecord: any): any {
|
|
194
|
+
const latestRowPath = buildRowPathFromFieldIndex(fieldIndex);
|
|
195
|
+
const latestRecord = latestRowPath ? form?.getFieldValue?.(latestRowPath) : undefined;
|
|
196
|
+
return typeof latestRecord === 'undefined' ? fallbackRecord : latestRecord;
|
|
197
|
+
}
|
|
198
|
+
|
|
180
199
|
function shouldCommitImmediately(value: any) {
|
|
181
200
|
if (Array.isArray(value)) {
|
|
182
201
|
return true;
|
|
@@ -574,6 +593,9 @@ export class SubTableColumnModel<
|
|
|
574
593
|
const rowForkKey = `row:${baseIndexKey}:${rowIdentity}:${String(rowIdx)}`;
|
|
575
594
|
const rowFork: any = (() => {
|
|
576
595
|
const fork = this.createFork({}, rowForkKey);
|
|
596
|
+
fork.context.defineProperty('subTableRowFork', {
|
|
597
|
+
value: true,
|
|
598
|
+
});
|
|
577
599
|
const associationFieldPath =
|
|
578
600
|
(this.parent as any)?.fieldPath ??
|
|
579
601
|
(this.parent as any)?.context?.fieldPath ??
|
|
@@ -592,9 +614,11 @@ export class SubTableColumnModel<
|
|
|
592
614
|
}
|
|
593
615
|
fork.context.defineProperty('item', {
|
|
594
616
|
get: () => {
|
|
617
|
+
const form = (fork.context as any)?.form || (this.context?.blockModel as any)?.context?.form;
|
|
618
|
+
const rowRecord = getLatestSubTableRowRecord(form, fork.context.fieldIndex, record);
|
|
595
619
|
const parentItemCtx = (parentItem ?? this.context?.item) as any;
|
|
596
|
-
const isNew =
|
|
597
|
-
const isStored =
|
|
620
|
+
const isNew = rowRecord?.__is_new__;
|
|
621
|
+
const isStored = rowRecord?.__is_stored__;
|
|
598
622
|
const list = (this.parent as any)?.props?.value;
|
|
599
623
|
const length = Array.isArray(list) ? list.length : undefined;
|
|
600
624
|
return {
|
|
@@ -602,7 +626,7 @@ export class SubTableColumnModel<
|
|
|
602
626
|
length,
|
|
603
627
|
__is_new__: isNew,
|
|
604
628
|
__is_stored__: isStored,
|
|
605
|
-
value:
|
|
629
|
+
value: rowRecord,
|
|
606
630
|
parentItem: parentItemCtx,
|
|
607
631
|
};
|
|
608
632
|
},
|
|
@@ -14,8 +14,41 @@ import { useTranslation } from 'react-i18next';
|
|
|
14
14
|
import { PlusOutlined } from '@ant-design/icons';
|
|
15
15
|
import React, { useEffect, useMemo, useState } from 'react';
|
|
16
16
|
import { ActionWithoutPermission } from '../../../base/ActionModel';
|
|
17
|
+
import { parsePathString } from '../../../blocks/form/value-runtime/path';
|
|
17
18
|
import { getSubTableRowIdentity, normalizeSubTableRows } from './rowIdentity';
|
|
18
19
|
|
|
20
|
+
type NamePath = Array<string | number>;
|
|
21
|
+
|
|
22
|
+
function isSamePathPrefix(prefix: NamePath, path: NamePath) {
|
|
23
|
+
if (!prefix.length || prefix.length > path.length) return false;
|
|
24
|
+
return prefix.every((seg, index) => seg === path[index]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isRelatedPath(a: NamePath, b: NamePath) {
|
|
28
|
+
return isSamePathPrefix(a, b) || isSamePathPrefix(b, a);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function normalizeChangedPath(path: unknown): NamePath | null {
|
|
32
|
+
const rawPath = Array.isArray(path) ? path : typeof path === 'string' ? [path] : null;
|
|
33
|
+
if (!rawPath) return null;
|
|
34
|
+
const normalized = rawPath.flatMap((seg) => {
|
|
35
|
+
if (typeof seg === 'number') return [seg];
|
|
36
|
+
if (typeof seg !== 'string') return [];
|
|
37
|
+
return parsePathString(seg).filter((parsed): parsed is string | number => typeof parsed !== 'object');
|
|
38
|
+
});
|
|
39
|
+
return normalized.length ? normalized : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function shouldRefreshForChangedPaths(fieldPath: unknown, changedPaths: unknown) {
|
|
43
|
+
const currentFieldPath = normalizeChangedPath(fieldPath);
|
|
44
|
+
if (!currentFieldPath) return false;
|
|
45
|
+
const paths = Array.isArray(changedPaths) ? changedPaths : [];
|
|
46
|
+
return paths.some((path) => {
|
|
47
|
+
const changedPath = normalizeChangedPath(path);
|
|
48
|
+
return changedPath ? isRelatedPath(currentFieldPath, changedPath) : false;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
19
52
|
export function SubTableField(props) {
|
|
20
53
|
const { t } = useTranslation();
|
|
21
54
|
const {
|
|
@@ -35,9 +68,12 @@ export function SubTableField(props) {
|
|
|
35
68
|
resetPage,
|
|
36
69
|
filterTargetKey = 'id',
|
|
37
70
|
getCurrentValue,
|
|
71
|
+
fieldPathArray,
|
|
72
|
+
formValuesChangeEmitter,
|
|
38
73
|
} = props;
|
|
39
74
|
const [currentPage, setCurrentPage] = useState(1);
|
|
40
75
|
const [currentPageSize, setCurrentPageSize] = useState(pageSize);
|
|
76
|
+
const [, forceRefresh] = useState(0);
|
|
41
77
|
const rawCurrentValue = getCurrentValue();
|
|
42
78
|
const currentValue = useMemo(() => normalizeSubTableRows(rawCurrentValue), [rawCurrentValue]);
|
|
43
79
|
const getRecordIdentity = React.useCallback(
|
|
@@ -50,6 +86,17 @@ export function SubTableField(props) {
|
|
|
50
86
|
useEffect(() => {
|
|
51
87
|
resetPage && setCurrentPage(1);
|
|
52
88
|
}, [resetPage]);
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (!formValuesChangeEmitter?.on || !formValuesChangeEmitter?.off) return;
|
|
91
|
+
const listener = (payload: any) => {
|
|
92
|
+
if (!shouldRefreshForChangedPaths(fieldPathArray, payload?.changedPaths)) return;
|
|
93
|
+
forceRefresh((v) => v + 1);
|
|
94
|
+
};
|
|
95
|
+
formValuesChangeEmitter.on('formValuesChange', listener);
|
|
96
|
+
return () => {
|
|
97
|
+
formValuesChangeEmitter.off('formValuesChange', listener);
|
|
98
|
+
};
|
|
99
|
+
}, [fieldPathArray, formValuesChangeEmitter]);
|
|
53
100
|
const applyValue = React.useCallback((nextValue: any) => onChange?.(normalizeSubTableRows(nextValue)), [onChange]);
|
|
54
101
|
const getLatestValue = React.useCallback(() => normalizeSubTableRows(getCurrentValue()), [getCurrentValue]);
|
|
55
102
|
useEffect(() => {
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { describe, expect, it, vi } from 'vitest';
|
|
11
|
+
import { getLatestSubTableRowRecord, buildRowPathFromFieldIndex } from '../SubTableColumnModel';
|
|
12
|
+
|
|
13
|
+
describe('SubTableColumnModel row record helpers', () => {
|
|
14
|
+
it('builds the row path from fieldIndex entries', () => {
|
|
15
|
+
expect(buildRowPathFromFieldIndex(['roles:0'])).toEqual(['roles', 0]);
|
|
16
|
+
expect(buildRowPathFromFieldIndex(['users:1', 'roles:2'])).toEqual(['users', 1, 'roles', 2]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('prefers the latest row value from form over the fallback record', () => {
|
|
20
|
+
const form = {
|
|
21
|
+
getFieldValue: vi.fn((path: any) => {
|
|
22
|
+
if (JSON.stringify(path) === JSON.stringify(['roles', 0])) {
|
|
23
|
+
return { uid: 'role-uid-1', __is_new__: true };
|
|
24
|
+
}
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
const fallback = { uid: 'stale-role', __is_new__: false };
|
|
28
|
+
|
|
29
|
+
expect(getLatestSubTableRowRecord(form, ['roles:0'], fallback)).toEqual({
|
|
30
|
+
uid: 'role-uid-1',
|
|
31
|
+
__is_new__: true,
|
|
32
|
+
});
|
|
33
|
+
expect(form.getFieldValue).toHaveBeenCalledWith(['roles', 0]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('falls back to the record when latest row value is unavailable', () => {
|
|
37
|
+
const form = { getFieldValue: vi.fn(() => undefined) };
|
|
38
|
+
const fallback = { uid: 'stale-role', __is_new__: false };
|
|
39
|
+
|
|
40
|
+
expect(getLatestSubTableRowRecord(form, ['roles:0'], fallback)).toBe(fallback);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
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 React from 'react';
|
|
11
|
+
import { EventEmitter } from 'events';
|
|
12
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
13
|
+
import { act, render, screen } from '@nocobase/test/client';
|
|
14
|
+
import { SubTableField } from '../SubTableField';
|
|
15
|
+
|
|
16
|
+
vi.mock('react-i18next', async (importOriginal) => ({
|
|
17
|
+
...(await importOriginal<any>()),
|
|
18
|
+
useTranslation: () => ({
|
|
19
|
+
t: (value: string) => value,
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
vi.mock('antd', async () => {
|
|
24
|
+
const actual = await vi.importActual<any>('antd');
|
|
25
|
+
return {
|
|
26
|
+
...actual,
|
|
27
|
+
Table: ({ dataSource = [], columns = [] }: any) => (
|
|
28
|
+
<div data-testid="subtable">
|
|
29
|
+
{dataSource.map((record: any, rowIdx: number) => (
|
|
30
|
+
<div data-testid={`row-${rowIdx}`} key={record.__index__ || rowIdx}>
|
|
31
|
+
{columns.map((column: any) => (
|
|
32
|
+
<div data-testid={`cell-${rowIdx}-${String(column.dataIndex || column.key)}`} key={column.key}>
|
|
33
|
+
{column.render?.(record[column.dataIndex], record, rowIdx)}
|
|
34
|
+
</div>
|
|
35
|
+
))}
|
|
36
|
+
</div>
|
|
37
|
+
))}
|
|
38
|
+
</div>
|
|
39
|
+
),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('SubTableField refresh', () => {
|
|
44
|
+
it('rerenders from current form value when a nested subtable path changes', () => {
|
|
45
|
+
const emitter = new EventEmitter();
|
|
46
|
+
const store = {
|
|
47
|
+
roles: [{ __is_new__: true, __index__: 'row-1', uid: 'role-uid-1', name: '' }],
|
|
48
|
+
};
|
|
49
|
+
const columns = [
|
|
50
|
+
{
|
|
51
|
+
key: 'name',
|
|
52
|
+
dataIndex: 'name',
|
|
53
|
+
render: ({ value }: any) => <span>{value || 'empty'}</span>,
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
render(
|
|
58
|
+
<SubTableField
|
|
59
|
+
columns={columns}
|
|
60
|
+
pageSize={10}
|
|
61
|
+
filterTargetKey="id"
|
|
62
|
+
fieldPathArray={['roles']}
|
|
63
|
+
formValuesChangeEmitter={emitter}
|
|
64
|
+
getCurrentValue={() => store.roles}
|
|
65
|
+
/>,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(screen.getByTestId('cell-0-name')).toHaveTextContent('empty');
|
|
69
|
+
|
|
70
|
+
act(() => {
|
|
71
|
+
store.roles = [{ ...store.roles[0], name: 'role-uid-1' }];
|
|
72
|
+
emitter.emit('formValuesChange', {
|
|
73
|
+
source: 'linkage',
|
|
74
|
+
changedPaths: [['roles', 0, 'name']],
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(screen.getByTestId('cell-0-name')).toHaveTextContent('role-uid-1');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('ignores unrelated form value changes', () => {
|
|
82
|
+
const emitter = new EventEmitter();
|
|
83
|
+
let renderCount = 0;
|
|
84
|
+
const store = {
|
|
85
|
+
roles: [{ __is_new__: true, __index__: 'row-1', uid: 'role-uid-1', name: '' }],
|
|
86
|
+
};
|
|
87
|
+
const columns = [
|
|
88
|
+
{
|
|
89
|
+
key: 'name',
|
|
90
|
+
dataIndex: 'name',
|
|
91
|
+
render: ({ value }: any) => {
|
|
92
|
+
renderCount += 1;
|
|
93
|
+
return <span>{value || 'empty'}</span>;
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
render(
|
|
99
|
+
<SubTableField
|
|
100
|
+
columns={columns}
|
|
101
|
+
pageSize={10}
|
|
102
|
+
filterTargetKey="id"
|
|
103
|
+
fieldPathArray={['roles']}
|
|
104
|
+
formValuesChangeEmitter={emitter}
|
|
105
|
+
getCurrentValue={() => store.roles}
|
|
106
|
+
/>,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
expect(renderCount).toBe(1);
|
|
110
|
+
|
|
111
|
+
act(() => {
|
|
112
|
+
store.roles = [{ ...store.roles[0], name: 'role-uid-1' }];
|
|
113
|
+
emitter.emit('formValuesChange', {
|
|
114
|
+
source: 'user',
|
|
115
|
+
changedPaths: [['profile', 'name']],
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(renderCount).toBe(1);
|
|
120
|
+
expect(screen.getByTestId('cell-0-name')).toHaveTextContent('empty');
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -120,6 +120,8 @@ export class SubTableFieldModel extends AssociationFieldModel {
|
|
|
120
120
|
parentFieldIndex={this.context.fieldIndex}
|
|
121
121
|
parentItem={this.context.item}
|
|
122
122
|
filterTargetKey={this.collection.filterTargetKey}
|
|
123
|
+
formValuesChangeEmitter={this.context.blockModel?.emitter}
|
|
124
|
+
fieldPathArray={this.parent?.context?.fieldPathArray}
|
|
123
125
|
getCurrentValue={this.getCurrentValue}
|
|
124
126
|
/>
|
|
125
127
|
);
|
|
@@ -11,10 +11,9 @@ import { CollectionField, tExpr } from '@nocobase/flow-engine';
|
|
|
11
11
|
import { Tag } from 'antd';
|
|
12
12
|
import { castArray, get } from 'lodash';
|
|
13
13
|
import React from 'react';
|
|
14
|
-
import { EllipsisWithTooltip } from '../../components';
|
|
14
|
+
import { EllipsisWithTooltip } from '../../components/EllipsisWithTooltip';
|
|
15
15
|
import { openViewFlow } from '../../flows/openViewFlow';
|
|
16
16
|
import { FieldModel } from '../base/FieldModel';
|
|
17
|
-
import { EditFormModel } from '../blocks/form/EditFormModel';
|
|
18
17
|
|
|
19
18
|
export function transformNestedData(inputData) {
|
|
20
19
|
const resultArray = [];
|
|
@@ -36,6 +35,8 @@ export function transformNestedData(inputData) {
|
|
|
36
35
|
const hasAssociationPathName = (parent: unknown): parent is { associationPathName?: string } =>
|
|
37
36
|
!!parent && typeof parent === 'object' && 'associationPathName' in parent;
|
|
38
37
|
|
|
38
|
+
const hasUsableSourceId = (sourceId: unknown) => sourceId !== undefined && sourceId !== null && String(sourceId) !== '';
|
|
39
|
+
|
|
39
40
|
export class ClickableFieldModel extends FieldModel {
|
|
40
41
|
get collectionField(): CollectionField {
|
|
41
42
|
return this.context.collectionField;
|
|
@@ -62,14 +63,18 @@ export class ClickableFieldModel extends FieldModel {
|
|
|
62
63
|
const parentObj = associationPathName
|
|
63
64
|
? get(this.context.blockModel?.form?.getFieldsValue?.(true) || this.context.record, associationPathName)
|
|
64
65
|
: this.context.record;
|
|
66
|
+
const sourceId = parentObj?.[sourceKey];
|
|
67
|
+
const useAssociationResource = hasUsableSourceId(sourceId);
|
|
65
68
|
this.dispatchEvent(
|
|
66
69
|
'click',
|
|
67
70
|
{
|
|
68
71
|
event,
|
|
69
72
|
filterByTk,
|
|
70
|
-
collectionName:
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
collectionName: useAssociationResource
|
|
74
|
+
? this.collectionField.collection.name
|
|
75
|
+
: targetCollection?.name || this.collectionField.target,
|
|
76
|
+
associationName: useAssociationResource ? `${sourceCollection.name}.${this.collectionField.name}` : null,
|
|
77
|
+
sourceId: useAssociationResource ? sourceId : null,
|
|
73
78
|
},
|
|
74
79
|
{
|
|
75
80
|
debounce: true,
|
|
@@ -95,6 +100,10 @@ export class ClickableFieldModel extends FieldModel {
|
|
|
95
100
|
const parentObj = associationPathName.includes('.')
|
|
96
101
|
? get(this.context.record, associationPathName.split('.')[0])
|
|
97
102
|
: this.context.record;
|
|
103
|
+
const sourceId = hasUsableSourceId(parentObj?.[sourceKey])
|
|
104
|
+
? parentObj?.[sourceKey]
|
|
105
|
+
: this.context.record?.[foreignKey];
|
|
106
|
+
const useAssociationResource = hasUsableSourceId(sourceId);
|
|
98
107
|
let filterByTk = associationRecord?.[targetKey];
|
|
99
108
|
if (associationField.interface === 'm2m') {
|
|
100
109
|
// also incorrect for v1
|
|
@@ -106,10 +115,13 @@ export class ClickableFieldModel extends FieldModel {
|
|
|
106
115
|
{
|
|
107
116
|
event,
|
|
108
117
|
filterByTk,
|
|
109
|
-
collectionName:
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
118
|
+
collectionName: useAssociationResource
|
|
119
|
+
? this.collectionField.collection.name
|
|
120
|
+
: targetCollection?.name || associationField.target || this.collectionField.collection.name,
|
|
121
|
+
associationName: useAssociationResource
|
|
122
|
+
? `${associationField.collection.name}.${this.collectionField.name}`
|
|
123
|
+
: null,
|
|
124
|
+
sourceId: useAssociationResource ? sourceId : null,
|
|
113
125
|
},
|
|
114
126
|
{
|
|
115
127
|
debounce: true,
|
|
@@ -12,17 +12,26 @@ import { EditableItemModel, useFlowModelContext } from '@nocobase/flow-engine';
|
|
|
12
12
|
import React from 'react';
|
|
13
13
|
import { DateTimeFieldModel } from './DateTimeFieldModel';
|
|
14
14
|
import { MobileDatePicker } from '../mobile-components/MobileDatePicker';
|
|
15
|
+
import { useDateLimit } from './dateLimit';
|
|
15
16
|
|
|
16
17
|
export const DateOnlyPicker = (props) => {
|
|
17
18
|
const { value, format = 'YYYY-MM-DD', picker = 'date', showTime, ...rest } = props;
|
|
18
19
|
const parsedValue = value && dayjs(value).isValid() ? dayjs(value) : null;
|
|
19
20
|
const ctx = useFlowModelContext();
|
|
21
|
+
const { disabledDate, disabledTime, minDate, maxDate } = useDateLimit({
|
|
22
|
+
...props,
|
|
23
|
+
currentForm: ctx.model?.context?.form,
|
|
24
|
+
});
|
|
20
25
|
const componentProps = {
|
|
21
26
|
...rest,
|
|
22
27
|
value: parsedValue,
|
|
23
28
|
format,
|
|
24
29
|
picker,
|
|
25
30
|
showTime,
|
|
31
|
+
disabledDate,
|
|
32
|
+
disabledTime,
|
|
33
|
+
minDate,
|
|
34
|
+
maxDate,
|
|
26
35
|
onChange: (val: any) => {
|
|
27
36
|
const outputFormat = 'YYYY-MM-DD';
|
|
28
37
|
if (!val) {
|
|
@@ -12,17 +12,26 @@ import React from 'react';
|
|
|
12
12
|
import { EditableItemModel, useFlowModelContext } from '@nocobase/flow-engine';
|
|
13
13
|
import { DateTimeFieldModel } from './DateTimeFieldModel';
|
|
14
14
|
import { MobileDatePicker } from '../mobile-components/MobileDatePicker';
|
|
15
|
+
import { useDateLimit } from './dateLimit';
|
|
15
16
|
|
|
16
17
|
export const DateTimeNoTzPicker = (props) => {
|
|
17
18
|
const { value, format = 'YYYY-MM-DD HH:mm:ss', showTime, picker = 'date', onChange, ...rest } = props;
|
|
18
19
|
const parsedValue = value ? dayjs(value) : null;
|
|
19
20
|
const ctx = useFlowModelContext();
|
|
21
|
+
const { disabledDate, disabledTime, minDate, maxDate } = useDateLimit({
|
|
22
|
+
...props,
|
|
23
|
+
currentForm: ctx.model?.context?.form,
|
|
24
|
+
});
|
|
20
25
|
const componentProps = {
|
|
21
26
|
...rest,
|
|
22
27
|
value: parsedValue,
|
|
23
28
|
format,
|
|
24
29
|
picker,
|
|
25
30
|
showTime,
|
|
31
|
+
disabledDate,
|
|
32
|
+
disabledTime,
|
|
33
|
+
minDate,
|
|
34
|
+
maxDate,
|
|
26
35
|
onChange: (val: any) => {
|
|
27
36
|
if (!val) {
|
|
28
37
|
return onChange(val);
|
|
@@ -12,6 +12,7 @@ import React from 'react';
|
|
|
12
12
|
import { DateTimeFieldModel } from './DateTimeFieldModel';
|
|
13
13
|
import { MobileDatePicker } from '../mobile-components/MobileDatePicker';
|
|
14
14
|
import { DatePicker } from 'antd';
|
|
15
|
+
import { useDateLimit } from './dateLimit';
|
|
15
16
|
|
|
16
17
|
function parseToDate(value: string | Date | dayjs.Dayjs | undefined, format?: string): Date | undefined {
|
|
17
18
|
if (!value) return undefined;
|
|
@@ -49,12 +50,20 @@ function parseInitialValue(value: string | Date | undefined, format?: string): d
|
|
|
49
50
|
export const DateTimeTzPicker = (props) => {
|
|
50
51
|
const { value, format = 'YYYY-MM-DD HH:mm:ss', picker = 'date', showTime, ...rest } = props;
|
|
51
52
|
const ctx = useFlowModelContext();
|
|
53
|
+
const { disabledDate, disabledTime, minDate, maxDate } = useDateLimit({
|
|
54
|
+
...props,
|
|
55
|
+
currentForm: ctx.model?.context?.form,
|
|
56
|
+
});
|
|
52
57
|
const componentProps = {
|
|
53
58
|
...rest,
|
|
54
59
|
value: parseInitialValue(value, format),
|
|
55
60
|
format,
|
|
56
61
|
picker,
|
|
57
62
|
showTime,
|
|
63
|
+
disabledDate,
|
|
64
|
+
disabledTime,
|
|
65
|
+
minDate,
|
|
66
|
+
maxDate,
|
|
58
67
|
onChange: (val: any) => {
|
|
59
68
|
let result = parseToDate(val, format);
|
|
60
69
|
// Adjust to start of period for month/quarter/year pickers
|