refactor: only pass setter to handler; initializeProps on attribute changes
This commit is contained in:
parent
f8ca80da72
commit
341424187b
1 changed files with 21 additions and 6 deletions
|
@ -67,10 +67,7 @@ export class WebComponent extends HTMLElement {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
if (!this.#props) {
|
this.#initializeProps();
|
||||||
const { props, ...clone } = this;
|
|
||||||
this.#props = new Proxy(clone, this.#handler(this));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,6 +131,7 @@ export class WebComponent extends HTMLElement {
|
||||||
if (previousValue !== currentValue) {
|
if (previousValue !== currentValue) {
|
||||||
this[property] = currentValue === "" || currentValue;
|
this[property] = currentValue === "" || currentValue;
|
||||||
this[camelCaps] = this[property]; // remove on v2
|
this[camelCaps] = this[property]; // remove on v2
|
||||||
|
this.#initializeProps();
|
||||||
this.props[camelCaps] = this[property];
|
this.props[camelCaps] = this[property];
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
|
@ -150,13 +148,23 @@ export class WebComponent extends HTMLElement {
|
||||||
return kebab.replace(/-./g, (x) => x[1].toUpperCase());
|
return kebab.replace(/-./g, (x) => x[1].toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
#handler = (el) => ({
|
/**
|
||||||
|
* Proxy handler for observed attribute - property counterpart
|
||||||
|
* @param {(qualifiedName: string, value: string) => void} setter
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
#handler = (setter) => ({
|
||||||
set(obj, prop, newval) {
|
set(obj, prop, newval) {
|
||||||
const oldval = obj[prop];
|
const oldval = obj[prop];
|
||||||
console.log(">>> old value:", oldval, typeof oldval);
|
console.log(">>> old value:", oldval, typeof oldval);
|
||||||
|
|
||||||
obj[prop] = newval;
|
obj[prop] = newval;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts camelCaps string into kebab-case
|
||||||
|
* @param {string} str
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
const kebabize = (str) =>
|
const kebabize = (str) =>
|
||||||
str.replace(
|
str.replace(
|
||||||
/[A-Z]+(?![a-z])|[A-Z]/g,
|
/[A-Z]+(?![a-z])|[A-Z]/g,
|
||||||
|
@ -166,12 +174,19 @@ export class WebComponent extends HTMLElement {
|
||||||
if (JSON.stringify(oldval) !== newval) {
|
if (JSON.stringify(oldval) !== newval) {
|
||||||
console.log(oldval, newval);
|
console.log(oldval, newval);
|
||||||
const kebab = kebabize(prop);
|
const kebab = kebabize(prop);
|
||||||
el.setAttribute(kebab, newval);
|
setter(kebab, newval);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
#initializeProps() {
|
||||||
|
if (!this.#props) {
|
||||||
|
const { ...clone } = this;
|
||||||
|
this.#props = new Proxy(clone, this.#handler(this.setAttribute));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default WebComponent; // remove on v2
|
export default WebComponent; // remove on v2
|
||||||
|
|
Loading…
Reference in a new issue