Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support enum input type in launch form #178

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: not a blocker but do we need makeStyles here; couldn't we just send styles as a generic object? (is there a specific reason for useStyle?)

eg,

const formStyle = {
   minWidth: 100%; 
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we define a style that way, we should use style props of React component.
But I found that we currently use className prop for styling.

That's why I went with such solution, just tried to replicate how it's already implemented.

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