Browse Source

Fix input of decimal digits that are fixed at 2

Popov72 4 năm trước cách đây
mục cha
commit
2bda953183
1 tập tin đã thay đổi với 16 bổ sung8 xóa
  1. 16 8
      nodeEditor/src/sharedComponents/floatLineComponent.tsx

+ 16 - 8
nodeEditor/src/sharedComponents/floatLineComponent.tsx

@@ -12,14 +12,15 @@ interface IFloatLineComponentProps {
     isInteger?: boolean;
     onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
     additionalClass?: string;
-    step?: string,
+    step?: string;
     digits?: number;
-    globalState: GlobalState
+    globalState: GlobalState;
 }
 
 export class FloatLineComponent extends React.Component<IFloatLineComponentProps, { value: string }> {
     private _localChange = false;
     private _store: number;
+    private _regExp: RegExp;
 
     constructor(props: IFloatLineComponentProps) {
         super(props);
@@ -27,6 +28,15 @@ export class FloatLineComponent extends React.Component<IFloatLineComponentProps
         let currentValue = this.props.target[this.props.propertyName];
         this.state = { value: currentValue ? (this.props.isInteger ? currentValue.toFixed(0) : currentValue.toFixed(this.props.digits || 2)) : "0" };
         this._store = currentValue;
+
+        let rexp = "(.*\\.";
+        let numDigits = this.props.digits || 2;
+        while (numDigits--) {
+            rexp += ".";
+        }
+        rexp += ").+";
+
+        this._regExp = new RegExp(rexp);
     }
 
     shouldComponentUpdate(nextProps: IFloatLineComponentProps, nextState: { value: string }) {
@@ -62,12 +72,11 @@ export class FloatLineComponent extends React.Component<IFloatLineComponentProps
     }
 
     updateValue(valueString: string) {
-
         if (/[^0-9\.\-]/g.test(valueString)) {
             return;
         }
 
-        valueString = valueString.replace(/(.+\...).+/, "$1");
+        valueString = valueString.replace(this._regExp, "$1");
 
         let valueAsNumber: number;
 
@@ -77,7 +86,6 @@ export class FloatLineComponent extends React.Component<IFloatLineComponentProps
             valueAsNumber = parseFloat(valueString);
         }
 
-
         this._localChange = true;
         this.setState({ value: valueString});
 
@@ -100,12 +108,12 @@ export class FloatLineComponent extends React.Component<IFloatLineComponentProps
                             {this.props.label}
                         </div>
                         <div className="value">
-                            <input type="number" step={this.props.step || "0.01"} className="numeric-input" 
-                            onBlur={evt => {
+                            <input type="number" step={this.props.step || "0.01"} className="numeric-input"
+                            onBlur={(evt) => {
                                 this.props.globalState.blockKeyboardEvents = false;
                             }}
                             onFocus={() => this.props.globalState.blockKeyboardEvents = true}
-                            value={this.state.value} onChange={evt => this.updateValue(evt.target.value)} />
+                            value={this.state.value} onChange={(evt) => this.updateValue(evt.target.value)} />
                         </div>
                     </div>
                 }