Created
October 1, 2019 18:28
-
-
Save maparent/438371ca6d2132321886526fd661b3a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import app, { Component, EventOptions } from 'apprun'; | |
export class TreeComponent extends Component { | |
view = (state) => { | |
var li = <li> | |
{state.id || "1"} | |
<button onclick={() => this.run("-1")}>-1</button> | |
<button onclick={() => this.run("+1")}>+1</button> | |
</li>; | |
if (state['sub']) { | |
li.children.push(<ul> | |
{state.sub.map((childState)=> app.createElement(TreeComponent, childState, {history: true}))} | |
</ul>) | |
} | |
return li | |
} | |
update = { | |
'+1': (state) => { | |
var sub = state.sub?state.sub.slice():[] | |
sub.push({"id": (state.id||"1") + "." + (sub.length+1), "sub": []}) | |
return {...state, sub} | |
}, | |
'-1': (state) => { | |
var sub = state.sub || [] | |
var len = sub.length | |
if (len) { | |
sub = sub.slice(0, len-1) | |
} | |
return {...state, sub} | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment