npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@remiguan/react-autocomplete-hint

v2.0.2

Published

A React component for Autocomplete hint

Downloads

15

Readme

react-autocomplete-hint

A React component for Autocomplete Hint.

NPM npm Build Status codecov

Demo

Demo can be found here: https://ejmudi.github.io/react-autocomplete-hint/

Installation

npm install --save react-autocomplete-hint

or

yarn add react-autocomplete-hint

Usage

import { Hint } from 'react-autocomplete-hint';

const options = ["orange", "banana", "apple"];

// OR

const options = [
    {id: 1, label: "orange"}, 
    {id: '2', label: "banana"}, 
    {id: 3, label: "apple"}
];

<Hint options={options}>
    <input
        value={text}
        onChange={e => setText(e.target.value)} />
</Hint>

Click on the hint or use your keyboard Right key, Tab key (if allowTabFill is set to true), or Enter key (if allowEnterFill is set to true) to fill your input with the suggested hint.

Props

options (required): Array<string> | Array<object>

disableHint (optional): Boolean

allowTabFill (optional): Boolean

allowEnterFill (optional): Boolean

onFill (optional): (value: string | object)=> void

onHint (optional): (value: string | object | undefined)=> void

valueModifier (optional): (value: string)=> string

object option

If you're using objects for your options. object schema is as follows:

id: string | number

label: string

onFill

Returns the option selected immediately the input is filled with the suggested hint.

Note that it won't return the selected option with the casing the user typed, rather it returns the option with the casing specified in your options prop. For example, if the options are specified like this:...

const options = ["orange", "banana", "apple"];

...and the input gets filled with "ORange", onFill will still return "orange".

onHint

Returns the current hint.

valueModifier

This prop accepts a function that modifies your input value before it is saved in state.

It is typically useful when you are not setting e.target.value directly in state and need to modify the target value to some other value first before setting it in state.

Example: A case where you need to set the input value to uppercase irrespective of the casing the user types in:

const options = ["orange", "banana", "apple"];

const modifyValue = (value: string) => value.toUpperCase();

<Hint options={options} valueModifier={modifyValue}>
    <input
        value={text}
        onChange={e => setText(modifyValue(e.target.value))} />
</Hint>

Note: Not setting the valueModifier prop in cases like this might result to a malformed hint.

Duplicate data

If you are using objects for your options, You may have unexpected results if your data options contain objects with duplicate labels. For this reason, it is highly recommended that you pass in objects with unique labels if possible.

For example, if you pass in optionsWithDuplicateLabels as seen below and you then fill the input with the orange hint, the orange will be the first orange object found in the array as can be seen in the onFill prop:

const optionsWithDuplicateLabels = [
    {id: "1", label: "orange"},
    {id: "2", label: "orange"},
    {id: "3", label: "banana"}
];

<Hint options={optionsWithDuplicateLabels} onFill={value=> {
    // will always log {id: "1", label: "orange"} when orange is selected
    // {id: "2", label: "orange"} will never be logged.
    console.log(value);
}}>
    <input
        value={text}
        onChange={e => setText(e.target.value)} />
</Hint>

License

MIT

Inspired by React Bootstrap Typeahead.