Skip to content

Commit 0658fb3

Browse files
authored
feat(LoginPage): added isPasswordRequired prop (#11673)
* feat(LoginPage): added isPasswordRequired prop * updated test names * mades changes to test file
1 parent fdde961 commit 0658fb3

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

packages/react-core/src/components/LoginPage/LoginForm.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface LoginFormProps extends Omit<React.HTMLProps<HTMLFormElement>, '
2828
onChangeUsername?: (event: React.FormEvent<HTMLInputElement>, value: string) => void;
2929
/** Flag indicating if the username is valid */
3030
isValidUsername?: boolean;
31+
/** Flag indicating if password is required */
32+
isPasswordRequired?: boolean;
3133
/** Label for the password input field */
3234
passwordLabel?: string;
3335
/** Value for the password */
@@ -66,6 +68,7 @@ export const LoginForm: React.FunctionComponent<LoginFormProps> = ({
6668
usernameValue = '',
6769
onChangeUsername = () => undefined as any,
6870
isValidUsername = true,
71+
isPasswordRequired = true,
6972
passwordLabel = 'Password',
7073
passwordValue = '',
7174
onChangePassword = () => undefined as any,
@@ -85,7 +88,7 @@ export const LoginForm: React.FunctionComponent<LoginFormProps> = ({
8588

8689
const passwordInput = (
8790
<TextInput
88-
isRequired
91+
isRequired={isPasswordRequired}
8992
type={passwordHidden ? 'password' : 'text'}
9093
id="pf-login-password-id"
9194
name="pf-login-password-id"
@@ -118,7 +121,7 @@ export const LoginForm: React.FunctionComponent<LoginFormProps> = ({
118121
onChange={onChangeUsername}
119122
/>
120123
</FormGroup>
121-
<FormGroup label={passwordLabel} isRequired fieldId="pf-login-password-id">
124+
<FormGroup label={passwordLabel} isRequired={isPasswordRequired} fieldId="pf-login-password-id">
122125
{isShowPasswordEnabled && (
123126
<InputGroup>
124127
<InputGroupItem isFill>{passwordInput}</InputGroupItem>

packages/react-core/src/components/LoginPage/__tests__/LoginForm.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ describe('LoginForm', () => {
4848
const { asFragment } = render(<LoginForm isShowPasswordEnabled />);
4949
expect(asFragment()).toMatchSnapshot();
5050
});
51+
52+
test('Renders LoginForm with password field required by default', () => {
53+
render(<LoginForm />);
54+
const passwordField = screen.getByLabelText(/password/i);
55+
expect(passwordField).toBeRequired();
56+
});
57+
58+
test('Renders LoginForm with password field not required when isPasswordRequired set to false', () => {
59+
render(<LoginForm isPasswordRequired={false} />);
60+
const passwordField = screen.getByLabelText(/password/i);
61+
expect(passwordField).not.toBeRequired();
62+
});
5163
});

packages/react-core/src/components/LoginPage/examples/LoginPage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import GitlabIcon from '@patternfly/react-icons/dist/esm/icons/gitlab-icon';
3131

3232
### Basic
3333

34+
By default, a login page requires users to enter both a username and a password into their respective fields. The username must always be a required field, but you can make the password optional by passing the `isPasswordRequired` property to the `<LoginForm>`.
3435
```ts file='./LoginPageBasic.tsx' isFullscreen
3536

3637
```

0 commit comments

Comments
 (0)