textInputLineComponent.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "./propertyChangedEvent";
  4. import { GlobalState } from '../globalState';
  5. interface ITextInputLineComponentProps {
  6. label: string;
  7. globalState: GlobalState;
  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 || this.props.target[this.props.propertyName!] || "" }
  19. }
  20. shouldComponentUpdate(nextProps: ITextInputLineComponentProps, nextState: { value: string }) {
  21. if (this._localChange) {
  22. this._localChange = false;
  23. return true;
  24. }
  25. const newValue = nextProps.value || nextProps.target[nextProps.propertyName!];
  26. if (newValue !== nextState.value) {
  27. nextState.value = newValue || "";
  28. return true;
  29. }
  30. return false;
  31. }
  32. raiseOnPropertyChanged(newValue: string, previousValue: string) {
  33. if (this.props.onChange) {
  34. this.props.onChange(newValue);
  35. return;
  36. }
  37. if (!this.props.onPropertyChangedObservable) {
  38. return;
  39. }
  40. this.props.onPropertyChangedObservable.notifyObservers({
  41. object: this.props.target,
  42. property: this.props.propertyName!,
  43. value: newValue,
  44. initialValue: previousValue
  45. });
  46. }
  47. updateValue(value: string, raisePropertyChanged: boolean) {
  48. this._localChange = true;
  49. const store = this.props.value || this.props.target[this.props.propertyName!];
  50. this.setState({ value: value });
  51. if (raisePropertyChanged) {
  52. this.raiseOnPropertyChanged(value, store);
  53. }
  54. if (this.props.propertyName) {
  55. this.props.target[this.props.propertyName] = value;
  56. }
  57. }
  58. render() {
  59. return (
  60. <div className="textInputLine">
  61. <div className="label">
  62. {this.props.label}
  63. </div>
  64. <div className="value">
  65. <input value={this.state.value}
  66. onFocus={() => this.props.globalState.blockKeyboardEvents = true}
  67. onChange={evt => this.updateValue(evt.target.value, false)}
  68. onKeyDown={evt => {
  69. if (evt.keyCode !== 13) {
  70. return;
  71. }
  72. this.updateValue(this.state.value, true);
  73. }} onBlur={evt => {
  74. this.updateValue(evt.target.value, true)
  75. this.props.globalState.blockKeyboardEvents = false;
  76. }}/>
  77. </div>
  78. </div>
  79. );
  80. }
  81. }