Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lowewenzel/use-autocomplete

React use-autocomplete hook for autocomplete components
https://github.com/lowewenzel/use-autocomplete

autocompletion hook react

Last synced: about 1 month ago
JSON representation

React use-autocomplete hook for autocomplete components

Awesome Lists containing this project

README

        

## React use-autocomplete

[![npm version](https://badge.fury.io/js/use-autocomplete.svg)](https://badge.fury.io/js/use-autocomplete)

## About

This is a hook, `useAutocomplete`, that returns autocomplete values using a prefix tree. This is for front-end autocompletion rather than remote autocompletion, which is more commonly used.

Use it in any input component, see [examples](./examples) for more.

## How to install

Run the following to install the hook

```bash
yarn install react-autocomplete
```

## How to use

Import from `use-autocomplete` and pass in the search term and list of values. Small example:

#### With fake data

```js
import React, { useState } from 'react';
import useAutocomplete from 'use-autocomplete';

import testWords from './data/testWords.json';

const Example = () => {
const [textState, setTextState] = useState('');
const [completions] = useAutocomplete(textState, testWords);

return (


setTextState(e.target.value)}
/>

{completions.map((val, index) => (

{val}


))}


);
};
```

#### Data conversion

```js
import React, { useState } from 'react';
import useAutocomplete from 'use-autocomplete';

const Example = ({ someSampleObject }) => {
const [textState, setTextState] = useState('');
const [completions] = useAutocomplete(
textState,
Object.keys(someSampleObject)
);

return (


setTextState(e.target.value)}
/>

{completions.map((val, index) => (

{val}


))}


);
};
```

## Contributing

Pull requests welcome!