Skip to content

Commit 5dd36c1

Browse files
authored
Feat: uui-input-lock: Only focus input when unlocked (#1096)
* implement tabIndex for input * prevent tabbing into input when locked
1 parent f60c2f4 commit 5dd36c1

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/uui-input-lock/lib/uui-input-lock.element.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,37 @@ export class UUIInputLockElement extends UUIInputElement {
2222
* @default true
2323
*/
2424
@property({ type: Boolean, reflect: true })
25-
public locked: boolean = true;
25+
public set locked(lock: boolean) {
26+
this.#locked = lock;
27+
this.tabIndex = lock ? -1 : 0;
28+
}
29+
public get locked(): boolean {
30+
return this.#locked;
31+
}
32+
#locked: boolean = true;
33+
34+
/**
35+
* Define the label for the unlock button.
36+
* @type {string}
37+
* @attr
38+
* @default true
39+
*/
40+
@property({ type: String, reflect: false, attribute: 'unlock-label' })
41+
public unlockLabel: string = 'Unlock input';
42+
43+
/**
44+
* Define the label for the lock button.
45+
* @type {string}
46+
* @attr
47+
* @default true
48+
*/
49+
@property({ type: String, reflect: false, attribute: 'lock-label' })
50+
public lockLabel: string = 'Lock input';
2651

2752
constructor() {
2853
super();
2954
this.readonly = true;
55+
this.tabIndex = -1;
3056
}
3157

3258
connectedCallback(): void {
@@ -40,6 +66,9 @@ export class UUIInputLockElement extends UUIInputElement {
4066
this.readonly = this.locked = !this.locked;
4167
this.pristine = false;
4268
this.dispatchEvent(new UUIInputLockEvent(UUIInputLockEvent.LOCK_CHANGE));
69+
if (!this.locked) {
70+
this._input?.focus();
71+
}
4372
}
4473

4574
renderIcon() {
@@ -56,7 +85,7 @@ export class UUIInputLockElement extends UUIInputElement {
5685
@click=${this._onLockToggle}
5786
compact
5887
id="lock"
59-
label="${this.locked ? 'Unlock input' : 'Lock input'}">
88+
label="${this.locked ? this.unlockLabel : this.lockLabel}">
6089
${this.renderIcon()}
6190
</uui-button>`;
6291
}

packages/uui-input/lib/uui-input.element.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ export class UUIInputElement extends UUIFormControlMixin(
214214
@property({ type: String })
215215
pattern?: string;
216216

217+
/**
218+
* Set the input tabindex, set this to `-1` to avoid tabbing into the input.
219+
* @type {number}
220+
* @attr
221+
*/
222+
@property({ type: Number, reflect: false, attribute: 'tabindex' })
223+
tabIndex: number = 0;
224+
217225
@query('#input')
218226
_input!: HTMLInputElement;
219227

@@ -349,6 +357,7 @@ export class UUIInputElement extends UUIFormControlMixin(
349357
?autofocus=${this.autofocus}
350358
?required=${this.required}
351359
?readonly=${this.readonly}
360+
tabindex=${ifDefined(this.tabIndex)}
352361
@input=${this.onInput}
353362
@change=${this.onChange} />`;
354363
}

0 commit comments

Comments
 (0)