fix: updating a non-existing property will add a new property to the component

This commit is contained in:
xzdry 2022-01-10 13:49:34 +08:00
parent d3d0030f96
commit f63c105786
3 changed files with 12 additions and 3 deletions

View File

@ -33,12 +33,17 @@ describe('update component property', () => {
const origin = appModel.toSchema();
const text1 = appModel.getComponentById('text1' as any);
text1!.updateComponentProperty('value', { raw: 'hello', format: 'md' });
text1!.updateComponentProperty('newProperty', "a property that didn't exist before");
const newSchema = appModel.toSchema();
it('update component properties', () => {
expect(newSchema[5].properties.value).toEqual({ raw: 'hello', format: 'md' });
});
it("update a new property that component don't have",()=>{
expect(newSchema[5].properties.newProperty).toEqual("a property that didn't exist before");
})
it('keep immutable after updating component properties', () => {
expect(origin).not.toBe(newSchema);
expect(origin[0]).toBe(newSchema[0]);

View File

@ -146,7 +146,11 @@ export class ComponentModel implements IComponentModel {
}
updateComponentProperty(propertyName: string, value: any) {
this.properties[propertyName].update(value);
if (!Reflect.has(this.properties, propertyName)) {
this.properties[propertyName] = new FieldModel(value)
} else {
this.properties[propertyName].update(value);
}
this._isDirty = true;
}

View File

@ -14,8 +14,8 @@ export class ModifyComponentPropertiesLeafOperation extends BaseLeafOperation<Mo
const appModel = new AppModel(prev);
const component = appModel.getComponentById(this.context.componentId as ComponentId);
if (component) {
for (const property in component.properties) {
const oldValue = component.properties[property].value;
for (const property in this.context.properties) {
const oldValue = component.properties[property]?.value;
// assign previous data
this.previousState[property] = oldValue;
let newValue = this.context.properties[property];