commandDropdownComponent.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as React from "react";
  2. import { GlobalState } from '../globalState';
  3. import { Utilities } from '../tools/utilities';
  4. interface ICommandDropdownComponentProps {
  5. globalState: GlobalState;
  6. icon: string;
  7. tooltip: string;
  8. items: {
  9. label: string,
  10. onClick?: () => void,
  11. onCheck?: (value: boolean) => void,
  12. storeKey?: string,
  13. defaultValue?: boolean | string;
  14. subItems?: string[];
  15. }[];
  16. }
  17. export class CommandDropdownComponent extends React.Component<ICommandDropdownComponentProps, {isExpanded: boolean}> {
  18. public constructor(props: ICommandDropdownComponentProps) {
  19. super(props);
  20. this.state = {isExpanded: false}
  21. }
  22. public render() {
  23. return (
  24. <>
  25. {
  26. this.state.isExpanded &&
  27. <div className="command-dropdown-blocker" onClick={() => this.setState({isExpanded: false})}>
  28. </div>
  29. }
  30. <div className="command-dropdown-root">
  31. <div className={"command-dropdown" + (this.state.isExpanded ? " activated" : "")} title={this.props.tooltip} onClick={() => this.setState({isExpanded: !this.state.isExpanded})}>
  32. <img src={"imgs/" + this.props.icon + ".svg"}/>
  33. </div>
  34. {
  35. this.state.isExpanded &&
  36. <div className="command-dropdown-content sub1">
  37. {
  38. this.props.items.map(m => {
  39. return (
  40. <div className="command-dropdown-label" key={m.label} onClick={() => {
  41. if (! m.onClick) {
  42. let newValue = !Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false);
  43. Utilities.StoreBoolFromStore(m.storeKey!, newValue);
  44. this.forceUpdate();
  45. m.onCheck!(newValue);
  46. return;
  47. }
  48. m.onClick();
  49. this.setState({isExpanded: false});
  50. }} title={m.label}>
  51. <div className="command-dropdown-label-text">
  52. {m.label}
  53. </div>
  54. {
  55. m.onCheck &&
  56. <input type="checkBox" className="command-dropdown-label-check"
  57. onChange={(evt) => {
  58. Utilities.StoreBoolFromStore(m.storeKey!, evt.target.checked);
  59. this.forceUpdate();
  60. m.onCheck!(evt.target.checked);
  61. }}
  62. checked={Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false)}/>
  63. }
  64. {
  65. m.subItems &&
  66. <div className={"sub-items " + (this.props.globalState.language === "JS" ? "background-js" : "background-ts")}>
  67. {
  68. m.subItems.map(s => {
  69. return (
  70. <div key={s} className="sub-item">
  71. {s}
  72. </div>
  73. )
  74. })
  75. }
  76. </div>
  77. }
  78. </div>
  79. )
  80. })
  81. }
  82. </div>
  83. }
  84. </div>
  85. </>
  86. );
  87. }
  88. }