Skip to content

Commit

Permalink
PasswordField - adding a new prop to render the "Show/Hide" button (#…
Browse files Browse the repository at this point in the history
…2759)

* chore: added renderShowHideButton prop to PasswordField component

* chore: updated README

* chore: test update

* chore: changeset

* chore: comment update
  • Loading branch information
chloe0592 committed Mar 26, 2024
1 parent 4c7fff5 commit 3f91547
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-months-bow.md
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/password-field': patch
---

Adding a prop to `PasswordField` that determines whether to render the "Show/Hide" button for password.
1 change: 1 addition & 0 deletions packages/components/fields/password-field/README.md
Expand Up @@ -71,6 +71,7 @@ export default Example;
| `onInfoButtonClick` | `Function`<br/>[See signature.](#signature-onInfoButtonClick) | | | Function called when info button is pressed.&#xA;<br />&#xA;Info button will only be visible when this prop is passed. |
| `hintIcon` | `ReactElement` | | | Icon to be displayed beside the hint text.&#xA;<br />&#xA;Will only get rendered when `hint` is passed as well. |
| `badge` | `ReactNode` | | | Badge to be displayed beside the label.&#xA;<br />&#xA;Might be used to display additional information about the content of the field (E.g verified email) |
| `renderShowHideButton` | `boolean` | | `true` | Determines whether to render the "Show/Hide" button for the password field. |

## Signatures

Expand Down
Expand Up @@ -92,6 +92,7 @@ storiesOf('Components|Fields', module)
}
hintIcon={hintIcon}
badge={text('badge', '')}
renderShowHideButton={boolean('renderShowHideButton', true)}
/>
);
}}
Expand Down
13 changes: 11 additions & 2 deletions packages/components/fields/password-field/src/password-field.tsx
Expand Up @@ -176,6 +176,10 @@ export type TPasswordField = {
* Might be used to display additional information about the content of the field (E.g verified email)
*/
badge?: ReactNode;
/**
* Determines whether to render the "Show/Hide" button for the password field.
*/
renderShowHideButton?: boolean;
};

type TTogglePasswordVisibilityHandler = (
Expand All @@ -185,8 +189,12 @@ type TTogglePasswordVisibilityHandler = (
| KeyboardEvent<HTMLButtonElement>
) => void;

const defaultProps: Pick<TPasswordField, 'horizontalConstraint'> = {
const defaultProps: Pick<
TPasswordField,
'horizontalConstraint' | 'renderShowHideButton'
> = {
horizontalConstraint: 'scale',
renderShowHideButton: true,
};

const PasswordField = (props: TPasswordField) => {
Expand All @@ -195,7 +203,8 @@ const PasswordField = (props: TPasswordField) => {
const id = useFieldId(props.id, sequentialId);
const hasError = props.touched && hasErrors(props.errors);
const hasWarning = props.touched && hasWarnings(props.warnings);
const canInteract = !props.isDisabled && !props.isReadOnly;
const canInteract =
!props.isDisabled && !props.isReadOnly && props.renderShowHideButton;

if (!props.isReadOnly) {
warning(
Expand Down
Expand Up @@ -102,5 +102,14 @@ export const component = () => (
renderWarning={() => 'Custom warning'}
/>
</Spec>
<Spec label="with not rendered show/hide password button">
<PasswordField
title="Welcome Text"
value={value}
onChange={() => {}}
horizontalConstraint={7}
renderShowHideButton={false}
/>
</Spec>
</Suite>
);

0 comments on commit 3f91547

Please sign in to comment.