Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace route input with typeahead select menu
- Loading branch information
1 parent
20dd730
commit c37779d
Showing
6 changed files
with
120 additions
and
89 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
71 changes: 71 additions & 0 deletions
71
frontend/packages/dev-console/src/components/import/route/PortInputField.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,71 @@ | ||
import * as React from 'react'; | ||
import { useField, useFormikContext, FormikValues } from 'formik'; | ||
import { FormGroup, Select, SelectVariant, SelectOption } from '@patternfly/react-core'; | ||
import { useFormikValidationFix, getFieldId } from '@console/shared'; | ||
|
||
interface RouteInputFieldProps { | ||
name: string; | ||
label: string; | ||
options: string[]; | ||
placeholderText: string; | ||
helpText: string; | ||
} | ||
|
||
const PortInputField: React.FC<RouteInputFieldProps> = ({ | ||
name, | ||
label, | ||
options, | ||
placeholderText, | ||
helpText, | ||
}) => { | ||
const [field, { touched, error }] = useField<string>(name); | ||
const { setFieldValue, setFieldTouched } = useFormikContext<FormikValues>(); | ||
const [isOpen, setIsOpen] = React.useState<boolean>(false); | ||
const fieldId = getFieldId(name, 'select-input'); | ||
const isValid = !(touched && error); | ||
const errorMessage = !isValid ? error : ''; | ||
|
||
useFormikValidationFix(field.value); | ||
|
||
const onToggle = () => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
const onSelect = (_event, selection: string) => { | ||
setFieldValue(name, selection); | ||
setFieldTouched(name); | ||
onToggle(); | ||
}; | ||
|
||
const onClearSelection = () => { | ||
setFieldValue(name, ''); | ||
setFieldTouched(name); | ||
}; | ||
|
||
return ( | ||
<FormGroup | ||
fieldId={fieldId} | ||
validated={isValid ? 'default' : 'error'} | ||
label={label} | ||
helperText={helpText} | ||
helperTextInvalid={errorMessage} | ||
> | ||
<Select | ||
variant={SelectVariant.typeahead} | ||
onToggle={onToggle} | ||
onSelect={onSelect} | ||
onClear={onClearSelection} | ||
isOpen={isOpen} | ||
selections={field.value} | ||
placeholderText={placeholderText} | ||
isCreatable | ||
> | ||
{options.map((val) => ( | ||
<SelectOption value={val} key={val} /> | ||
))} | ||
</Select> | ||
</FormGroup> | ||
); | ||
}; | ||
|
||
export default PortInputField; |
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