Skip to content

Commit

Permalink
Make autocomplete consistent with radar sdk js (#243)
Browse files Browse the repository at this point in the history
* onSelect --> onSelection

* Remove README, radar.com/documentation is the docs reside

* Changes to match web autocomplete SDK

* countryCode --> country

* Revert "countryCode --> country"

This reverts commit 943861d.

* Take countryCode param

* Enable disabled option

* Update map logo

* Move style to options prop

* location --> near
  • Loading branch information
lmeier authored Aug 7, 2023
1 parent be23434 commit 8291c1f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 171 deletions.
2 changes: 1 addition & 1 deletion android/src/main/java/io/radar/react/RNRadarModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public void autocomplete(ReadableMap optionsMap, final Promise promise) {
near.setLatitude(latitude);
near.setLongitude(longitude);
int limit = optionsMap.hasKey("limit") ? optionsMap.getInt("limit") : 10;
String country = optionsMap.hasKey("country") ? optionsMap.getString("country") : null;
String country = optionsMap.hasKey("countryCode") ? optionsMap.getString("countryCode") : optionsMap.hasKey("country") ? optionsMap.getString("country") : null;
String[] layers = optionsMap.hasKey("layers") ? RNRadarUtils.stringArrayForArray(optionsMap.getArray("layers")) : null;

Radar.autocomplete(query, near, layers, limit, country, new Radar.RadarGeocodeCallback() {
Expand Down
5 changes: 4 additions & 1 deletion ios/RNRadar.m
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,10 @@ - (void)didLogMessage:(NSString *)message {
}

NSArray *layers = optionsDict[@"layers"];
NSString *country = optionsDict[@"country"];
NSString *country = optionsDict[@"countryCode"];
if (country == nil) {
country = optionsDict[@"country"];
}

__block RCTPromiseResolveBlock resolver = resolve;
__block RCTPromiseRejectBlock rejecter = reject;
Expand Down
157 changes: 0 additions & 157 deletions js/ui/README.md

This file was deleted.

34 changes: 22 additions & 12 deletions js/ui/autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import {
import { default as defaultStyles } from './styles';

const defaultAutocompleteOptions = {
debounceMS: 200,
threshold: 3,
limit: 8,
placeholder: "Search address",
showPin: true,
debounceMS: 200, // Debounce time in milliseconds
threshold: 3, // Minimum number of characters to trigger autocomplete
limit: 8, // Maximum number of results to return
placeholder: "Search address", // Placeholder text for the input field
showMarkers: true,
disabled: false,
};

const autocompleteUI = ({ options = {}, onSelect, location, style = {} }) => {
const autocompleteUI = ({ options = {} }) => {
const [query, setQuery] = useState("");
const [results, setResults] = useState([]);
const [isOpen, setIsOpen] = useState(false);
Expand All @@ -44,19 +45,26 @@ const autocompleteUI = ({ options = {}, onSelect, location, style = {} }) => {
const textInputRef = useRef(null);

const config = { ...defaultAutocompleteOptions, ...options };
const style = config.style || {};

const fetchResults = useCallback(
async (searchQuery) => {
if (searchQuery.length < config.threshold) return;

const params = { query: searchQuery, limit: config.limit };
const { limit, layers, countryCode } = config;
const params = { query: searchQuery, limit, layers, countryCode };

if (location && location.latitude && location.longitude) {
params.near = location;
if (config.near && config.near.latitude && config.near.longitude) {
params.near = config.near;
}

try {
const result = await Radar.autocomplete(params);

if (config.onResults && typeof config.onResults === "function") {
config.onResults(result.addresses);
}

setResults(result.addresses);
setIsOpen(true);
} catch (error) {
Expand Down Expand Up @@ -93,8 +101,8 @@ const autocompleteUI = ({ options = {}, onSelect, location, style = {} }) => {
setQuery(item.formattedAddress);
setIsOpen(false);

if (typeof onSelect === "function") {
onSelect(item);
if (typeof config.onSelection === "function") {
config.onSelection(item);
}
};

Expand Down Expand Up @@ -129,7 +137,7 @@ const autocompleteUI = ({ options = {}, onSelect, location, style = {} }) => {
>
<View style={styles.addressContainer}>
<View style={styles.pinIconContainer}>
{config.showPin ? (
{config.showMarkers ? (
<Image source={MARKER_ICON} style={styles.pinIcon} />
) : null}
</View>
Expand Down Expand Up @@ -219,6 +227,8 @@ const autocompleteUI = ({ options = {}, onSelect, location, style = {} }) => {
<TouchableOpacity
style={styles.inputContainer}
onPress={() => {
if (config.disabled) return;

setIsOpen(true);
// Set the focus on the other textinput after it opens
setTimeout(() => {
Expand Down
Binary file modified js/ui/map-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8291c1f

Please sign in to comment.