From 5dc734bd6dbcc46a9ad18341eaf94093bab586a9 Mon Sep 17 00:00:00 2001 From: danstarns Date: Wed, 28 Aug 2024 17:54:56 -0700 Subject: [PATCH] feat: changeProp usage --- src/dom_components/model/Component.ts | 16 +++++---- .../data_sources/model/TraitDataVariable.ts | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/dom_components/model/Component.ts b/src/dom_components/model/Component.ts index c689e19e4..071225cd9 100644 --- a/src/dom_components/model/Component.ts +++ b/src/dom_components/model/Component.ts @@ -909,14 +909,18 @@ export default class Component extends StyleableModel { const traitDataVariableAttr: ObjectAny = {}; const traits = this.traits; traits.each((trait) => { - if (!trait.changeProp) { - const name = trait.getName(); - const value = trait.getInitValue(); - if (trait.dataVariable) { - traitDataVariableAttr[name] = trait.dataVariable; - } + const name = trait.getName(); + const value = trait.getInitValue(); + + if (trait.changeProp) { + this.set(name, value); + } else { if (name && value) attrs[name] = value; } + + if (trait.dataVariable) { + traitDataVariableAttr[name] = trait.dataVariable; + } }); traits.length && this.set('attributes', attrs); Object.keys(traitDataVariableAttr).length && this.set('attributes-data-variable', traitDataVariableAttr); diff --git a/test/specs/data_sources/model/TraitDataVariable.ts b/test/specs/data_sources/model/TraitDataVariable.ts index fdbecb485..988bfbacb 100644 --- a/test/specs/data_sources/model/TraitDataVariable.ts +++ b/test/specs/data_sources/model/TraitDataVariable.ts @@ -287,4 +287,40 @@ describe('TraitDataVariable', () => { expect(cmp?.getAttributes().href).toBe('url-to-dog-image'); }); }); + + describe('changeProp', () => { + test('component initializes and updates data-variable value using changeProp', () => { + const inputDataSource: DataSourceProps = { + id: 'test-change-prop-datasource', + records: [{ id: 'id1', value: 'I love grapes' }], + }; + dsm.add(inputDataSource); + + const cmp = cmpRoot.append({ + tagName: 'div', + type: 'default', + traits: [ + { + name: 'test-change-prop', + type: 'text', + changeProp: true, + value: { + type: DataVariableType, + value: 'default', + path: `${inputDataSource.id}.id1.value`, + }, + }, + ], + })[0]; + + let property = cmp.get('test-change-prop'); + expect(property).toBe('I love grapes'); + + const testDs = dsm.get(inputDataSource.id); + testDs.getRecord('id1')?.set({ value: 'I really love grapes' }); + + property = cmp.get('test-change-prop'); + expect(property).toBe('I really love grapes'); + }); + }); });