2.0.0 • Published 5 years ago

react-native-searchbar-controlled v2.0.0

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

react-native-searchbar

A pretty awesome search bar for React Native. It has a built in simple search capability which can be utilised by handing the component data and providing a handleResults function which will receive the filtered copy of the data array.

The search function uses a depth-first algorithm to traverse the data and simply looks for an indexOf the user input within all strings, numbers, and boolean values in the data.

Works on both iOS and Android.

react-native-searchbar

(Rendered with the example in these docs and repository)

Installation

  • npm install react-native-searchbar-controlled --save
  • Install react-native-vector-icons if the project doesn't have them already. The search bar accesses MaterialIcons.
  • Now you can require the search bar with import SearchBar from 'react-native-searchbar-controlled' or var SearchBar = require('react-native-searchbar-controlled')

Available Props

PropertyTypeDefaultDescription
inputstring''The value of the searchbox
placeholderstringSearchThe placeholder for the search bar.
handleChangeText(input)functionnullFires when the input text changes.
onSubmitEditingfunctionnullFired after pressing 'search' on the device keyboard
onHide()functionnullFires when the search bar is hidden.
onBackfunctionHides the search barFires when the back icon is pressed.
onXfunctionnullFires when the X button is pressed. Still clears the input.
onFocusfunctionnullFires when the search bar receives focus.
onBlurfunctionnullFires when the search bar loses focus.
backButtonComponentrequiredSets the back button component.
backButtonAccessibilityLabelstringNavigate upAccessibility label for the back button.
closeButtonComponentrequiredSets the close button component.
closeButtonAccessibilityLabelstringClear search textAccessibility label for the close button.
backCloseSizenumber28Sets the size of back button & close button.
heightAdjustnumber0Adjust the height of the search bar.
backgroundColorstringwhiteThe background colour of the search bar.
iconColorstringgrayThe color of the back and X icons.
textColorstringgrayThe color of the search bar text.
selectionColorstringlightskyblueThe color of the the search bar cursor and text selection.
placeholderTextColorstringlightgrayThe color of the placeholder text.
animatebooleantrueAnimate the search bar when it is shown and hidden.
animationDurationnumber200The duration of the above animation in milliseconds.
showOnLoadbooleanfalseShow the search bar when it is first rendered.
hideBackbooleanfalseHide the back button.
hideXbooleanfalseHide the X icon which clears the input.
iOSPaddingbooleantruePad the size of the iOS status bar.
iOSHideShadowbooleanfalseHide the shadow under the search bar in iOS.
iOSPaddingBackgroundColorstringtransparentChange the background color of the padding.
clearOnShowbooleanfalseClear input when the search bar is shown.
clearOnHidebooleantrueClear input when the search bar is hidden.
clearOnBlurbooleanfalseClear input when the search bar is blurred.
focusOnLayoutbooleantrueFocus the text input box whenever it is shown.
autoCorrectbooleantrueAutoCorrect the search bar text.
autoCapitalizestringsentencesAuto capitialize behaviour of the text input - enum('none', 'sentences', 'words', 'characters')
keyboardAppearancestring'default'Determines the color of the keyboard.
fontFamilystringSystemThe font family to be used.
fontSizenumber20Sets the font size.
allDataOnEmptySearchbooleanfalseSearch results behave as a .filter, returning all data when the input is an empty string.

Usage

  • Use a ref to show and hide the search bar and set the text input value ref={(ref) => this.searchBar = ref}
  • this.searchBar.show()
  • this.searchBar.hide()
Notes for Android
  • Render the search bar component after the component it is supposed to display over. iOS handles this nicely with a zIndex of 10. Android elevation is set to 2.
  • The bottom of the search bar will have a thin border instead of a shadow.

Example

Full example at example/

import SearchBar from 'react-native-searchbar';

const items = [
  1337,
  'janeway',
  {
    lots: 'of',
    different: {
      types: 0,
      data: false,
      that: {
        can: {
          be: {
            quite: {
              complex: {
                hidden: [ 'gold!' ],
              },
            },
          },
        },
      },
    },
  },
  [ 4, 2, 'tree' ],
];

...
_handleResults(results) {
  this.setState({ results });
}
...

...
<SearchBar
  ref={(ref) => this.searchBar = ref}
  data={items}
  handleResults={this._handleResults}
  showOnLoad
/>
...

Contributing

Contributing to react-native-searchbar is easy! With four simple steps:

Create a branch

  1. Fork the repository
  2. git clone <your-repo-url> to clone your GitHub repo to your local one
  3. git pull origin master to pull the latest code
  4. npm install to install the project's dependencies
  5. git checkout -b the-name-of-my-branch to create a branch (use something short and comprehensible, such as: fix-styling-of-search-bar).

Make the change

Test the change

  1. Run npm run fix from the project root (This will run Prettier and ESLint and automatically fix any issues).
  2. If possible, test any visual changes in Android and iOS.

Push the change!

  1. git add -A && git commit -m "My message" (replacing My message with a commit message, such as Fixed styling on search bar) to stage and commit your changes
  2. git push my-fork-name the-name-of-my-branch