textInputLineComponent.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. validator?: (value: string) => boolean;
  13. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  14. }
  15. export class TextInputLineComponent extends React.Component<ITextInputLineComponentProps, { value: string }> {
  16. private _localChange = false;
  17. constructor(props: ITextInputLineComponentProps) {
  18. super(props);
  19. this.state = { value: this.props.value !== undefined ? this.props.value : this.props.target[this.props.propertyName!] || "" }
  20. }
  21. shouldComponentUpdate(nextProps: ITextInputLineComponentProps, nextState: { value: string }) {
  22. if (this._localChange) {
  23. this._localChange = false;
  24. return true;
  25. }
  26. const newValue = nextProps.value !== undefined ? nextProps.value : nextProps.target[nextProps.propertyName!];
  27. if (newValue !== nextState.value) {
  28. nextState.value = newValue || "";
  29. return true;
  30. }
  31. return false;
  32. }
  33. raiseOnPropertyChanged(newValue: string, previousValue: string) {
  34. if (this.props.onChange) {
  35. this.props.onChange(newValue);
  36. return;
  37. }
  38. if (!this.props.onPropertyChangedObservable) {
  39. return;
  40. }
  41. this.props.onPropertyChangedObservable.notifyObservers({
  42. object: this.props.target,
  43. property: this.props.propertyName!,
  44. value: newValue,
  45. initialValue: previousValue
  46. });
  47. }
  48. updateValue(value: string, raisePropertyChanged: boolean) {
  49. this._localChange = true;
  50. const store = this.props.value !== undefined ? this.props.value : this.props.target[this.props.propertyName!];
  51. if(this.props.validator && raisePropertyChanged) {
  52. if(this.props.validator(value) == false) {
  53. value = store;
  54. }
  55. }
  56. this.setState({ value: value });
  57. if (raisePropertyChanged) {
  58. this.raiseOnPropertyChanged(value, store);
  59. }
  60. if (this.props.propertyName) {
  61. this.props.target[this.props.propertyName] = value;
  62. }
  63. }
  64. render() {
  65. return (
  66. <div className="textInputLine">
  67. <div className="label">
  68. {this.props.label}
  69. </div>
  70. <div className="value">
  71. <input value={this.state.value}
  72. onFocus={() => this.props.globalState.blockKeyboardEvents = true}
  73. onChange={evt => this.updateValue(evt.target.value, false)}
  74. onKeyDown={evt => {
  75. if (evt.keyCode !== 13) {
  76. return;
  77. }
  78. this.updateValue(this.state.value, true);
  79. }} onBlur={evt => {
  80. this.updateValue(evt.target.value, true)
  81. this.props.globalState.blockKeyboardEvents = false;
  82. }}/>
  83. </div>
  84. </div>
  85. );
  86. }
  87. }