MainWidget.tsx
82 lines
| 2.5 KiB
| text/x-typescript
|
TypeScriptLexer
|
|
r99 | import { djbase, djclass } from "@implab/djx/declare"; | ||
| import { DjxWidgetBase } from "@implab/djx/tsx/DjxWidgetBase"; | ||||
|
|
r102 | import { createElement, watch, prop, attach, all, bind, toggleClass } from "@implab/djx/tsx"; | ||
|
|
r100 | import ProgressBar from "./ProgressBar"; | ||
|
|
r101 | import Button = require("dijit/form/Button"); | ||
|
|
r102 | import { interval } from "rxjs"; | ||
|
|
r99 | |||
| @djclass | ||||
| export default class MainWidget extends djbase(DjxWidgetBase) { | ||||
|
|
r100 | |||
| titleNode?: HTMLHeadingElement; | ||||
| progressBar?: ProgressBar; | ||||
|
|
r101 | count = 0; | ||
| showCounter = false; | ||||
|
|
r102 | counterNode?: HTMLInputElement; | ||
| paused = false; | ||||
|
|
r99 | render() { | ||
|
|
r102 | const Counter = ({ children }: { children: unknown[] }) => <span>Counter: {children}</span>; | ||
|
|
r101 | return <div className="tundra"> | ||
|
|
r102 | <h2 ref={attach(this, "titleNode")}>Hi!</h2> | ||
| <ProgressBar ref={attach(this, "progressBar")} /> | ||||
| <section style={{ padding: "10px" }}> | ||||
| {watch(prop(this, "showCounter"), flag => flag && | ||||
| [ | ||||
| <Counter><input ref={all( | ||||
| bind("value", prop(this, "count") | ||||
| .map(x => x*10) | ||||
| .map(String) | ||||
| ), | ||||
| attach(this, "counterNode") | ||||
| )} /> <span>ms</span></Counter>, | ||||
| " | ", | ||||
| <span ref={bind("innerHTML", interval(1000))}></span>, | ||||
| " | ", | ||||
| <Button | ||||
| ref={all( | ||||
| bind("label", prop(this, "paused") | ||||
| .map(x => x ? "Unpause" : "Pause") | ||||
| ), | ||||
| toggleClass("paused", prop(this,"paused")) | ||||
| )} | ||||
| onClick={this._onPauseClick} | ||||
| /> | ||||
| ] | ||||
| )} | ||||
| </section> | ||||
|
|
r101 | <Button onClick={this._onToggleCounterClick}>Toggle counter</Button> | ||
|
|
r99 | </div>; | ||
| } | ||||
|
|
r101 | |||
| postCreate(): void { | ||||
| super.postCreate(); | ||||
|
|
r102 | const h = setInterval( | ||
| () => { | ||||
| this.set("count", this.count + 1); | ||||
| }, | ||||
| 10 | ||||
| ); | ||||
| this.own({ | ||||
| destroy: () => { | ||||
| clearInterval(h); | ||||
| } | ||||
| }); | ||||
| } | ||||
|
|
r101 | |||
|
|
r102 | private _onPauseClick = () => { | ||
| this.set("paused", !this.paused); | ||||
|
|
r101 | } | ||
| private _onToggleCounterClick = () => { | ||||
| this.set("showCounter", !this.showCounter); | ||||
| } | ||||
|
|
r99 | } | ||
