Improve validation
[arvados-workbench2.git] / src / views / run-process-panel / inputs / int-input.tsx
index 5b6f95dbcbc8f1b176edbfc68eee51ef5ded9074..193de26ccb2d3e224d4ba120eea524a03ded87b3 100644 (file)
@@ -3,10 +3,11 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { IntCommandInputParameter, getInputLabel } from '~/models/workflow';
+import { IntCommandInputParameter, getInputLabel, isRequiredInput } from '~/models/workflow';
 import { Field } from 'redux-form';
-import { TextField } from '~/components/text-field/text-field';
 import { isInteger } from '~/validators/is-integer';
+import { GenericInputProps, GenericInput } from '~/views/run-process-panel/inputs/generic-input';
+import { Input as MaterialInput } from '@material-ui/core';
 
 export interface IntInputProps {
     input: IntCommandInputParameter;
@@ -14,9 +15,22 @@ export interface IntInputProps {
 export const IntInput = ({ input }: IntInputProps) =>
     <Field
         name={input.id}
-        label={getInputLabel(input)}
-        component={TextField}
+        commandInput={input}
+        component={IntInputComponent}
         parse={value => parseInt(value, 10)}
         format={value => isNaN(value) ? '' : JSON.stringify(value)}
-        validate={[isInteger]} />;
+        validate={[
+            isRequiredInput(input)
+                ? isInteger
+                : () => undefined,
+        ]} />;
+
+const IntInputComponent = (props: GenericInputProps) =>
+    <GenericInput
+        component={Input}
+        {...props} />;
+
+
+const Input = (props: GenericInputProps) =>
+    <MaterialInput fullWidth type='number' {...props.input} error={props.meta.touched && !!props.meta.error} />;