1.0.4 • Published 3 years ago

react-native-contact-book v1.0.4

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

react-native-contact-book

Props

propstypedefaultdesc
contactListarray[]通讯录数据,必须有nickname
headerComponentnodenull用于传递顶部导航栏header
topComponentnodenull滚动列表头部扩展组件,要给定高度,否则影响滚动位置计算
topComponentHeightnumber0如果传了头部扩展组件,则必须传该组件的高度
titleHeightnumber25通讯录字母标题的高度
itemHeightnumber60通讯录项的高度
letterHeightnumber20侧边字母栏每个字母的高度
activeLetterBgstring'#125FED'侧边字母栏选中字母的背景色
activeLetterShowTypestring'side'触摸侧边字母栏滚动时选中字母的显示方式,可选side和center
showAvatarbooleantrue是否显示头像
titleStyleobject{}自定义通讯录字母标题样式
itemStyleobject{}自定义通讯录项的样式
onPressItemfunction()=>{}点击通讯录项事件,返回item数据

用法

步骤 1 - 安装

$ npm install react-native-contact-book --save

or

$ yarn add react-native-contact-book

步骤 2 - 引入

import React from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
import Feather from 'react-native-vector-icons/Feather';
import ContactBook from 'react-native-contact-book';
import contactList from '@/assets/contactList.json';

export default function TestContactBookPlugin() {
  const options = {
    contactList: contactList,
    headerComponent: header(),
    topComponent: topEl(),
    topComponentHeight: 80,  //若传了topComponent,这个高度必传
    activeLetterShowType: 'side',
  };

  function header() {
    return (
      <View
        style={{
          height: 60,
          backgroundColor: '#F5F5F5',
          alignItems: 'center',
          justifyContent: 'center',
        }}>
        <Text style={{fontSize: 20, fontWeight: 'bold'}}>通讯录</Text>
      </View>
    );
  }

  function topEl() {
    return (
      <View
        style={{
          height: 80,
          backgroundColor: '#FFF',
          flexDirection: 'row',
          alignItems: 'center',
          justifyContent: 'space-around',
        }}>
        <TouchableOpacity activeOpacity={0.6} style={{alignItems:'center'}}>
          <Feather name="user-plus" color={'#125FED'} size={30} />
          <Text style={{fontSize: 16, marginTop: 5}}>新朋友</Text>
        </TouchableOpacity>
        <TouchableOpacity activeOpacity={0.6} style={{alignItems:'center'}}>
          <Feather name="users" color={'#125FED'} size={30} />
          <Text style={{fontSize: 16, marginTop: 5}}>群聊</Text>
        </TouchableOpacity>
      </View>
    );
  }

  return <ContactBook {...options} />;
}

注意

传入的数据contactList 是array类型,列表项必须要有nickname,如果有头像则添加avatar,格式如下:

[
  {
    "nickname": "路飞",
    "avatar": "https://z3.ax1x.com/2021/05/07/g123h6.jpg",
    "id": "openid1"
  },
  {
    "nickname": "钢铁侠",
    "avatar": "https://z3.ax1x.com/2021/05/07/g121tx.jpg",
    "id": "openid2"
  },

  ...

]