Home

Awesome

A searchable ListView which supports Chinese PinYin and alphabetical index.

npm version FOSSA Status <a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard - JavaScript Style Guide"></a> Open Source Love

React Native Search List

A searchable ListView which supports Chinese PinYin and alphabetical index.

<p align="center"> <img src='https://raw.githubusercontent.com/UnPourTous/react-native-search-list/develop-refactor/screenshots/search-list-demo-v2.gif' /> </p>

The following pic may be helpful when understanding the structure and APIs:

<p align="center"> <img src='https://user-images.githubusercontent.com/1309744/36627720-fba7dade-1981-11e8-941f-03fc94af00ec.png' /> </p>

Installation

$ npm install @unpourtous/react-native-search-list --save

Usage

To Use SearchList, need a array of object as data source,and each object has searchStr property, eample code are put in ./entry.js.

export default class example extends Component {
  constructor (props) {
    super(props)
    this.state = {
      dataSource: demoList
    }
  }

  // custom render row
  renderRow (item, sectionID, rowID, highlightRowFunc, isSearching) {
    return (
      <Touchable onPress={() => {
        Alert.alert('Clicked!', `sectionID: ${sectionID}; item: ${item.searchStr}`,
          [
            {text: 'OK', onPress: () => console.log('OK Pressed')},
          ],
          {cancelable: true})
      }}>
        <View key={rowID} style={{flex: 1, marginLeft: 20, height: rowHeight, justifyContent: 'center'}}>
          {/*use `HighlightableText` to highlight the search result*/}
          <HighlightableText
            matcher={item.matcher}
            text={item.searchStr}
            textColor={'#000'}
            hightlightTextColor={'#0069c0'}
          />
        </View>
      </Touchable>
    )
  }

  // render empty view when datasource is empty
  renderEmpty () {
    return (
      <View style={styles.emptyDataSource}>
        <Text style={{color: '#979797', fontSize: 18, paddingTop: 20}}> No Content </Text>
      </View>
    )
  }

  // render empty result view when search result is empty
  renderEmptyResult (searchStr) {
    return (
      <View style={styles.emptySearchResult}>
        <Text style={{color: '#979797', fontSize: 18, paddingTop: 20}}> No Result For <Text
          style={{color: '#171a23', fontSize: 18}}>{searchStr}</Text></Text>
        <Text style={{color: '#979797', fontSize: 18, alignItems: 'center', paddingTop: 10}}>Please search again</Text>
      </View>
    )
  }

  render () {
    return (
      <View style={styles.container}>
        <StatusBar backgroundColor='#F00' barStyle='light-content' />
        <SearchList
          data={this.state.dataSource}
          renderRow={this.renderRow.bind(this)}
          renderEmptyResult={this.renderEmptyResult.bind(this)}
          renderBackButton={() => null}
          renderEmpty={this.renderEmpty.bind(this)}

          rowHeight={rowHeight}

          toolbarBackgroundColor={'#2196f3'}
          title='Search List Demo'
          cancelTitle='取消'
          onClickBack={() => {}}

          searchListBackgroundColor={'#2196f3'}

          searchBarToggleDuration={300}

          searchInputBackgroundColor={'#0069c0'}
          searchInputBackgroundColorActive={'#6ec6ff'}
          searchInputPlaceholderColor={'#FFF'}
          searchInputTextColor={'#FFF'}
          searchInputTextColorActive={'#000'}
          searchInputPlaceholder='Search'
          sectionIndexTextColor={'#6ec6ff'}
          searchBarBackgroundColor={'#2196f3'}
        />
      </View>
    )
  }
}

APIs

prop nametypedescriptiondefault value
dataarrayThe rows of list view.each object should contain searchStr, it will be used for search source. If you have custom row id,you should set searchKey for each object.
renderRownumberRender your custom row content.
rowHeightnumberThe height of the default row content, it will be used for scroll calculate.40
sectionHeaderHeightnumberThe height of section header content.24
searchListBackgroundColorstringBackgroundColor for searchList.#171a23
toolbarBackgroundColorstringToolbar background color.#171a23
searchBarToggleDurationnumberCustom search bar animation duration.300
searchBarBackgroundColorstringCustom search bar background color.#171a23
searchInputBackgroundColorstringCustom search input default state background color.
searchInputBackgroundColorActivestringCustom search input searching state background color.
searchInputPlaceholderstringCustom search input placeholder text.
searchInputPlaceholderColorstringCustom search input placeholder text color.
searchInputTextColorstringCustom search input default state text color.
searchInputTextColorActivestringCustom search input searching state text color.
searchBarBackgroundColorstringCustom search bar background color.
titlestringToolbar title.
titleTextColorstringToolbar title text color.
cancelTextColorstringSearch bar cancel text color.
cancelTitlestringSearch bar cancel text color.
sectionIndexTextColorstringSection index text color.
hideSectionListboolWhether to hide the alphabetical section listing view or not.
renderSectionIndexItemfuncCustom render SectionIndexItem.
sortFuncfuncThe sort function for the list view data source,sorting alphabetical by default
resultSortFuncfuncThe sort function for the search result,sorting first match position by default
onScrollToSectionfuncThe callback of alphabetical section view be clicked or touch.
renderBackButtonfuncRender a custom back buttom on Toolbar.
renderEmptyfuncRender a view when data is empty.
renderEmptyResultfuncRender a view when search result is empty.
renderSeparatorfuncRender row separator.
renderSectionHeaderfuncrenderSectionHeader for the internal ListView
renderHeaderfuncrenderHeader for the internal ListView
renderFooterfuncrenderFooter for the internal ListView
renderRowfuncrenderRow for the internal ListView
onSearchStartfuncCallback when searching start.
onSearchEndfuncCallback when searching end.

Thanks

TODO

  1. add hightlight demo
  2. test ios & android & android with status bar and without

Our Other Projects

License

This library is distributed under MIT Licence.