textInputLineComponent.tsx 2.2 KB

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