Skip to content

Commit

Permalink
Add convenience bootstrapping
Browse files Browse the repository at this point in the history
Looks horrible. Great!
  • Loading branch information
eirikb committed Apr 30, 2020
1 parent 0dd6f7c commit 6a41d4e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ export function isProbablyPlainObject(obj) {
return typeof obj === 'object' && obj !== null && obj.constructor === Object;
}

export default (data = Data()) => {
export default (dataOrParent, view, orData) => {
let data = dataOrParent;
if (dataOrParent && view) {
data = orData;
}
if (!data) {
data = Data();
}
const React = {
createElement(tagName, props, ...children) {
if (typeof tagName === 'function') {
Expand Down Expand Up @@ -109,12 +116,16 @@ export default (data = Data()) => {
global.React = React;
}

return {
append(parent, template) {
const element = React.createElement(template);
const res = {
append(parent, view) {
const element = React.createElement(view);
parent.appendChild(element);
element.mounted();
},
...data
};
if (dataOrParent && view) {
res.append(dataOrParent, view);
}
return res;
}
13 changes: 13 additions & 0 deletions test/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1211,3 +1211,16 @@ test('dd-model select before options are set', t => {
t.is(select.value, 'hello');
});
});

test('Convenience', t => {
const dd = domdom(document.body, ({ on }) => <div>Hello {on('test')}</div>);
dd.set('test', 'world!');
t.pass();
});

test('Convenience view before domdom', t => {
const view = ({ on }) => <div>Hello {on('test')}</div>;
const dd = domdom(document.body, view);
dd.set('test', 'world!');
t.pass();
});

0 comments on commit 6a41d4e

Please sign in to comment.