checkBoxLineComponent.tsx 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, isDisabled?: 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. if (this.props.disabled) {
  27. this.state = { ...this.state, isDisabled: this.props.disabled };
  28. }
  29. }
  30. shouldComponentUpdate(nextProps: ICheckBoxLineComponentProps, nextState: { isSelected: boolean, isDisabled: boolean }) {
  31. var currentState: boolean;
  32. if (nextProps.isSelected) {
  33. currentState = nextProps.isSelected!();
  34. } else {
  35. currentState = nextProps.target[nextProps.propertyName!] == true;
  36. }
  37. if (currentState !== nextState.isSelected || this._localChange) {
  38. nextState.isSelected = currentState;
  39. this._localChange = false;
  40. return true;
  41. }
  42. if(nextProps.disabled !== nextState.isDisabled){
  43. return true;
  44. }
  45. return nextProps.label !== this.props.label || nextProps.target !== this.props.target;
  46. }
  47. onChange() {
  48. this._localChange = true;
  49. if (this.props.onSelect) {
  50. this.props.onSelect(!this.state.isSelected);
  51. } else {
  52. if (this.props.onPropertyChangedObservable) {
  53. this.props.onPropertyChangedObservable.notifyObservers({
  54. object: this.props.target,
  55. property: this.props.propertyName!,
  56. value: !this.state.isSelected,
  57. initialValue: this.state.isSelected
  58. });
  59. }
  60. this.props.target[this.props.propertyName!] = !this.state.isSelected;
  61. }
  62. if (this.props.onValueChanged) {
  63. this.props.onValueChanged();
  64. }
  65. this.setState({ isSelected: !this.state.isSelected });
  66. }
  67. render() {
  68. return (
  69. <div className="checkBoxLine">
  70. <div className="label">
  71. {this.props.label}
  72. </div>
  73. <div className="checkBox">
  74. <input type="checkbox" id={"checkbox" + this._uniqueId} className="cbx hidden" checked={this.state.isSelected} onChange={() => this.onChange()} disabled={!!this.props.disabled}/>
  75. <label htmlFor={"checkbox" + this._uniqueId} className={`lbl${!!this.props.disabled ? ' disabled' : ''}`}></label>
  76. </div>
  77. </div>
  78. );
  79. }
  80. }