dropUpButton.tsx 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as React from "react";
  2. import { GlobalState } from '../globalState';
  3. import { Nullable } from 'babylonjs/types';
  4. import { Observer } from 'babylonjs/Misc/observable';
  5. var iconUp = require("../img/icon-up.svg");
  6. var iconDown = require("../img/icon-down.svg");
  7. interface IDropUpButtonProps {
  8. globalState: GlobalState;
  9. enabled: boolean;
  10. icon?: any;
  11. label: string;
  12. options: string[];
  13. activeEntry: () => string;
  14. selectedOption?: string;
  15. onOptionPicked: (option: string) => void;
  16. }
  17. export class DropUpButton extends React.Component<IDropUpButtonProps, {isOpen: boolean}> {
  18. private _onClickInterceptorClickedObserver: Nullable<Observer<void>>;
  19. public constructor(props: IDropUpButtonProps) {
  20. super(props);
  21. this.state = {isOpen: false};
  22. this._onClickInterceptorClickedObserver = props.globalState.onClickInterceptorClicked.add(() => {
  23. this.setState({isOpen: false});
  24. });
  25. }
  26. componentWillUnmount() {
  27. this.props.globalState.onClickInterceptorClicked.remove(this._onClickInterceptorClickedObserver);
  28. }
  29. switchDropUp() {
  30. this.props.globalState.onRequestClickInterceptor.notifyObservers();
  31. this.setState({isOpen: !this.state.isOpen});
  32. }
  33. clickOption(option: string) {
  34. this.switchDropUp()
  35. this.props.onOptionPicked(option);
  36. }
  37. public render() {
  38. if (!this.props.enabled) {
  39. return null;
  40. }
  41. return (
  42. <div className="dropup">
  43. {
  44. this.props.icon &&
  45. <div className={"button" + (this.state.isOpen ? " active" : "")} onClick={() => this.switchDropUp()}>
  46. <img src={this.props.icon} alt={this.props.label} title={this.props.label} />
  47. </div>
  48. }
  49. {
  50. this.props.selectedOption &&
  51. <div className={"button long" + (this.state.isOpen ? " active" : "")} onClick={() => this.switchDropUp()}>
  52. {
  53. this.state.isOpen &&
  54. <img className="button-icon" src={iconDown} alt="Close the list" title="Close the list" />
  55. }
  56. {
  57. !this.state.isOpen &&
  58. <img className="button-icon" src={iconUp} alt="Open the list" title="Open the list" />
  59. }
  60. <div className="button-text" title={this.props.selectedOption}>
  61. {this.props.selectedOption}
  62. </div>
  63. </div>
  64. }
  65. {
  66. this.state.isOpen &&
  67. <div className={"dropup-content" + (this.props.selectedOption ? " long-mode" : "")}>
  68. {
  69. this.props.options.map(o => {
  70. return(
  71. <div key={o} onClick={() => this.clickOption(o)} className="dropup-content-line">
  72. <div style={{
  73. opacity: this.props.activeEntry() === o ? "1.0" : "0.8",
  74. fontSize: this.props.activeEntry() === o ? "var(--active-font-size)" : "var(--font-size)"
  75. }}>
  76. {o}
  77. </div>
  78. </div>
  79. )
  80. })
  81. }
  82. </div>
  83. }
  84. </div>
  85. )
  86. }
  87. }