Skip to content
Merged
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
20 changes: 11 additions & 9 deletions client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ export default function render(node, options) {
element.innerHTML = node.attributes[name];
anchorableElement(element);
} else if (name.startsWith('on')) {
const eventName = name.replace('on', '');
const key = '_event.' + eventName;
node[key] = (event) => {
if (node.attributes.default !== true) {
event.preventDefault();
}
node.attributes[name]({ ...node.attributes, event });
};
element.addEventListener(eventName, node[key]);
if (node.attributes[name] !== undefined) {
const eventName = name.replace('on', '');
const key = '_event.' + eventName;
node[key] = (event) => {
if (node.attributes.default !== true) {
event.preventDefault();
}
node.attributes[name]({ ...node.attributes, event });
};
element.addEventListener(eventName, node[key]);
}
} else {
const type = typeof (node.attributes[name]);
if (type !== 'object' && type !== 'function') {
Expand Down
19 changes: 11 additions & 8 deletions tests/src/StatefulComponent.njs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ import Nullstack from 'nullstack';
class StatefulComponent extends Nullstack {

count = 1;
object = {count: 0};
object = { count: 0 };
prepared = 0;
date = new Date('1992-10-16');
empty = '';
visible = false;

prepare() {
this.prepared++;
}

increment({by}) {
increment({ by }) {
this.count += by;
}

incrementByOne() {
this.count++;
}
render({self}) {

render({ self }) {
return (
<form>
<form>
{self.hydrated &&
<div data-tag={self.element.tagName.toLowerCase()} />
}
Expand All @@ -32,20 +33,22 @@ class StatefulComponent extends Nullstack {
<button class="increment-by-two" onclick={this.increment} by={2}>
+2
</button>
<button class="set-to-one" onclick={{count: 1}}>
<button class="set-to-one" onclick={{ count: 1 }}>
=1
</button>
<button class="set-object-to-one" source={this.object} onclick={{count: 1}}>
<button class="set-object-to-one" source={this.object} onclick={{ count: 1 }}>
=1
</button>
<p data-empty={this.empty}>{this.empty}</p>
<button onclick={{empty: 'not'}} data-fill> fill </button>
<button onclick={{ empty: 'not' }} data-fill> fill </button>
<>
<div data-prepared={this.prepared} />
<div data-count={this.count} />
<div data-object-count={this.object.count} />
<div data-year={this.date.getFullYear()} />
</>
<button onclick={{ visible: !this.visible }} data-toggle> Toggle </button>
{this.visible && <button onclick={undefined} data-undefined-event> button </button>}
</form>
)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/src/StatefulComponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ describe('StatefulComponent', () => {
expect(text).toMatch('not');
});

test('rendered attributes undefined values do not raise errors', async () => {
await page.click('[data-toggle]');
await page.waitForSelector('[data-undefined-event]');
await page.click('[data-undefined-event]');
let hasConsoleError = false
page.on("console", () => hasConsoleError = true)
await page.waitForTimeout(2000)
expect(hasConsoleError).toBeFalsy();
});

});