feat: field suport change JSExpression value

This commit is contained in:
liujuping 2023-04-03 17:15:22 +08:00 committed by 林熠
parent 4f7a0b984c
commit e1864fdf71
2 changed files with 70 additions and 24 deletions

View File

@ -16,7 +16,7 @@ import type {
import { Transducer } from './utils';
import { ISettingPropEntry, SettingPropEntry } from './setting-prop-entry';
import { computed, obx, makeObservable, action, untracked, intl } from '@alilc/lowcode-editor-core';
import { cloneDeep, isCustomView, isDynamicSetter } from '@alilc/lowcode-utils';
import { cloneDeep, isCustomView, isDynamicSetter, isJSExpression } from '@alilc/lowcode-utils';
import { ISettingTopEntry } from './setting-top-entry';
import { IComponentMeta, INode } from '@alilc/lowcode-designer';
@ -38,28 +38,28 @@ export interface ISettingField extends ISettingPropEntry, Omit<IBaseModelSetting
IComponentMeta,
INode
>, 'setValue' | 'key' | 'node'> {
get items(): Array<ISettingField | IPublicTypeCustomView>;
get title(): string | ReactNode | undefined;
readonly isSettingField: true;
purge(): void;
extraProps: IPublicTypeFieldExtraProps;
get setter(): IPublicTypeSetterType | null;
get expanded(): boolean;
readonly isRequired: boolean;
readonly isGroup: boolean;
extraProps: IPublicTypeFieldExtraProps;
get items(): Array<ISettingField | IPublicTypeCustomView>;
get title(): string | ReactNode | undefined;
get setter(): IPublicTypeSetterType | null;
get expanded(): boolean;
get valueState(): number;
setExpanded(value: boolean): void;
purge(): void;
setValue(
val: any,
isHotValue?: boolean,
@ -271,16 +271,29 @@ export class SettingField extends SettingPropEntry implements ISettingField {
}
if (this.isUseVariable()) {
const oldValue = this.getValue();
this.setValue(
{
type: 'JSExpression',
value: oldValue.value,
mock: value,
},
false,
false,
options,
);
if (isJSExpression(value)) {
this.setValue(
{
type: 'JSExpression',
value: value.value,
mock: oldValue.mock,
},
false,
false,
options,
);
} else {
this.setValue(
{
type: 'JSExpression',
value: oldValue.value,
mock: value,
},
false,
false,
options,
);
}
} else {
this.setValue(value, false, false, options);
}
@ -297,7 +310,6 @@ export class SettingField extends SettingPropEntry implements ISettingField {
return this.designer!.autorun(action, true);
}
internalToShellField() {
return this.designer!.shellModelFactory.createSettingField(this);
}

View File

@ -168,6 +168,40 @@ describe('setting-field 测试', () => {
expect(arrField.getHotValue()).toEqual([undefined, {name: '2'}, {name: '3'}]);
});
it('js expression setValue / setHotValue', () => {
const settingEntry = mockNode.settingEntry;
const field = settingEntry.get('behavior');
const subField = field.createField({
name: 'sub',
title: 'sub',
});
subField.setValue({
type: 'JSExpression',
value: 'state.a',
mock: 'haha',
});
subField.setHotValue({
type: 'JSExpression',
value: 'state.b',
});
expect(subField.getValue()).toEqual({
type: 'JSExpression',
value: 'state.b',
mock: 'haha',
});
subField.setHotValue('mock02');
expect(subField.getValue()).toEqual({
type: 'JSExpression',
value: 'state.b',
mock: 'mock02',
});
});
it('onEffect', async () => {
const settingEntry = mockNode.settingEntry as SettingTopEntry;
const field = settingEntry.get('behavior');