Git Product home page Git Product logo

react-autocomplete-hint's Introduction

react-autocomplete-hint

A React component for Autocomplete Hint.

NPM npm Build Status codecov

Demo

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

Installation

npm install --save react-autocomplete-hint

or

yarn add react-autocomplete-hint

Changes

Since 2.1.0 there are breaking changes compared to original repo. Please check the new options/props before update.

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 (left click) or use your keyboard Right key (if allowArrowFill is set to true), 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.

API

props

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

  • disableHint (optional): Boolean

  • allowLeftClickFill (optional): Boolean

  • allowArrowFill (optional): Boolean

  • allowTabFill (optional): Boolean

  • allowEnterFill (optional): Boolean

  • detectFocus (optional): Boolean Whether to fill hint on focus

  • detectBlur (optional): Boolean Whether to clear hints on blur

  • continuousHint (optional): Boolean Do not clear hint candidates

  • hintColor (optional): string

prop functions

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

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

  • onEmpty (optional): ()=> 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 hints. Note that onHint will now return hint that's same with input.

onEmpty

Called whem input is empty.

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.

react-autocomplete-hint's People

Contributors

aiden0w0 avatar dependabot[bot] avatar ejmudi avatar hexadeciman avatar shubhamv123 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.