Merge branch '18692-frozen-projects-workbench-support' into main
[arvados.git] / src / views / run-process-panel / inputs / float-input.tsx
index 664425d3aefa33c0b273850becc4ea74fe6313ef..14b06fd459fce2444266427281f1b75c0cd5ea93 100644 (file)
@@ -2,44 +2,43 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
-import { getInputLabel, FloatCommandInputParameter } from '~/models/workflow';
-import { Field, WrappedFieldProps } from 'redux-form';
-import { TextField } from '~/components/text-field/text-field';
-import { isNumber } from '~/validators/is-number';
+import React from 'react';
+import { memoize } from 'lodash/fp';
+import { FloatCommandInputParameter, isRequiredInput } from 'models/workflow';
+import { Field } from 'redux-form';
+import { isNumber } from 'validators/is-number';
+import { GenericInputProps, GenericInput } from './generic-input';
+import { FloatInput as FloatInputComponent } from 'components/float-input/float-input';
 export interface FloatInputProps {
     input: FloatCommandInputParameter;
 }
 export const FloatInput = ({ input }: FloatInputProps) =>
     <Field
         name={input.id}
-        label={getInputLabel(input)}
-        component={DecimalInput}
+        commandInput={input}
+        component={Input}
         parse={parseFloat}
-        format={value => isNaN(value) ? '' : JSON.stringify(value)}
-        validate={[isNumber]} />;
+        format={format}
+        validate={getValidation(input)} />;
 
+const format = (value: any) => isNaN(value) ? '' : JSON.stringify(value);
 
-class DecimalInput extends React.Component<WrappedFieldProps & { label?: string }> {
-    state = {
-        endsWithDecimalSeparator: false,
-    };
+const getValidation = memoize(
+    (input: FloatCommandInputParameter) => ([
+        isRequiredInput(input)
+            ? isNumber
+            : () => undefined,])
+);
 
-    handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
-        const [base, fraction] = event.target.value.split('.');
-        this.setState({ endsWithDecimalSeparator: fraction === '' });
-        this.props.input.onChange(event);
-    }
+const Input = (props: GenericInputProps) =>
+    <GenericInput
+        component={InputComponent}
+        {...props} />;
+
+const InputComponent = ({ input, meta, commandInput }: GenericInputProps) =>
+    <FloatInputComponent
+        fullWidth
+        error={meta.touched && !!meta.error}
+        disabled={commandInput.disabled}
+        {...input} />;
 
-    render() {
-        const props = {
-            ...this.props,
-            input: {
-                ...this.props.input,
-                value: this.props.input.value + (this.state.endsWithDecimalSeparator ? '.' : ''),
-                onChange: this.handleChange,
-            },
-        };
-        return <TextField {...props} />;
-    }
-}