Skip to content

Commit

Permalink
fix(PasswordFiled): fix password readonly visibility (#2677)
Browse files Browse the repository at this point in the history
* fix(PasswordFiled): fix password readonly visibility

* test: improve tests for readOnly and disabled states

* test: improve test uniformity and coverage

* test: fix typo, format and lint the code

---------

Co-authored-by: serge-dynamic <[email protected]>
  • Loading branch information
serge-st and serge-dynamic authored Jan 9, 2024
1 parent 1e229b4 commit bca5ba9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-roses-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/password-field': patch
---

Fix password visibility in Disabled and ReadOnly states
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,68 @@ describe('when disabled', () => {
const { getByLabelText } = renderPasswordField({ isDisabled: true });
expect(getByLabelText('PasswordField')).toBeDisabled();
});
it('should set the input type to password', () => {
const { getByLabelText } = renderPasswordField({ isDisabled: true });
expect(getByLabelText('PasswordField')).toHaveAttribute('type', 'password');
});
describe('when has value', () => {
it('should set the input type to password', () => {
const { getByLabelText } = renderPasswordField({
value: 'foo',
isDisabled: true,
});
expect(getByLabelText('PasswordField')).toHaveAttribute(
'type',
'password'
);
});
});
});

describe('when readOnly', () => {
it('should disable the input', () => {
const { getByLabelText } = renderPasswordField({ isReadOnly: true });
expect(getByLabelText('PasswordField')).toHaveAttribute('readonly');
});
it('should set the input type to password', () => {
const { getByLabelText } = renderPasswordField({ isReadOnly: true });
expect(getByLabelText('PasswordField')).toHaveAttribute('type', 'password');
});
describe('when has value', () => {
it('should set the input type to password', () => {
const { getByLabelText } = renderPasswordField({
value: 'foo',
isReadOnly: true,
});
expect(getByLabelText('PasswordField')).toHaveAttribute(
'type',
'password'
);
});
});
});

describe('when disabled and readOnly', () => {
it('should set the input type to password', () => {
const { getByLabelText } = renderPasswordField({
isDisabled: true,
isReadOnly: true,
});
expect(getByLabelText('PasswordField')).toHaveAttribute('type', 'password');
});
describe('when has value', () => {
it('should set the input type to password', () => {
const { getByLabelText } = renderPasswordField({
value: 'foo',
isDisabled: true,
isReadOnly: true,
});
expect(getByLabelText('PasswordField')).toHaveAttribute(
'type',
'password'
);
});
});
});

describe('when required', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const PasswordField = (props: TPasswordField) => {
const [isPasswordVisible, togglePasswordVisibility] = useToggleState(false);
const id = useFieldId(props.id, sequentialId);
const hasError = props.touched && hasErrors(props.errors);
const canInteract = !props.isDisabled && !props.isReadOnly;

if (!props.isReadOnly) {
warning(
Expand Down Expand Up @@ -205,7 +206,7 @@ const PasswordField = (props: TPasswordField) => {
onInfoButtonClick={props.onInfoButtonClick}
hasRequiredIndicator={props.isRequired}
/>
{!props.isDisabled && !props.isReadOnly && (
{canInteract && (
<FlatButton
icon={isPasswordVisible ? <EyeCrossedIcon /> : <EyeIcon />}
label={
Expand All @@ -228,7 +229,7 @@ const PasswordField = (props: TPasswordField) => {
onBlur={props.onBlur}
onFocus={props.onFocus}
isAutofocussed={props.isAutofocussed}
isPasswordVisible={isPasswordVisible}
isPasswordVisible={canInteract ? isPasswordVisible : false}
isDisabled={props.isDisabled}
isReadOnly={props.isReadOnly}
hasError={hasError}
Expand Down

1 comment on commit bca5ba9

@vercel
Copy link

@vercel vercel bot commented on bca5ba9 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.