Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: focus detached input on iOS (#653) #1231

Merged
merged 2 commits into from
Jan 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions packages/autocomplete-js/src/__tests__/detached.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ describe('detached', () => {
// Open detached overlay
searchButton.click();

await waitFor(() => {
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;

expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
expect(document.body).toHaveClass('aa-Detached');
expect(input).toHaveFocus();
expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
expect(document.body).toHaveClass('aa-Detached');

fireEvent.input(input, { target: { value: 'a' } });
});
// Input should immediately be focused, to ensure the keyboard is shown on mobile
expect(input).toHaveFocus();

// Type a query in the focused input
fireEvent.input(input, { target: { value: 'a' } });

// Wait for the panel to open
await waitFor(() => {
Expand Down Expand Up @@ -391,16 +392,15 @@ describe('detached', () => {
// Open detached overlay
searchButton.click();

// Type a query in the focused input
await waitFor(() => {
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;

expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
expect(document.body).toHaveClass('aa-Detached');
expect(input).toHaveFocus();
expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
expect(document.body).toHaveClass('aa-Detached');
// Input should immediately be focused, to ensure the keyboard is shown on mobile
expect(input).toHaveFocus();

fireEvent.input(input, { target: { value: 'a' } });
});
// Type a query in the focused input
fireEvent.input(input, { target: { value: 'a' } });

// Wait for the panel to open
await waitFor(() => {
Expand Down
42 changes: 20 additions & 22 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,30 +375,28 @@ export function autocomplete<TItem extends BaseItem>(
}

function setIsModalOpen(value: boolean) {
requestAnimationFrame(() => {
const prevValue = props.value.core.environment.document.body.contains(
dom.value.detachedOverlay
);
const prevValue = props.value.core.environment.document.body.contains(
dom.value.detachedOverlay
);

if (value === prevValue) {
return;
}
if (value === prevValue) {
return;
}

if (value) {
props.value.core.environment.document.body.appendChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.add('aa-Detached');
dom.value.input.focus();
} else {
props.value.core.environment.document.body.removeChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.remove(
'aa-Detached'
);
}
});
if (value) {
props.value.core.environment.document.body.appendChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.add('aa-Detached');
dom.value.input.focus();
} else {
props.value.core.environment.document.body.removeChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.remove(
'aa-Detached'
);
}
}

warn(
Expand Down