Skip to content

Commit

Permalink
fix: added readonly state variant to passwordInput (#17031)
Browse files Browse the repository at this point in the history
* fix: added readonly state variant to passwordInput

* fix: disabled toggle button for readonly

* Update packages/react/src/components/TextInput/PasswordInput.tsx

Co-authored-by: Alison Joseph <alisonejoseph@Gmail.com>

* fix: icon for disable and readonly state

* fix: cursor

---------

Co-authored-by: Alison Joseph <alisonejoseph@Gmail.com>
  • Loading branch information
riddhybansal and alisonjoseph committed Aug 14, 2024
1 parent 185c4cf commit 695ef81
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6190,6 +6190,9 @@ Map {
"placeholder": Object {
"type": "string",
},
"readOnly": Object {
"type": "bool",
},
"showPasswordLabel": Object {
"type": "string",
},
Expand Down Expand Up @@ -8848,6 +8851,9 @@ Map {
"placeholder": Object {
"type": "string",
},
"readOnly": Object {
"type": "bool",
},
"showPasswordLabel": Object {
"type": "string",
},
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/components/TextInput/PasswordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ const PasswordInput = React.forwardRef(function PasswordInput(
placeholder,
type: inputType,
className: textInputClasses,
readOnly,
ref,
...rest,
};
Expand All @@ -250,6 +251,7 @@ const PasswordInput = React.forwardRef(function PasswordInput(
`${prefix}--text-input-wrapper`,
`${prefix}--password-input-wrapper`,
{
[`${prefix}--text-input-wrapper--readonly`]: readOnly,
[`${prefix}--text-input-wrapper--light`]: light,
[`${prefix}--text-input-wrapper--inline`]: inline,
[`${prefix}--text-input--fluid`]: isFluid,
Expand Down Expand Up @@ -362,7 +364,7 @@ const PasswordInput = React.forwardRef(function PasswordInput(
<button
type="button"
className={passwordVisibilityToggleClasses}
disabled={disabled}
disabled={disabled || readOnly}
onClick={handleTogglePasswordVisibility}>
{passwordVisibilityIcon}
</button>
Expand Down Expand Up @@ -448,6 +450,11 @@ PasswordInput.propTypes = {
*/
invalid: PropTypes.bool,

/**
* Whether the PasswordInput should be read-only
*/
readOnly: PropTypes.bool,

/**
* Provide the text that is displayed when the control is in an invalid state
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,45 @@ describe('PasswordInput', () => {
screen.getByLabelText('PasswordInput label')
);
});

it('should render input as read-only when readOnly is true', () => {
const { getByPlaceholderText } = render(
<PasswordInput
id="input-1"
labelText="PasswordInput label"
placeholder="Enter password"
readOnly
/>
);
const inputElement = getByPlaceholderText('Enter password');
expect(inputElement).toHaveAttribute('readonly');
});

it('should disable hide toggle button when readOnly is true', () => {
const { getByRole } = render(
<PasswordInput
id="input-1"
labelText="PasswordInput label"
placeholder="Enter password"
readOnly
/>
);
const toggleButton = getByRole('button');
expect(toggleButton).toBeDisabled();
});

it('should not allow input change when readOnly is true', () => {
const { getByPlaceholderText } = render(
<PasswordInput
id="input-1"
labelText="PasswordInput label"
placeholder="Enter password"
readOnly
/>
);
const inputElement = getByPlaceholderText('Enter password');
userEvent.type(inputElement, 'newpassword');
expect(inputElement.value).toBe('');
});
});
});
17 changes: 17 additions & 0 deletions packages/styles/scss/components/text-input/_text-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@
}
}

.#{$prefix}--text-input--password__visibility__toggle:disabled.#{$prefix}--tooltip__trigger {
svg {
fill: $icon-disabled;
}

cursor: default;
}

.#{$prefix}--text-input--password__visibility__toggle:disabled.#{$prefix}--tooltip__trigger:hover {
svg {
fill: $icon-disabled;
}

cursor: default;
}

.#{$prefix}--text-input__counter-alert {
position: absolute;
overflow: hidden;
Expand Down Expand Up @@ -354,6 +370,7 @@
> .#{$prefix}--text-input--warning:focus {
outline: none;
}

//-----------------------------
// Inline Text Input
//-----------------------------
Expand Down

0 comments on commit 695ef81

Please sign in to comment.