checkBoxLineComponent.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "./propertyChangedEvent";
  4. export interface ICheckBoxLineComponentProps {
  5. label: string;
  6. target?: any;
  7. propertyName?: string;
  8. isSelected?: () => boolean;
  9. onSelect?: (value: boolean) => void;
  10. onValueChanged?: () => void;
  11. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  12. disabled?: boolean;
  13. }
  14. export class CheckBoxLineComponent extends React.Component<ICheckBoxLineComponentProps, { isSelected: boolean }> {
  15. private static _UniqueIdSeed = 0;
  16. private _uniqueId: number;
  17. private _localChange = false;
  18. constructor(props: ICheckBoxLineComponentProps) {
  19. super(props);
  20. this._uniqueId = CheckBoxLineComponent._UniqueIdSeed++;
  21. if (this.props.isSelected) {
  22. this.state = { isSelected: this.props.isSelected() };
  23. } else {
  24. this.state = { isSelected: this.props.target[this.props.propertyName!] == true };
  25. }
  26. }
  27. shouldComponentUpdate(nextProps: ICheckBoxLineComponentProps, nextState: { isSelected: boolean }) {
  28. var currentState: boolean;
  29. if (nextProps.isSelected) {
  30. currentState = nextProps.isSelected!();
  31. } else {
  32. currentState = nextProps.target[nextProps.propertyName!] == true;
  33. }
  34. if (currentState !== nextState.isSelected || this._localChange) {
  35. nextState.isSelected = currentState;
  36. this._localChange = false;
  37. return true;
  38. }
  39. return nextProps.label !== this.props.label || nextProps.target !== this.props.target;
  40. }
  41. onChange() {
  42. this._localChange = true;
  43. if (this.props.onSelect) {
  44. this.props.onSelect(!this.state.isSelected);
  45. } else {
  46. if (this.props.onPropertyChangedObservable) {
  47. this.props.onPropertyChangedObservable.notifyObservers({
  48. object: this.props.target,
  49. property: this.props.propertyName!,
  50. value: !this.state.isSelected,
  51. initialValue: this.state.isSelected
  52. });
  53. }
  54. this.props.target[this.props.propertyName!] = !this.state.isSelected;
  55. }
  56. if (this.props.onValueChanged) {
  57. this.props.onValueChanged();
  58. }
  59. this.setState({ isSelected: !this.state.isSelected });
  60. }
  61. render() {
  62. return (
  63. <div className="checkBoxLine">
  64. <div className="label">
  65. {this.props.label}
  66. </div>
  67. <div className="checkBox">
  68. <input type="checkbox" id={"checkbox" + this._uniqueId} className="cbx hidden" checked={this.state.isSelected} onChange={() => this.onChange()} disabled={!!this.props.disabled}/>
  69. <label htmlFor={"checkbox" + this._uniqueId} className={`lbl${!!this.props.disabled ? ' disabled' : ''}`}></label>
  70. </div>
  71. </div>
  72. );
  73. }
  74. }