feat: support boolean attribute
if no value is passed to an attribute it returns 'true'
This commit is contained in:
parent
670c882e44
commit
bef423fde1
3 changed files with 23 additions and 7 deletions
17
demo/BooleanPropTest.mjs
Normal file
17
demo/BooleanPropTest.mjs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import WebComponent from "../src/WebComponent.js";
|
||||||
|
|
||||||
|
export class BooleanPropTest extends WebComponent {
|
||||||
|
isInline = false;
|
||||||
|
|
||||||
|
static properties = ["is-inline"];
|
||||||
|
|
||||||
|
onChanges(changes) {
|
||||||
|
console.log(">>> boolean prop test", changes);
|
||||||
|
}
|
||||||
|
|
||||||
|
get template() {
|
||||||
|
return `<span>${this.isInline}</span>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("boolean-prop-test", BooleanPropTest);
|
|
@ -6,23 +6,22 @@
|
||||||
<title>WC demo</title>
|
<title>WC demo</title>
|
||||||
<script type="module" src="HelloWorld.mjs"></script>
|
<script type="module" src="HelloWorld.mjs"></script>
|
||||||
<script type="module" src="SimpleText.mjs"></script>
|
<script type="module" src="SimpleText.mjs"></script>
|
||||||
|
<script type="module" src="BooleanPropTest.mjs"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<hello-world id="hey" data-name="Ayo" emotion="sad"></hello-world>
|
<hello-world id="hey" data-name="Ayo" emotion="sad"></hello-world>
|
||||||
<p>
|
<p>
|
||||||
<simple-text greeting="hey"></simple-text>
|
<simple-text greeting="hey"></simple-text>
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<boolean-prop-test></boolean-prop-test>
|
||||||
|
</p>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
const helloWorld = document.querySelector("hello-world");
|
const helloWorld = document.querySelector("hello-world");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
helloWorld.setAttribute("emotion", "excited");
|
helloWorld.setAttribute("emotion", "excited");
|
||||||
}, 2500);
|
}, 2500);
|
||||||
|
|
||||||
const clickableText = document.querySelector("simple-text");
|
|
||||||
setTimeout(() => {
|
|
||||||
clickableText.remove();
|
|
||||||
}, 2000);
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -90,8 +90,8 @@ export class WebComponent extends HTMLElement {
|
||||||
attributeChangedCallback(property, previousValue, currentValue) {
|
attributeChangedCallback(property, previousValue, currentValue) {
|
||||||
const camelCaps = this.#getCamelCaps(property);
|
const camelCaps = this.#getCamelCaps(property);
|
||||||
if (previousValue !== currentValue) {
|
if (previousValue !== currentValue) {
|
||||||
this[property] = currentValue;
|
this[property] = currentValue === "" || currentValue;
|
||||||
this[camelCaps] = currentValue;
|
this[camelCaps] = currentValue === "" || currentValue;
|
||||||
this.render();
|
this.render();
|
||||||
this.onChanges({ property, previousValue, currentValue });
|
this.onChanges({ property, previousValue, currentValue });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue