123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import * as React from "react";
- import { Observable } from "babylonjs";
- import { PropertyChangedEvent } from "components/propertyChangedEvent";
- interface ITextInputLineComponentProps {
- label: string,
- target: any,
- propertyName: string,
- onPropertyChangedObservable?: Observable<PropertyChangedEvent>
- }
- export class TextInputLineComponent extends React.Component<ITextInputLineComponentProps, { value: string }> {
- private _localChange = false;
- constructor(props: ITextInputLineComponentProps) {
- super(props);
- this.state = { value: this.props.target[this.props.propertyName] || "" }
- }
- shouldComponentUpdate(nextProps: ITextInputLineComponentProps, nextState: { value: string }) {
- if (this._localChange) {
- this._localChange = false;
- return true;
- }
- const newValue = nextProps.target[nextProps.propertyName];
- if (newValue !== nextState.value) {
- nextState.value = newValue || "";
- return true;
- }
- return false;
- }
- raiseOnPropertyChanged(newValue: string, previousValue: string) {
- if (!this.props.onPropertyChangedObservable) {
- return;
- }
- this.props.onPropertyChangedObservable.notifyObservers({
- object: this.props.target,
- property: this.props.propertyName,
- value: newValue,
- initialValue: previousValue
- });
- }
- updateValue(value: string) {
- this._localChange = true;
- const store = this.props.target[this.props.propertyName];
- this.setState({ value: value });
- this.raiseOnPropertyChanged(value, store);
- this.props.target[this.props.propertyName] = value;
- }
- render() {
- return (
- <div className="textInputLine">
- <div className="label">
- {this.props.label}
- </div>
- <div className="value">
- <input value={this.state.value} onChange={evt => this.updateValue(evt.target.value)} />
- </div>
- </div>
- );
- }
- }
|