diff --git a/packages/designer/src/designer/setting/setting-field.ts b/packages/designer/src/designer/setting/setting-field.ts index 139373c0b..1a63fb7a4 100644 --- a/packages/designer/src/designer/setting/setting-field.ts +++ b/packages/designer/src/designer/setting/setting-field.ts @@ -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, 'setValue' | 'key' | 'node'> { - get items(): Array; - - get title(): string | ReactNode | undefined; - readonly isSettingField: true; - purge(): void; + readonly isRequired: boolean; + + readonly isGroup: boolean; extraProps: IPublicTypeFieldExtraProps; - get setter(): IPublicTypeSetterType | null; + get items(): Array; - get expanded(): boolean; + get title(): string | ReactNode | undefined; - readonly isRequired: boolean; + get setter(): IPublicTypeSetterType | null; - readonly isGroup: boolean; + 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); } diff --git a/packages/designer/tests/designer/setting/setting-field.test.ts b/packages/designer/tests/designer/setting/setting-field.test.ts index f79bc9876..53ed2829d 100644 --- a/packages/designer/tests/designer/setting/setting-field.test.ts +++ b/packages/designer/tests/designer/setting/setting-field.test.ts @@ -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');