2.3.4 • Published 5 years ago

@zanechua/react-native-search-list v2.3.4

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

React Native Search List

A searchable ListView which supports Chinese PinYin and alphabetical index.

The original library was missing some commonly used features that are now implemented and the deprecated ListView was replaced with a SectionList to be compatible with future releases of React Native.

The following picture may be helpful to understand the structure and APIs:

Installation

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

Usage

To Use SearchList, you will need an array of objects as the data source, and each object has to have a searchStr property, example code is 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
          key={this.state.dataSourceKey}
          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.
renderRownumberRender your custom row content
rowHeightnumberThe height of the default row content, it will be used for scroll calculate40
sectionHeaderHeightnumberThe height of section header content24
listContainerStyleobjectStyle properties for the internal TextInput Component
searchListBackgroundColorstringBackgroundColor for searchList#171a23
toolbarBackgroundColorstringToolbar background color#171a23
searchBarToggleDurationnumberCustom search bar animation duration300
searchBarBackgroundColorstringCustom search bar background color#171a23
searchBarContainerStyleobjectStyle properties for the SearchBar Container Component
searchBarStyleobjectStyle properties for the SearchBar Component
searchOnDefaultValueboolEnable filtered results based on default value
searchInputBackgroundColorstringCustom search input default state background color#ffffff
searchInputBackgroundColorActivestringCustom search input searching state background color
searchInputPlaceholderstringCustom search input placeholder text
searchInputDefaultValuestringCustom search input default value text
searchInputPlaceholderColorstringCustom search input placeholder text color#979797
searchInputTextColorstringCustom search input default state text color#171a23
searchInputTextColorActivestringCustom search input searching state text color#ffffff
searchInputStyleobjectStyle properties for the internal TextInput Component
statusBarHeightnumberThe height of the status bar
toolbarHeightnumberThe height of the tool bar44
searchBarBackgroundColorstringCustom search bar background color
staticCancelButtonboolEnable/Disable a static cancel button with no slide in animationfalse
showSearchIconboolShow/Hide the search icontrue
displayMaskboolShow/Hide the mask during searchingtrue
titlestringToolbar title
titleTextColorstringToolbar title text color
cancelTitlestringSearch bar cancel text colorCancel
cancelTextColorstringSearch bar cancel text color#ffffff
cancelContainerStyleobjectStyle properties for the cancel button container
hideSectionListboolWhether to hide the alphabetical section listing view or not.
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
sectionIndexTextColorstringSection index text color
sectionIndexContainerStyleobjectStyle properties for the Section Index Container Component
renderSectionIndexItemfuncCustom render SectionIndexItem.
renderBackButtonfuncRender a custom back buttom on Toolbar.
renderEmptyfuncRender a view when data is empty.
renderEmptyResultfuncRender a view when search result is empty.
renderItemSeparatorfuncRender row separator.
renderSectionHeaderfuncrenderSectionHeader for the internal ListView
renderHeaderfuncrenderHeader for the internal ListView
renderFooterfuncrenderFooter for the internal ListView
renderStickyHeaderfuncrenderStickyHeader for the section below the SearchBar
renderRowfuncrenderRow for the internal ListView
renderToolbarfuncrenderToolbar for the Toolbar
renderCancelfuncrenderCancel for custom rendering of the cancel button
renderCancelWhileSearchingfuncrenderCancelWhileSearching for custom rendering of the cancel button during search
renderToolbarfuncrenderToolbar for the Toolbar
onSearchStartfuncCallback when searching start.
onSearchEndfuncCallback when searching end.

Contributions

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.9

5 years ago

2.2.8

5 years ago

2.2.7

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago