feat(input): add input-password-toggle component (#29175)

Issue number: Internal

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

When given a password input it is hard to know what users are typing as
the contents of the input are obscured. As a result, it is a common
pattern to have a button that lets users temporarily toggle the
visibility of the password so they can correct any mistakes. Ionic
currently has the infrastructure for developers to implement this on
their own, but this use case is so common that the team thinks it is
worth having this functionality built-in to Ionic.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Introduces the `ion-input-password-toggle` component. This component
is a button that toggles the visibility of the text in the input it is
slotted into.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!--
  If this introduces a breaking change:
1. Describe the impact and migration path for existing applications
below.
  2. Update the BREAKING.md file with the breaking change.
3. Add "BREAKING CHANGE: [...]" to the commit description when merging.
See
https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer
for more information.
-->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->


⚠️ Give co-author credit to
https://github.com/ionic-team/ionic-framework/pull/29141 on merge.

Docs PR: https://github.com/ionic-team/ionic-docs/pull/3541

Note: We did not do the approach listed in the other PR due to
https://github.com/ionic-team/ionic-framework/pull/29141#discussion_r1523631811.

---------

Co-authored-by: OS-giulianasilva <OS-giulianasilva@users.noreply.github.com>
This commit is contained in:
Liam DeBeasi
2024-03-25 13:22:06 -04:00
committed by GitHub
parent 6e477b743e
commit 6c500fd6b2
19 changed files with 563 additions and 4 deletions

View File

@ -605,3 +605,12 @@
margin-inline-start: $form-control-label-margin;
margin-inline-end: 0;
}
/**
* The input password toggle component should be hidden when the input is readonly/disabled
* because it is not possible to edit a password.
*/
:host([disabled]) ::slotted(ion-input-password-toggle),
:host([readonly]) ::slotted(ion-input-password-toggle) {
display: none;
}

View File

@ -130,7 +130,7 @@ export class Input implements ComponentInterface {
/**
* If `true`, the user cannot interact with the input.
*/
@Prop() disabled = false;
@Prop({ reflect: true }) disabled = false;
/**
* A hint to the browser for which enter key to display.
@ -226,7 +226,7 @@ export class Input implements ComponentInterface {
/**
* If `true`, the user cannot modify the value.
*/
@Prop() readonly = false;
@Prop({ reflect: true }) readonly = false;
/**
* If `true`, the user must fill in a value before submitting a form.
@ -254,6 +254,20 @@ export class Input implements ComponentInterface {
*/
@Prop() type: TextFieldTypes = 'text';
/**
* Whenever the type on the input changes we need
* to update the internal type prop on the password
* toggle so that that correct icon is shown.
*/
@Watch('type')
onTypeChange() {
const passwordToggle = this.el.querySelector('ion-input-password-toggle');
if (passwordToggle) {
passwordToggle.type = this.type;
}
}
/**
* The value of the input.
*/
@ -344,6 +358,14 @@ export class Input implements ComponentInterface {
componentDidLoad() {
this.originalIonInput = this.ionInput;
/**
* Set the type on the password toggle in the event that this input's
* type was set async and does not match the default type for the password toggle.
* This can happen when the type is bound using a JS framework binding syntax
* such as [type] in Angular.
*/
this.onTypeChange();
}
componentDidRender() {