ion-input-password-toggle.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
  5. import { a as printIonWarning } from './index4.js';
  6. import { c as createColorClasses } from './theme.js';
  7. import { l as eye, m as eyeOff } from './index7.js';
  8. import { b as getIonMode } from './ionic-global.js';
  9. import { d as defineCustomElement$4 } from './button.js';
  10. import { d as defineCustomElement$3 } from './icon.js';
  11. import { d as defineCustomElement$2 } from './ripple-effect.js';
  12. const iosInputPasswordToggleCss = "";
  13. const IonInputPasswordToggleIosStyle0 = iosInputPasswordToggleCss;
  14. const mdInputPasswordToggleCss = "";
  15. const IonInputPasswordToggleMdStyle0 = mdInputPasswordToggleCss;
  16. const InputPasswordToggle = /*@__PURE__*/ proxyCustomElement(class InputPasswordToggle extends HTMLElement {
  17. constructor() {
  18. super();
  19. this.__registerHost();
  20. this.__attachShadow();
  21. this.togglePasswordVisibility = () => {
  22. const { inputElRef } = this;
  23. if (!inputElRef) {
  24. return;
  25. }
  26. inputElRef.type = inputElRef.type === 'text' ? 'password' : 'text';
  27. };
  28. this.color = undefined;
  29. this.showIcon = undefined;
  30. this.hideIcon = undefined;
  31. this.type = 'password';
  32. }
  33. /**
  34. * Whenever the input type changes we need to re-run validation to ensure the password
  35. * toggle is being used with the correct input type. If the application changes the type
  36. * outside of this component we also need to re-render so the correct icon is shown.
  37. */
  38. onTypeChange(newValue) {
  39. if (newValue !== 'text' && newValue !== 'password') {
  40. printIonWarning(`[ion-input-password-toggle] - Only inputs of type "text" or "password" are supported. Input of type "${newValue}" is not compatible.`, this.el);
  41. return;
  42. }
  43. }
  44. connectedCallback() {
  45. const { el } = this;
  46. const inputElRef = (this.inputElRef = el.closest('ion-input'));
  47. if (!inputElRef) {
  48. printIonWarning('[ion-input-password-toggle] - No ancestor ion-input found. This component must be slotted inside of an ion-input.', el);
  49. return;
  50. }
  51. /**
  52. * Important: Set the type in connectedCallback because the default value
  53. * of this.type may not always be accurate. Usually inputs have the "password" type
  54. * but it is possible to have the input to initially have the "text" type. In that scenario
  55. * the wrong icon will show briefly before switching to the correct icon. Setting the
  56. * type here allows us to avoid that flicker.
  57. */
  58. this.type = inputElRef.type;
  59. }
  60. disconnectedCallback() {
  61. this.inputElRef = null;
  62. }
  63. render() {
  64. var _a, _b;
  65. const { color, type } = this;
  66. const mode = getIonMode(this);
  67. const showPasswordIcon = (_a = this.showIcon) !== null && _a !== void 0 ? _a : eye;
  68. const hidePasswordIcon = (_b = this.hideIcon) !== null && _b !== void 0 ? _b : eyeOff;
  69. const isPasswordVisible = type === 'text';
  70. return (h(Host, { key: '1a28e078c83e74c72d8bb8189ece93ec2e3fa3d0', class: createColorClasses(color, {
  71. [mode]: true,
  72. }) }, h("ion-button", { key: '039d1bab764093bb6fe4a34299b0872abda087fd', mode: mode, color: color, fill: "clear", shape: "round", "aria-checked": isPasswordVisible ? 'true' : 'false', "aria-label": "show password", role: "switch", type: "button", onPointerDown: (ev) => {
  73. /**
  74. * This prevents mobile browsers from
  75. * blurring the input when the password toggle
  76. * button is activated.
  77. */
  78. ev.preventDefault();
  79. }, onClick: this.togglePasswordVisibility }, h("ion-icon", { key: '26477ee97b808c3d79944bf5e33d4e05f1ae0b3f', slot: "icon-only", "aria-hidden": "true", icon: isPasswordVisible ? hidePasswordIcon : showPasswordIcon }))));
  80. }
  81. get el() { return this; }
  82. static get watchers() { return {
  83. "type": ["onTypeChange"]
  84. }; }
  85. static get style() { return {
  86. ios: IonInputPasswordToggleIosStyle0,
  87. md: IonInputPasswordToggleMdStyle0
  88. }; }
  89. }, [33, "ion-input-password-toggle", {
  90. "color": [513],
  91. "showIcon": [1, "show-icon"],
  92. "hideIcon": [1, "hide-icon"],
  93. "type": [1025]
  94. }, undefined, {
  95. "type": ["onTypeChange"]
  96. }]);
  97. function defineCustomElement$1() {
  98. if (typeof customElements === "undefined") {
  99. return;
  100. }
  101. const components = ["ion-input-password-toggle", "ion-button", "ion-icon", "ion-ripple-effect"];
  102. components.forEach(tagName => { switch (tagName) {
  103. case "ion-input-password-toggle":
  104. if (!customElements.get(tagName)) {
  105. customElements.define(tagName, InputPasswordToggle);
  106. }
  107. break;
  108. case "ion-button":
  109. if (!customElements.get(tagName)) {
  110. defineCustomElement$4();
  111. }
  112. break;
  113. case "ion-icon":
  114. if (!customElements.get(tagName)) {
  115. defineCustomElement$3();
  116. }
  117. break;
  118. case "ion-ripple-effect":
  119. if (!customElements.get(tagName)) {
  120. defineCustomElement$2();
  121. }
  122. break;
  123. } });
  124. }
  125. const IonInputPasswordToggle = InputPasswordToggle;
  126. const defineCustomElement = defineCustomElement$1;
  127. export { IonInputPasswordToggle, defineCustomElement };