Ecosyste.ms: Awesome

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

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

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

autocompletion hook react

Last synced: 3 months ago
JSON representation

React use-autocomplete hook for autocomplete components

Lists

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!