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
5 changes: 5 additions & 0 deletions .changeset/disable-browser-autocomplete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-loqate": minor
---

Add disableBrowserAutocomplete prop to prevent browser autocomplete interference with address suggestions. This prop defaults to true, disabling browser autocomplete by setting autoComplete="react-loqate-address-search" on the input field.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ import 'react-loqate/dist/index.css';

### Props

| name | type | required | example | description |
| ---------- | ----------------------------------------------------- | -------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| apiKey | string | yes | "AA11-AA11-AA11-AA11" | Loqate API key |
| locale | string | yes | "en-GB" | Language to be used |
| onSelect | (address) => void | yes | address => console.log(address) | Callback with for Loqate response |
| countries | string[] | no | ["GB", "NL"] | Countries to search in |
| limit | number | no | 10 | Number of options to show |
| classes | `{ input?: string, list?: string, listItem?: string}` | no | { list: 'list' } | Classnames for the components |
| components | see [Customization](#Customization) | no | { Input: CustomInput, List: CustomList, ListItem: CustomListItem, } | Components to overwrite the default ones |
| inline | boolean | no | true | Render results inline with the input |
| debounce | number | no | 100 | Debounce the calls to the Loqate API |
| bias | boolean | no | true | Bias feature when using capture v4 enabled key.<br>Requires origin to be set. |
| origin | string | no | "93.184.216.34" | Name or ISO 2 or 3 character code of a country, WGS84 coordinates (comma separated) or IP address |
| name | type | required | example | description |
| -------------------------- | ----------------------------------------------------- | -------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| apiKey | string | yes | "AA11-AA11-AA11-AA11" | Loqate API key |
| locale | string | yes | "en-GB" | Language to be used |
| onSelect | (address) => void | yes | address => console.log(address) | Callback with for Loqate response |
| countries | string[] | no | ["GB", "NL"] | Countries to search in |
| limit | number | no | 10 | Number of options to show |
| classes | `{ input?: string, list?: string, listItem?: string}` | no | { list: 'list' } | Classnames for the components |
| components | see [Customization](#Customization) | no | { Input: CustomInput, List: CustomList, ListItem: CustomListItem, } | Components to overwrite the default ones |
| inline | boolean | no | true | Render results inline with the input |
| debounce | number | no | 100 | Debounce the calls to the Loqate API |
| bias | boolean | no | true | Bias feature when using capture v4 enabled key.<br>Requires origin to be set. |
| origin | string | no | "93.184.216.34" | Name or ISO 2 or 3 character code of a country, WGS84 coordinates (comma separated) or IP address |
| disableBrowserAutocomplete | boolean | no | false | Disable browser autocomplete on the input field (defaults to true) |

### Customization

Expand Down
35 changes: 35 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,38 @@ it('accepts origin and bias options', async () => {
text: 'a',
});
});

it('disables browser autocomplete by default', () => {
render(<AddressSearch locale="en-GB" apiKey="some-key" onSelect={vi.fn()} />);

const input = screen.getByRole('textbox');
expect(input).toHaveAttribute('autocomplete', 'react-loqate-address-search');
});

it('allows enabling browser autocomplete when disableBrowserAutocomplete is false', () => {
render(
<AddressSearch
locale="en-GB"
apiKey="some-key"
onSelect={vi.fn()}
disableBrowserAutocomplete={false}
/>
);

const input = screen.getByRole('textbox');
expect(input).not.toHaveAttribute('autocomplete');
});

it('disables browser autocomplete when disableBrowserAutocomplete is true', () => {
render(
<AddressSearch
locale="en-GB"
apiKey="some-key"
onSelect={vi.fn()}
disableBrowserAutocomplete={true}
/>
);

const input = screen.getByRole('textbox');
expect(input).toHaveAttribute('autocomplete', 'react-loqate-address-search');
});
10 changes: 8 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clsx from 'clsx';
import React, {
ChangeEvent,
ComponentType,
type ChangeEvent,
type ComponentType,
useMemo,
useRef,
useState,
Expand Down Expand Up @@ -32,6 +32,7 @@ export interface Props {
apiUrl?: string;
bias?: boolean;
origin?: string;
disableBrowserAutocomplete?: boolean;
}

interface Components {
Expand Down Expand Up @@ -134,6 +135,7 @@ function AddressSearch(props: Props): JSX.Element {
apiUrl,
bias,
origin,
disableBrowserAutocomplete = true,
} = props;
const loqate = useMemo(() => Loqate.create(apiKey, apiUrl), [apiKey]);

Expand Down Expand Up @@ -227,6 +229,10 @@ function AddressSearch(props: Props): JSX.Element {
className={clsx(classes?.input)}
onChange={handleChange}
value={value}
// disabling autocomplete in order to avoid conflict with loqate suggestions
autoComplete={
disableBrowserAutocomplete ? 'react-loqate-address-search' : undefined
}
onKeyDown={(e) => {
if (e.key === 'Escape') {
setSuggestions([]);
Expand Down
5 changes: 5 additions & 0 deletions src/testing/snapshots/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`allows for alternative url 1`] = `
<body>
<div>
<input
autocomplete="react-loqate-address-search"
class="react-loqate-input"
value="a"
/>
Expand Down Expand Up @@ -43,6 +44,7 @@ exports[`renders default 1`] = `
<body>
<div>
<input
autocomplete="react-loqate-address-search"
class="react-loqate-input"
value="a"
/>
Expand Down Expand Up @@ -169,6 +171,7 @@ exports[`renders with custom components 1`] = `
<body>
<div>
<input
autocomplete="react-loqate-address-search"
class=""
data-testid="custom-input"
value="a"
Expand Down Expand Up @@ -287,6 +290,7 @@ exports[`renders with inline results 1`] = `
data-testid="react-loqate"
>
<input
autocomplete="react-loqate-address-search"
class="react-loqate-input"
value="a"
/>
Expand Down Expand Up @@ -413,6 +417,7 @@ exports[`renders with portal results 1`] = `
data-testid="react-loqate"
>
<input
autocomplete="react-loqate-address-search"
class="react-loqate-input"
value="a"
/>
Expand Down