# autocomplete-search-react

> This package enables you to perform prefix searches with exceptional speed in react native and react.

Latest version **0.2.0** (published 2023-03-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install autocomplete-search-react
pnpm add autocomplete-search-react
yarn add autocomplete-search-react
bun add autocomplete-search-react
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2023-03-25 |
| First published | 2023-03-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Neosh |
| Maintainers | neosh11 |
| Keywords | autocomplete, search, react, react-native, hook |

## Links

- npm: https://www.npmjs.com/package/autocomplete-search-react
- Repository: https://github.com/neosh11/npm-autocomplete-search-react
- Homepage: https://github.com/neosh11/npm-autocomplete-search-react#readme
- Issues: https://github.com/neosh11/npm-autocomplete-search-react/issues
- npm.io page: https://npm.io/package/autocomplete-search-react

## Dependencies (1)

- [@neosh11/autocomplete-search](https://npm.io/package/@neosh11/autocomplete-search.md) ^1.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2023-03-25
- 0.1.6 — 2023-03-25
- 0.1.5 — 2023-03-25
- 0.1.4 — 2023-03-25
- 0.1.3 — 2023-03-25
- 0.1.2 — 2023-03-25
- 0.1.1 — 2023-03-25
- 0.1.0 — 2023-03-25
- 0.0.9 — 2023-03-23
- 0.0.8 — 2023-03-23
- 0.0.6 — 2023-03-23
- 0.0.5 — 2023-03-23
- 0.0.4 — 2023-03-23
- 0.0.3 — 2023-03-23
- 0.0.1 — 2023-03-23

## README

# npm-autocomplete-search-react

<p>
<img src="https://img.shields.io/pypi/wheel/pip?color=green&label=es6"/>
<img src="https://img.shields.io/pypi/wheel/pip?color=green&label=React"/>
<img src="https://img.shields.io/pypi/wheel/pip?color=green&label=React-Native"/>

<img src="https://img.shields.io/bundlephobia/min/autocomplete-search-react/0.2.0"/>
<img src="https://img.shields.io/bundlephobia/minzip/autocomplete-search-react/0.2.0"/>
<img src="https://img.shields.io/npm/v/autocomplete-search-react"/>

<img src="https://img.shields.io/twitter/follow/IGrowNeo?style=social"/>

</p>

## Overview

`useSearch` is a custom React hook that provides autocomplete search functionality for an array of objects using the `String2ObjectAutoCompleteSearch` class from the [`@neosh11/autocomplete-search`](https://github.com/neosh11/npm-autocomplete) package. The hook filters the data based on a given search key and manages search text and filtered results internally.

### Example -> [Link](http://neosh11.github.io/Linker/)

| Function                                       | Time                                                                      |
| ---------------------------------------------- | ------------------------------------------------------------------------- |
| `insert(word: string, id: string)`             | `O(m)`, where m is the length of the word being inserted.                 |
| `findWords(prefix: string, maxCount?: number)` | `O(k + n)`, where n is the number of nodes, k is the lenght of the prefix |

## Why use this and not a simple filter?

We tested out a simple filter on 100,000 tokens on the browser and these were the results:

| Method             | Time (ms) |
| ------------------ | --------- |
| Simple filter      | 467       |
| autoCompleteSearch | 4         |

If you want your results to instantly load as you're typing with a lot of data, this library will be a life saver!

## Usage

### install

```
# if using yarn
yarn add autocomplete-search-react

# if using npm
npm install autocomplete-search-react
```

### Importing the module

```ts
import { useSearch } from 'autocomplete-search-react';
```

### Usage

```ts
const data = [
  // your array of objects
];

const searchId = 'id'; // the property that will act as an ID
const searchKey = 'name'; // The property to search for in the objects
// or we can also use a function to map things
const searchFunction = (d) => d.title + d.description; // The property to search for in the objects
const maxResults = 10; // Maximum number of results to display
const tokeniser = ' '; // what to use to split the strings -> default is ' ', can be a RegExp
const [onTextChange, filteredObjects, searchText] = useSearch({
  data: data
  searchId: searchId;
  searchKey: searchKey; // or function searchFunction
  maxResults: maxResults;
  tokenizer: tokeniser // or it can be regex - optional -> default is ' '
});
```

### Example

```ts
function App() {
  const [onTextChange, filteredObjects, searchText] = useSearch({
  data: data
  searchId: 'id';
  searchKey: 'name'; // or function searchFunction
  maxResults: 10;
  tokenizer: ', ' // or it can be regex - optional -> default is ' '
});

  return (
    <div>
      <input
        type="text"
        value={searchText}
        onChange={(e) => onTextChange(e.target.value)}
        placeholder="Search"
      />
      <ul>
        {filteredObjects.map((item) => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
}

export default App;
```

This example demonstrates a simple search feature for a list of objects with name property. The search is case-insensitive and will display up to 10 results.

---
_Source: https://npm.io/package/autocomplete-search-react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
