import * as React from "react"; import { GlobalState } from '../globalState'; import { Nullable } from 'babylonjs/types'; import { Observer } from 'babylonjs/Misc/observable'; var iconUp = require("../img/icon-up.svg"); var iconDown = require("../img/icon-down.svg"); interface IDropUpButtonProps { globalState: GlobalState; enabled: boolean; icon?: any; label: string; options: string[]; activeEntry: () => string; selectedOption?: string; onOptionPicked: (option: string) => void; } export class DropUpButton extends React.Component { private _onClickInterceptorClickedObserver: Nullable>; public constructor(props: IDropUpButtonProps) { super(props); this.state = {isOpen: false}; this._onClickInterceptorClickedObserver = props.globalState.onClickInterceptorClicked.add(() => { this.setState({isOpen: false}); }); } componentWillUnmount() { this.props.globalState.onClickInterceptorClicked.remove(this._onClickInterceptorClickedObserver); } switchDropUp() { this.props.globalState.onRequestClickInterceptor.notifyObservers(); this.setState({isOpen: !this.state.isOpen}); } clickOption(option: string) { this.switchDropUp() this.props.onOptionPicked(option); } public render() { if (!this.props.enabled) { return null; } return (
{ this.props.icon &&
this.switchDropUp()}> {this.props.label}
} { this.props.selectedOption &&
this.switchDropUp()}> { this.state.isOpen && Close the list } { !this.state.isOpen && Open the list }
{this.props.selectedOption}
} { this.state.isOpen &&
{ this.props.options.map(o => { return(
this.clickOption(o)} className="dropup-content-line">
{o}
) }) }
}
) } }