|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | + |
| 3 | +class default_1 extends Controller { |
| 4 | + constructor() { |
| 5 | + super(...arguments); |
| 6 | + this.isDisplayed = false; |
| 7 | + this.visibleIcon = `<svg xmlns="http://www.w3.org/2000/svg" class="toggle-password-icon" viewBox="0 0 20 20" fill="currentColor"> |
| 8 | +<path d="M10 12a2 2 0 100-4 2 2 0 000 4z" /> |
| 9 | +<path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd" /> |
| 10 | +</svg>`; |
| 11 | + this.hiddenIcon = `<svg xmlns="http://www.w3.org/2000/svg" class="toggle-password-icon" viewBox="0 0 20 20" fill="currentColor"> |
| 12 | +<path fill-rule="evenodd" d="M3.707 2.293a1 1 0 00-1.414 1.414l14 14a1 1 0 001.414-1.414l-1.473-1.473A10.014 10.014 0 0019.542 10C18.268 5.943 14.478 3 10 3a9.958 9.958 0 00-4.512 1.074l-1.78-1.781zm4.261 4.26l1.514 1.515a2.003 2.003 0 012.45 2.45l1.514 1.514a4 4 0 00-5.478-5.478z" clip-rule="evenodd" /> |
| 13 | +<path d="M12.454 16.697L9.75 13.992a4 4 0 01-3.742-3.741L2.335 6.578A9.98 9.98 0 00.458 10c1.274 4.057 5.065 7 9.542 7 .847 0 1.669-.105 2.454-.303z" /> |
| 14 | +</svg>`; |
| 15 | + } |
| 16 | + connect() { |
| 17 | + if (this.visibleIconValue !== 'Default') { |
| 18 | + this.visibleIcon = this.visibleIconValue; |
| 19 | + } |
| 20 | + if (this.hiddenIconValue !== 'Default') { |
| 21 | + this.hiddenIcon = this.hiddenIconValue; |
| 22 | + } |
| 23 | + const button = this.createButton(); |
| 24 | + this.element.insertAdjacentElement('afterend', button); |
| 25 | + this.dispatchEvent('connect', { element: this.element, button: button }); |
| 26 | + } |
| 27 | + createButton() { |
| 28 | + const button = document.createElement('button'); |
| 29 | + button.type = 'button'; |
| 30 | + button.classList.add(...this.buttonClassesValue); |
| 31 | + button.setAttribute('tabindex', '-1'); |
| 32 | + button.addEventListener('click', this.toggle.bind(this)); |
| 33 | + button.innerHTML = this.visibleIcon + ' ' + this.visibleLabelValue; |
| 34 | + return button; |
| 35 | + } |
| 36 | + toggle(event) { |
| 37 | + this.isDisplayed = !this.isDisplayed; |
| 38 | + const toggleButtonElement = event.currentTarget; |
| 39 | + toggleButtonElement.innerHTML = this.isDisplayed |
| 40 | + ? this.hiddenIcon + ' ' + this.hiddenLabelValue |
| 41 | + : this.visibleIcon + ' ' + this.visibleLabelValue; |
| 42 | + this.element.setAttribute('type', this.isDisplayed ? 'text' : 'password'); |
| 43 | + this.dispatchEvent(this.isDisplayed ? 'show' : 'hide', { element: this.element, button: toggleButtonElement }); |
| 44 | + } |
| 45 | + dispatchEvent(name, payload) { |
| 46 | + this.dispatch(name, { detail: payload, prefix: 'toggle-password' }); |
| 47 | + } |
| 48 | +} |
| 49 | +default_1.values = { |
| 50 | + visibleLabel: String, |
| 51 | + visibleIcon: String, |
| 52 | + hiddenLabel: String, |
| 53 | + hiddenIcon: String, |
| 54 | + buttonClasses: Array, |
| 55 | +}; |
| 56 | + |
| 57 | +export { default_1 as default }; |
0 commit comments