Skip to content

Commit 390021e

Browse files
authored
refactor: Do not manually call connectedCallback (#103)
* refactor: Do not manually call connectedCallback * test: Copy test case from #71
1 parent 9ba57bd commit 390021e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { h, cloneElement, render, hydrate } from 'preact';
44
* @typedef {import('./index.d.ts').PreactCustomElement} PreactCustomElement
55
*/
66

7-
87
/**
98
* @type {import('./index.d.ts').default}
109
*/
@@ -48,15 +47,16 @@ export default function register(Component, tagName, propNames, options) {
4847
propNames.forEach((name) => {
4948
Object.defineProperty(PreactElement.prototype, name, {
5049
get() {
51-
return this._vdom.props[name];
50+
return this._vdom
51+
? this._vdom.props[name]
52+
: this._props[name];
5253
},
5354
set(v) {
5455
if (this._vdom) {
5556
this.attributeChangedCallback(name, null, v);
5657
} else {
5758
if (!this._props) this._props = {};
5859
this._props[name] = v;
59-
this.connectedCallback();
6060
}
6161

6262
// Reflect property changes to attributes if the value is a primitive

src/index.test.jsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,33 @@ describe('web components', () => {
130130
});
131131
assert.equal(other, 1);
132132
});
133+
134+
it('sets complex property after other property', () => {
135+
const el = document.createElement('x-dummy-button');
136+
137+
// set simple property first
138+
el.text = 'click me';
139+
140+
let clicks = 0;
141+
const onClick = () => clicks++;
142+
el.onClick = onClick;
143+
144+
assert.equal(el.text, 'click me');
145+
assert.equal(el.onClick, onClick);
146+
147+
root.appendChild(el);
148+
assert.equal(
149+
root.innerHTML,
150+
'<x-dummy-button text="click me"><button>click me</button></x-dummy-button>'
151+
);
152+
153+
act(() => {
154+
el.querySelector('button').click();
155+
});
156+
157+
assert.equal(el.onClick, onClick);
158+
assert.equal(clicks, 1);
159+
});
133160
});
134161

135162
function Foo({ text, children }) {
@@ -327,8 +354,7 @@ describe('web components', () => {
327354

328355
const child = document
329356
.querySelector('x-adopted-style-sheets')
330-
.shadowRoot
331-
.querySelector('.styled-child');
357+
.shadowRoot.querySelector('.styled-child');
332358

333359
const style = getComputedStyle(child);
334360
assert.equal(style.color, 'rgb(255, 0, 0)');

0 commit comments

Comments
 (0)