Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ export function createElementType<T>(
this.__releaseCallbacks = [];
this.__propertyChangedCallbacks = [];
this.__updating = {};
this.props = {};
this.props = initializeProps(this as any, propDefinition);
}

connectedCallback() {
if (this.__initialized) return;
this.__releaseCallbacks = [];
this.__propertyChangedCallbacks = [];
this.__updating = {};
this.props = initializeProps(this as any, propDefinition);
const props = propValues<T>(this.props as PropsDefinition<T>),
ComponentType = this.Component as
| Function
Expand Down Expand Up @@ -89,7 +88,6 @@ export function createElementType<T>(
}

attributeChangedCallback(name: string, oldVal: string, newVal: string) {
if (!this.__initialized) return;
if (this.__updating[name]) return;
name = this.lookupProp(name)!;
if (name in propDefinition) {
Expand Down
8 changes: 1 addition & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ export function initializeProps<T>(
const props = cloneProps(propDefinition),
propKeys = Object.keys(propDefinition) as Array<keyof PropsDefinition<T>>;
propKeys.forEach((key) => {
const prop = props[key],
attr = element.getAttribute(prop.attribute),
value = element[key];
if (attr != null) prop.value = prop.parse ? parseAttributeValue(attr) : attr;
if (value != null)
prop.value = Array.isArray(value) ? value.slice(0) : value;
prop.reflect && reflect(element, prop.attribute, prop.value, !!prop.parse);
const prop = props[key]
Object.defineProperty(element, key, {
get() {
return prop.value;
Expand Down
6 changes: 5 additions & 1 deletion test/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ describe('Creating a Custom Element', () => {
expect(TestElem).toBeDefined();
});

it('should upgrade element connected to DOM', () => {
it('should initialized props in new element', () => {
elem = new TestElem();
expect('name' in elem).toBe(true)
});

it('should upgrade element connected to DOM', () => {
document.body.append(elem);
expect(elem.renderRoot.innerHTML).toBe(FIXTURES[0]);
});
Expand Down