0.3.2 • Published 2 years ago

react-global-search v0.3.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

react-global-search

A Centralized Search Component for your React Application.

Use the shortcut key ctrl + k to open the search modal (Customizable). Search through the items (you can add routes, components, etc) and navigate to the selected item.

Additionally, you can use the Arrow keys to navigate through the items and press Enter to choose the selected item.

Github Repo: https://github.com/Shlok-Zanwar/react-global-search

Installation

npm install react-global-search

Usage

import React from 'react'
import GlobalSearch from 'react-global-search'

const App = () => {
  return (
    <GlobalSearch
      items={[
        {
            name: 'Search Item 1',
            description: 'Search Item 1 Description',
            icon: <i className="fa fa-search" />,
            onClick: () => console.log('Search Item 1 Clicked'),
            pathname: '/search-item-1',
            search: 'search item 1 keywords',
        },
        {
            name: 'Search Item 2',
            description: 'Search Item 2 Description',
            icon: <i className="fa fa-search" />,
            onClick: () => console.log('Search Item 2 Clicked'),
            pathname: '/search-item-2',
            search: 'search item 2 keywords',
        },
      ]}
    />
  )
}

export default App

Props

NameTypeDefaultDescription
itemsArray[]Array of search items
shorcutKeyString'k'Shorcut key to open the search modal
searchPropString'search'The search query prop name
closeOnClickBooleantrueClose the search modal on item click
displayButtonBooleantrueDisplay the search button
displayButtonRenderString / React Element'Search'The search button render
itemClassNameString''The search item class name
itemStyleObject{}The search item style
highlightedItemStyleObject{}The highlighted search item style
highlightedItemClassNameString''The highlighted search item class name
searchInputClassNameString''The search input class name
searchInputStyleObject{}The search input style
searchInputPlaceholderString'Search...'The search input placeholder
itemRenderFunction(item, index) => {}The search item render
modalTitleString / React Element'Global Search'The search modal title
modalMaskClassNameString''The search modal mask class name
modalMaskStyleObject{}The search modal mask style
modalContainerClassNameString''The search modal container class name
modalContainerStyleObject{}The search modal container style
modalHeaderClassNameString''The search modal header class name
modalHeaderStyleObject{}The search modal header style
modalBodyClassNameString''The search modal body class name
modalBodyStyleObject{}The search modal body style

Item Props

When the user clicks or selects any item, the onClick function will be called.

NameTypeDefaultDescription
nameString / React Element''The search item name
descriptionString / React Element''The search item description
iconString / React Element''The search item icon
onClickFunction() => {}The search item click handler
searchString''The search item search query

Future Improvements / TODO

  • Change to typescript
  • Add Custom key bindings ( Array of key bindings )
  • Support for multiple search props
  • Support tree data structure for items.
  • Add Custom search function / search algorithm
  • Add more props to customize the search modal, items, etc