-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add element decorator to handle initializing attribute values
- v4.1.0
- v4.0.1
- v4.0.1-next.0
- v4.0.0
- v4.0.0-next.52
- v4.0.0-next.51
- v4.0.0-next.50
- v4.0.0-next.49
- v4.0.0-next.48
- v4.0.0-next.47
- v4.0.0-next.46
- v4.0.0-next.45
- v4.0.0-next.43
- v4.0.0-next.42
- v4.0.0-next.41
- v4.0.0-next.40
- v4.0.0-next.39
- v4.0.0-next.38
- v4.0.0-next.37
- v4.0.0-next.36
- v4.0.0-next.35
- v4.0.0-next.33
- v4.0.0-next.32
- v4.0.0-next.31
- v4.0.0-next.30
- v4.0.0-next.29
- v4.0.0-next.28
- v4.0.0-next.27
- v4.0.0-next.26
- v4.0.0-next.25
- v4.0.0-next.24
- v4.0.0-next.23
- v4.0.0-next.22
- v4.0.0-next.21
- v4.0.0-next.20
- v4.0.0-next.19
- v4.0.0-next.18
- v4.0.0-next.17
- v4.0.0-next.16
- v4.0.0-next.15
- v4.0.0-next.14
- v4.0.0-next.13
- v4.0.0-next.12
- v4.0.0-next.11
- v4.0.0-next.10
- v4.0.0-next.9
- v4.0.0-next.8
- v4.0.0-next.7
- v4.0.0-next.6
- v4.0.0-next.5
- v4.0.0-next.4
- v4.0.0-next.3
- v4.0.0-next.2
- v4.0.0-next.1
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.0
- v3.6.0
- v3.5.0
- v3.4.0
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.3
- v3.1.3-next.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.8-next.1
- v3.0.8-next.0
- rm
deebloo
committed
Feb 25, 2024
1 parent
620f6aa
commit 398a00e
Showing
4 changed files
with
81 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect, fixture, html } from '@open-wc/testing'; | ||
|
||
import { attr } from './attr.js'; | ||
import { element } from './element.js'; | ||
import { tagName } from './tag-name.js'; | ||
|
||
describe('element()', () => { | ||
it('should write default value to attribute', async () => { | ||
@element | ||
class MyElement extends HTMLElement { | ||
@tagName static tag = 'element-1'; | ||
|
||
@attr accessor value1 = 'hello'; // no attribute | ||
@attr accessor value2 = 0; // number | ||
@attr accessor value3 = true; // boolean | ||
} | ||
|
||
const el = await fixture<MyElement>(html`<element-1></element-1>`); | ||
|
||
expect(el.getAttribute('value1')).to.equal('hello'); | ||
expect(el.getAttribute('value2')).to.equal('0'); | ||
expect(el.getAttribute('value3')).to.equal(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export class ElementMetadata { | ||
attrs: string[] = []; | ||
} | ||
|
||
export interface ElementCtx { | ||
metadata: { | ||
el: ElementMetadata; | ||
}; | ||
} | ||
|
||
export function element<T extends new (...args: any[]) => HTMLElement>( | ||
Base: T, | ||
ctx: ClassDecoratorContext<T> | ||
) { | ||
const { metadata } = ctx as unknown as ElementCtx; | ||
|
||
return class JoistElement extends Base { | ||
// make all attrs observable | ||
static observedAttributes = [...metadata.el.attrs]; | ||
|
||
constructor(...args: any[]) { | ||
super(...args); | ||
} | ||
|
||
connectedCallback() { | ||
for (let attr of metadata.el.attrs) { | ||
const value = Reflect.get(this, attr); | ||
|
||
// reflect values back to attributes | ||
if (value !== null && value !== undefined && value !== '') { | ||
if (typeof value === 'boolean') { | ||
if (value === true) { | ||
// set boolean attribute | ||
this.setAttribute(attr, ''); | ||
} | ||
} else { | ||
// set key/value attribute | ||
this.setAttribute(attr, String(value)); | ||
} | ||
} | ||
} | ||
|
||
if (super.connectedCallback) { | ||
super.connectedCallback(); | ||
} | ||
} | ||
}; | ||
} |