From d9260799c8c68e9adb8c2dbf974a1d40a908c472 Mon Sep 17 00:00:00 2001 From: Zacky Ma Date: Tue, 9 Jun 2026 13:49:45 -0700 Subject: [PATCH 1/8] fix autofocus for text input --- .../web-components/docs/web-components.api.md | 2 - .../src/text-input/text-input.base.ts | 38 ++----------------- .../src/text-input/text-input.template.ts | 6 +-- .../web-components/src/utils/autofocus.ts | 13 +++++++ 4 files changed, 17 insertions(+), 42 deletions(-) create mode 100644 packages/web-components/src/utils/autofocus.ts diff --git a/packages/web-components/docs/web-components.api.md b/packages/web-components/docs/web-components.api.md index ae1de40ebd7f8..b1f813bf5676e 100644 --- a/packages/web-components/docs/web-components.api.md +++ b/packages/web-components/docs/web-components.api.md @@ -1011,7 +1011,6 @@ export class BaseTextArea extends FASTElement { // @public export class BaseTextInput extends FASTElement { autocomplete?: string; - autofocus: boolean; // @internal changeHandler(e: InputEvent): boolean | void; checkValidity(): boolean; @@ -1035,7 +1034,6 @@ export class BaseTextInput extends FASTElement { disabled?: boolean; // @internal elementInternals: ElementInternals; - focusinHandler(e: FocusEvent): boolean | void; get form(): HTMLFormElement | null; static readonly formAssociated = true; formAttribute?: string; diff --git a/packages/web-components/src/text-input/text-input.base.ts b/packages/web-components/src/text-input/text-input.base.ts index dfcb652490ca9..9040df0566252 100644 --- a/packages/web-components/src/text-input/text-input.base.ts +++ b/packages/web-components/src/text-input/text-input.base.ts @@ -1,12 +1,5 @@ -import { - attr, - FASTElement, - nullableNumberConverter, - Observable, - observable, - type Subscriber, - Updates, -} from '@microsoft/fast-element'; +import { attr, FASTElement, nullableNumberConverter, Observable, observable, Updates } from '@microsoft/fast-element'; +import { setAutoFocus } from '../utils/autofocus.js'; import { ImplicitSubmissionBlockingTypes, TextInputType } from './text-input.options.js'; /** @@ -33,17 +26,6 @@ export class BaseTextInput extends FASTElement { @attr public autocomplete?: string; - /** - * Indicates that the element should get focus after the page finishes loading. - * @see The {@link https://developer.mozilla.org/docs/Web/HTML/Element/input#autofocus | `autofocus`} attribute - * - * @public - * @remarks - * HTML Attribute: `autofocus` - */ - @attr({ mode: 'boolean' }) - public autofocus!: boolean; - /** * The current value of the input. * @public @@ -463,24 +445,10 @@ export class BaseTextInput extends FASTElement { public connectedCallback(): void { super.connectedCallback(); - this.tabIndex = Number(this.getAttribute('tabindex') ?? 0) < 0 ? -1 : 0; - this.setFormValue(this.value); this.setValidity(); - } - /** - * Focuses the inner control when the component is focused. - * - * @param e - the event object - * @public - */ - public focusinHandler(e: FocusEvent): boolean | void { - if (e.target === this) { - this.control?.focus(); - } - - return true; + setAutoFocus(this); } /** diff --git a/packages/web-components/src/text-input/text-input.template.ts b/packages/web-components/src/text-input/text-input.template.ts index 5a2e701d24893..be19023a99857 100644 --- a/packages/web-components/src/text-input/text-input.template.ts +++ b/packages/web-components/src/text-input/text-input.template.ts @@ -10,10 +10,7 @@ import type { TextInputOptions } from './text-input.options.js'; */ export function textInputTemplate(options: TextInputOptions = {}): ElementViewTemplate { return html` -