-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ValidatedPasswordInput): Add ValidatedPasswordInput component (#87)
* feat(ValidatedPasswordInput): Add ValidatedPasswordInput component * Fix deps * Fix icon imports to make CI happy
- Loading branch information
Showing
5 changed files
with
65 additions
and
2 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
61 changes: 61 additions & 0 deletions
61
src/components/ValidatedTextInput/ValidatedPasswordInput.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,61 @@ | ||
import * as React from 'react'; | ||
import { | ||
Button, | ||
FormGroup, | ||
FormGroupProps, | ||
InputGroup, | ||
TextInput, | ||
TextInputProps, | ||
} from '@patternfly/react-core'; | ||
import { EyeIcon, EyeSlashIcon } from '@patternfly/react-icons'; | ||
import { IValidatedFormField, getFormGroupProps, getTextInputProps } from '../..'; | ||
|
||
interface IValidatedPasswordInputProps | ||
extends Pick<FormGroupProps, 'label' | 'fieldId' | 'isRequired'> { | ||
/** A field returned from useFormField() or useFormState().fields. */ | ||
field: IValidatedFormField<string> | IValidatedFormField<string | undefined>; | ||
/** Any extra props for the PatternFly FormGroup */ | ||
formGroupProps?: Partial<FormGroupProps>; | ||
/** Any extra props for the PatternFly TextInput */ | ||
inputProps?: Partial<TextInputProps>; | ||
showPasswordAriaLabel?: string; | ||
hidePasswordAriaLabel?: string; | ||
} | ||
|
||
export const ValidatedPasswordInput: React.FunctionComponent<IValidatedPasswordInputProps> = ({ | ||
field, | ||
label, | ||
fieldId, | ||
isRequired, | ||
formGroupProps = {}, | ||
inputProps = {}, | ||
showPasswordAriaLabel = `Show ${label}`, | ||
hidePasswordAriaLabel = `Hide ${label}`, | ||
}: IValidatedPasswordInputProps) => { | ||
const [isValueVisible, toggleValueVisible] = React.useReducer((isVisible) => !isVisible, false); | ||
return ( | ||
<FormGroup | ||
label={label} | ||
isRequired={isRequired} | ||
fieldId={fieldId} | ||
{...getFormGroupProps(field as IValidatedFormField<string | undefined>)} | ||
{...formGroupProps} | ||
> | ||
<InputGroup> | ||
<TextInput | ||
id={fieldId} | ||
type={isValueVisible ? 'text' : 'password'} | ||
{...getTextInputProps(field)} | ||
{...(inputProps as Partial<TextInputProps>)} | ||
/> | ||
<Button | ||
variant="control" | ||
onClick={toggleValueVisible} | ||
aria-label={isValueVisible ? hidePasswordAriaLabel : showPasswordAriaLabel} | ||
> | ||
{!isValueVisible ? <EyeIcon /> : <EyeSlashIcon />} | ||
</Button> | ||
</InputGroup> | ||
</FormGroup> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './ValidatedTextInput'; | ||
export * from './ValidatedPasswordInput'; |