19359: Support 'optional' enum type parameters
[arvados-workbench2.git] / src / views / run-process-panel / inputs / enum-input.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { Field } from 'redux-form';
7 import { memoize } from 'lodash/fp';
8 import { require } from 'validators/require';
9 import { Select, MenuItem } from '@material-ui/core';
10 import { EnumCommandInputParameter, CommandInputEnumSchema, isRequiredInput, getEnumType } from 'models/workflow';
11 import { GenericInputProps, GenericInput } from './generic-input';
12
13 export interface EnumInputProps {
14     input: EnumCommandInputParameter;
15 }
16
17 const getValidation = memoize(
18     (input: EnumCommandInputParameter) => ([
19         isRequiredInput(input)
20             ? require
21             : () => undefined,
22     ]));
23
24 export const EnumInput = ({ input }: EnumInputProps) =>
25     <Field
26         name={input.id}
27         commandInput={input}
28         component={EnumInputComponent}
29         validate={getValidation(input)}
30     />;
31
32 const EnumInputComponent = (props: GenericInputProps) =>
33     <GenericInput
34         component={Input}
35         {...props} />;
36
37 const Input = (props: GenericInputProps) => {
38     const type = getEnumType(props.commandInput) as CommandInputEnumSchema;
39     return <Select
40         value={props.input.value}
41         onChange={props.input.onChange}
42         disabled={props.commandInput.disabled} >
43         {type.symbols.map(symbol =>
44             <MenuItem key={symbol} value={extractValue(symbol)}>
45                 {extractValue(symbol)}
46             </MenuItem>)}
47     </Select>;
48 };
49
50 /**
51  * Values in workflow definition have an absolute form, for example:
52  *
53  * ```#input_collector.cwl/enum_type/Pathway table```
54  *
55  * We want a value that is in form accepted by backend.
56  * According to the example above, the correct value is:
57  *
58  * ```Pathway table```
59  */
60 const extractValue = (symbol: string) => symbol.split('/').pop();