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

Autocomplete fixes (race condition, prevent autofill, style fixes) #173

Merged
merged 2 commits into from
Jul 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from '../types';

class SearchAPI {
static async autocomplete(params: RadarAutocompleteParams): Promise<RadarAutocompleteResponse> {
static async autocomplete(params: RadarAutocompleteParams, requestId?: string): Promise<RadarAutocompleteResponse> {
const options = Config.get();

let {
Expand Down Expand Up @@ -45,6 +45,7 @@ class SearchAPI {
expandUnits,
mailable,
},
requestId,
});

const autocompleteRes = {
Expand Down
22 changes: 22 additions & 0 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface HttpResponse {
data: any;
}

const inFlightRequests = new Map<string, XMLHttpRequest>();

class Http {
static async request({
method,
Expand All @@ -34,6 +36,7 @@ class Http {
version,
headers = {},
responseType,
requestId,
}: {
method: HttpMethod;
path: string;
Expand All @@ -42,6 +45,7 @@ class Http {
version?: string;
headers?: Record<string, string>;
responseType?: XMLHttpRequestResponseType;
requestId?: string;
}) {
return new Promise<HttpResponse>((resolve, reject) => {
const options = Config.get();
Expand Down Expand Up @@ -79,9 +83,22 @@ class Http {
body = undefined; // dont send body for GET request
}

// check for in-flight requests with matching requestIds
if (requestId) {
const request = inFlightRequests.get(requestId);
if (request) {
request.abort(); // abort request
}
}

const xhr = new XMLHttpRequest();
xhr.open(method, url, true);

// save reference to request
if (requestId) {
inFlightRequests.set(requestId, xhr);
}

const defaultHeaders = {
'Authorization': publishableKey,
'Content-Type': 'application/json',
Expand Down Expand Up @@ -109,6 +126,11 @@ class Http {

xhr.onload = () => {
let response: any;

if (requestId) { // clear in-flight request
inFlightRequests.delete(requestId);
}

try {
if (xhr.responseType === 'blob') {
response = { code: xhr.status, data: xhr.response };
Expand Down
5 changes: 4 additions & 1 deletion src/ui/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ class AutocompleteUI {
this.container.appendChild(this.wrapper);
}

// disable browser autofill
this.inputField.setAttribute('autocomplete', 'off');

Comment on lines +171 to +173
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we wanna pass this in as an option to in case someone wants to use autofill?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in that scenario, they can use their own input field.

// set aria roles
this.inputField.setAttribute('role', 'combobox');
this.inputField.setAttribute('aria-controls', CLASSNAMES.RESULTS_LIST);
Expand Down Expand Up @@ -263,7 +266,7 @@ class AutocompleteUI {
onRequest(params);
}

const { addresses } = await SearchAPI.autocomplete(params);
const { addresses } = await SearchAPI.autocomplete(params, 'autocomplete-ui');
return addresses;
}

Expand Down
9 changes: 6 additions & 3 deletions styles/radar.css
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@
}

.radar-powered a {
text-decoration: none;
color: var(--radar-gray6);
text-decoration: none !important;
color: var(--radar-gray6) !important;
}

.radar-powered a:visited {
color: var(--radar-gray6);
color: var(--radar-gray6) !important;
}

.radar-powered #radar-powered-logo {
Expand All @@ -211,3 +211,6 @@
.maplibregl-ctrl-attrib.hidden {
display: none !important;
}
.maplibregl-popup-close-button:focus-visible {
outline: none;
}
Loading