From 216125051d2d51e59cd03c86ac82b1ea78a341a7 Mon Sep 17 00:00:00 2001 From: rdlabo Date: Fri, 20 Nov 2026 13:45:30 +0900 Subject: [PATCH 1/4] Preserve inherited searchbar focus methods in native tabs --- .../src/native/components/searchable-tabs.ts | 14 +++++++-- .../tests/native-ui-search.spec.ts | 30 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/native-ui-core/src/native/components/searchable-tabs.ts b/packages/native-ui-core/src/native/components/searchable-tabs.ts index 9c2dcea..4af8f10 100644 --- a/packages/native-ui-core/src/native/components/searchable-tabs.ts +++ b/packages/native-ui-core/src/native/components/searchable-tabs.ts @@ -82,6 +82,7 @@ export const createSearchSupport = (doc: Document, id: (element: Element) => str state.bar.querySelector('input.searchbar-input') === state.input; const install = (binding: NativeSearchBinding, bar: HTMLIonSearchbarElement, input: HTMLInputElement): SearchState => { const originalFocus = bar.setFocus; + const ownFocus = Object.getOwnPropertyDescriptor(bar, 'setFocus'); const ownValue = Object.getOwnPropertyDescriptor(bar, 'value'); let owner: object | null = bar; let value = ownValue; @@ -90,7 +91,13 @@ export const createSearchSupport = (doc: Document, id: (element: Element) => str if (projected(binding) && (await requestNativeSearch(binding, true, true))) return; await originalFocus.call(bar); }; - bar.setFocus = focus; + // Stencil's lazy loader exposes setFocus as a read-only prototype method. + const wrappedFocus = Reflect.defineProperty(bar, 'setFocus', { + configurable: ownFocus?.configurable ?? true, + enumerable: ownFocus?.enumerable ?? false, + writable: true, + value: focus, + }); let wrapped: PropertyDescriptor | undefined; if (value?.get && value.set && ownValue?.configurable !== false) { const descriptor = value; @@ -118,7 +125,10 @@ export const createSearchSupport = (doc: Document, id: (element: Element) => str valueVersion: 0, applyingInput: false, restore: () => { - if (bar.setFocus === focus) bar.setFocus = originalFocus; + if (wrappedFocus && Object.getOwnPropertyDescriptor(bar, 'setFocus')?.value === focus) { + if (ownFocus) Object.defineProperty(bar, 'setFocus', ownFocus); + else Reflect.deleteProperty(bar, 'setFocus'); + } if (wrapped && Object.getOwnPropertyDescriptor(bar, 'value')?.set === wrapped.set) { if (ownValue) Object.defineProperty(bar, 'value', ownValue); else Reflect.deleteProperty(bar, 'value'); diff --git a/packages/native-ui-core/tests/native-ui-search.spec.ts b/packages/native-ui-core/tests/native-ui-search.spec.ts index 551b2a1..da1ed42 100644 --- a/packages/native-ui-core/tests/native-ui-search.spec.ts +++ b/packages/native-ui-core/tests/native-ui-search.spec.ts @@ -1,6 +1,6 @@ import { afterEach, expect, test, vi } from 'vitest'; import { createSearchSupport } from '../src/native/components/searchable-tabs'; -import { registerNativeSearch } from '../src/native-integration'; +import { registerNativeSearch, setNativeUIIntegration } from '../src/native-integration'; import { createCandidate } from '../src/native/shared/candidate'; afterEach(() => { @@ -173,3 +173,31 @@ test('an unavailable cached search cannot cover its visible Web trigger but surv search.destroy(); } }); + +test('lazy searchbars restore inherited readonly focus across repeated native search registrations', async () => { + const { bar, page, footer, trigger, tabs, candidates, search } = setup(); + const webFocus = vi.fn(async () => {}); + Reflect.deleteProperty(bar, 'setFocus'); + Object.setPrototypeOf(bar, Object.create(Object.getPrototypeOf(bar), { setFocus: { value: webFocus } })); + const nativeFocus = vi.fn(async () => true); + setNativeUIIntegration(document, { suspend: vi.fn(), search: nativeFocus }); + page.classList.remove('ion-page-hidden'); + try { + for (let cycle = 0; cycle < 2; cycle++) { + const binding = registerNativeSearch(tabs, trigger, footer); + expect(candidates()[0].control.search?.available).toBe(true); + footer.setAttribute('data-native-ui-projected', ''); + await bar.setFocus(); + expect(nativeFocus).toHaveBeenCalledWith(binding, true, true); + footer.removeAttribute('data-native-ui-projected'); + await bar.setFocus(); + expect(webFocus.mock.contexts.at(-1)).toBe(bar); + search.destroy(); + expect(Object.getOwnPropertyDescriptor(bar, 'setFocus')).toBeUndefined(); + expect(bar.setFocus).toBe(webFocus); + } + } finally { + search.destroy(); + setNativeUIIntegration(document); + } +}); From 4adba9ebd91e0b43b6d78e621f520d8ebde13f6e Mon Sep 17 00:00:00 2001 From: rdlabo Date: Fri, 20 Nov 2026 13:54:40 +0900 Subject: [PATCH 2/4] Verify focus routing independently on each registration --- packages/native-ui-core/tests/native-ui-search.spec.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/native-ui-core/tests/native-ui-search.spec.ts b/packages/native-ui-core/tests/native-ui-search.spec.ts index da1ed42..abd7fad 100644 --- a/packages/native-ui-core/tests/native-ui-search.spec.ts +++ b/packages/native-ui-core/tests/native-ui-search.spec.ts @@ -184,14 +184,20 @@ test('lazy searchbars restore inherited readonly focus across repeated native se page.classList.remove('ion-page-hidden'); try { for (let cycle = 0; cycle < 2; cycle++) { + nativeFocus.mockClear(); + webFocus.mockClear(); const binding = registerNativeSearch(tabs, trigger, footer); expect(candidates()[0].control.search?.available).toBe(true); footer.setAttribute('data-native-ui-projected', ''); await bar.setFocus(); + expect(nativeFocus).toHaveBeenCalledTimes(1); expect(nativeFocus).toHaveBeenCalledWith(binding, true, true); + expect(webFocus).not.toHaveBeenCalled(); footer.removeAttribute('data-native-ui-projected'); await bar.setFocus(); - expect(webFocus.mock.contexts.at(-1)).toBe(bar); + expect(nativeFocus).toHaveBeenCalledTimes(1); + expect(webFocus).toHaveBeenCalledTimes(1); + expect(webFocus.mock.contexts[0]).toBe(bar); search.destroy(); expect(Object.getOwnPropertyDescriptor(bar, 'setFocus')).toBeUndefined(); expect(bar.setFocus).toBe(webFocus); From 53063ce6c6cc9292e49d13b07848604f5e7c6688 Mon Sep 17 00:00:00 2001 From: rdlabo Date: Fri, 20 Nov 2026 13:56:29 +0900 Subject: [PATCH 3/4] Focus regression coverage on read-only method installation and teardown --- .../tests/native-ui-search.spec.ts | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/packages/native-ui-core/tests/native-ui-search.spec.ts b/packages/native-ui-core/tests/native-ui-search.spec.ts index abd7fad..b79a5d2 100644 --- a/packages/native-ui-core/tests/native-ui-search.spec.ts +++ b/packages/native-ui-core/tests/native-ui-search.spec.ts @@ -1,6 +1,6 @@ import { afterEach, expect, test, vi } from 'vitest'; import { createSearchSupport } from '../src/native/components/searchable-tabs'; -import { registerNativeSearch, setNativeUIIntegration } from '../src/native-integration'; +import { registerNativeSearch } from '../src/native-integration'; import { createCandidate } from '../src/native/shared/candidate'; afterEach(() => { @@ -174,36 +174,16 @@ test('an unavailable cached search cannot cover its visible Web trigger but surv } }); -test('lazy searchbars restore inherited readonly focus across repeated native search registrations', async () => { - const { bar, page, footer, trigger, tabs, candidates, search } = setup(); - const webFocus = vi.fn(async () => {}); +test('lazy searchbars regain their inherited readonly focus method after teardown', () => { + const { bar, page, candidates, search } = setup(); + const originalFocus = bar.setFocus; Reflect.deleteProperty(bar, 'setFocus'); - Object.setPrototypeOf(bar, Object.create(Object.getPrototypeOf(bar), { setFocus: { value: webFocus } })); - const nativeFocus = vi.fn(async () => true); - setNativeUIIntegration(document, { suspend: vi.fn(), search: nativeFocus }); + Object.setPrototypeOf(bar, Object.create(Object.getPrototypeOf(bar), { setFocus: { value: originalFocus } })); page.classList.remove('ion-page-hidden'); - try { - for (let cycle = 0; cycle < 2; cycle++) { - nativeFocus.mockClear(); - webFocus.mockClear(); - const binding = registerNativeSearch(tabs, trigger, footer); - expect(candidates()[0].control.search?.available).toBe(true); - footer.setAttribute('data-native-ui-projected', ''); - await bar.setFocus(); - expect(nativeFocus).toHaveBeenCalledTimes(1); - expect(nativeFocus).toHaveBeenCalledWith(binding, true, true); - expect(webFocus).not.toHaveBeenCalled(); - footer.removeAttribute('data-native-ui-projected'); - await bar.setFocus(); - expect(nativeFocus).toHaveBeenCalledTimes(1); - expect(webFocus).toHaveBeenCalledTimes(1); - expect(webFocus.mock.contexts[0]).toBe(bar); - search.destroy(); - expect(Object.getOwnPropertyDescriptor(bar, 'setFocus')).toBeUndefined(); - expect(bar.setFocus).toBe(webFocus); - } - } finally { - search.destroy(); - setNativeUIIntegration(document); - } + + expect(candidates()[0].control.search?.available).toBe(true); + expect(bar.setFocus).not.toBe(originalFocus); + search.destroy(); + expect(Object.getOwnPropertyDescriptor(bar, 'setFocus')).toBeUndefined(); + expect(bar.setFocus).toBe(originalFocus); }); From 94f1150e09bbbe44a95832545427cd2931a6cd01 Mon Sep 17 00:00:00 2001 From: rdlabo Date: Fri, 20 Nov 2026 14:00:13 +0900 Subject: [PATCH 4/4] Cover both Ionic searchbar registration modes --- packages/native-ui-core/tests/native-ui-search.spec.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/native-ui-core/tests/native-ui-search.spec.ts b/packages/native-ui-core/tests/native-ui-search.spec.ts index b79a5d2..2d77fde 100644 --- a/packages/native-ui-core/tests/native-ui-search.spec.ts +++ b/packages/native-ui-core/tests/native-ui-search.spec.ts @@ -174,15 +174,17 @@ test('an unavailable cached search cannot cover its visible Web trigger but surv } }); -test('lazy searchbars regain their inherited readonly focus method after teardown', () => { +test.each([false, true])('searchbar focus is restored after teardown (prototype writable: %s)', async (writable) => { const { bar, page, candidates, search } = setup(); - const originalFocus = bar.setFocus; + const originalFocus = vi.fn(async () => {}); Reflect.deleteProperty(bar, 'setFocus'); - Object.setPrototypeOf(bar, Object.create(Object.getPrototypeOf(bar), { setFocus: { value: originalFocus } })); + Object.setPrototypeOf(bar, Object.create(Object.getPrototypeOf(bar), { setFocus: { value: originalFocus, writable } })); page.classList.remove('ion-page-hidden'); expect(candidates()[0].control.search?.available).toBe(true); expect(bar.setFocus).not.toBe(originalFocus); + await bar.setFocus(); + expect(originalFocus.mock.contexts[0]).toBe(bar); search.destroy(); expect(Object.getOwnPropertyDescriptor(bar, 'setFocus')).toBeUndefined(); expect(bar.setFocus).toBe(originalFocus);