| 1 |
import { useState } from 'react'; |
| 2 |
import { Form, FormGroup, ActionGroup, FormHelperText } from '../Form'; |
| 3 |
import { TextInput } from '../TextInput'; |
| 4 |
import { Button } from '../Button'; |
| 5 |
import { Checkbox } from '../Checkbox'; |
| 6 |
import { ValidatedOptions } from '../../helpers/constants'; |
| 7 |
import { InputGroup, InputGroupItem } from '../InputGroup'; |
| 8 |
import RhUiViewFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-view-fill-icon'; |
| 9 |
import RhUiViewOffFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-view-off-fill-icon'; |
| 10 |
import { HelperText, HelperTextItem } from '../HelperText'; |
| 11 |
|
| 12 |
export interface LoginFormProps extends Omit<React.HTMLProps<HTMLFormElement>, 'ref'> { |
| 13 |
/** Flag to indicate if the first dropdown item should not gain initial focus */ |
| 14 |
noAutoFocus?: boolean; |
| 15 |
/** Additional classes added to the login main body's form */ |
| 16 |
className?: string; |
| 17 |
/** Flag indicating the helper text is visible * */ |
| 18 |
showHelperText?: boolean; |
| 19 |
/** Content displayed in the helper text component * */ |
| 20 |
helperText?: React.ReactNode; |
| 21 |
/** Icon displayed to the left in the helper text */ |
| 22 |
helperTextIcon?: React.ReactNode; |
| 23 |
/** Label for the username input field */ |
| 24 |
usernameLabel?: string; |
| 25 |
/** Value for the username */ |
| 26 |
usernameValue?: string; |
| 27 |
/** Function that handles the onChange event for the username */ |
| 28 |
onChangeUsername?: (event: React.FormEvent<HTMLInputElement>, value: string) => void; |
| 29 |
/** Flag indicating if the username is valid */ |
| 30 |
isValidUsername?: boolean; |
| 31 |
/** Flag indicating if password is required */ |
| 32 |
isPasswordRequired?: boolean; |
| 33 |
/** Label for the password input field */ |
| 34 |
passwordLabel?: string; |
| 35 |
/** Value for the password */ |
| 36 |
passwordValue?: string; |
| 37 |
/** Function that handles the onChange event for the password */ |
| 38 |
onChangePassword?: (event: React.FormEvent<HTMLInputElement>, value: string) => void; |
| 39 |
/** Flag indicating if the password is valid */ |
| 40 |
isValidPassword?: boolean; |
| 41 |
/** Flag indicating if the user can toggle hiding the password */ |
| 42 |
isShowPasswordEnabled?: boolean; |
| 43 |
/** Accessible label for the show password button */ |
| 44 |
showPasswordAriaLabel?: string; |
| 45 |
/** Accessible label for the hide password button */ |
| 46 |
hidePasswordAriaLabel?: string; |
| 47 |
/** Label for the log in button input */ |
| 48 |
loginButtonLabel?: string; |
| 49 |
/** Flag indicating if the login button is disabled */ |
| 50 |
isLoginButtonDisabled?: boolean; |
| 51 |
/** Function that is called when the login button is clicked */ |
| 52 |
onLoginButtonClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void; |
| 53 |
/** Label for the remember me checkbox that indicates the user should be kept logged in. If the label is not provided, the checkbox will not show. */ |
| 54 |
rememberMeLabel?: string; |
| 55 |
/** Flag indicating if the remember me checkbox is checked. */ |
| 56 |
isRememberMeChecked?: boolean; |
| 57 |
/** Function that handles the onChange event for the remember me checkbox */ |
| 58 |
onChangeRememberMe?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void; |
| 59 |
} |
| 60 |
|
| 61 |
export const LoginForm: React.FunctionComponent<LoginFormProps> = ({ |
| 62 |
noAutoFocus = false, |
| 63 |
className = '', |
| 64 |
showHelperText = false, |
| 65 |
helperText = null, |
| 66 |
helperTextIcon = null, |
| 67 |
usernameLabel = 'Username', |
| 68 |
usernameValue = '', |
| 69 |
onChangeUsername = () => undefined as any, |
| 70 |
isValidUsername = true, |
| 71 |
isPasswordRequired = true, |
| 72 |
passwordLabel = 'Password', |
| 73 |
passwordValue = '', |
| 74 |
onChangePassword = () => undefined as any, |
| 75 |
isShowPasswordEnabled = false, |
| 76 |
hidePasswordAriaLabel = 'Hide password', |
| 77 |
showPasswordAriaLabel = 'Show password', |
| 78 |
isValidPassword = true, |
| 79 |
loginButtonLabel = 'Log In', |
| 80 |
isLoginButtonDisabled = false, |
| 81 |
onLoginButtonClick = () => undefined as any, |
| 82 |
rememberMeLabel = '', |
| 83 |
isRememberMeChecked = false, |
| 84 |
onChangeRememberMe = () => undefined as any, |
| 85 |
...props |
| 86 |
}: LoginFormProps) => { |
| 87 |
const [passwordHidden, setPasswordHidden] = useState(true); |
| 88 |
|
| 89 |
const passwordInput = ( |
| 90 |
<TextInput |
| 91 |
isRequired={isPasswordRequired} |
| 92 |
type={passwordHidden ? 'password' : 'text'} |
| 93 |
id="pf-login-password-id" |
| 94 |
name="pf-login-password-id" |
| 95 |
validated={isValidPassword ? ValidatedOptions.default : ValidatedOptions.error} |
| 96 |
value={passwordValue} |
| 97 |
onChange={onChangePassword} |
| 98 |
/> |
| 99 |
); |
| 100 |
|
| 101 |
return ( |
| 102 |
<Form className={className} {...props}> |
| 103 |
{showHelperText && ( |
| 104 |
<FormHelperText> |
| 105 |
<HelperText> |
| 106 |
<HelperTextItem variant={!isValidUsername || !isValidPassword ? 'error' : 'default'} icon={helperTextIcon}> |
| 107 |
{helperText} |
| 108 |
</HelperTextItem> |
| 109 |
</HelperText> |
| 110 |
</FormHelperText> |
| 111 |
)} |
| 112 |
<FormGroup label={usernameLabel} isRequired fieldId="pf-login-username-id"> |
| 113 |
<TextInput |
| 114 |
autoFocus={!noAutoFocus} |
| 115 |
id="pf-login-username-id" |
| 116 |
isRequired |
| 117 |
validated={isValidUsername ? ValidatedOptions.default : ValidatedOptions.error} |
| 118 |
type="text" |
| 119 |
name="pf-login-username-id" |
| 120 |
value={usernameValue} |
| 121 |
onChange={onChangeUsername} |
| 122 |
/> |
| 123 |
</FormGroup> |
| 124 |
<FormGroup label={passwordLabel} isRequired={isPasswordRequired} fieldId="pf-login-password-id"> |
| 125 |
{isShowPasswordEnabled && ( |
| 126 |
<InputGroup> |
| 127 |
<InputGroupItem isFill>{passwordInput}</InputGroupItem> |
| 128 |
<InputGroupItem> |
| 129 |
<Button |
| 130 |
variant="control" |
| 131 |
onClick={() => setPasswordHidden(!passwordHidden)} |
| 132 |
aria-label={passwordHidden ? showPasswordAriaLabel : hidePasswordAriaLabel} |
| 133 |
icon={passwordHidden ? <RhUiViewFillIcon /> : <RhUiViewOffFillIcon />} |
| 134 |
/> |
| 135 |
</InputGroupItem> |
| 136 |
</InputGroup> |
| 137 |
)} |
| 138 |
{!isShowPasswordEnabled && passwordInput} |
| 139 |
</FormGroup> |
| 140 |
{rememberMeLabel.length > 0 && ( |
| 141 |
<FormGroup fieldId="pf-login-remember-me-id"> |
| 142 |
<Checkbox |
| 143 |
id="pf-login-remember-me-id" |
| 144 |
label={rememberMeLabel} |
| 145 |
isChecked={isRememberMeChecked} |
| 146 |
onChange={onChangeRememberMe} |
| 147 |
/> |
| 148 |
</FormGroup> |
| 149 |
)} |
| 150 |
<ActionGroup> |
| 151 |
<Button variant="primary" type="submit" onClick={onLoginButtonClick} isBlock isDisabled={isLoginButtonDisabled}> |
| 152 |
{loginButtonLabel} |
| 153 |
</Button> |
| 154 |
</ActionGroup> |
| 155 |
</Form> |
| 156 |
); |
| 157 |
}; |
| 158 |
LoginForm.displayName = 'LoginForm'; |