| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import * as React from "react";
- import { Observable } from "babylonjs/Misc/observable";
- import { PropertyChangedEvent } from "./propertyChangedEvent";
- import { GlobalState } from '../globalState';
- interface ITextInputLineComponentProps {
- label: string;
- globalState: GlobalState;
- target?: any;
- propertyName?: string;
- value?: string;
- multilines?: boolean;
- onChange?: (value: string) => void;
- validator?: (value: string) => boolean;
- 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.value !== undefined ? this.props.value : this.props.target[this.props.propertyName!] || "" };
- }
- shouldComponentUpdate(nextProps: ITextInputLineComponentProps, nextState: { value: string }) {
- if (this._localChange) {
- this._localChange = false;
- return true;
- }
- const newValue = nextProps.value !== undefined ? nextProps.value : nextProps.target[nextProps.propertyName!];
- if (newValue !== nextState.value) {
- nextState.value = newValue || "";
- return true;
- }
- return false;
- }
- raiseOnPropertyChanged(newValue: string, previousValue: string) {
- if (this.props.onChange) {
- this.props.onChange(newValue);
- return;
- }
- if (!this.props.onPropertyChangedObservable) {
- return;
- }
- this.props.onPropertyChangedObservable.notifyObservers({
- object: this.props.target,
- property: this.props.propertyName!,
- value: newValue,
- initialValue: previousValue
- });
- }
- updateValue(value: string, raisePropertyChanged: boolean) {
- this._localChange = true;
- const store = this.props.value !== undefined ? this.props.value : this.props.target[this.props.propertyName!];
- if (this.props.validator && raisePropertyChanged) {
- if (this.props.validator(value) == false) {
- value = store;
- }
- }
- this.setState({ value: value });
- if (raisePropertyChanged) {
- this.raiseOnPropertyChanged(value, store);
- }
- if (this.props.propertyName) {
- this.props.target[this.props.propertyName] = value;
- }
- }
- render() {
- return (
- <div className={this.props.multilines ? "textInputArea" : "textInputLine"}>
- <div className="label" title={this.props.label}>
- {this.props.label}
- </div>
- <div className="value">
- {this.props.multilines && <>
- <textarea value={this.state.value}
- onFocus={() => this.props.globalState.blockKeyboardEvents = true}
- onChange={(evt) => this.updateValue(evt.target.value, false)}
- onKeyDown={(evt) => {
- if (evt.keyCode !== 13) {
- return;
- }
- this.updateValue(this.state.value, true);
- }} onBlur={(evt) => {
- this.updateValue(evt.target.value, true);
- this.props.globalState.blockKeyboardEvents = false;
- }}/>
- </>}
- {!this.props.multilines && <>
- <input value={this.state.value}
- onFocus={() => this.props.globalState.blockKeyboardEvents = true}
- onChange={(evt) => this.updateValue(evt.target.value, false)}
- onKeyDown={(evt) => {
- if (evt.keyCode !== 13) {
- return;
- }
- this.updateValue(this.state.value, true);
- }} onBlur={(evt) => {
- this.updateValue(evt.target.value, true);
- this.props.globalState.blockKeyboardEvents = false;
- }}/>
- </>}
- </div>
- </div>
- );
- }
- }
|