X Tutup
The Wayback Machine - https://web.archive.org/web/20200919083138/https://github.com/lowewenzel/use-autocomplete
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

React use-autocomplete

npm version

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 for more.

How to install

Run the following to install the hook

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

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 (
    <div>
      <input
        type='text'
        value={textState}
        onChange={(e) => setTextState(e.target.value)}
      />
      <div>
        {completions.map((val, index) => (
          <p key={index}>{val}</p>
        ))}
      </div>
    </div>
  );
};

Data conversion

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 (
    <div>
      <input
        type='text'
        value={textState}
        onChange={(e) => setTextState(e.target.value)}
      />
      <div>
        {completions.map((val, index) => (
          <p key={index}>{val}</p>
        ))}
      </div>
    </div>
  );
};

Contributing

Pull requests welcome!

About

React use-autocomplete hook for autocomplete components

Topics

Resources

License

Releases

No releases published

Contributors 4

  •  
  •  
  •  
  •  
You can’t perform that action at this time.
X Tutup