Skip to content

Commit

Permalink
feat: add enum input type in launch form (#178)
Browse files Browse the repository at this point in the history
Signed-off-by: Pianist038801 <[email protected]>

Co-authored-by: Pianist038801 <[email protected]>
  • Loading branch information
Pianist038801 and Pianist038801 authored Aug 4, 2021
1 parent 49225c4 commit 847461f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/components/Launch/LaunchForm/SimpleInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@ import {
FormControl,
FormControlLabel,
FormHelperText,
MenuItem,
Select,
Switch,
TextField
} from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import * as React from 'react';
import { DatetimeInput } from './DatetimeInput';
import { makeStringChangeHandler, makeSwitchChangeHandler } from './handlers';
import { InputProps, InputType } from './types';
import { UnsupportedInput } from './UnsupportedInput';
import { getLaunchInputId } from './utils';

const useStyles = makeStyles(theme => ({
formControl: {
minWidth: '100%'
}
}));

/** Handles rendering of the input component for any primitive-type input */
export const SimpleInput: React.FC<InputProps> = props => {
const {
error,
label,
name,
onChange,
typeDefinition: { type },
typeDefinition: { type, literalType },
value = ''
} = props;
const hasError = !!error;
const helperText = hasError ? error : props.helperText;
const classes = useStyles();

const handleEnumChange = (event: React.ChangeEvent<{ value: unknown }>) => {
onChange(event.target.value as string);
};

switch (type) {
case InputType.Boolean:
return (
Expand Down Expand Up @@ -61,6 +76,23 @@ export const SimpleInput: React.FC<InputProps> = props => {
variant="outlined"
/>
);
case InputType.Enum:
return (
<FormControl className={classes.formControl}>
<Select
id={getLaunchInputId(name)}
label={label}
value={value}
onChange={handleEnumChange}
>
{literalType &&
literalType.enumType?.values.map(item => (
<MenuItem value={item}>{item}</MenuItem>
))}
</Select>
<FormHelperText>{label}</FormHelperText>
</FormControl>
);
default:
return <UnsupportedInput {...props} />;
}
Expand Down
1 change: 1 addition & 0 deletions src/components/Launch/LaunchForm/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const typeLabels: { [k in InputType]: string } = {
[InputType.Datetime]: 'datetime - UTC',
[InputType.Duration]: 'duration - ms',
[InputType.Error]: 'error',
[InputType.Enum]: 'enum',
[InputType.Float]: 'float',
[InputType.Integer]: 'integer',
[InputType.Map]: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const inputHelpers: Record<InputType, InputHelper> = {
[InputType.Collection]: collectionHelper,
[InputType.Datetime]: datetimeHelper,
[InputType.Duration]: durationHelper,
[InputType.Enum]: stringHelper,
[InputType.Error]: unsupportedHelper,
[InputType.Float]: floatHelper,
[InputType.Integer]: integerHelper,
Expand Down
1 change: 1 addition & 0 deletions src/components/Launch/LaunchForm/inputHelpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function typeIsSupported(typeDefinition: InputTypeDefinition): boolean {
case InputType.Blob:
case InputType.Datetime:
case InputType.Duration:
case InputType.Enum:
case InputType.Float:
case InputType.Integer:
case InputType.Schema:
Expand Down
1 change: 1 addition & 0 deletions src/components/Launch/LaunchForm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export enum InputType {
Datetime = 'DATETIME',
Duration = 'DURATION',
Error = 'ERROR',
Enum = 'ENUM',
Float = 'FLOAT',
Integer = 'INTEGER',
Map = 'MAP',
Expand Down
2 changes: 2 additions & 0 deletions src/components/Launch/LaunchForm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export function getInputDefintionForLiteralType(
result.type = InputType.Schema;
} else if (literalType.simple) {
result.type = simpleTypeToInputType[literalType.simple];
} else if (literalType.enumType) {
result.type = InputType.Enum;
}
return result;
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/Common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ export interface LiteralType extends Core.ILiteralType {
metadata?: ProtobufStruct;
schema?: SchemaType;
simple?: SimpleType;
enumType?: EnumType;
}

export type SimpleType = Core.SimpleType;
export const SimpleType = Core.SimpleType;
export type EnumType = Core.EnumType;
export const EnumType = Core.EnumType;

export interface Variable extends Core.IVariable {
type: LiteralType;
Expand Down

0 comments on commit 847461f

Please sign in to comment.