Skip to content

Commit

Permalink
Merge pull request #1486 from Availity/feat/change-password-addtl-fields
Browse files Browse the repository at this point in the history
feat(change-password): add additionalFields prop
  • Loading branch information
nylon22 authored Nov 27, 2024
2 parents 153a589 + 1cb9088 commit d92aae5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
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 @@ const ChangePasswordForm = ({
onHandleSubmit,
onError,
additionalButtons,
additionalFields,
header,
maxLength = 30,
currentPasswordProps,
showCurrentPassword = true,
newPasswordProps,
confirmNewPasswordProps,
}) => {
Expand All @@ -41,11 +43,11 @@ const ChangePasswordForm = ({
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 @@ const ChangePasswordForm = ({
<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);
}
}}
tabIndex={0}
aria-label={currentPasswordVisible ? 'Hide Password' : 'Show Password'}
ref={currentPasswordIconRef}
/>
</div>
) : null
}

<div className="password-with-icon">
<Field
Expand Down Expand Up @@ -183,6 +190,7 @@ const ChangePasswordForm = ({
ref={confirmNewPasswordIconRef}
/>
</div>
{additionalFields}
</Col>
<Col>
<ChangePasswordFeedback />
Expand All @@ -208,9 +216,11 @@ ChangePasswordForm.propTypes = {
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

0 comments on commit d92aae5

Please sign in to comment.