forked from MapComplete/MapComplete
Add convenience method to add spinner on click of a subtlebutton
This commit is contained in:
parent
b5d2746a2a
commit
5696b80a5e
1 changed files with 29 additions and 0 deletions
|
@ -5,6 +5,9 @@ import Link from "./Link";
|
||||||
import Img from "./Img";
|
import Img from "./Img";
|
||||||
import {UIEventSource} from "../../Logic/UIEventSource";
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
||||||
import {UIElement} from "../UIElement";
|
import {UIElement} from "../UIElement";
|
||||||
|
import {VariableUiElement} from "./VariableUIElement";
|
||||||
|
import Lazy from "./Lazy";
|
||||||
|
import Loading from "./Loading";
|
||||||
|
|
||||||
|
|
||||||
export class SubtleButton extends UIElement {
|
export class SubtleButton extends UIElement {
|
||||||
|
@ -55,5 +58,31 @@ export class SubtleButton extends UIElement {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public OnClickWithLoading(
|
||||||
|
loadingText: BaseUIElement | string,
|
||||||
|
action: () => Promise<void> ) : BaseUIElement{
|
||||||
|
const state = new UIEventSource<"idle" | "running">("idle")
|
||||||
|
const button = this;
|
||||||
|
|
||||||
|
button.onClick(async() => {
|
||||||
|
state.setData("running")
|
||||||
|
try{
|
||||||
|
await action()
|
||||||
|
}catch(e){
|
||||||
|
console.error(e)
|
||||||
|
}finally {
|
||||||
|
state.setData("idle")
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
const loading = new Lazy(() => new Loading(loadingText) )
|
||||||
|
return new VariableUiElement(state.map(st => {
|
||||||
|
console.log("State is: ", st)
|
||||||
|
if(st === "idle"){
|
||||||
|
return button
|
||||||
|
}
|
||||||
|
return loading
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue