Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/native-ui-core/src/native/components/searchable-tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
16 changes: 16 additions & 0 deletions packages/native-ui-core/tests/native-ui-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,19 @@ test('an unavailable cached search cannot cover its visible Web trigger but surv
search.destroy();
}
});

test.each([false, true])('searchbar focus is restored after teardown (prototype writable: %s)', async (writable) => {
const { bar, page, candidates, search } = setup();
const originalFocus = vi.fn(async () => {});
Reflect.deleteProperty(bar, 'setFocus');
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);
});
Loading