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

https://github.com/chototoss/search-suggestion

🔍 Simple, lightweight, flexible search suggestion, autocomplete component 🔍
https://github.com/chototoss/search-suggestion

autocomplete autoselect combobox function-as-child react suggestions

Last synced: about 1 month ago
JSON representation

🔍 Simple, lightweight, flexible search suggestion, autocomplete component 🔍

Awesome Lists containing this project

README

        


downshift logo




[![Build Status](https://travis-ci.org/ChoTotOSS/search-suggestion.svg?branch=master)](https://travis-ci.org/ChoTotOSS/search-suggestion)
[![codecov](https://codecov.io/gh/ChoTotOSS/search-suggestion/branch/master/graph/badge.svg)](https://codecov.io/gh/ChoTotOSS/search-suggestion)
[![npm version](https://badge.fury.io/js/search-suggestion.svg)](https://badge.fury.io/js/search-suggestion)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ChoTotOSS/search-suggestion/blob/master/LICENSE)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

# Search Suggestion

Simple lightweight search suggestion component. This component was inspired by
[downshift (paypal)](https://github.com/paypal/downshift) component.

The component applied `Function as Child Components` pattern. You can read more
about this pattern
[here](https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9)

## Table content

* [Installation](https://github.com/ChoTotOSS/search-suggestion#installation)
* [Usage](https://github.com/ChoTotOSS/search-suggestion#usage)
* [Examples](https://github.com/ChoTotOSS/search-suggestion#examples)
* [Props](https://github.com/ChoTotOSS/search-suggestion#props)
* [Child callback functions](https://github.com/ChoTotOSS/search-suggestion#child-callback-functions)
* [Actions](https://github.com/ChoTotOSS/search-suggestion#actions)
* [States](https://github.com/ChoTotOSS/search-suggestion#states)
* [Showcase](https://github.com/ChoTotOSS/search-suggestion#showcase)
* [Alternatives](https://github.com/ChoTotOSS/search-suggestion#alternatives)

## Installation

```bash
npm install --save search-suggestion
```

or

```bash
yarn add search-suggestion
```

## Usage

You can check out the basic demo here:
[https://codesandbox.io/s/n45j5zjwyj](https://codesandbox.io/s/n45j5zjwyj)

```js
import React, { Component } from 'react';
import { render } from 'react-dom';
import Suggestion from 'search-suggestion';

const items = ['apple', 'pear', 'orange', 'grape', 'banana'];

class App extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: []
};
}

handleChange = e => {
const value = e.target.value;
let dataSource = [];
if (value) {
dataSource = items.filter(item => item.toLowerCase().includes(value.toLowerCase()));
}
this.setState({
dataSource
});
};

render() {
return (
item}
items={this.state.dataSource}
onSelectedItem={item => alert(item)}
>
{({
getInputProps,
getListItemProps,
getItemProps,
inputValue,
selectedItem,
highlightedIndex,
items,
isOpen,
clearInputValue
}) => (


selected item: {selectedItem}



{isOpen && (

{items.map((item, index) => (

{item}

))}

)}

)}

);
}
}

render(, document.getElementById('root'));
```

## Examples

* [with simple basic data](https://github.com/ChoTotOSS/search-suggestion/tree/master/examples/withBasic)
* [with simple list object data](https://github.com/ChoTotOSS/search-suggestion/tree/master/examples/withStaticData)
* [with redux](https://github.com/ChoTotOSS/search-suggestion/tree/master/examples/withRedux)
* [with redux & debounce component](https://github.com/ChoTotOSS/search-suggestion/tree/master/examples/withDebounceInput)

## Props

### items

> `Array` | default is an empty array

Use as a datasource to render list result.

### getDisplayName

> `function(item: any)` => return a text

Use to display text content of selected item.

### onSelectedItem

> `function(item: any)`

Use to set new selected item value and extend the actions based on that selected
item.

## Child callback functions

### getInputProps

> `function({})`

Returns the props you should apply to the `input` element that you render.

### getListItemProps

> `function({})`

Returns the props you should apply to the list item element that you render.

### getItemProps

> `function({})`

Returns the props you should apply to any menu item elements you render.

## Actions

### clearInputValue

> `function()`

Clear current value in `input` element

## States

### inputValue

> `string`

The current value of `input` element

### highlightedIndex

> `int`

The current index of highlighted item

### items

> `array`

The current data of menu

### isOpen

> `boolean`

The menu open state

### selectedItem

The value of selected item

## Showcase

Websites built with Search Suggestion component

### [Chợ tốt](https://www.chotot.com/toan-quoc/mua-ban)

![suggestion](https://user-images.githubusercontent.com/6290720/32546438-b18b9ba4-c4b1-11e7-99d4-e2a7e39191ce.gif)

### [Chợ tốt nhà](https://nha.chotot.com/toan-quoc/du-an-rg0-cg10000)

![new_property_suggestion](https://user-images.githubusercontent.com/6290720/32546472-d117d3d4-c4b1-11e7-9d10-145a0595463e.gif)

## Alternatives

If you don’t agree with the choices made in this project, you might want to
explore alternatives with different tradeoffs. Some of the more popular and
actively maintained ones are:

* [react-select](https://github.com/JedWatson/react-select)
* [react-autocomplete](https://github.com/reactjs/react-autocomplete)
* [downshift](https://github.com/paypal/downshift/)