Create DecimalInput for storing decimal separator info
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 1 Oct 2018 08:02:19 +0000 (10:02 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 1 Oct 2018 08:02:19 +0000 (10:02 +0200)
Feature #13863

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/views/run-process-panel/inputs/float-input.tsx

index 0f5a116280793671dda3c3d86ca23f4f8c983a49..664425d3aefa33c0b273850becc4ea74fe6313ef 100644 (file)
@@ -4,10 +4,9 @@
 
 import * as React from 'react';
 import { getInputLabel, FloatCommandInputParameter } from '~/models/workflow';
-import { Field } from 'redux-form';
+import { Field, WrappedFieldProps } from 'redux-form';
 import { TextField } from '~/components/text-field/text-field';
 import { isNumber } from '~/validators/is-number';
-import { toNumber } from 'lodash';
 export interface FloatInputProps {
     input: FloatCommandInputParameter;
 }
@@ -15,8 +14,32 @@ export const FloatInput = ({ input }: FloatInputProps) =>
     <Field
         name={input.id}
         label={getInputLabel(input)}
-        component={TextField}
-        parse={value => toNumber(value)}
+        component={DecimalInput}
+        parse={parseFloat}
         format={value => isNaN(value) ? '' : JSON.stringify(value)}
         validate={[isNumber]} />;
 
+
+class DecimalInput extends React.Component<WrappedFieldProps & { label?: string }> {
+    state = {
+        endsWithDecimalSeparator: false,
+    };
+
+    handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+        const [base, fraction] = event.target.value.split('.');
+        this.setState({ endsWithDecimalSeparator: fraction === '' });
+        this.props.input.onChange(event);
+    }
+
+    render() {
+        const props = {
+            ...this.props,
+            input: {
+                ...this.props.input,
+                value: this.props.input.value + (this.state.endsWithDecimalSeparator ? '.' : ''),
+                onChange: this.handleChange,
+            },
+        };
+        return <TextField {...props} />;
+    }
+}