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

wizard: create new component with eyeIcon button inside textInput (HMS-5289) #2727

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions src/Components/CreateImageWizard/ValidatedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
HelperTextItem,
TextInput,
TextInputProps,
Button,
InputGroup,
InputGroupItem,
} from '@patternfly/react-core';
import { EyeIcon, EyeSlashIcon } from '@patternfly/react-icons';

import type { StepValidation } from './utilities/useValidation';

Expand All @@ -30,6 +34,65 @@ interface HookValidatedTextInputPropTypes extends TextInputProps {
warning?: string;
}

interface HookPasswordValidatedTextInputPropTypes
extends HookValidatedTextInputPropTypes {
isPasswordVisible?: boolean;
togglePasswordVisibility?: () => void;
}

export const HookPasswordValidatedInput = ({
ariaLabel,
placeholder,
dataTestId,
value,
ouiaId,
stepValidation,
fieldName,
onChange,
type = 'text',
isPasswordVisible,
togglePasswordVisibility,
warning = undefined,
}: HookPasswordValidatedTextInputPropTypes) => {
return (
<>
<InputGroup>
<InputGroupItem isFill>
<HookValidatedInput
isRequired
type={type}
ouiaId={ouiaId || ''}
data-testid={dataTestId}
aria-label={ariaLabel || ''}
value={value}
onChange={onChange!}
stepValidation={stepValidation}
ariaLabel={ariaLabel || ''}
fieldName={fieldName}
placeholder={placeholder || ''}
/>
</InputGroupItem>
<InputGroupItem>
<Button
variant="control"
onClick={togglePasswordVisibility}
aria-label={isPasswordVisible ? 'Show password' : 'Hide password'}
>
{isPasswordVisible ? <EyeSlashIcon /> : <EyeIcon />}
</Button>
</InputGroupItem>
</InputGroup>
{warning !== undefined && (
<HelperText>
<HelperTextItem variant="warning" hasIcon>
{warning}
</HelperTextItem>
</HelperText>
)}
</>
);
};

export const HookValidatedInput = ({
dataTestId,
ouiaId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';

import { Button, FormGroup } from '@patternfly/react-core';
import { ExternalLinkAltIcon } from '@patternfly/react-icons';
Expand All @@ -13,7 +13,10 @@ import {
setUserPasswordByIndex,
setUserSshKeyByIndex,
} from '../../../../../store/wizardSlice';
import { HookValidatedInput } from '../../../ValidatedTextInput';
import {
HookPasswordValidatedInput,
HookValidatedInput,
} from '../../../ValidatedTextInput';
const UserInfo = () => {
const dispatch = useAppDispatch();
const index = 0;
Expand All @@ -23,6 +26,7 @@ const UserInfo = () => {
const userPassword = useAppSelector(userPasswordSelector);
const userSshKeySelector = selectUserSshKeyByIndex(index);
const userSshKey = useAppSelector(userSshKeySelector);
const [isPasswordVisible, setIsPasswordVisible] = useState(false);

const handleNameChange = (
_e: React.FormEvent<HTMLInputElement>,
Expand All @@ -45,6 +49,10 @@ const UserInfo = () => {
dispatch(setUserSshKeyByIndex({ index: index, sshKey: value }));
};

const togglePasswordVisibility = () => {
setIsPasswordVisible(!isPasswordVisible);
};

const stepValidation = {
errors: {},
disabledNext: false,
Expand All @@ -63,13 +71,16 @@ const UserInfo = () => {
/>
</FormGroup>
<FormGroup isRequired label="Password">
<HookValidatedInput
<HookPasswordValidatedInput
type={isPasswordVisible ? 'text' : 'password'}
ariaLabel="blueprint user password"
value={userPassword || ''}
onChange={(_e, value) => handlePasswordChange(_e, value)}
placeholder="Enter password"
stepValidation={stepValidation}
fieldName="userPassword"
togglePasswordVisibility={togglePasswordVisibility}
isPasswordVisible={isPasswordVisible}
/>
</FormGroup>
<FormGroup isRequired label="SSH key">
Expand Down
Loading