-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from lyft/launch-errors
Handle validation of list inputs
- Loading branch information
Showing
29 changed files
with
833 additions
and
480 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/components/Launch/LaunchWorkflowForm/LaunchWorkflowFormInputs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as React from 'react'; | ||
import { CollectionInput } from './CollectionInput'; | ||
import { SimpleInput } from './SimpleInput'; | ||
import { useStyles } from './styles'; | ||
import { | ||
InputProps, | ||
InputType, | ||
LaunchWorkflowFormInputsRef, | ||
ParsedInput | ||
} from './types'; | ||
import { UnsupportedInput } from './UnsupportedInput'; | ||
import { useFormInputsState } from './useFormInputsState'; | ||
|
||
function getComponentForInput(input: InputProps, showErrors: boolean) { | ||
const props = { ...input, error: showErrors ? input.error : undefined }; | ||
switch (input.typeDefinition.type) { | ||
case InputType.Collection: | ||
return <CollectionInput {...props} />; | ||
case InputType.Map: | ||
case InputType.Schema: | ||
case InputType.Unknown: | ||
case InputType.None: | ||
return <UnsupportedInput {...props} />; | ||
default: | ||
return <SimpleInput {...props} />; | ||
} | ||
} | ||
|
||
export interface LaunchWorkflowFormInputsProps { | ||
inputs: ParsedInput[]; | ||
showErrors?: boolean; | ||
} | ||
|
||
export const LaunchWorkflowFormInputsImpl: React.RefForwardingComponent< | ||
LaunchWorkflowFormInputsRef, | ||
LaunchWorkflowFormInputsProps | ||
> = ({ inputs: parsedInputs, showErrors = true }, ref) => { | ||
const { getValues, inputs, validate } = useFormInputsState(parsedInputs); | ||
const styles = useStyles(); | ||
React.useImperativeHandle(ref, () => ({ | ||
getValues, | ||
validate | ||
})); | ||
|
||
return ( | ||
<> | ||
{inputs.map(input => ( | ||
<div key={input.label} className={styles.formControl}> | ||
{getComponentForInput(input, showErrors)} | ||
</div> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
/** Renders an array of `ParsedInput` values using the appropriate | ||
* components. A `ref` to this component is used to access the current | ||
* form values and trigger manual validation if needed. | ||
*/ | ||
export const LaunchWorkflowFormInputs = React.forwardRef( | ||
LaunchWorkflowFormInputsImpl | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.