Skip to content

Commit 23e23c9

Browse files
authored
Fix selecting func/args at RHS at delta 1 (#1211)
1 parent 2bae9eb commit 23e23c9

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

packages/core/modules/stores/tree.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ const setFuncValue = (config, state, path, delta, parentFuncs, argKey, argValue,
450450
if (funcK !== targetFV.get("func")) {
451451
const funcPath = funcsPath.map(([f, a]) => `${f}(${a})`).join("/") || "root";
452452
throw new Error(
453-
`In ${isLHS ? "LHS" : "RHS"} for path ${funcPath} expected func key ${funcK} but got ${parent.get("func")}`
453+
`In ${isLHS ? "LHS" : "RHS"} for path ${funcPath} expected func key ${funcK} but got ${targetFV.get("func")}`
454454
);
455455
}
456456
targetFV = targetFV.getIn(["args", argK, "value"]);

packages/core/modules/utils/getNewValueForFieldOp.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from "./configUtils";
55
import { getOpCardinality, getFirstDefined } from "./stuff";
66
import { translateValidation } from "../i18n";
7+
import { jsToImmutable } from "./treeUtils";
78

89
/**
910
* @param {Immutable.Map} current
@@ -239,7 +240,7 @@ export const getNewValueForFieldOp = function (
239240
firstWidgetConfig?.defaultValue
240241
]);
241242
valueFixes[i] = dv;
242-
if (dv?.func) {
243+
if (dv?.func || dv?.get?.("func")) {
243244
valueSrcFixes[i] = "func";
244245
//tip: defaultValue of src "field" is not supported, todo
245246
}
@@ -251,6 +252,7 @@ export const getNewValueForFieldOp = function (
251252
for (let i = 0 ; i < operatorCardinality ; i++) {
252253
let vs = canReuseValue && currentValueSrc.get(i) || null;
253254
let vt = canReuseValue && currentValueType.get(i) || null;
255+
const v = valueFixes[i] !== undefined ? valueFixes[i] : (canReuseValue ? currentValue.get(i) : undefined);
254256
if (canReuseValue && canExtendValueToRange && i === 1) {
255257
vs = valueSrcFixes[i] ?? currentValueSrc.get(0);
256258
vt = valueTypeFixes[i] ?? currentValueType.get(0);
@@ -264,6 +266,11 @@ export const getNewValueForFieldOp = function (
264266
if (!vt) {
265267
valueTypeFixes[i] = defaultValueType;
266268
}
269+
// Fix if func in LHS has `defaultValue: { func: ..., args: {...} }`
270+
if (v?.func) {
271+
valueFixes[i] = jsToImmutable(v);
272+
valueSrcFixes[i] = "func";
273+
}
267274
}
268275

269276
// build new values

packages/ui/modules/components/rule/FuncWidget.jsx

+12-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class FuncWidget extends Component {
2525
setFuncValue: PropTypes.func,
2626
readonly: PropTypes.bool,
2727
parentFuncs: PropTypes.array,
28+
parentDelta: PropTypes.number,
2829
fieldDefinition: PropTypes.object,
2930
isFuncArg: PropTypes.bool,
3031
isLHS: PropTypes.bool,
@@ -60,35 +61,35 @@ export default class FuncWidget extends Component {
6061
}
6162

6263
setFunc = (funcKey, _meta = {}) => {
63-
const { isLHS, delta, parentFuncs, id } = this.props;
64+
const { isLHS, parentDelta, parentFuncs, id } = this.props;
6465
if (!_meta.widgetId) {
65-
const widgetId = getWidgetId({ id, isLHS, delta, parentFuncs });
66+
const widgetId = getWidgetId({ id, isLHS, delta: parentDelta, parentFuncs });
6667
_meta.widgetId = widgetId;
6768
}
6869

6970
this.props.setFuncValue(
70-
isLHS ? -1 : (delta || 0), parentFuncs, null, funcKey, "!func", undefined, _meta
71+
isLHS ? -1 : (parentDelta || 0), parentFuncs, null, funcKey, "!func", undefined, _meta
7172
);
7273
};
7374

7475
setArgValue = (argKey, argVal, widgetType, asyncListValues, _meta) => {
75-
const {config, delta, isLHS, parentFuncs} = this.props;
76+
const {config, parentDelta, isLHS, parentFuncs} = this.props;
7677

7778
this.props.setFuncValue(
78-
isLHS ? -1 : (delta || 0), parentFuncs, argKey, argVal, widgetType, asyncListValues, _meta
79+
isLHS ? -1 : (parentDelta || 0), parentFuncs, argKey, argVal, widgetType, asyncListValues, _meta
7980
);
8081
};
8182

8283
setArgValueSrc = (argKey, argValSrc, _meta) => {
83-
const {config, delta, isLHS, parentFuncs} = this.props;
84+
const {config, parentDelta, isLHS, parentFuncs} = this.props;
8485

8586
this.props.setFuncValue(
86-
isLHS ? -1 : (delta || 0), parentFuncs, argKey, argValSrc, "!valueSrc", undefined, _meta
87+
isLHS ? -1 : (parentDelta || 0), parentFuncs, argKey, argValSrc, "!valueSrc", undefined, _meta
8788
);
8889
};
8990

9091
renderFuncSelect = () => {
91-
const {config, field, fieldType, fieldSrc, isLHS, operator, customProps, value, readonly, parentFuncs, id, groupId, isFuncArg, fieldDefinition} = this.props;
92+
const {config, field, fieldType, fieldSrc, isLHS, operator, customProps, value, readonly, parentFuncs, id, groupId, isFuncArg, fieldDefinition, parentDelta} = this.props;
9293
const funcKey = value?.get?.("func") ?? null;
9394
const selectProps = {
9495
value: funcKey,
@@ -139,7 +140,7 @@ export default class FuncWidget extends Component {
139140
renderArgVal = (funcKey, argKey, argDefinition) => {
140141
const {
141142
config, field, fieldType, fieldSrc, isLHS, operator, value, readonly, parentFuncs, id, groupId,
142-
fieldError, valueError, setFuncValue,
143+
fieldError, valueError, setFuncValue, parentDelta,
143144
} = this.props;
144145
const arg = value ? value.getIn(["args", argKey]) : null;
145146
const argVal = arg ? arg.get("value") : undefined;
@@ -171,6 +172,7 @@ export default class FuncWidget extends Component {
171172
parentFuncs,
172173
id,
173174
groupId,
175+
parentDelta,
174176
};
175177
//tip: value & valueSrc will be converted to Immutable.List at <Widget>
176178

@@ -299,6 +301,7 @@ class ArgWidget extends Component {
299301
return (
300302
<Widget
301303
{...this.props}
304+
parentDelta={this.props.parentDelta}
302305
setValue={this.setValue}
303306
setValueSrc={this.setValueSrc}
304307
isFuncArg={true}

packages/ui/modules/components/rule/Widget.jsx

+9-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class Widget extends Component {
4747
// for func in func
4848
parentFuncs: PropTypes.array,
4949
isLHS: PropTypes.bool,
50+
parentDelta: PropTypes.number,
5051
// for case_value
5152
isCaseValue: PropTypes.bool,
5253
};
@@ -64,7 +65,7 @@ export default class Widget extends Component {
6465
const keysForMeta = [
6566
"config", "id", "parentFuncs",
6667
"field", "fieldId", "fieldSrc", "fieldType", "fieldFunc", "fieldArg", "leftField", "operator", "valueSrc", "asyncListValues",
67-
"isLHS", "isFuncArg", "isForRuleGroup", "isCaseValue", "value",
68+
"isLHS", "isFuncArg", "isForRuleGroup", "isCaseValue", "value", "parentDelta",
6869
];
6970
const changedKeys = keysForMeta
7071
.filter(k => {
@@ -124,7 +125,7 @@ export default class Widget extends Component {
124125

125126
getMeta({
126127
config, field: simpleField, fieldSrc, fieldType, fieldFunc, fieldArg, operator, valueSrc: valueSrcs, value: values,
127-
isForRuleGroup, isCaseValue, isFuncArg, leftField, asyncListValues, parentFuncs, isLHS, id,
128+
isForRuleGroup, isCaseValue, isFuncArg, leftField, asyncListValues, parentFuncs, isLHS, id, parentDelta,
128129
}, changedKeys = []) {
129130
const {valueSourcesInfo} = config.settings;
130131
const field = isFuncArg ? {func: fieldFunc, arg: fieldArg} : simpleField;
@@ -178,7 +179,7 @@ export default class Widget extends Component {
178179
label: valueSourcesInfo[srcKey]?.label ?? defaultValueSourcesLabels[srcKey] ?? srcKey,
179180
}]);
180181
}
181-
const widgets = range(0, cardinality).map(delta => {
182+
const widgets = (isFuncArg ? [0] : range(0, cardinality)).map(delta => {
182183
const oldWidgetMeta = this.meta?.widgets?.[delta];
183184
const valueSrc = iValueSrcs?.get(delta) || null;
184185
let widget = getWidgetForFieldOp(config, field, operator, valueSrc);
@@ -210,7 +211,7 @@ export default class Widget extends Component {
210211
textSeparators = operatorDefinition?.textSeparators;
211212
}
212213

213-
const widgetId = getWidgetId({ id, isLHS, delta, parentFuncs });
214+
const widgetId = getWidgetId({ id, isLHS, delta: parentDelta ?? delta, parentFuncs });
214215
const vsId = widgetId + ":" + "VS";
215216

216217
let setValueSrc = oldWidgetMeta?.setValueSrc;
@@ -260,7 +261,7 @@ export default class Widget extends Component {
260261
renderWidget = (delta, meta, props) => {
261262
const {
262263
config, isFuncArg, leftField, operator, value: values, valueError, fieldError,
263-
readonly, parentField, parentFuncs, id, groupId, fieldSrc, fieldType, isLHS, setFuncValue,
264+
readonly, parentField, parentFuncs, id, groupId, fieldSrc, fieldType, isLHS, setFuncValue, parentDelta,
264265
} = props;
265266
const {settings} = config;
266267
const { widgets, iValues, aField, valueSources } = meta;
@@ -301,6 +302,7 @@ export default class Widget extends Component {
301302
fieldType={fieldType}
302303
parentField={parentField}
303304
parentFuncs={parentFuncs}
305+
parentDelta={parentDelta ?? delta}
304306
operator={operator}
305307
readonly={readonly}
306308
/>
@@ -373,6 +375,7 @@ export default class Widget extends Component {
373375
render() {
374376
if (!this.meta) return null;
375377
const { defaultWidget, cardinality } = this.meta;
378+
const { isFuncArg } = this.meta;
376379
if (!defaultWidget) return null;
377380
const name = defaultWidget;
378381

@@ -381,7 +384,7 @@ export default class Widget extends Component {
381384
className={`rule--widget rule--widget--${name.toUpperCase()}`}
382385
key={"widget-col-"+name}
383386
>
384-
{range(0, cardinality).map(this.renderWidgetDelta)}
387+
{(isFuncArg ? [0] : range(0, cardinality)).map(this.renderWidgetDelta)}
385388
</Col>
386389
);
387390
}

packages/ui/modules/components/rule/WidgetFactory.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const WidgetFactoryMemo = memo(({
3636
WidgetFactoryMemo.displayName = "WidgetFactoryMemo";
3737

3838
const WidgetFactory = ({
39-
delta, isFuncArg, valueSrc,
39+
delta, parentDelta, isFuncArg, valueSrc,
4040
value: immValue, valueError: immValueError, fieldError, asyncListValues,
4141
isSpecialRange, fieldDefinition,
4242
widget, widgetDefinition, widgetValueLabel, valueLabels, textSeparators, setValue, setFuncValue,
@@ -85,6 +85,7 @@ const WidgetFactory = ({
8585
fieldDefinition,
8686
operator,
8787
delta,
88+
parentDelta,
8889
isSpecialRange,
8990
isFuncArg,
9091
value,

0 commit comments

Comments
 (0)