MyWidget.tsx
86 lines
| 2.2 KiB
| text/x-typescript
|
TypeScriptLexer
cin
|
r65 | import { djbase, djclass, bind, prototype, AbstractConstructor } from "../declare"; | |
import { DjxWidgetBase } from "../tsx/DjxWidgetBase"; | |||
cin
|
r89 | import { createElement, on } from "../tsx"; | |
cin
|
r109 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
cin
|
r65 | ||
interface MyWidgetAttrs { | |||
title: string; | |||
counter: number; | |||
} | |||
interface MyWidgetEvents { | |||
cin
|
r71 | "count-inc": Event & { | |
detail: number; | |||
}; | |||
cin
|
r65 | ||
cin
|
r71 | "count-dec": Event & { | |
detail: number; | |||
}; | |||
cin
|
r65 | } | |
cin
|
r109 | interface FrameProps { | |
ref?: JSX.Ref<HTMLDivElement>; | |||
children?: unknown[]; | |||
} | |||
const Frame = ({children, ref}: FrameProps) => <div ref={ref} >{children}</div>; | |||
cin
|
r65 | ||
@djclass | |||
export class MyWidget extends djbase(DjxWidgetBase as AbstractConstructor<DjxWidgetBase<MyWidgetAttrs, MyWidgetEvents>>) { | |||
@bind({ node: "titleNode", type: "innerHTML" }) | |||
title = ""; | |||
cin
|
r108 | @prototype(0) | |
cin
|
r65 | counter = 0; | |
cin
|
r109 | frameNode?: HTMLDivElement; | |
cin
|
r65 | render() { | |
cin
|
r109 | ||
return <div className="myWidget" onsubmit={this._onSubmit} tabIndex={3} style={{ alignContent: "center", border: "1px solid" }} > | |||
cin
|
r65 | <h1 data-dojo-attach-point="titleNode"></h1> | |
cin
|
r109 | <Frame ref={this._setFrameElement}> | |
<span class="up-button" onclick={this._onIncClick}>[+]</span> | |||
cin
|
r65 | <span class="down-button" onclick={() => this._onDecClick()}>[-]</span> | |
</Frame> | |||
</div>; | |||
} | |||
cin
|
r109 | private readonly _setFrameElement = (node?: HTMLDivElement) => { | |
this.frameNode = node; | |||
}; | |||
cin
|
r72 | postCreate() { | |
super.postCreate(); | |||
this.on("click", () => {}); | |||
} | |||
cin
|
r109 | private readonly _onSubmit = (evt: Event) => { | |
argumentNotNull(evt, "evt"); | |||
}; | |||
cin
|
r65 | ||
cin
|
r109 | private readonly _onIncClick = (evt: MouseEvent) => { | |
argumentNotNull(evt, "evt"); | |||
cin
|
r72 | this.set("counter", this.counter + 1); | |
cin
|
r71 | ||
cin
|
r65 | this.emit("count-inc", { bubbles: false }); | |
cin
|
r109 | }; | |
cin
|
r65 | ||
_onDecClick() { | |||
cin
|
r71 | this.emit("count-dec", { bubbles: false, detail: this.counter }); | |
cin
|
r65 | } | |
cin
|
r72 | ||
@on("count-inc") | |||
cin
|
r120 | protected _onCounterInc(evt: Event & { detail: number; x?: number; }) { | |
cin
|
r109 | argumentNotNull(evt, "evt"); | |
cin
|
r73 | } | |
cin
|
r85 | @on("click", "keydown") | |
cin
|
r109 | protected _onClick(evt: MouseEvent | KeyboardEvent) { | |
argumentNotNull(evt, "evt"); | |||
cin
|
r72 | } | |
cin
|
r73 | } |