Skip to content

Commit fb5d0f8

Browse files
feat: show password toggle
1 parent 1b1f418 commit fb5d0f8

File tree

1 file changed

+22
-12
lines changed
  • packages/ui-library/src/components/text-input

1 file changed

+22
-12
lines changed

packages/ui-library/src/components/text-input/index.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { LitElement, html } from 'lit';
22
import { classMap } from 'lit/directives/class-map.js';
3-
import { customElement, property } from 'lit/decorators.js';
3+
import { customElement, property, state } from 'lit/decorators.js';
44
import { styleCustom } from './index.css';
55
import { form } from '../../foundation/semantic-tokens/form.css';
66
import { InputTypes, FormSizesType } from '../../globals/types';
77
import { BlrFormLabel } from '../form-label';
88
import { BlrFormHint } from '../form-hint';
99
import { IconType } from '@boiler/icons';
10+
import { iconButton } from '../../foundation/component-tokens/action.css';
1011

1112
@customElement('blr-text-input')
1213
export class BlrTextInput extends LitElement {
13-
static styles = [styleCustom, form];
14+
static styles = [styleCustom, form, iconButton];
1415

1516
@property() textInputId!: string;
1617
@property() type: InputTypes = 'text';
@@ -30,17 +31,21 @@ export class BlrTextInput extends LitElement {
3031
@property() hint?: string;
3132
@property() hintIcon?: IconType;
3233

34+
@state() protected currentType: InputTypes = this.type;
35+
3336
toggleInputType() {
34-
this.type = this.type === 'password' ? 'text' : 'password';
37+
this.currentType = this.currentType === 'password' ? 'text' : 'password';
3538
}
3639

3740
render() {
41+
const wasInitialPasswordField = Boolean(this.type === 'password');
42+
3843
const classes = classMap({
3944
[`${this.size}`]: this.size || 'md',
4045
[`disabled`]: this.disabled || false,
4146
});
4247

43-
const inputclasses = classMap({
48+
const inputClasses = classMap({
4449
[`error`]: this.hasError || false,
4550
[`error-input`]: this.hasError || false,
4651
[`${this.size}`]: this.size || 'md',
@@ -50,9 +55,9 @@ export class BlrTextInput extends LitElement {
5055
<div class="blr-input ${classes}">
5156
${BlrFormLabel({ labelText: this.label })}
5257
<input
53-
class="blr-form-element ${inputclasses}"
58+
class="blr-form-element ${inputClasses}"
5459
id=${this.textInputId}
55-
type="${this.type}"
60+
type="${this.currentType}"
5661
value="${this.value}"
5762
placeholder="${this.placeholder}"
5863
?disabled="${this.disabled}"
@@ -64,12 +69,17 @@ export class BlrTextInput extends LitElement {
6469
pattern="${this.pattern}"
6570
hasError="${this.hasError}"
6671
/>
67-
<blr-icon
68-
class="blr-input-icon ${inputclasses}"
69-
@click=${this.toggleInputType}
70-
name="${this.type.includes('password') ? 'blrEyeSm' : this.hasError ? 'blrFlagSm' : this.hintIcon}"
71-
aria-hidden
72-
></blr-icon>
72+
73+
${wasInitialPasswordField
74+
? html`<blr-icon
75+
class="blr-input-icon"
76+
@click=${this.toggleInputType}
77+
icon="${this.currentType.includes('password') ? 'blrCloseSm' : 'blrEyeSm'}"
78+
size="md"
79+
name="${this.currentType.includes('password') ? 'blrEyeSm' : this.hasError ? 'blrFlagSm' : this.hintIcon}"
80+
aria-hidden
81+
></blr-icon>`
82+
: html``}
7383
${BlrFormHint({
7484
message: this.hasError ? this.errorMessage : this.hint,
7585
variant: this.hasError ? 'error' : 'hint',

0 commit comments

Comments
 (0)