123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- import React, { Component } from 'react'
- import {
- SafeAreaView,
- View,
- Text,
- Image,
- FlatList,
- StyleSheet,
- TouchableOpacity,
- } from 'react-native'
- import {connect} from 'react-redux'
- import {Device} from '../../../../tool'
- import SearchBar from '../searchBar'
- import ItemInContact from '../ItemInContact';
- import SeparatorLine from './SeparatorLine';
- import simplePinyin from 'simple-pinyin';
- class ContactList extends Component {
- constructor(props) {
- super(props)
- this.state = {
- labels: [],
- }
- }
- componentWillMount() {
- const {userGroup} =this.props
- console.log(userGroup)
- this._dataConvert(this._pySegSort(userGroup.friendArr, 'nickname'))
- }
- componentDidMount() {
- }
- // 排序并添加首字母
- _pySegSort(arr, key) {
- const letters = "abcdefghjklmnopqrstwxyz".split('')
- let segs = []
- let curr
- let otherName = []
- let t = new Date()
- arr.map((a,m)=>{
- let firstKey = a[key][0]
- let reg = new RegExp("^[A-Za-z\u4e00-\u9fa5]+$");
- if(!reg.test(firstKey)){
- otherName.push(JSON.parse(JSON.stringify(a)))
- }
- })
- letters.map((l,i) => {
- curr = {label: l.toUpperCase(), data:[]}
- arr.map((a,m) =>{
- if(a[key]==='') return
- let firstKey = a[key][0]
- let nameKey = simplePinyin(firstKey,{ pinyinOnly: false })
- if(nameKey.length===0) return
- if(nameKey[0][0].toUpperCase()===l.toUpperCase()){
- curr.data.push(JSON.parse(JSON.stringify(a)))
- arr[m][key]=''
- }
- })
- if(curr.data.length) {
- segs.push(curr)
- curr.data.sort(function(a,b){
- return a[key].localeCompare(b[key])
- })
- }
- })
- console.log(new Date() -t)
- if(otherName.length){
- segs.push({
- label:'#',
- data:otherName
- })
- }
- segs.unshift({
- label:'@',
- data:[]
- })
- return segs
- }
- // 转换成 FlatList 需要的数据
- _dataConvert(names) {
- if (!names || names.length === 0) {
- return
- }
- let contacts = []
- let labels = []
- let index = 0
- let labelItem = {}
- names.map((item) => {
- labelItem = {
- index: index,
- label: item.label
- }
- contacts.push(labelItem)
- labels.push(labelItem)
- index++
- if (item.data && item.data.length > 0) {
- item.data.map((nameItem) => {
- contacts.push({ index: index, ...nameItem })
- index++
- })
- }
- })
- this.setState({
- contacts: contacts,
- labels: labels
- })
- }
- render() {
- const { contacts, labels } = this.state
- const {navigation} =this.props.props
- const Icon_Contact_search = require('../../../../../assets/2x/contact/search.png')
- const scollTo = (item) => {
- // 当viewPosition 为 0 时将它滚动到屏幕顶部,为 1 时将它滚动到屏幕底部,为 0.5 时将它滚动到屏幕中央。
- this._flatList.scrollToIndex({viewPosition:0, index:item.index})
- }
- const renderItem = ({item}) => {
- const onPress = () => {
- //跳转到 个人详情
- navigation.navigate("DetailedInfo",{item})
- }
- if(item.index===0){
- return (<SearchBar props={this.props.props}/>)
- }
- return (
- item.username ?
- <TouchableOpacity style={styles.listItem} onPress={onPress}>
- <ItemInContact item={item}></ItemInContact>
- </TouchableOpacity> :
- <View style={styles.labelItem}>
- {/* title */}
- <Text style={styles.itemLabel}>{`${item.label}`}</Text>
- </View>
- )
- }
- return (
- <SafeAreaView style={styles.container}>
- <FlatList data={contacts}
- keyExtractor={(item, index) => index + ''}
- ref={(flatList)=>this._flatList = flatList}
- ItemSeparatorComponent={() => <SeparatorLine />}
- showsVerticalScrollIndicator = {false}
- renderItem={renderItem}/>
-
- <View style={styles.slideCtn}>
- {labels.map((item) =>{
- if(item.index===0 && item.label==='@'){
- return (
- <TouchableOpacity
- key={item.label}
- style={styles.labelCtn}
- onPress={()=> scollTo(item)}>
- <Image
- style={{width:Device.scale(12),height:Device.scale(12)}}
- source={Icon_Contact_search}></Image>
- </TouchableOpacity>
- )
- }
- return (
- <TouchableOpacity
- key={item.label}
- style={styles.labelCtn}
- onPress={()=> scollTo(item)}>
- <Text style={styles.slideLabel}>{item.label}</Text>
- </TouchableOpacity>
- )
- }
-
- )}
- </View>
- </SafeAreaView>
- )
- }
- }
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: 'white'
- },
- listItem: {
- height: Device.scale(45),
- justifyContent: 'center',
- // marginLeft:Device.scale(20),
- height:Device.scale(55)
- },
- labelItem: {
- justifyContent: 'center',
- backgroundColor: '#f0f0f0',
- height:Device.scale(25)
- },
- itemLabel: {
- fontSize: Device.scale(17),
- color: 'rgba(0,0,0,0.5)',
- marginLeft: Device.scale(10),
- // fontWeight: 'bold',
- fontSize:Device.scale(15)
- },
- itemName: {
- fontSize: Device.scale(16),
- color: '#4C5361',
- marginLeft: Device.scale(10)
- },
- slideCtn: {
- top: Device.scale(80),
- right: 0,
- position: 'absolute',
- backgroundColor: 'transparent',
- alignItems: 'center',
- },
- labelCtn: {
- paddingLeft: Device.scale(10),
- paddingRight: Device.scale(10),
- paddingTop:Device.scale(3),
- paddingBottom:Device.scale(3)
- },
- slideLabel: {
- color: '#504f4f',
- fontSize: Device.scale(9),
- },
- })
- const getUserGroup=(state)=>{
- const {userGroup} = state
- return {userGroup}
- }
- export default connect(getUserGroup)(ContactList);
|