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

Apply some changes from feedback #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
build/
.history/
.vscode/
54 changes: 30 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ yarn add @syndicode/react-algolia-places
## Usage

```jsx
const placesAppId = process.env.REACT_APP_ALGOLIA_PLACES_APP_ID;
const placesApiKey = process.env.REACT_APP_ALGOLIA_PLACES_API_KEY;
const searchClient = algoliasearch.initPlaces(placesAppId, placesApiKey);

...

<AlgoliaPlaces
defaultValue={defaultValue}
apiKey={process.env.REACT_APP_ALGOLIA_PLACES_API_KEY}
appId={process.env.REACT_APP_ALGOLIA_PLACES_APP_ID}
onSelect={objectID => {
console.log(objectID);
}}
onSelect={objectID => { console.log(objectID) }}
searchParams={searchParams}
searchClient={searchClient}
>
{({ clear, error, loading, options, getInputProps, getOptionProps }) => (
<>
Expand All @@ -34,36 +37,39 @@ yarn add @syndicode/react-algolia-places
{options &&
options.map(option => {
// NOTE: you have to pass the option down to the getOptionProps() method
return <li {...getOptionProps(option)} key={option.objectID}>{option.city}</li>;
return (
<li {...getOptionProps(option)} key={option.objectID}>
{option.city}
</li>
);
})}
</ul>
</>
)}
</AlgoliaPlaces>
</AlgoliaPlaces>;
```

### Component's props

```
apiKey: String
appId: String # Your algolia's app apiKey and appId
defaultValue: String # Pass down objectID to prefetch result and fill in the input
onSelect(objectID: String, option: Option): String #
searchParams: searchParams
render | children - render prop
formatInputValue(hit: Hit): String - redefine default transformer for the inputs value on option select
hitTransformer(hit: Hit): Option - redefine default transformer for formatted option
**`defaultValue: String`** - Pass down a `objectID` to prefetch result and render the stored value
**`formatInputValue(hit: Hit): String`** - redefine default transformer for the inputs value on option select
**`onSelect(objectID: String, option: Option)`** - onselect callback
**`render`** | **`children`** - render prop
**`searchClient`** - initiated algoliasearch client
**`searchParams: searchParams`** - algoliasearch client search options
**`transformItems(hit: Hit): Option`** - redefine default transformer for formatted option
```

### Render props parameters

```
clear() - change selected value to `null`, reset all search results
error: Object -
getInputProps() - method that creates props object to pass onto input element
getOptionProps(option: Option) - method that creates props object to pass onto option component
loading: Boolean - whether search request is in progress
options: [Option] - list of formatted algolia places search results
**`clear()`** - change selected value to `null`, reset all search results
**`error: Object`** - search request error object
**`getInputProps()`** - method that creates props object to pass onto input element
**`getOptionProps(option: Option)`** - method that creates props object to pass onto option component
**`loading: Boolean`** - whether search request is in progress
**`options: [Option]`** - list of formatted algolia places search results
```

### Types
Expand All @@ -79,7 +85,7 @@ type Option {
}
```

See algolia docs for more info
See algolia docs for more info:

- search params options - https://community.algolia.com/places/api-clients.html#search-parameters
- hit's shape - https://community.algolia.com/places/api-clients.html#json-answer
- **`searchParams`** options - https://community.algolia.com/places/api-clients.html#search-parameters
- **`Hit`**'s shape - https://community.algolia.com/places/api-clients.html#json-answer
31 changes: 11 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import { useEffect, useState, useMemo } from "react";
import algoliasearch from "algoliasearch";

import {
transformHit,
formatInputValue as defaultFormatInputValue,
formatInputValue as defaultFormatInputValue
} from "./utils";

const noop = () => {};

const AlgoliaPlaces = ({
apiKey,
appId,
children,
defaultValue,
formatInputValue = defaultFormatInputValue,
hitTransformer = transformHit,
onSelect = noop,
render,
searchClient,
Copy link

Choose a reason for hiding this comment

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

maybe throw an error to let the user know that searchClient is required?

searchParams,
transformItems = transformHit
}) => {
// Setup algoliasearch placesClient client
const placesClient = useMemo(() => algoliasearch.initPlaces(appId, apiKey), [
appId,
apiKey,
]);
const searchPlace = query => placesClient.search(query, searchParams);

const searchPlace = query => searchClient.search(query, searchParams);
// Setup hooks
const [error, setError] = useState(null);
const [inputValue, setInputValue] = useState("");
Expand All @@ -40,7 +32,7 @@ const AlgoliaPlaces = ({
getPlaceObject(defaultValue)
.then(result => {
setLoading(false);
const { city, country } = hitTransformer(result);
const { city, country } = transformItems(result);
setInputValue(formatInputValue(city, country));
})
.catch(error => {
Expand All @@ -55,9 +47,9 @@ const AlgoliaPlaces = ({
}, [
defaultValue,
formatInputValue,
hitTransformer,
transformItems,
placesClient,
n1stre marked this conversation as resolved.
Show resolved Hide resolved
searchParams,
searchParams
]);

const clear = () => {
Expand All @@ -83,30 +75,29 @@ const AlgoliaPlaces = ({
searchPlace(query)
.then(results => {
setLoading(false);
const optionsList = results.hits.map(hitTransformer);
const optionsList = results.hits.map(transformItems);
setOptions(optionsList);
})
.catch(error => {
setLoading(false);
setError(error);
});
}
},
}
};
},
getOptionProps(option) {
const { city, country, objectID } = option;

return {
title: option.formatted,
disabled: loading,
onClick() {
setOptions(null);
setInputValue(formatInputValue(city, country));
onSelect(objectID, option);
},
}
};
},
}
});
};

Expand Down