Skip to content

Commit dbc909e

Browse files
committed
add reflect as a property config
1 parent 636c92a commit dbc909e

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"description": "Base Custom Webcomponent",
33
"name": "@node-projects/base-custom-webcomponent",
4-
"version": "0.25.4",
4+
"version": "0.25.5",
55
"type": "module",
66
"main": "./dist/index.js",
77
"author": "",

sample/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ <h1>DSD Demo</h1>
3737
</my-element>
3838
<br><br>
3939
<h1>DCE Demo</h1>
40-
<node-projects-dce name="simple-dce-demo" enable-bindings properties='{"list":"Array"}' >
40+
<node-projects-dce name="simple-dce-demo" enable-bindings properties='{"list":"Array","ctx":{"type":"String","reflect":true}}' >
4141
<template>
4242
<style>h1 {color: blue}</style>
4343
<h1>Hello World</h1>
44+
<div>[[this.ctx]]</div>
4445
<template repeat:myitem="[[this.list]]">
4546
<button>[[myitem]] - [[index]]</button>
4647
</template>
4748
</template>
4849
</node-projects-dce>
4950

50-
<simple-dce-demo list='["aa","bb"]'>
51-
51+
<simple-dce-demo list='["aa","bb"]' ctx="test">
5252
</simple-dce-demo>
5353
</body>
5454

src/BaseCustomWebComponent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const cssFromString = function (value: string | CSSStyleSheet | any): CSS
4444
};
4545

4646
type propertySimpleDefinition = Object | BooleanConstructor | DateConstructor | NumberConstructor | StringConstructor | ArrayConstructor | ObjectConstructor //| Object //| (new (...args: any[]) => object)
47-
type propertyComplexDefinition = { type: propertySimpleDefinition; };
47+
type propertyComplexDefinition = { type: propertySimpleDefinition; reflect?: boolean, attribute?: string, noattribute?: boolean, default?: any };
4848
type propertyDefinition = propertyComplexDefinition | propertySimpleDefinition;
4949

5050
// decorators

src/DeclaritiveBaseCustomWebcomponent.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttached
4242
if (this.properties[0] === '{') {
4343
const obj = JSON.parse(this.properties);
4444
for (let i in obj) {
45-
props[i] = window[obj[i]];
45+
if (typeof obj[i] === 'string')
46+
props[i] = window[obj[i]];
47+
else {
48+
props[i] = obj[i];
49+
props[i].type = obj[i].type ? window[obj[i].type] : String;
50+
}
4651
}
4752
} else {
4853
props = this.properties.split(/[\s,;]+/).reduce((a, v) => ({ ...a, [v]: String }), {});
@@ -70,6 +75,20 @@ class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttached
7075
set(newValue) {
7176
if (this['_' + p] !== newValue) {
7277
this['_' + p] = newValue;
78+
79+
if (props[p].reflect) {
80+
if (props[p].type === Boolean) {
81+
if (newValue === true)
82+
this.setAttribute(props[p].attribute ?? camelToDashCase(p), '');
83+
else
84+
this.removeAttribute(props[p].attribute ?? camelToDashCase(p));
85+
} else {
86+
if (props[p].type === Object || props[p].type === Array)
87+
this.setAttribute(props[p].attribute ?? camelToDashCase(p), JSON.stringify(newValue));
88+
else
89+
this.setAttribute(props[p].attribute ?? camelToDashCase(p), newValue);
90+
}
91+
}
7392
//@ts-ignore
7493
if (this.constructor._enableBindings)
7594
this._bindingsRefresh(p);
@@ -98,4 +117,22 @@ class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttached
98117
}
99118
}
100119

101-
customElements.define("node-projects-dce", DeclaritiveBaseCustomWebcomponent);
120+
customElements.define("node-projects-dce", DeclaritiveBaseCustomWebcomponent);
121+
122+
/*
123+
<node-projects-dce name="simple-dce-demo" properties='{"list":"Array", "list2":"Array"}' enable-bindings >
124+
<template>
125+
<style>h1 {color: red}</style>
126+
<h1>Hello World</h1>
127+
<template repeat:myitem="[[this.list]]">
128+
<button>[[myitem.toUpperCase()]] - <b>[[myitem.toLowerCase()]]</b> - [[index]]</button>
129+
<ul>
130+
<template repeat:myitem2="[[this.list2]]" repeat-index="inneridx">
131+
<button>[[myitem.toUpperCase()]] - <b>[[myitem2.toLowerCase()]]</b> - [[inneridx * 100]]</button>
132+
</template>
133+
</ul>
134+
</template>
135+
</template>
136+
</node-projects-dce>
137+
<simple-dce-demo list='["aa","bb","cc"]' list2='["hello", "you"]' style="position:absolute;left:184px;top:-53px;"></simple-dce-demo>
138+
*/

0 commit comments

Comments
 (0)