textInputLineComponent.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "../propertyChangedEvent";
  4. import { LockObject } from "../tabs/propertyGrids/lockObject";
  5. interface ITextInputLineComponentProps {
  6. label: string;
  7. lockObject: LockObject;
  8. target?: any;
  9. propertyName?: string;
  10. value?: string;
  11. onChange?: (value: string) => void;
  12. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  13. }
  14. export class TextInputLineComponent extends React.Component<ITextInputLineComponentProps, { value: string }> {
  15. private _localChange = false;
  16. constructor(props: ITextInputLineComponentProps) {
  17. super(props);
  18. this.state = { value: (this.props.value !== undefined ? this.props.value : this.props.target[this.props.propertyName!]) || "" }
  19. }
  20. componentWillUnmount() {
  21. this.props.lockObject.lock = false;
  22. }
  23. shouldComponentUpdate(nextProps: ITextInputLineComponentProps, nextState: { value: string }) {
  24. if (this._localChange) {
  25. this._localChange = false;
  26. return true;
  27. }
  28. const newValue = nextProps.value !== undefined ? nextProps.value : nextProps.target[nextProps.propertyName!];
  29. if (newValue !== nextState.value) {
  30. nextState.value = newValue || "";
  31. return true;
  32. }
  33. return false;
  34. }
  35. raiseOnPropertyChanged(newValue: string, previousValue: string) {
  36. if (this.props.onChange) {
  37. this.props.onChange(newValue);
  38. return;
  39. }
  40. if (!this.props.onPropertyChangedObservable) {
  41. return;
  42. }
  43. this.props.onPropertyChangedObservable.notifyObservers({
  44. object: this.props.target,
  45. property: this.props.propertyName!,
  46. value: newValue,
  47. initialValue: previousValue
  48. });
  49. }
  50. updateValue(value: string) {
  51. this._localChange = true;
  52. const store = this.props.value !== undefined ? this.props.value : this.props.target[this.props.propertyName!];
  53. this.setState({ value: value });
  54. this.raiseOnPropertyChanged(value, store);
  55. if (this.props.propertyName) {
  56. this.props.target[this.props.propertyName] = value;
  57. }
  58. }
  59. render() {
  60. return (
  61. <div className="textInputLine">
  62. <div className="label" title={this.props.label}>
  63. {this.props.label}
  64. </div>
  65. <div className="value">
  66. <input value={this.state.value} onBlur={() => this.props.lockObject.lock = false} onFocus={() => this.props.lockObject.lock = true} onChange={evt => this.updateValue(evt.target.value)} />
  67. </div>
  68. </div>
  69. );
  70. }
  71. }