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

feat(change-password): add additionalFields prop #1486

Merged
merged 3 commits into from
Nov 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Function to call when an error occurs calling the `changePassword` method on the

Additional Buttons to render to the right of the Submit button.

#### `additionalFields?: React.ReactNode`

Additional Fields to render in the `<Form />`

#### `header?: React.ReactNode`

The header to render above the form inputs
Expand All @@ -38,6 +42,10 @@ The maximum length of the user's password

Additional props to spread onto the current password input field

#### `showCurrentPassword?: boolean`

Conditionally render the current password input field. Defaults to `true`

#### `newPasswordProps?: object`

Additional props to spread onto the new password input field
Expand Down
76 changes: 43 additions & 33 deletions packages/change-password/src/ChangePasswordForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
onHandleSubmit,
onError,
additionalButtons,
additionalFields,
header,
maxLength = 30,
currentPasswordProps,
showCurrentPassword = true,
newPasswordProps,
confirmNewPasswordProps,
}) => {
Expand All @@ -41,11 +43,11 @@
setSubmitted,
} = useChangePasswordContext();

const handleSubmit = async ({ currentPassword, newPassword }) => {
const handleSubmit = async ({ ...formFields }) => {
setLoading(true);
setSubmitted(true);
try {
const result = await resource.changePassword({ currentPassword, newPassword });
const result = await resource.changePassword({ ...formFields });
setSuccess('Your password was successfully changed');
if (onHandleSubmit) {
await onHandleSubmit({ result, setSuccess, setError });
Expand Down Expand Up @@ -86,37 +88,42 @@
<Alert isOpen={!!success} color="success" toggle={_onSuccessToggle}>
{success}
</Alert>
<div className="password-with-icon">
<Field
name="currentPassword"
data-testid="current-password-input"
label="Current Password"
maxLength={maxLength}
placeholder="Input your current password"
type={currentPasswordVisible ? 'text' : 'password'}
{...currentPasswordProps}
/>
<Icon
name={`eye${currentPasswordVisible ? '' : '-off'}`}
data-testid="current-password-icon"
id="current-password-eye"
role="button"
label="password-visibility"
onMouseDown={(e) => {
currentPasswordIconRef?.current?.focus();
e.preventDefault();
setCurrentPasswordVisible(!currentPasswordVisible);
}}
onKeyDown={(e) => {
if (e.keyCode === 13 || e.keyCode === 32) {
setCurrentPasswordVisible(!currentPasswordVisible);
}
}}
tabIndex={0}
aria-label={currentPasswordVisible ? 'Hide Password' : 'Show Password'}
ref={currentPasswordIconRef}
/>
</div>

{
showCurrentPassword ? (
<div className="password-with-icon">
<Field
name="currentPassword"
data-testid="current-password-input"
label="Current Password"
maxLength={maxLength}
placeholder="Input your current password"
type={currentPasswordVisible ? 'text' : 'password'}
{...currentPasswordProps}
/>
<Icon
name={`eye${currentPasswordVisible ? '' : '-off'}`}
data-testid="current-password-icon"
id="current-password-eye"
role="button"
label="password-visibility"
onMouseDown={(e) => {
currentPasswordIconRef?.current?.focus();
e.preventDefault();
setCurrentPasswordVisible(!currentPasswordVisible);
}}
onKeyDown={(e) => {
if (e.keyCode === 13 || e.keyCode === 32) {
setCurrentPasswordVisible(!currentPasswordVisible);

Check warning on line 117 in packages/change-password/src/ChangePasswordForm.js

View check run for this annotation

Codecov / codecov/patch

packages/change-password/src/ChangePasswordForm.js#L115-L117

Added lines #L115 - L117 were not covered by tests
}
}}
tabIndex={0}
aria-label={currentPasswordVisible ? 'Hide Password' : 'Show Password'}
ref={currentPasswordIconRef}
/>
</div>
) : null

Check warning on line 125 in packages/change-password/src/ChangePasswordForm.js

View check run for this annotation

Codecov / codecov/patch

packages/change-password/src/ChangePasswordForm.js#L125

Added line #L125 was not covered by tests
}

<div className="password-with-icon">
<Field
Expand Down Expand Up @@ -183,6 +190,7 @@
ref={confirmNewPasswordIconRef}
/>
</div>
{additionalFields}
</Col>
<Col>
<ChangePasswordFeedback />
Expand All @@ -208,9 +216,11 @@
onHandleSubmit: PropTypes.func,
onError: PropTypes.func,
additionalButtons: PropTypes.node,
additionalFields: PropTypes.node,
header: PropTypes.node,
maxLength: PropTypes.number,
currentPasswordProps: PropTypes.object,
showCurrentPassword: PropTypes.bool,
newPasswordProps: PropTypes.object,
confirmNewPasswordProps: PropTypes.object,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/change-password/types/ChangePasswordForm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ export interface ChangePasswordFormProps {
onHandleSubmit?: (arg: { result: any }) => void;
onError?: (arg: { error: Error }) => void;
additionalButtons?: React.ReactNode;
additionalFields?: React.ReactNode;
header?: React.ReactNode;
maxLength?: number;
currentPasswordProps?: any;
showCurrentPassword?: boolean;
newPasswordProps?: any;
confirmNewPasswordProps?: any;
}
Expand Down
Loading