Skip to content

Commit

Permalink
feat: render function
Browse files Browse the repository at this point in the history
  • Loading branch information
mohaalak committed Jul 23, 2019
1 parent b6895da commit 5cfd5ed
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
</head>
<body>
<div id="root"></div>
<script src="./index.js"></script>
<script src="./test.ts"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TodoApplication {
setState(state) {
this.state = { ...this.state, ...state };
console.log(this.state);
render();
renderApp();
}

setStatus(status) {
Expand Down Expand Up @@ -138,10 +138,10 @@ class TodoApplication {

const todoApp = new TodoApplication();

function render() {
function renderApp() {
const dom = todoApp.render();
removeChildren();
root.appendChild(dom);
}

render();
renderApp();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"typescript": "^3.5.3"
}
}
30 changes: 30 additions & 0 deletions rahnemaak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface Element {
type: string;
props: { [key: string]: any };
children: Element[];
}

function render(parentDom: HTMLElement, element: Element) {
const dom = document.createElement(element.type);

const isEventListener = (x: string) => x.substr(0, 2) === 'on';
Object.keys(element.props)
.filter(x => isEventListener(x))
.forEach((key: string) =>
dom.addEventListener(key.substr(2).toLowerCase(), element.props[key])
);

Object.keys(element.props)
.filter(x => !isEventListener(x))
.forEach((key: string) => {
dom[key] = element.props[key];
});

element.children.filter(x => x !== null).forEach(x => render(dom, x));

parentDom.appendChild(dom);
}

export default {
render
};
15 changes: 15 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Rahnemaak, { Element } from './rahnemaak';

const kooft: Element = {
type: 'div',
props: {
className: 'container'
},
children: [
{ type: 'h1', props: { textContent: 'Hello world' }, children: [] },
{ type: 'button', props: { onClick: e => alert('salam') }, children: [] }
]
};

const root = document.getElementById('root');
Rahnemaak.render(root, kooft);
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


typescript@^3.5.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==

0 comments on commit 5cfd5ed

Please sign in to comment.