index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React, { Component } from 'react'
  2. import {
  3. SafeAreaView,
  4. View,
  5. Text,
  6. Image,
  7. FlatList,
  8. StyleSheet,
  9. TouchableOpacity,
  10. } from 'react-native'
  11. import {connect} from 'react-redux'
  12. import 'pinyin4js'
  13. import {Device,Icon} from '../../../../tool'
  14. import SearchBar from '../searchBar'
  15. import ItemInContact from '../ItemInContact';
  16. import SeparatorLine from './SeparatorLine';
  17. class ContactList extends Component {
  18. constructor(props) {
  19. super(props)
  20. this.state = {
  21. contacts: [
  22. {id: 101, username: '阿菊',telphone:15528300169},
  23. {id: 102, username: '爱莲',telphone:15528300169},
  24. {id: 103, username: '昂立拉克',telphone:15528300169},
  25. {id: 104, username: '冰冰',telphone:15528300169},
  26. {id: 101, username: '阿菊1'},
  27. {id: 105, username: '贝贝'},
  28. {id: 106, username: '陈伟'},
  29. {id: 107, username: '程浩'},
  30. {id: 108, username: '点啥'},
  31. {id: 109, username: '到啥'},
  32. {id: 108, username: '鄂啥'},
  33. {id: 108, username: '峨啥'},
  34. {id: 108, username: '方啥'},
  35. {id: 108, username: '付啥'},
  36. {id: 108, username: '丰啥'},
  37. {id: 108, username: '关啥'},
  38. {id: 108, username: '高啥'},
  39. {id: 108, username: '黄啥'},
  40. {id: 108, username: '解啥'},
  41. {id: 108, username: '文杰'},
  42. {id: 108, username: '也文杰'},
  43. {id: 108, username: 'YYH'},
  44. {id: 108, username: 'yYH'},
  45. {id: 108, username: '#*'},
  46. {id: 108, username: '!*'},
  47. {id: 108, username: '@*'},
  48. {id: 108, username: '张龙'},
  49. {id: 108, username: ' A张龙'},
  50. {id: 108, username: 'B张龙'},
  51. ],
  52. labels: [],
  53. }
  54. }
  55. componentWillMount() {
  56. // console.log(this.props)
  57. this._dataConvert(this._pySegSort(this.state.contacts, 'username'))
  58. }
  59. componentDidMount() {
  60. }
  61. // 排序并添加首字母
  62. _pySegSort(arr, key) {
  63. const letters = "abcdefghjklmnopqrstwxyz".split('')
  64. let segs = []
  65. let curr
  66. let otherName = []
  67. let t = new Date()
  68. arr.map((a,m)=>{
  69. let firstKey = a[key][0]
  70. let reg = new RegExp("^[A-Za-z\u4e00-\u9fa5]+$");
  71. if(!reg.test(firstKey)){
  72. otherName.push(JSON.parse(JSON.stringify(a)))
  73. }
  74. })
  75. letters.map((l,i) => {
  76. curr = {label: l.toUpperCase(), data:[]}
  77. arr.map((a,m) =>{
  78. if(a[key]=='') return
  79. let firstKey = a[key][0]
  80. let nameKey = PinyinHelper.convertToPinyinString(a[key], '', PinyinFormat.FIRST_LETTER)[0].toUpperCase()
  81. if(nameKey===l.toUpperCase()){
  82. curr.data.push(JSON.parse(JSON.stringify(a)))
  83. arr[m][key]=''
  84. }
  85. })
  86. if(curr.data.length) {
  87. segs.push(curr)
  88. curr.data.sort(function(a,b){
  89. return a[key].localeCompare(b[key])
  90. })
  91. }
  92. })
  93. console.log(new Date() -t)
  94. segs.push({
  95. label:'#',
  96. data:otherName
  97. })
  98. segs.unshift({
  99. label:'@',
  100. data:[]
  101. })
  102. return segs
  103. }
  104. // 转换成 FlatList 需要的数据
  105. _dataConvert(names) {
  106. if (!names || names.length === 0) {
  107. return
  108. }
  109. let contacts = []
  110. let labels = []
  111. let index = 0
  112. let labelItem = {}
  113. names.map((item) => {
  114. labelItem = {
  115. index: index,
  116. label: item.label
  117. }
  118. contacts.push(labelItem)
  119. labels.push(labelItem)
  120. index++
  121. if (item.data && item.data.length > 0) {
  122. item.data.map((nameItem) => {
  123. contacts.push({ index: index, ...nameItem })
  124. index++
  125. })
  126. }
  127. })
  128. this.setState({
  129. contacts: contacts,
  130. labels: labels
  131. })
  132. }
  133. render() {
  134. const { contacts, labels } = this.state
  135. const {navigation} =this.props.props
  136. const scollTo = (item) => {
  137. // 当viewPosition 为 0 时将它滚动到屏幕顶部,为 1 时将它滚动到屏幕底部,为 0.5 时将它滚动到屏幕中央。
  138. this._flatList.scrollToIndex({viewPosition:0, index:item.index})
  139. }
  140. const renderItem = ({item}) => {
  141. const onPress = () => {
  142. navigation.navigate({ routeName:"DetailedInfo" })
  143. }
  144. if(item.index===0){
  145. return (<SearchBar props={this.props.props}/>)
  146. }
  147. return (
  148. item.id ?
  149. <TouchableOpacity style={styles.listItem} onPress={onPress}>
  150. <ItemInContact item={item} props={this.props.props}></ItemInContact>
  151. </TouchableOpacity> :
  152. <View style={styles.labelItem}>
  153. {/* title */}
  154. <Text style={styles.itemLabel}>{`${item.label}`}</Text>
  155. </View>
  156. )
  157. }
  158. return (
  159. <SafeAreaView style={styles.container}>
  160. <FlatList data={contacts}
  161. keyExtractor={(item, index) => index + ''}
  162. ref={(flatList)=>this._flatList = flatList}
  163. ItemSeparatorComponent={() => <SeparatorLine />}
  164. showsVerticalScrollIndicator = {false}
  165. renderItem={renderItem}/>
  166. <View style={styles.slideCtn}>
  167. {labels.map((item) =>{
  168. if(item.index===0 && item.label==='@'){
  169. return (
  170. <TouchableOpacity
  171. key={item.label}
  172. style={styles.labelCtn}
  173. onPress={()=> scollTo(item)}>
  174. <Image
  175. style={{width:Device.scale(12),height:Device.scale(12)}}
  176. source={Icon.Contact.search}></Image>
  177. </TouchableOpacity>
  178. )
  179. }
  180. return (
  181. <TouchableOpacity
  182. key={item.label}
  183. style={styles.labelCtn}
  184. onPress={()=> scollTo(item)}>
  185. <Text style={styles.slideLabel}>{item.label}</Text>
  186. </TouchableOpacity>
  187. )
  188. }
  189. )}
  190. </View>
  191. </SafeAreaView>
  192. )
  193. }
  194. }
  195. const styles = StyleSheet.create({
  196. container: {
  197. flex: 1,
  198. backgroundColor: 'white'
  199. },
  200. listItem: {
  201. height: Device.scale(45),
  202. justifyContent: 'center',
  203. // marginLeft:Device.scale(20),
  204. height:Device.scale(55)
  205. },
  206. labelItem: {
  207. justifyContent: 'center',
  208. backgroundColor: '#f0f0f0',
  209. height:Device.scale(25)
  210. },
  211. itemLabel: {
  212. fontSize: Device.scale(17),
  213. color: 'rgba(0,0,0,0.5)',
  214. marginLeft: Device.scale(10),
  215. // fontWeight: 'bold',
  216. fontSize:Device.scale(15)
  217. },
  218. itemName: {
  219. fontSize: Device.scale(16),
  220. color: '#4C5361',
  221. marginLeft: Device.scale(10)
  222. },
  223. slideCtn: {
  224. top: Device.scale(80),
  225. right: 0,
  226. position: 'absolute',
  227. backgroundColor: 'transparent',
  228. alignItems: 'center',
  229. },
  230. labelCtn: {
  231. paddingLeft: Device.scale(10),
  232. paddingRight: Device.scale(10),
  233. paddingTop:Device.scale(3),
  234. paddingBottom:Device.scale(3)
  235. },
  236. slideLabel: {
  237. color: '#504f4f',
  238. fontSize: Device.scale(9),
  239. },
  240. })
  241. export default connect()(ContactList);