|
1 | | -import React, { memo } from 'react' |
| 1 | +import React, { memo, useMemo } from 'react' |
2 | 2 | import { StyleSheet, Text, TouchableOpacity, View } from 'react-native' |
3 | 3 |
|
4 | | -export const ScrollViewListItem = memo( |
5 | | - ({ titleHighlighted, titleStart, titleEnd, style, onPress, numberOfLines = 2 }) => { |
6 | | - return ( |
7 | | - <TouchableOpacity onPress={onPress}> |
8 | | - <View style={styles.container}> |
9 | | - <Text numberOfLines={numberOfLines}> |
10 | | - <Text numberOfLines={1} style={{ ...styles.text, ...style }}> |
11 | | - {titleStart} |
12 | | - </Text> |
13 | | - <Text numberOfLines={1} style={{ ...styles.text, ...style, fontWeight: 'bold' }}> |
14 | | - {titleHighlighted} |
15 | | - </Text> |
16 | | - <Text numberOfLines={1} style={{ ...styles.text, ...style }}> |
17 | | - {titleEnd} |
18 | | - </Text> |
| 4 | +export const ScrollViewListItem = memo(({ highlight, title, style, onPress, numberOfLines = 2 }) => { |
| 5 | + const titleParts = useMemo(() => { |
| 6 | + let titleHighlighted = '' |
| 7 | + let titleStart = title |
| 8 | + let titleEnd = '' |
| 9 | + |
| 10 | + if (typeof title === 'string' && title.length > 0 && highlight.length > 0) { |
| 11 | + const substrIndex = title.toLowerCase().indexOf(highlight.toLowerCase()) |
| 12 | + if (substrIndex !== -1) { |
| 13 | + titleStart = title.slice(0, substrIndex) |
| 14 | + titleHighlighted = title.slice(substrIndex, substrIndex + highlight.length) |
| 15 | + titleEnd = title.slice(substrIndex + highlight.length) |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + return { titleHighlighted, titleStart, titleEnd } |
| 20 | + }, [title, highlight]) |
| 21 | + |
| 22 | + return ( |
| 23 | + <TouchableOpacity onPress={onPress}> |
| 24 | + <View style={styles.container}> |
| 25 | + <Text numberOfLines={numberOfLines}> |
| 26 | + <Text numberOfLines={1} style={{ ...styles.text, ...style }}> |
| 27 | + {titleParts.titleStart} |
19 | 28 | </Text> |
20 | | - </View> |
21 | | - </TouchableOpacity> |
22 | | - ) |
23 | | - } |
24 | | -) |
| 29 | + <Text numberOfLines={1} style={{ ...styles.text, ...style, fontWeight: 'bold' }}> |
| 30 | + {titleParts.titleHighlighted} |
| 31 | + </Text> |
| 32 | + <Text numberOfLines={1} style={{ ...styles.text, ...style }}> |
| 33 | + {titleParts.titleEnd} |
| 34 | + </Text> |
| 35 | + </Text> |
| 36 | + </View> |
| 37 | + </TouchableOpacity> |
| 38 | + ) |
| 39 | +}) |
25 | 40 |
|
26 | 41 | const styles = StyleSheet.create({ |
27 | 42 | container: { |
|
0 commit comments