2.7.0 • Published 5 years ago

react-searchable-input v2.7.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

react-searchable-input

A react based text input with search/select/multi-select build in

example usage

import SearchableInput from 'react-searchable-input';

const handleSelect = (valueArr) => {
  // do something with the selected items array (the returned value is alway an array even we have only one item selected)
}

const handleSearch = (value) => {
  // do something with the input search value
  // EX: perfect place to ajax fetch
}

//what ever you like, just don't forget the `key`
//for validated html structure, better use span as wrapper element
const renderListItem = (item) => (
  <span key={item.id}>
    <img src={item.img} alt={item.label} />
    <span>{`${item.id} = ${item.label}`}</span>
  </span>
)

const someComponent = ({ collection }) => (
  <wrapper>
    <SearchableInput
      name='sample'
      onListItemClick={handleSelect}
      asyncSearch={handleSearch}
      collection={collection.map(col => ({...col, id: col.id, label: col.name}))}
      placeholder='search for something'
      theme={{
        disabledColor: "#DDDDDD",
        inputHeight: '34px',
        listMaxHeight: "500px",
        listPadding: '20px',
        listWidth: '100%',
        listTop: '30px',
        listLeft: '0px',
        listBg: 'transparent'
      }}
      renderListItem={renderListItem}
    />
  </wrapper>
)

Props:

Nametypedefaultdescription
collectionarrayThe only required props, can be an array of string, or an array of object. In later case, each object should contain minimum an id and a label
placeholderstring"Choose an item"placeholder text for input field
isDisabledbooleanfalsedisable the search input all together
onListItemClickfunctiona callback function takes an array of the current selected items as argument, to trigger further actions
asyncSearchfunctiona callback function to trigger ajax search when current input changed
enableCachebooleanfalseallow local cache for search result, useful when you have ayncSearch but don't use advanced async flow control solution like rx.js or redux-saga, it will throttle onChange event to prevent fetch on every keystroke, and cache a maximum of 10 queries results to be retrieved directly instead of calling the server again
showLabelTextbooleanfalseLabel Text is a text overlay after you selected some items in search result, without modifying the underlying search input. It would display n selected for multi selection (where n is the number of items selected), or the label of item for single selection
multibooleanfalseenable multiple item selection, turn the result list to checkbox items
closeOnSelectbooleantruecontrol if the search list should be hidden after selected an item, always false for multi-selection
enableSelectAllbooleanfalseused when active multi option, add an option on top of search list, to select/deselect all avaiable items on the list
selectAllobject{selectAllText: "Select all",unSelectAllText: "Unselect all"}text to display for select/deselect all when enableSelectAll is true
themeobject{disabledColor:"#DDDDDD",inputHeight: "34px",listMaxHeight: "500px",listPadding: "20px",listWidth: "100%",listTop: "40px",listLeft: "0px",listBg: "transparent"}theme variables allow to custom the appearance of list)
renderListItemfunctiona render function allows to customize the list items, it takes the shape of item inside collection props, and should return a validated DOM node/ react node
onPressEnterfunctionfunction to call with current input value when user typed enter key
onBlurfunctionadditional function to call when the whole component lost focus
onFocusfunctionaddtional function when the input field is focused
showErrorbooleanto display an error message if something went wrong (EX: ajax fetch failed)
defaultErrorstring or nodes"please select a valid label"if showError is true, display it

TODO:

  • SSR support (ClickOutside component)
  • Better cache strategy (attach unique id to a given request to associate response)
  • More customization options for elements (multi-selections, etc)
  • TypeScript
2.7.0

5 years ago

2.6.0

5 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.0.0

5 years ago